作为一个从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. spark入门(二)RDD基础操作

    1 简述 spark中的RDD是一个分布式的元素集合. 在spark中,对数据的所有操作不外乎创建RDD,转化RDD以及调用RDD操作进行求值,而这些操作,spark会自动将RDD中的数据分发到集群上 ...

  2. scrapy基础知识之 RedisCrawlSpider:

    这个RedisCrawlSpider类爬虫继承了RedisCrawlSpider,能够支持分布式的抓取.因为采用的是crawlSpider,所以需要遵守Rule规则,以及callback不能写pars ...

  3. Nginx+Lua+MySQL/Redis实现高性能动态网页展现

    Nginx结合Lua脚本,直接绕过Tomcat应用服务器,连接MySQL/Redis直接获取数据,再结合Lua中Template组件,直接写入动态数据,渲染成页面,响应前端,一次请求响应过程结束.最终 ...

  4. 满足高密度设备存储需求 一颗ICMAXLPDDR4X 8GB就行

    通讯技术在当代无疑更新越来越快速,随着5G时代即将到来,对通讯的设备的要求也将提出更高的要求.具备AI功能的硬件设备将普及化,其智能化程度也将越来越高,同时对手机等智能移动设备的内存容量和带宽也提出了 ...

  5. springboot不加载mapper文件问题解析

    1. 场景描述 启动的时候报"springboot available: expected at least 1 bean which qualifies as autowire candi ...

  6. mac环境下java项目无创建文件的权限

    1.问题: 先抛问题,由于刚刚换用mac环境,之前windows上开发的代码调试完毕,还未上线.之后上线部署之前,tl直连测试本地环境(mac)环境,功能无法使用,显示java.io.IOExcept ...

  7. 网页内嵌html遇到的问题

    在项目中遇到个问题 充值功能是点击一个按钮这个按钮会弹出模态框,输入充值金额会执行一段脚本自动提交数据到https://openapi.alipay.com/gateway.do上 结果:本网页跳转到 ...

  8. TF项目实战(基于SSD目标检测)——人脸检测1

    SSD实战——人脸检测 Tensorflow 一 .人脸检测的困难: 1. 姿态问题 2.不同种族人, 3.光照 遮挡 带眼睛 4.视角不同 5. 不同尺度 二. 数据集介绍以及转化VOC: 1. F ...

  9. UVA-10608 Friends 【并查集】

    There is a town with N citizens. It is known that some pairs of people are friends. According to the ...

  10. 【CYH-02】NOIp考砸后虐题赛:数学:题解

    赛后放上.