mask-component

This commit is contained in:
Leslie
2019-01-21 16:28:48 +08:00
parent 5fa96147cc
commit 8c7cdaf623
3 changed files with 288 additions and 311 deletions
-144
View File
@@ -1,144 +0,0 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const StencilManager = require('../stencil-manager');
const Node = require('../../../CCNode');
const Mask = require('../../../components/CCMask');
const Graphics = require('../../../graphics/graphics');
const RenderFlow = require('../../render-flow');
const spriteAssembler = require('./sprite/simple');
const graphicsAssembler = require('./graphics');
let _stencilMgr = StencilManager.sharedManager;
// for nested mask, we might need multiple graphics component to avoid data conflict
let _graphicsPool = [];
let _graphicsUsing = [];
function getGraphics () {
let graphics = _graphicsPool.pop();
if (!graphics) {
let graphicsNode = new Node();
graphics = graphicsNode.addComponent(Graphics);
graphics._activateMaterial();
graphics.lineWidth = 0;
graphics.rect(0, 0, cc.visibleRect.width, cc.visibleRect.height);
graphics.fill();
graphicsAssembler.updateRenderData(graphics);
}
return graphics;
}
let maskFrontAssembler = {
updateRenderData (mask) {
if (!mask._renderData) {
if (mask._type === Mask.Type.IMAGE_STENCIL) {
mask._renderData = spriteAssembler.createData(mask);
}
else {
// for updateGraphics calculation
mask._renderData = mask.requestRenderData();
}
}
let renderData = mask._renderData;
if (mask._type === Mask.Type.IMAGE_STENCIL) {
if (mask.spriteFrame) {
let size = mask.node._contentSize;
let anchor = mask.node._anchorPoint;
renderData.updateSizeNPivot(size.width, size.height, anchor.x, anchor.y);
renderData.dataLength = 4;
spriteAssembler.updateRenderData(mask);
renderData.material = mask._material;
}
else {
mask._material = null;
}
}
else {
mask._graphics._material = mask._material;
graphicsAssembler.updateRenderData(mask._graphics);
}
},
fillBuffers (mask, renderer) {
// Invalid state
if (mask._type !== Mask.Type.IMAGE_STENCIL || mask.spriteFrame) {
// HACK: Must push mask after batch, so we can only put this logic in fillVertexBuffer or fillIndexBuffer
_stencilMgr.pushMask(mask);
_stencilMgr.clear();
mask._clearGraphics = getGraphics();
graphicsAssembler.fillBuffers(mask._clearGraphics, renderer);
_stencilMgr.enterLevel();
// vertex buffer
renderer.node = mask.node;
renderer.material = mask._material;
if (mask._type === Mask.Type.IMAGE_STENCIL) {
spriteAssembler.fillBuffers(mask, renderer);
renderer._flush();
}
else {
graphicsAssembler.fillBuffers(mask._graphics, renderer);
}
}
mask.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA;
}
};
let maskEndAssembler = {
fillBuffers (mask, renderer) {
// Invalid state
if (mask._type !== Mask.Type.IMAGE_STENCIL || mask.spriteFrame) {
// HACK: Must pop mask after batch, so we can only put this logic in fillBuffers
_stencilMgr.exitMask();
_graphicsUsing.push(mask._clearGraphics);
mask._clearGraphics = null;
// Clear graphics can only be recycled after the mask stack exits entirely.
if (_stencilMgr.stage === StencilManager.Stage.DISABLED) {
for (let i = 0; i < _graphicsUsing.length; i++) {
_graphicsPool.push(_graphicsUsing[i]);
}
_graphicsUsing.length = 0;
}
}
mask.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA;
}
};
Mask._assembler = maskFrontAssembler;
Mask._postAssembler = maskEndAssembler;
module.exports = {
front: maskFrontAssembler,
end: maskEndAssembler
}
+175
View File
@@ -0,0 +1,175 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
import { GFXComparisonFunc, GFXStencilOp } from '../../../gfx/define';
import { UI } from '../../../renderer/ui/ui';
import { Material } from '../../assets/material';
import { MaskComponent } from '../components/mask-component';
import { MeshBuffer } from '../mesh-buffer';
import { IAssembler } from './assembler';
interface IStencilState {
stencilTest?: boolean;
func?: GFXComparisonFunc;
readMask?: number;
writeMask?: number;
failOp?: GFXStencilOp;
zFailOp?: GFXStencilOp;
passOp?: GFXStencilOp;
ref?: number;
}
interface IStencilStateUpdate {
func: GFXComparisonFunc;
writeMask: number;
ref: number;
}
function overrideStencilState (material: Material, state: IStencilState) {
material.overridePipelineStates({
depthStencilState: {
stencilTestFront: state.stencilTest,
stencilFuncFront: state.func,
stencilReadMaskFront: state.readMask,
stencilWriteMaskFront: state.writeMask,
stencilFailOpFront: state.failOp,
stencilZFailOpFront: state.zFailOp,
stencilPassOpFront: state.passOp,
stencilRefFront: state.ref,
stencilTestBack: state.stencilTest,
stencilFuncBack: state.func,
stencilReadMaskBack: state.readMask,
stencilWriteMaskBack: state.writeMask,
stencilFailOpBack: state.failOp,
stencilZFailOpBack: state.zFailOp,
stencilPassOpBack: state.passOp,
stencilRefBack: state.ref,
},
});
}
function updateDynamicStencilStates (material: Material, state: IStencilStateUpdate) {
}
class StencilManager {
private _maskStack: MaskComponent[] = [];
public enter (mask: MaskComponent) {
this._maskStack.push(mask);
}
public exit () {
this._maskStack.pop();
}
public updateMaskMaterial (material: Material) {
updateDynamicStencilStates(material, {
func: GFXComparisonFunc.ALWAYS,
writeMask: this._protectAncestorMasks(),
ref: this._getWriteMaskForTopMask(),
});
}
public updateClearMaskMaterial (material: Material) {
updateDynamicStencilStates(material, {
func: GFXComparisonFunc.ALWAYS,
writeMask: this._protectAncestorMasks(),
ref: 0,
});
}
private _readAncestorMasks (): number {
let result = 0;
this._maskStack.forEach((mask, iBit) => {
result |= (1 << iBit);
});
return result;
}
private _protectAncestorMasks (): number {
return ~this._readAncestorMasks();
}
private _getWriteMaskForTopMask (): number {
return 1 << this._maskStack.length;
}
}
const stencilManager = new StencilManager();
export const maskAssembler: IAssembler = {
useModel : false,
updateRenderData (mask: MaskComponent) {
},
fillBuffers (mask: MaskComponent, ui: UI) {
stencilManager.enter(mask);
mask._createRenderData(ui);
},
};
export const maskEndAssembler: IAssembler = {
fillBuffers (mask: MaskComponent, meshBuffer: MeshBuffer) {
stencilManager.exit();
},
};
export function setupMaskMaterial (material: Material) {
overrideStencilState(material, {
stencilTest: true,
func: GFXComparisonFunc.ALWAYS,
readMask: 0xffffffff,
failOp: GFXStencilOp.KEEP,
zFailOp: GFXStencilOp.KEEP,
passOp: GFXStencilOp.REPLACE,
});
}
export function setupClearMaskMaterial (material: Material) {
overrideStencilState(material, {
stencilTest: true,
func: GFXComparisonFunc.ALWAYS,
readMask: 0xffffffff,
failOp: GFXStencilOp.KEEP,
zFailOp: GFXStencilOp.KEEP,
passOp: GFXStencilOp.REPLACE,
ref: 0,
});
}
export function setupMaskTargetMaterial (material: Material) {
overrideStencilState(material, {
stencilTest: true,
func: GFXComparisonFunc.NOT_EQUAL,
readMask: 0xffffffff,
writeMask: 0xffffffff,
failOp: GFXStencilOp.KEEP,
zFailOp: GFXStencilOp.KEEP,
passOp: GFXStencilOp.KEEP,
ref: 0,
});
}
+113 -167
View File
@@ -27,18 +27,19 @@ import {SpriteFrame} from '../../../assets/CCSpriteFrame';
import { ccclass, executeInEditMode, executionOrder,
menu, property, requireComponent } from '../../../core/data/class-decorator';
import { ccenum } from '../../../core/value-types/enum';
import { clamp } from '../../../core/vmath';
import { clamp, vec3 } from '../../../core/vmath';
import { GFXPrimitiveMode } from '../../../gfx/define';
import { IUIRenderData, UI } from '../../../renderer/ui/ui';
import { Node } from '../../../scene-graph/node';
import { Material } from '../../assets/material';
import circle from '../../primitive/circle';
import { IGeometry } from '../../primitive/define';
import quad from '../../primitive/quad';
import { scale, translate } from '../../primitive/transform';
import { setupClearMaskMaterial, setupMaskMaterial } from '../assembler/mask-assembler';
import { MeshBuffer } from '../mesh-buffer';
import { UIRenderComponent } from './ui-render-component';
import { UITransformComponent } from './ui-transfrom-component';
const renderEngine = require('../../../2d/renderer/render-engine');
const math = renderEngine.math;
const StencilMaterial = renderEngine.StencilMaterial;
// const RenderFlow = require('../../../2d/renderer/render-flow');
const Graphics = require('../../../2d/graphics/graphics');
const _vec2_temp = cc.v2();
const _mat4_temp = math.mat4.create();
/**
* !#en the type for mask.
@@ -112,18 +113,10 @@ export class MaskComponent extends UIRenderComponent {
}
public set spriteFrame (value) {
const lastSprite = this._spriteFrame;
if (CC_EDITOR) {
if ((lastSprite && lastSprite._uuid) === (value && value._uuid)) {
return;
}
} else {
if (lastSprite === value) {
return;
}
if (this._spriteFrame === value) {
return;
}
this._spriteFrame = value;
this._applySpriteFrame(lastSprite);
this._applySpriteFrame(value);
}
/**
@@ -149,14 +142,6 @@ export class MaskComponent extends UIRenderComponent {
public set alphaThreshold (value) {
this._alphaThreshold = value;
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
cc.warnID(4201);
return;
}
if (this._material) {
this._material.alphaThreshold = this.alphaThreshold;
this._material.updateHash();
}
}
/**
@@ -189,15 +174,6 @@ export class MaskComponent extends UIRenderComponent {
this._segments = clamp(value, SEGEMENTS_MIN, SEGEMENTS_MAX);
}
// _resizeToTarget: {
// animatable: false,
// set (value) {
// if (value) {
// this._resizeNodeToTargetNode();
// }
// },
// },
@property
private _spriteFrame: SpriteFrame | null = null;
@@ -213,68 +189,35 @@ export class MaskComponent extends UIRenderComponent {
@property
private _segments: number = 64;
private _graphics: Graphics | null = null;
private _maskMaterial: Material | null = null;
private _material: StencilMaterial | null = null;
private _clearMaskMaterial: Material | null = null;
private _maskGeometry: IGeometry | null = null;
public onLoad () {
this._graphics = new Graphics();
this._graphics.node = this.node;
this._graphics.lineWidth = 0;
this._graphics.strokeColor = cc.color(0, 0, 0, 0);
}
public onRestore () {
if (!this._graphics) {
this._graphics = new Graphics();
this._graphics.node = this.node;
this._graphics.lineWidth = 0;
}
if (this._type !== MaskType.IMAGE_STENCIL) {
this._updateGraphics();
}
this._activateMaterial();
}
public onEnable () {
super.onEnable();
if (this._type === MaskType.IMAGE_STENCIL) {
if (!this._spriteFrame || !this._spriteFrame.textureLoaded()) {
// Do not render when sprite frame is not ready
this.markForRender(false);
if (this._spriteFrame) {
this._spriteFrame.once('load', this._onTextureLoaded, this);
this._spriteFrame.ensureLoadTexture();
}
}
} else {
this._updateGraphics();
}
this.node.on(Node.EventType.POSITION_CHANGED, this._updateGraphics, this);
this.node.on(Node.EventType.ROTATION_CHANGED, this._updateGraphics, this);
this.node.on(Node.EventType.SCALE_CHANGED, this._updateGraphics, this);
this.node.on(Node.EventType.SIZE_CHANGED, this._updateGraphics, this);
this.node.on(Node.EventType.ANCHOR_CHANGED, this._updateGraphics, this);
// this.node._renderFlag |= RenderFlow.FLAG_POST_RENDER;
this._activateMaterial();
}
public onDisable () {
super.onDisable();
this.node.off(Node.EventType.POSITION_CHANGED, this._updateGraphics, this);
this.node.off(Node.EventType.ROTATION_CHANGED, this._updateGraphics, this);
this.node.off(Node.EventType.SCALE_CHANGED, this._updateGraphics, this);
this.node.off(Node.EventType.SIZE_CHANGED, this._updateGraphics, this);
this.node.off(Node.EventType.ANCHOR_CHANGED, this._updateGraphics, this);
// this.node.off(Node.EventType.POSITION_CHANGED, this._updateGraphics, this);
// this.node.off(Node.EventType.ROTATION_CHANGED, this._updateGraphics, this);
// this.node.off(Node.EventType.SCALE_CHANGED, this._updateGraphics, this);
// this.node.off(Node.EventType.SIZE_CHANGED, this._updateGraphics, this);
// this.node.off(Node.EventType.ANCHOR_CHANGED, this._updateGraphics, this);
// this.node._renderFlag &= ~RenderFlow.FLAG_POST_RENDER;
}
public onDestroy () {
super.onDestroy();
this._graphics.destroy();
}
// _resizeNodeToTargetNode: CC_EDITOR && function () {
@@ -289,7 +232,6 @@ export class MaskComponent extends UIRenderComponent {
if (this._renderData) {
this._renderData.uvDirty = true;
this._renderData.vertDirty = true;
this.markForUpdateRenderData(true);
}
// Reactivate material
if (this.enabledInHierarchy) {
@@ -313,106 +255,110 @@ export class MaskComponent extends UIRenderComponent {
}
public _activateMaterial () {
// cannot be activated if texture not loaded yet
if (this._type === MaskType.IMAGE_STENCIL && (!this.spriteFrame || !this.spriteFrame.textureLoaded())) {
this.markForRender(false);
return;
if (!this._maskMaterial) {
this._maskMaterial = Material.getInstantiatedMaterial(cc.BuiltinResMgr['sprite-material'], this);
setupMaskMaterial(this._maskMaterial);
}
// WebGL
if (cc.game.renderType !== cc.game.RENDER_TYPE_CANVAS) {
// Init material
if (!this._material) {
this._material = new StencilMaterial();
}
// Reset material
if (this._type === MaskType.IMAGE_STENCIL) {
const texture = this._spriteFrame;
this._material.useModel = false;
this._material.useTexture = true;
this._material.useColor = true;
this._material.texture = texture;
this._material.alphaThreshold = this.alphaThreshold;
} else {
this._material.useModel = true;
this._material.useTexture = false;
this._material.useColor = false;
}
}
this.markForRender(true);
}
public _updateGraphics () {
const node = this.node;
const graphics = this._graphics;
// Share render data with graphics content
graphics.clear(false);
const width = node._contentSize.width;
const height = node._contentSize.height;
const x = -width * node._anchorPoint.x;
const y = -height * node._anchorPoint.y;
if (this._type === MaskType.RECT) {
graphics.rect(x, y, width, height);
} else if (this._type === MaskType.ELLIPSE) {
const cx = x + width / 2;
const cy = y + height / 2;
const rx = width / 2;
const ry = height / 2;
graphics.ellipse(cx, cy, rx, ry);
}
if (cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
graphics.stroke();
} else {
graphics.fill();
if (!this._clearMaskMaterial) {
this._clearMaskMaterial = Material.getInstantiatedMaterial(cc.BuiltinResMgr['sprite-material'], this);
setupClearMaskMaterial(this._clearMaskMaterial);
}
}
public _hitTest (cameraPt) {
const node = this.node;
const size = node.getContentSize();
const w = size.width;
const h = size.height;
const testPt = _vec2_temp;
public _createRenderData (ui: UI) {
if (!this._maskGeometry || !this._maskMaterial || !this._clearMaskMaterial) {
return null;
}
node._updateWorldMatrix();
math.mat4.invert(_mat4_temp, node._worldMatrix);
math.vec2.transformMat4(testPt, cameraPt, _mat4_temp);
testPt.x += node._anchorPoint.x * w;
testPt.y += node._anchorPoint.y * h;
const maskMeshBuffer = this._createMeshBuffer(ui, this._maskGeometry);
this._fillMaskMeshBuffer(maskMeshBuffer, this._maskGeometry);
const maskRenderData = ui.createUIRenderData() as IUIRenderData;
maskRenderData.meshBuffer = maskMeshBuffer;
maskRenderData.material = this._maskMaterial;
if (this.type === MaskType.RECT || this.type === MaskType.IMAGE_STENCIL) {
return testPt.x >= 0 && testPt.y >= 0 && testPt.x <= w && testPt.y <= h;
} else if (this.type === MaskType.ELLIPSE) {
const rx = w / 2;
const ry = h / 2;
const px = testPt.x - 0.5 * w;
const py = testPt.y - 0.5 * h;
return px * px / (rx * rx) + py * py / (ry * ry) < 1;
const clearMaskGeometry = this._drawRect(0, 0, cc.visibleRect.width, cc.visibleRect.height);
const clearMaskMeshBuffer = this._createMeshBuffer(ui, clearMaskGeometry);
this._fillClearMaskMeshBuffer(clearMaskMeshBuffer, clearMaskGeometry);
const clearMaskRenderData = ui.createUIRenderData() as IUIRenderData;
clearMaskRenderData.meshBuffer = clearMaskMeshBuffer;
clearMaskRenderData.material = this._clearMaskMaterial;
}
public _getMaskMaterial (): Material | null {
return this._maskMaterial;
}
public _getClearMaterial (): Material | null {
return this._clearMaskMaterial;
}
private _updateGraphics () {
this._maskGeometry = this._getGraphics();
}
private _getGraphics () {
const uiTransform = this.node.getComponent(UITransformComponent);
if (!uiTransform) {
return null;
}
const width = uiTransform.width;
const height = uiTransform.height;
const x = -width * uiTransform.anchorPoint.x;
const y = -height * uiTransform.anchorPoint.y;
switch (this._type) {
case MaskType.RECT:
case MaskType.IMAGE_STENCIL:
return this._drawRect(x, y, width, height);
case MaskType.ELLIPSE:
const cx = x + width / 2;
const cy = y + height / 2;
const rx = width / 2;
const ry = height / 2;
return this._drawEllipse(cx, cy, rx, ry);
}
return null;
}
private _fillMaskMeshBuffer (meshBuffer: MeshBuffer, geometry: IGeometry) {
const nVert = Math.floor(geometry.positions.length / 3);
const v = new vec3();
const worldMatrix = this.node.getWorldMatrix();
for (let i = 0; i < nVert; ++i) {
vec3.set(v, geometry.positions[3 * i + 0], geometry.positions[3 * i + 1], geometry.positions[3 * i + 2]);
vec3.transformMat4(v, v, worldMatrix);
meshBuffer.vData![3 * i + 0] = v.x;
meshBuffer.vData![3 * i + 1] = v.y;
meshBuffer.vData![3 * i + 2] = v.z;
}
}
public markForUpdateRenderData (enable) {
// if (enable && this.enabledInHierarchy) {
// this.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA;
// } else if (!enable) {
// this.node._renderFlag &= ~RenderFlow.FLAG_UPDATE_RENDER_DATA;
// }
private _fillClearMaskMeshBuffer (meshBuffer: MeshBuffer, geometry: IGeometry) {
meshBuffer.vData!.set(geometry.positions);
}
public markForRender (enable) {
// if (enable && this.enabledInHierarchy) {
// this.node._renderFlag |= (RenderFlow.FLAG_RENDER | RenderFlow.FLAG_UPDATE_RENDER_DATA |
// RenderFlow.FLAG_POST_RENDER);
// } else if (!enable) {
// this.node._renderFlag &= ~(RenderFlow.FLAG_RENDER | RenderFlow.FLAG_POST_RENDER);
// }
private _createMeshBuffer (ui: UI, geometry: IGeometry) {
const nVert = Math.floor(geometry.positions.length / 3);
const meshBuffer = ui.createBuffer(nVert, geometry.indices ? geometry.indices.length : 0);
if (geometry.indices) {
meshBuffer.iData!.set(geometry.indices);
}
// geometry.primitiveMode === undefined ? GFXPrimitiveMode.TRIANGLE_LIST : geometry.primitiveMode;
return meshBuffer;
}
public disableRender () {
// this.node._renderFlag &= ~(RenderFlow.FLAG_RENDER | RenderFlow.FLAG_UPDATE_RENDER_DATA |
// RenderFlow.FLAG_POST_RENDER);
private _drawRect (x: number, y: number, width: number, height: number) {
const geometry = quad({ includeNormal: false, includeUV: false });
scale(geometry, { x: width, y: height });
translate(geometry, { x: x + width / 2, y: y + height / 2 });
return geometry;
}
private _drawEllipse (cx: number, cy: number, rx: number, ry: number) {
const geometry = circle({ includeNormal: false, includeUV: false });
scale(geometry, { x: rx, y: ry });
translate(geometry, { x: cx, y: cy });
return geometry;
}
}