作为一个从c++转过来的程序员,flash原生的自定义mask实在是太好用,能方便实现各种效果,比如新手引导的高亮、viewport效果等。可惜starling的显示对象并不支持mask特性,查阅google,终于找到pixelmask这个开源代码,实现了想要的效果,感谢这位作者。(注:该mask只有渲染的裁剪功能,并没有原生mask的hitTest功能)

使用方法:

// myCustomDisplayObject and myCustomMaskDisplayObject can be any Starling display object:
var myCustomDisplayObject:Sprite = new Sprite();
var myCustomMaskDisplayObject:Sprite = new Sprite(); // for masks with animation: 较费,因为每次render都需要刷新RenderTexture,尽量避免同时出现大量这种带mask的动画。(宿主为动画或者mask本身也会动,都需要将animate设置成true)
var maskedDisplayObject:PixelMaskDisplayObject = new PixelMaskDisplayObject();
maskedDisplayObject.addChild(myCustomDisplayObject); // for masks with no animation (note, MUCH better performance!) 静态
var maskedDisplayObject:PixelMaskDisplayObject = new PixelMaskDisplayObject(-1, false);
maskedDisplayObject.addChild(myCustomDisplayObject); // Apply the masking as you would in classic flash.display style.
// Note: the mask display object should not be added to the display list. maskedDisplayObject.mask = myCustomMaskDisplayObject; //mask基于像素,可以自定义形状,而不是传统的矩形viewport
addChild(maskedDisplayObject);

pixelMask通过两个renderTexture的Blend来实现,一个是要渲染的对象,另外一个是mask。

刷新RenderTexture

        private function refreshRenderTextures(e:Event=null) : void {
if (_mask) { clearRenderTextures(); _maskRenderTexture = new RenderTexture(_mask.width, _mask.height, false, _scaleFactor);
_renderTexture = new RenderTexture(_mask.width, _mask.height, false, _scaleFactor); // create image with the new render texture
_image = new Image(_renderTexture); // create image to blit the mask onto
_maskImage = new Image(_maskRenderTexture); // set the blending mode to MASK (ZERO, SRC_ALPHA)
         // BlendMode.register(MASK_MODE_NORMAL,Context3DBlendFactor.ZERO,Context3DBlendFactor.SOURCE_ALPHA);
         // BlendMode.register(MASK_MODE_INVERTED,Context3DBlendFactor.ZERO,Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
if (_inverted) {
_maskImage.blendMode = MASK_MODE_INVERTED;
} else {
_maskImage.blendMode = MASK_MODE_NORMAL;
}
}
_maskRendered = false;
}

将mask叠加到目标texture

        private function drawRenderTextures() : void
{
// undo scaling and positioning temporarily because its already applied in this execution stack var matrix:Matrix = this.transformationMatrix.clone(); this.transformationMatrix = new Matrix();
_superRenderFlag = true;
_renderTexture.draw(this);
_superRenderFlag = false; this.transformationMatrix = matrix;
_renderTexture.draw(_maskImage);
}

渲染

        public override function render(support:RenderSupport, parentAlpha:Number):void
{
if (_isAnimated || (!_isAnimated && !_maskRendered)) {
if (_superRenderFlag || !_mask) {
super.render(support, parentAlpha);
} else {
if (_mask) {
_maskRenderTexture.draw(_mask);
_renderTexture.drawBundled(drawRenderTextures);
_image.render(support, parentAlpha);
_maskRendered = true;
}
}
} else {
_image.render(support, parentAlpha);
}
}

PixelMask: http://wiki.starling-framework.org/extensions/pixelmask

git: https://github.com/jonathanhart/pixelmask

