作为一个从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. 使用SSH+SFTP操作终端全解析,告别XShell

    1.前言 在Windows系统下操作远程服务器的方式很多,比如XShell+XFTP组合,亦或是PuTTY+WinSCP组合,但在Mac系统下登陆远程服务器,并没有这些工具供我们使用.相比较而言,在M ...

  2. POJ 1651:Multiplication Puzzle(区间DP)

    http://poj.org/problem?id=1651 题意:给出n个数字,每取中间一个数,就会使得权值加上中间这个数和两边的乘积,求取剩两个数最少的权值是多少. 思路:区间dp. 一开始想了挺 ...

  3. 基于C#的机器学习--我应该接受这份工作吗-使用决策树

    决策树 要使决策树完整而有效,它必须包含所有的可能性.事件序列也必须提供,并且是互斥的,这意味着如果一个事件发生,另一个就不能发生. 决策树是监督机器学习的一种形式,因为我们必须解释输入和输出应该是什 ...

  4. MYSQL事务、锁

    MYSQL事务 事务: 原子性 : 要么都执行,要么都不执行. 一致性: 结果要么都成功 ,要么都失败. 隔离性: 事务之间是互不干扰的 持久性: 事务一旦被提交,对数据库的改变是永久性的. 事务的隔 ...

  5. c++ 函数知识点汇总

    c++ 函数知识点汇总 swap函数 交换两个数组元素 比如 swap(a[i],a[j]); 就是交换a[i] 和 a[j] 的值 strcpy() 复制一个数组元素的值到另一个数组元素里 strc ...

  6. Spring Boot2(十一):Mybatis使用总结(自增长、多条件、批量操作、多表查询等等)

    一.前言 上次用Mybatis还是2017年做项目的时候,已经很久过去了.中途再没有用过Mybatis.导致现在学习SpringBoot过程中遇到一些Mybatis的问题,以此做出总结(XML极简模式 ...

  7. Spring Boot2(十二):手摸手教你搭建Shiro安全框架

    一.前言 SpringBoot+Shiro+Mybatis完成的. 之前看了一位小伙伴的Shiro教程,跟着做了,遇到蛮多坑的(´இ皿இ`) 修改整理了一下,成功跑起来了.可以通过postman进行测 ...

  8. C++学习书籍推荐《C++ Primer 第四版》下载

    百度云及其他网盘下载地址:点我 编辑推荐 <C++ Primer中文版(第4版)>对C++基本概念和技术全面而且权威的阐述,对现代C++编程风格的强调,使<C++ Primer中文版 ...

  9. Vue快速学习_第三节

    过滤器 局部过滤器(组件内部使用的过滤器,跟django的很像, filters: {过滤器的名字: {function(val, a,b){}}} 全局过滤器(全局过滤器,只要过滤器一创建,在任何组 ...

  10. linux server 发送邮件

    用linux服务器发送邮件centos1.安装mailx 和sendmail,系统一般会安装的yum -y isntall mailx sendmail 2.修改/etc/mail.rcset fro ...