mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
Update stencil manager impl to avoid rendering artifact on mi5 (#3133)
* Update stencil manager impl to avoid rendering artifact on mi5 * Cleanup maskEndAssembler * Fix post update render data in mask * Fix image stencil issue * Merge frontMaterial & endMaterial
This commit is contained in:
@@ -176,11 +176,9 @@ let Mask = cc.Class({
|
||||
cc.warnID(4201);
|
||||
return;
|
||||
}
|
||||
if (this._frontMaterial) {
|
||||
this._frontMaterial.alphaThreshold = this.alphaThreshold;
|
||||
this._endMaterial.alphaThreshold = this.alphaThreshold;
|
||||
this._frontMaterial.updateHash();
|
||||
this._endMaterial._hash = -this._frontMaterial._hash;
|
||||
if (this._material) {
|
||||
this._material.alphaThreshold = this.alphaThreshold;
|
||||
this._material.updateHash();
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -277,7 +275,7 @@ let Mask = cc.Class({
|
||||
this.node.on(cc.Node.EventType.SIZE_CHANGED, this._updateGraphics, this);
|
||||
this.node.on(cc.Node.EventType.ANCHOR_CHANGED, this._updateGraphics, this);
|
||||
|
||||
this.node._renderFlag |= RenderFlow.FLAG_POST_RENDER | RenderFlow.FLAG_POST_UPDATE_RENDER_DATA;
|
||||
this.node._renderFlag |= RenderFlow.FLAG_POST_RENDER;
|
||||
this._activateMaterial();
|
||||
},
|
||||
|
||||
@@ -290,14 +288,12 @@ let Mask = cc.Class({
|
||||
this.node.off(cc.Node.EventType.SIZE_CHANGED, this._updateGraphics, this);
|
||||
this.node.off(cc.Node.EventType.ANCHOR_CHANGED, this._updateGraphics, this);
|
||||
|
||||
this.node._renderFlag &= ~(RenderFlow.FLAG_POST_RENDER | RenderFlow.FLAG_POST_UPDATE_RENDER_DATA);
|
||||
this.node._renderFlag &= ~RenderFlow.FLAG_POST_RENDER;
|
||||
},
|
||||
|
||||
onDestroy () {
|
||||
this._super();
|
||||
this._graphics.destroy();
|
||||
this._frontMaterial = null;
|
||||
this._endMaterial = null;
|
||||
},
|
||||
|
||||
_resizeNodeToTargetNode: CC_EDITOR && function () {
|
||||
@@ -346,40 +342,24 @@ let Mask = cc.Class({
|
||||
// WebGL
|
||||
if (cc.game.renderType !== cc.game.RENDER_TYPE_CANVAS) {
|
||||
// Init material
|
||||
if (!this._frontMaterial) {
|
||||
this._frontMaterial = new StencilMaterial();
|
||||
this._endMaterial = new StencilMaterial();
|
||||
if (!this._material) {
|
||||
this._material = new StencilMaterial();
|
||||
}
|
||||
|
||||
// Reset material
|
||||
if (this._type === MaskType.IMAGE_STENCIL) {
|
||||
let texture = this.spriteFrame.getTexture();
|
||||
this._frontMaterial.useModel = false;
|
||||
this._frontMaterial.useTexture = true;
|
||||
this._frontMaterial.useColor = true;
|
||||
this._frontMaterial.texture = texture;
|
||||
this._frontMaterial.alphaThreshold = this.alphaThreshold;
|
||||
this._endMaterial.useModel = false;
|
||||
this._endMaterial.useTexture = true;
|
||||
this._endMaterial.useColor = true;
|
||||
this._endMaterial.texture = texture;
|
||||
this._endMaterial.alphaThreshold = this.alphaThreshold;
|
||||
this._material.useModel = false;
|
||||
this._material.useTexture = true;
|
||||
this._material.useColor = true;
|
||||
this._material.texture = texture;
|
||||
this._material.alphaThreshold = this.alphaThreshold;
|
||||
}
|
||||
else {
|
||||
this._frontMaterial.useModel = true;
|
||||
this._frontMaterial.useTexture = false;
|
||||
this._frontMaterial.useColor = false;
|
||||
this._endMaterial.useModel = true;
|
||||
this._endMaterial.useTexture = false;
|
||||
this._endMaterial.useColor = false;
|
||||
this._material.useModel = true;
|
||||
this._material.useTexture = false;
|
||||
this._material.useColor = false;
|
||||
}
|
||||
|
||||
if (!this._material) {
|
||||
this._updateMaterial(this._frontMaterial);
|
||||
}
|
||||
|
||||
// Avoid next up mask material to be batch with the exit one
|
||||
this._endMaterial._hash = -this._frontMaterial._hash;
|
||||
}
|
||||
|
||||
this.markForRender(true);
|
||||
@@ -437,17 +417,17 @@ let Mask = cc.Class({
|
||||
|
||||
markForUpdateRenderData (enable) {
|
||||
if (enable && this.enabledInHierarchy) {
|
||||
this.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA | RenderFlow.FLAG_POST_UPDATE_RENDER_DATA;
|
||||
this.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA;
|
||||
}
|
||||
else if (!enable) {
|
||||
this.node._renderFlag &= ~(RenderFlow.FLAG_UPDATE_RENDER_DATA | RenderFlow.FLAG_POST_UPDATE_RENDER_DATA);
|
||||
this.node._renderFlag &= ~RenderFlow.FLAG_UPDATE_RENDER_DATA;
|
||||
}
|
||||
},
|
||||
|
||||
markForRender (enable) {
|
||||
if (enable && this.enabledInHierarchy) {
|
||||
this.node._renderFlag |= (RenderFlow.FLAG_RENDER | RenderFlow.FLAG_UPDATE_RENDER_DATA |
|
||||
RenderFlow.FLAG_POST_RENDER | RenderFlow.FLAG_POST_UPDATE_RENDER_DATA);
|
||||
RenderFlow.FLAG_POST_RENDER);
|
||||
}
|
||||
else if (!enable) {
|
||||
this.node._renderFlag &= ~(RenderFlow.FLAG_RENDER | RenderFlow.FLAG_POST_RENDER);
|
||||
@@ -456,7 +436,7 @@ let Mask = cc.Class({
|
||||
|
||||
disableRender () {
|
||||
this.node._renderFlag &= ~(RenderFlow.FLAG_RENDER | RenderFlow.FLAG_UPDATE_RENDER_DATA |
|
||||
RenderFlow.FLAG_POST_RENDER | RenderFlow.FLAG_POST_UPDATE_RENDER_DATA);
|
||||
RenderFlow.FLAG_POST_RENDER);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -24,13 +24,32 @@
|
||||
****************************************************************************/
|
||||
|
||||
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 = [];
|
||||
|
||||
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) {
|
||||
@@ -45,7 +64,6 @@ let maskFrontAssembler = {
|
||||
}
|
||||
let renderData = mask._renderData;
|
||||
|
||||
mask._material = mask._frontMaterial;
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL) {
|
||||
if (mask.spriteFrame) {
|
||||
let size = mask.node._contentSize;
|
||||
@@ -53,7 +71,7 @@ let maskFrontAssembler = {
|
||||
renderData.updateSizeNPivot(size.width, size.height, anchor.x, anchor.y);
|
||||
renderData.dataLength = 4;
|
||||
spriteAssembler.updateRenderData(mask);
|
||||
renderData.material = mask.getMaterial();
|
||||
renderData.material = mask._material;
|
||||
}
|
||||
else {
|
||||
mask._material = null;
|
||||
@@ -70,10 +88,19 @@ let maskFrontAssembler = {
|
||||
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);
|
||||
@@ -85,43 +112,17 @@ let maskFrontAssembler = {
|
||||
};
|
||||
|
||||
let maskEndAssembler = {
|
||||
updateRenderData (mask) {
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL && !mask.spriteFrame) {
|
||||
mask._material = null;
|
||||
}
|
||||
else {
|
||||
mask._material = mask._endMaterial;
|
||||
}
|
||||
let material = mask._material;
|
||||
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL) {
|
||||
let data = mask._renderData;
|
||||
data.material = material;
|
||||
}
|
||||
else {
|
||||
let datas = mask._graphics._impl._renderDatas;
|
||||
for (let i = 0; i < datas.length; i++) {
|
||||
datas[i].material = material;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
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.popMask();
|
||||
_stencilMgr.exitMask();
|
||||
|
||||
// vertex buffer
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL) {
|
||||
spriteAssembler.fillBuffers(mask, renderer);
|
||||
}
|
||||
else {
|
||||
graphicsAssembler.fillBuffers(mask._graphics, renderer);
|
||||
}
|
||||
_graphicsPool.push(mask._clearGraphics);
|
||||
mask._clearGraphics = null;
|
||||
}
|
||||
|
||||
mask.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA | RenderFlow.FLAG_POST_UPDATE_RENDER_DATA;
|
||||
mask.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -30,12 +30,14 @@ const gfx = renderEngine.gfx;
|
||||
var Stage = cc.Enum({
|
||||
// Stencil disabled
|
||||
DISABLED: 0,
|
||||
// Clear stencil buffer
|
||||
CLEAR: 1,
|
||||
// Entering a new level, should handle new stencil
|
||||
ENTER_LEVEL: 1,
|
||||
// In stencil
|
||||
ENABLED: 2,
|
||||
ENTER_LEVEL: 2,
|
||||
// In content
|
||||
ENABLED: 3,
|
||||
// Exiting a level, should restore old stencil or disable
|
||||
EXIT_LEVEL: 3,
|
||||
EXIT_LEVEL: 4,
|
||||
});
|
||||
|
||||
function StencilManager () {
|
||||
@@ -86,18 +88,26 @@ StencilManager.prototype = {
|
||||
stencilMask = ref;
|
||||
}
|
||||
failOp = gfx.STENCIL_OP_KEEP;
|
||||
writeMask = 0;
|
||||
writeMask = this.getWriteMask();
|
||||
}
|
||||
else {
|
||||
func = gfx.DS_FUNC_NEVER;
|
||||
failOp = gfx.STENCIL_OP_REPLACE;
|
||||
|
||||
if (this.stage === Stage.ENTER_LEVEL) {
|
||||
if (this.stage === Stage.CLEAR) {
|
||||
mask = this._maskStack[this._maskStack.length - 1];
|
||||
func = gfx.DS_FUNC_NEVER;
|
||||
ref = this.getWriteMask();
|
||||
stencilMask = ref;
|
||||
writeMask = ref;
|
||||
failOp = mask.inverted ? gfx.STENCIL_OP_REPLACE : gfx.STENCIL_OP_ZERO;
|
||||
}
|
||||
else if (this.stage === Stage.ENTER_LEVEL) {
|
||||
mask = this._maskStack[this._maskStack.length - 1];
|
||||
this.stage = Stage.ENABLED;
|
||||
// Fill stencil mask
|
||||
ref = this.getStencilRef();
|
||||
stencilMask = this.getWriteMask();
|
||||
writeMask = stencilMask;
|
||||
func = gfx.DS_FUNC_NEVER;
|
||||
ref = this.getWriteMask();
|
||||
stencilMask = ref;
|
||||
writeMask = ref;
|
||||
failOp = mask.inverted ? gfx.STENCIL_OP_ZERO : gfx.STENCIL_OP_REPLACE;
|
||||
}
|
||||
else if (this.stage === Stage.EXIT_LEVEL) {
|
||||
// Pop mask after getting the correct stencil mask
|
||||
@@ -108,9 +118,11 @@ StencilManager.prototype = {
|
||||
this.stage = Stage.ENABLED;
|
||||
}
|
||||
// Clear stencil mask
|
||||
ref = 0;
|
||||
stencilMask = this.getExitWriteMask();
|
||||
writeMask = stencilMask;
|
||||
func = gfx.DS_FUNC_EQUAL;
|
||||
ref = this.getExitWriteMask();
|
||||
stencilMask = ref;
|
||||
writeMask = ref;
|
||||
failOp = gfx.STENCIL_OP_KEEP;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,10 +139,17 @@ StencilManager.prototype = {
|
||||
cc.errorID(9000, this._maxLevel);
|
||||
}
|
||||
this._maskStack.push(mask);
|
||||
},
|
||||
|
||||
clear () {
|
||||
this.stage = Stage.CLEAR;
|
||||
},
|
||||
|
||||
enterLevel () {
|
||||
this.stage = Stage.ENTER_LEVEL;
|
||||
},
|
||||
|
||||
popMask () {
|
||||
exitMask () {
|
||||
if (this._maskStack.length === 0) {
|
||||
cc.errorID(9001);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user