基于Starling的mask实现的更多相关文章

  1. [转]基于Starling移动项目开发准备工作

    最近自己趁业余时间做的flash小游戏已经开发得差不多了,准备再完善下ui及数值后,投放到国外flash游戏站.期间也萌生想法,想把游戏拓展到手机平台.这两天尝试了下,除去要接入ane接口的工作,小游 ...

  2. 【Stage3D学习笔记续】山寨Starling(十一):Touch事件体系

    我们的山寨Starling版本将会在这里停止更新了,主要还是由于时间比较有限,而且我们的山寨版本也很好的完成了他的任务“了解Starling的核心渲染”,接下来的Starling解析我们将会直接阅读S ...

  3. 论文阅读笔记三十六:Mask R-CNN(CVPR2017)

    论文源址:https://arxiv.org/pdf/1703.06870.pdf 开源代码:https://github.com/matterport/Mask_RCNN 摘要 Mask R-CNN ...

  4. Mask RCNN 学习笔记

    下面会介绍基于ResNet50的Mask RCNN网络,其中会涉及到RPN.FPN.ROIAlign以及分类.回归使用的损失函数等 介绍时所采用的MaskRCNN源码(python版本)来源于GitH ...

  5. Robotlegs2的Starling扩展

    有个老外写了robotleges2的starling扩展,地址是 https://github.com/brean/robotlegs2-starling-viewmap 需要注意的是要先创建一个基于 ...

  6. 目标检测论文解读11——Mask R-CNN

    目的 让Faster R-CNN能做实例分割的任务. 方法 模型的结构图如下. 与Faster R-CNN相比,主要有两点变化. (1) 用RoI Align替代RoI Pool. 首先回顾一下RoI ...

  7. ASP.Net Core MVC6 RC2 启动过程分析[偏源码分析]

    入口程序 如果做过Web之外开发的人,应该记得这个是标准的Console或者Winform的入口.为什么会这样呢? .NET Web Development and Tools Blog ASP.NE ...

  8. Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm

    目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...

  9. DragonBone在FlashDevelop编译

    http://dragonbones.github.io/ dragonbones是一个强大的骨骼动画编辑器,基于Starling,用AS3语言编写,可以导出骨骼动画数据供其他程序使用. 下面来讲一下 ...

随机推荐

  1. Junit4学习使用和总结

    Junit4学习使用和总结 部分资料来源于网络 编辑于:20190710 一.Junit注解理解 1.@RunWith 首先要分清几个概念:测试方法.测试类.测试集.测试运行器.其中测试方法就是用@T ...

  2. Android native进程间通信实例-socket本地通信篇之——服务端进程异常退出解决办法

    导读: 好难受啊,为什么服务端说挂就挂,明明只是客户端关闭而已,服务端怎么能挂呢? 想想,如果手机上使用一个聊天程序的时候,手机端关闭了聊天程序,那么远端服务器程序总不能说挂就挂吧!所以一定要查明真相 ...

  3. python输出带颜色详解

    书写格式:     开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m      注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个:另外由于 ...

  4. ZIP:Checksum

    Checksum: long getValue() :返回当前的校验和值. void reset() :将校验和重置为其初始值. void update(byte[] b, int off, int ...

  5. MsgWaitForMultipleObjects

    Use caution when calling the wait functions and code that directly or indirectly creates windows. If ...

  6. Vincent的城堡

    \(\mathcal{Description}\) \(\mathcal{Solution}\) 除去前k部分,后面的是随便怎么选的所以后面的就是\((n-k)^{n-k}\)种方案 前k部分,由于k ...

  7. MySql的数据库优化到底优化啥了都(3)

    嘟嘟在上两个文章里面简单粗糙的讲了讲关于MySql存储引擎的一些特性以及选择.个人感觉如果面试官给我机会的话,至少能说个10分钟了吧.只可惜有时候生活就是这样:骨感的皮包骨头了还在那美呢.牢骚两句,北 ...

  8. windos10专业版激活(可用)

    电脑提示Windows许可证即将到期,于是自己就在网上找了一些教程,但是并没有激活成功,反而由即将到期变为了通知状态,尝试了各种密钥都不行,也下载了激活工具如暴风激活工具,KMS都不管用,尝试了好多方 ...

  9. JAVA-注解(2)-自定义注解及反射注解

    自定义注解开发 1.开发一个注解类 开发一个注解类的过程,非常类似于开发一个接口,只不过需要通过@interface关键字来声明 2.使用元注解修饰注解的声明 所谓的原注解是用来修饰注解声明的注释,可 ...

  10. Go语言圣经习题练习_1.7. Web服务

    练习 1.12: 修改Lissajour服务,从URL读取变量,比如你可以访问 http://localhost:8000/?cycles=20 这个URL,这样访问可以将程序里的cycles默认的5 ...