mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Merge branch 'next' of github.com:fireball-x/engine into next
# Conflicts: # cocos2d/core/value-types/CCColor.js
This commit is contained in:
@@ -621,6 +621,10 @@ if (CC_DEBUG) {
|
||||
"8301": "Camera does not support Canvas Mode.", //cc.Camera.enabled
|
||||
// AudioEngine: 8400
|
||||
"8400": "Wrong type arguments, 'filePath' must be a String.", // cc.audioEngine.play
|
||||
|
||||
// New Renderer 10000
|
||||
"9000": "Stencil manager does not support level bigger than %d in this device.",
|
||||
"9001": "Stencil manager is already empty, cannot pop any mask",
|
||||
};
|
||||
cc._LogInfos = logs;
|
||||
}
|
||||
|
||||
@@ -1,230 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2008-2010 Ricardo Quesada
|
||||
Copyright (c) 2011-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
||||
Copyright (c) 2012 Pierre-David Bélanger
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* cc.ClippingNode is a subclass of ccsg.Node. <br/>
|
||||
* It draws its content (children) clipped using a stencil. <br/>
|
||||
* The stencil is an other ccsg.Node that will not be drawn. <br/>
|
||||
* The clipping is done using the alpha part of the stencil (adjusted with an alphaThreshold).
|
||||
* </p>
|
||||
* @class
|
||||
* @extends _ccsg.Node
|
||||
* @param {_ccsg.Node} [stencil=null]
|
||||
*
|
||||
* @property {Number} alphaThreshold - Threshold for alpha value.
|
||||
* @property {Boolean} inverted - Indicate whether in inverted mode.
|
||||
* @property {_ccsg.Node} stencil - he ccsg.Node to use as a stencil to do the clipping.
|
||||
*/
|
||||
cc.ClippingNode = _ccsg.Node.extend(/** @lends cc.ClippingNode# */{
|
||||
inverted: false,
|
||||
_alphaThreshold: 0,
|
||||
|
||||
_stencil: null,
|
||||
_className: "ClippingNode",
|
||||
|
||||
_originStencilProgram: null,
|
||||
|
||||
/**
|
||||
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
|
||||
* @param {_ccsg.Node} [stencil=null]
|
||||
*/
|
||||
ctor: function (stencil) {
|
||||
stencil = stencil || null;
|
||||
_ccsg.Node.prototype.ctor.call(this);
|
||||
this._stencil = stencil;
|
||||
if (stencil) {
|
||||
this._originStencilProgram = stencil.getShaderProgram();
|
||||
}
|
||||
this.alphaThreshold = 1;
|
||||
this.inverted = false;
|
||||
this._renderCmd.initStencilBits();
|
||||
},
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Event callback that is invoked every time when node enters the 'stage'. <br/>
|
||||
* If the CCNode enters the 'stage' with a transition, this event is called when the transition starts. <br/>
|
||||
* During onEnter you can't access a "sister/brother" node. <br/>
|
||||
* If you override onEnter, you must call its parent's onEnter function with this._super().
|
||||
* </p>
|
||||
* @function
|
||||
*/
|
||||
onEnter: function () {
|
||||
_ccsg.Node.prototype.onEnter.call(this);
|
||||
if (this._stencil)
|
||||
this._stencil.performRecursive(_ccsg.Node.performType.onEnter);
|
||||
},
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Event callback that is invoked when the node enters in the 'stage'. <br/>
|
||||
* If the node enters the 'stage' with a transition, this event is called when the transition finishes. <br/>
|
||||
* If you override onEnterTransitionDidFinish, you shall call its parent's onEnterTransitionDidFinish with this._super()
|
||||
* </p>
|
||||
* @function
|
||||
*/
|
||||
onEnterTransitionDidFinish: function () {
|
||||
_ccsg.Node.prototype.onEnterTransitionDidFinish.call(this);
|
||||
if (this._stencil)
|
||||
this._stencil.performRecursive(_ccsg.Node.performType.onEnterTransitionDidFinish);
|
||||
},
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* callback that is called every time the node leaves the 'stage'. <br/>
|
||||
* If the node leaves the 'stage' with a transition, this callback is called when the transition starts. <br/>
|
||||
* If you override onExitTransitionDidStart, you shall call its parent's onExitTransitionDidStart with this._super()
|
||||
* </p>
|
||||
* @function
|
||||
*/
|
||||
onExitTransitionDidStart: function () {
|
||||
if (this._stencil) {
|
||||
this._stencil.performRecursive(_ccsg.Node.performType.onExitTransitionDidStart);
|
||||
}
|
||||
_ccsg.Node.prototype.onExitTransitionDidStart.call(this);
|
||||
},
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* callback that is called every time the node leaves the 'stage'. <br/>
|
||||
* If the node leaves the 'stage' with a transition, this callback is called when the transition finishes. <br/>
|
||||
* During onExit you can't access a sibling node. <br/>
|
||||
* If you override onExit, you shall call its parent's onExit with this._super().
|
||||
* </p>
|
||||
* @function
|
||||
*/
|
||||
onExit: function () {
|
||||
if (this._stencil) {
|
||||
this._stencil.performRecursive(_ccsg.Node.performType.onExit);
|
||||
}
|
||||
_ccsg.Node.prototype.onExit.call(this);
|
||||
},
|
||||
|
||||
visit: function (parent) {
|
||||
this._renderCmd.clippingVisit(parent && parent._renderCmd);
|
||||
},
|
||||
|
||||
_visitChildren: function () {
|
||||
if (this._reorderChildDirty) {
|
||||
this.sortAllChildren();
|
||||
}
|
||||
var children = this._children, child;
|
||||
for (var i = 0, len = children.length; i < len; i++) {
|
||||
child = children[i];
|
||||
if (child && child._visible) {
|
||||
child.visit(this);
|
||||
}
|
||||
}
|
||||
this._renderCmd._dirtyFlag = 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The alpha threshold. <br/>
|
||||
* The content is drawn only where the stencil have pixel with alpha greater than the alphaThreshold. <br/>
|
||||
* Should be a float between 0 and 1. <br/>
|
||||
* This default to 1 (so alpha test is disabled).
|
||||
* </P>
|
||||
* @return {Number}
|
||||
*/
|
||||
getAlphaThreshold: function () {
|
||||
return this._alphaThreshold;
|
||||
},
|
||||
|
||||
/**
|
||||
* set alpha threshold.
|
||||
* @param {Number} alphaThreshold
|
||||
*/
|
||||
setAlphaThreshold: function (alphaThreshold) {
|
||||
if (alphaThreshold === 1 && alphaThreshold !== this._alphaThreshold) {
|
||||
// should reset program used by _stencil
|
||||
this._renderCmd.resetProgramByStencil();
|
||||
}
|
||||
this._alphaThreshold = alphaThreshold;
|
||||
},
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Inverted. If this is set to YES, <br/>
|
||||
* the stencil is inverted, so the content is drawn where the stencil is NOT drawn. <br/>
|
||||
* This default to NO.
|
||||
* </p>
|
||||
* @return {Boolean}
|
||||
*/
|
||||
isInverted: function () {
|
||||
return this.inverted;
|
||||
},
|
||||
|
||||
/**
|
||||
* set whether or not invert of stencil
|
||||
* @param {Boolean} inverted
|
||||
*/
|
||||
setInverted: function (inverted) {
|
||||
this.inverted = inverted;
|
||||
},
|
||||
|
||||
/**
|
||||
* The ccsg.Node to use as a stencil to do the clipping. <br/>
|
||||
* The stencil node will be retained. This default to nil.
|
||||
* @return {_ccsg.Node}
|
||||
*/
|
||||
getStencil: function () {
|
||||
return this._stencil;
|
||||
},
|
||||
|
||||
/**
|
||||
* Set stencil.
|
||||
* @function
|
||||
* @param {_ccsg.Node} stencil
|
||||
*/
|
||||
setStencil: function (stencil) {
|
||||
if(this._stencil === stencil)
|
||||
return;
|
||||
|
||||
if (stencil)
|
||||
this._originStencilProgram = stencil.getShaderProgram();
|
||||
|
||||
this._renderCmd.setStencil(stencil);
|
||||
},
|
||||
|
||||
_createRenderCmd: function(){
|
||||
if(cc._renderType === cc.game.RENDER_TYPE_CANVAS)
|
||||
return new cc.ClippingNode.CanvasRenderCmd(this);
|
||||
else
|
||||
return new cc.ClippingNode.WebGLRenderCmd(this);
|
||||
}
|
||||
});
|
||||
|
||||
cc.ClippingNode.stencilBits = -1;
|
||||
|
||||
var _p = cc.ClippingNode.prototype;
|
||||
|
||||
// Extended properties
|
||||
cc.defineGetterSetter(_p, "stencil", _p.getStencil, _p.setStencil);
|
||||
cc.defineGetterSetter(_p, "alphaThreshold", _p.getAlphaThreshold, _p.setAlphaThreshold);
|
||||
@@ -1,106 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
require('../shape-nodes/CCDrawNode');
|
||||
|
||||
//-------------------------- ClippingNode's canvas render cmd --------------------------------
|
||||
cc.ClippingNode.CanvasRenderCmd = function(renderable){
|
||||
this._rootCtor(renderable);
|
||||
this._needDraw = false;
|
||||
|
||||
this._rendererClipCmd = new cc.CustomRenderCmd(this, this._drawStencilCommand);
|
||||
this._rendererRestoreCmd = new cc.CustomRenderCmd(this, this._restoreCmdCallback);
|
||||
};
|
||||
var proto = cc.ClippingNode.CanvasRenderCmd.prototype = Object.create(_ccsg.Node.CanvasRenderCmd.prototype);
|
||||
proto.constructor = cc.ClippingNode.CanvasRenderCmd;
|
||||
|
||||
proto.resetProgramByStencil = function () {};
|
||||
|
||||
proto.initStencilBits = function(){};
|
||||
|
||||
proto.setStencil = function(stencil){
|
||||
if(stencil == null)
|
||||
return;
|
||||
|
||||
this._node._stencil = stencil;
|
||||
|
||||
if (!(stencil instanceof cc.DrawNode)) {
|
||||
cc.errorID(6300);
|
||||
}
|
||||
};
|
||||
|
||||
proto._restoreCmdCallback = function (ctx) {
|
||||
var wrapper = ctx || cc._renderContext;
|
||||
wrapper.restore();
|
||||
};
|
||||
|
||||
proto._drawStencilCommand = function (ctx, scaleX, scaleY) {
|
||||
var wrapper = ctx || cc._renderContext, context = wrapper.getContext();
|
||||
wrapper.save();
|
||||
context.beginPath(); //save for clip
|
||||
wrapper.setTransform(this._worldTransform, scaleX, scaleY);
|
||||
|
||||
//draw elements
|
||||
var stencilBuffer = this._node._stencil._buffer;
|
||||
for(var index = 0; index < stencilBuffer.length; ++index) {
|
||||
var vertices = stencilBuffer[index].verts;
|
||||
if(vertices.length < 3) continue;
|
||||
context.moveTo(vertices[0].x, -vertices[0].y);
|
||||
for(var vIndex = 1; vIndex < vertices.length; ++vIndex) {
|
||||
context.lineTo(vertices[vIndex].x, -vertices[vIndex].y);
|
||||
}
|
||||
}
|
||||
//end draw elements
|
||||
context.clip();
|
||||
};
|
||||
|
||||
proto.clippingVisit = function (parentCmd) {
|
||||
var node = this._node;
|
||||
parentCmd = parentCmd || this.getParentRenderCmd();
|
||||
this._propagateFlagsDown(parentCmd);
|
||||
// quick return if not visible
|
||||
if (!node._visible)
|
||||
return;
|
||||
|
||||
if (parentCmd)
|
||||
this._curLevel = parentCmd._curLevel + 1;
|
||||
|
||||
this._syncStatus(parentCmd);
|
||||
if (node._stencil) {
|
||||
cc.renderer.pushRenderCommand(this._rendererClipCmd);
|
||||
}
|
||||
var children = node._children;
|
||||
|
||||
var i, len = children.length;
|
||||
if (len > 0) {
|
||||
node.sortAllChildren();
|
||||
for (i = 0; i < len; i++)
|
||||
children[i].visit(node);
|
||||
}
|
||||
|
||||
if (node._stencil) {
|
||||
cc.renderer.pushRenderCommand(this._rendererRestoreCmd);
|
||||
}
|
||||
this._dirtyFlag = 0;
|
||||
};
|
||||
@@ -1,249 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
require('../core/CCDrawingPrimitives');
|
||||
|
||||
// ------------------------------- ClippingNode's WebGL render cmd ------------------------------
|
||||
function setProgram (node, program) {
|
||||
node.shaderProgram = program;
|
||||
|
||||
var children = node.children;
|
||||
if (!children)
|
||||
return;
|
||||
|
||||
for (var i = 0; i < children.length; i++)
|
||||
setProgram(children[i], program);
|
||||
}
|
||||
|
||||
var _currentState = {
|
||||
stencilEnabled: false,
|
||||
depthWriteMask: true
|
||||
};
|
||||
|
||||
cc.ClippingNode.WebGLRenderCmd = function(renderable){
|
||||
this._rootCtor(renderable);
|
||||
this._needDraw = false;
|
||||
|
||||
this._beforeVisitCmd = new cc.CustomRenderCmd(this, this._onBeforeVisit);
|
||||
this._afterDrawStencilCmd = new cc.CustomRenderCmd(this, this._onAfterDrawStencil);
|
||||
this._afterVisitCmd = new cc.CustomRenderCmd(this, this._onAfterVisit);
|
||||
|
||||
this._previousState = null;
|
||||
this._state = {
|
||||
stencilEnabled: true,
|
||||
stencilWriteMask: 0,
|
||||
depthWriteMask: false,
|
||||
stencilFunc: 0,
|
||||
stencilRef: 0,
|
||||
stencilValueMask: 0
|
||||
};
|
||||
};
|
||||
|
||||
var proto = cc.ClippingNode.WebGLRenderCmd.prototype = Object.create(_ccsg.Node.WebGLRenderCmd.prototype);
|
||||
proto.constructor = cc.ClippingNode.WebGLRenderCmd;
|
||||
|
||||
cc.ClippingNode.WebGLRenderCmd._init_once = null;
|
||||
cc.ClippingNode.WebGLRenderCmd._visit_once = null;
|
||||
cc.ClippingNode.WebGLRenderCmd._layer = -1;
|
||||
|
||||
proto.initStencilBits = function () {
|
||||
// get (only once) the number of bits of the stencil buffer
|
||||
cc.ClippingNode.WebGLRenderCmd._init_once = true;
|
||||
if (cc.ClippingNode.WebGLRenderCmd._init_once) {
|
||||
cc.ClippingNode.stencilBits = cc._renderContext.getParameter(cc._renderContext.STENCIL_BITS);
|
||||
if (cc.ClippingNode.stencilBits <= 0)
|
||||
cc.logID(6301);
|
||||
cc.ClippingNode.WebGLRenderCmd._init_once = false;
|
||||
}
|
||||
};
|
||||
|
||||
proto.transform = function (parentCmd, recursive) {
|
||||
var node = this._node;
|
||||
this.originTransform(parentCmd, recursive);
|
||||
if (node._stencil) {
|
||||
node._stencil._renderCmd.transform(this, true);
|
||||
node._stencil._dirtyFlag &= ~_ccsg.Node._dirtyFlags.transformDirty;
|
||||
}
|
||||
};
|
||||
|
||||
proto.clippingVisit = function (parentCmd) {
|
||||
var node = this._node;
|
||||
parentCmd = parentCmd || this.getParentRenderCmd();
|
||||
this.visit(parentCmd);
|
||||
|
||||
// if stencil buffer disabled
|
||||
if (cc.ClippingNode.stencilBits < 1) {
|
||||
// draw everything, as if there where no stencil
|
||||
node._visitChildren();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!node._stencil || !node._stencil.visible) {
|
||||
if (node.inverted)
|
||||
node._visitChildren(); // draw everything
|
||||
return;
|
||||
}
|
||||
|
||||
if (cc.ClippingNode.WebGLRenderCmd._layer + 1 === cc.ClippingNode.stencilBits) {
|
||||
cc.ClippingNode.WebGLRenderCmd._visit_once = true;
|
||||
if (cc.ClippingNode.WebGLRenderCmd._visit_once) {
|
||||
cc.logID(6302, cc.ClippingNode.stencilBits);
|
||||
cc.ClippingNode.WebGLRenderCmd._visit_once = false;
|
||||
}
|
||||
// draw everything, as if there were no stencil
|
||||
node._visitChildren();
|
||||
return;
|
||||
}
|
||||
|
||||
cc.renderer.pushRenderCommand(this._beforeVisitCmd);
|
||||
|
||||
node._stencil.visit(node);
|
||||
|
||||
cc.renderer.pushRenderCommand(this._afterDrawStencilCmd);
|
||||
|
||||
// draw (according to the stencil test func) this node and its children
|
||||
var locChildren = node._children;
|
||||
if (locChildren && locChildren.length > 0) {
|
||||
var childLen = locChildren.length;
|
||||
node.sortAllChildren();
|
||||
// draw children zOrder < 0
|
||||
for (var i = 0; i < childLen; i++) {
|
||||
locChildren[i].visit(node);
|
||||
}
|
||||
}
|
||||
|
||||
cc.renderer.pushRenderCommand(this._afterVisitCmd);
|
||||
|
||||
this._dirtyFlag = 0;
|
||||
};
|
||||
|
||||
proto.setStencil = function(stencil){
|
||||
var node = this._node;
|
||||
if(node._stencil)
|
||||
node._stencil._parent = null;
|
||||
node._stencil = stencil;
|
||||
if(node._stencil)
|
||||
node._stencil._parent = node;
|
||||
};
|
||||
|
||||
proto._drawFullScreenQuadClearStencil = function () {
|
||||
// draw a fullscreen solid rectangle to clear the stencil buffer
|
||||
var projStack = cc.math.projection_matrix_stack;
|
||||
projStack.push();
|
||||
projStack.top.identity();
|
||||
|
||||
var modelViewStack = cc.math.modelview_matrix_stack;
|
||||
modelViewStack.push();
|
||||
modelViewStack.top.identity();
|
||||
|
||||
cc._drawingUtil.drawSolidRect(cc.p(-1, -1), cc.p(1, 1), cc.color(255, 255, 255, 255));
|
||||
|
||||
projStack.pop();
|
||||
|
||||
modelViewStack.pop();
|
||||
};
|
||||
|
||||
// should reset program used by _stencil
|
||||
proto.resetProgramByStencil = function () {
|
||||
var node = this._node;
|
||||
if (node._stencil) {
|
||||
var program = node._originStencilProgram;
|
||||
setProgram(node._stencil, program);
|
||||
}
|
||||
};
|
||||
|
||||
proto._onBeforeVisit = function(ctx){
|
||||
var gl = ctx || cc._renderContext, node = this._node;
|
||||
cc.ClippingNode.WebGLRenderCmd._layer++;
|
||||
|
||||
// mask of the current layer (ie: for layer 3: 00000100)
|
||||
var mask_layer = 0x1 << cc.ClippingNode.WebGLRenderCmd._layer;
|
||||
// mask of all layers less than the current (ie: for layer 3: 00000011)
|
||||
var mask_layer_l = mask_layer - 1;
|
||||
// mask of all layers less than or equal to the current (ie: for layer 3: 00000111)
|
||||
var mask_layer_le = mask_layer | mask_layer_l;
|
||||
// manually save the stencil state
|
||||
this._previousState = _currentState;
|
||||
|
||||
// enable stencil use
|
||||
gl.enable(gl.STENCIL_TEST);
|
||||
gl.stencilMask(mask_layer);
|
||||
|
||||
gl.depthMask(false);
|
||||
|
||||
gl.stencilFunc(gl.NEVER, mask_layer, mask_layer);
|
||||
gl.stencilOp(!this._node.inverted ? gl.ZERO : gl.REPLACE, gl.KEEP, gl.KEEP);
|
||||
|
||||
this._drawFullScreenQuadClearStencil();
|
||||
|
||||
gl.stencilFunc(gl.NEVER, mask_layer, mask_layer);
|
||||
gl.stencilOp(!this._node.inverted ? gl.REPLACE : gl.ZERO, gl.KEEP, gl.KEEP);
|
||||
|
||||
if (node.alphaThreshold < 1) { //TODO desktop
|
||||
var program = cc.shaderCache.programForKey(cc.macro.SHADER_POSITION_TEXTURECOLORALPHATEST);
|
||||
// set our alphaThreshold
|
||||
cc.gl.useProgram(program.getProgram());
|
||||
program.setUniformLocationWith1f(cc.macro.UNIFORM_ALPHA_TEST_VALUE_S, node.alphaThreshold);
|
||||
program.setUniformLocationWithMatrix4fv(cc.macro.UNIFORM_MVMATRIX_S, cc.renderer.mat4Identity.mat);
|
||||
setProgram(node._stencil, program);
|
||||
}
|
||||
|
||||
// Save the current stencil states
|
||||
this._state.stencilWriteMask = mask_layer;
|
||||
this._state.stencilRef = this._state.stencilValueMask = mask_layer_le;
|
||||
this._state.stencilFunc = gl.NEVER;
|
||||
|
||||
_currentState = this._state;
|
||||
};
|
||||
|
||||
proto._onAfterDrawStencil = function (ctx) {
|
||||
var gl = ctx || cc._renderContext;
|
||||
gl.depthMask(this._previousState.depthWriteMask);
|
||||
|
||||
gl.stencilFunc(gl.EQUAL, this._state.stencilRef, this._state.stencilValueMask);
|
||||
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
||||
|
||||
this._state.stencilFunc = gl.EQUAL;
|
||||
};
|
||||
|
||||
proto._onAfterVisit = function (ctx) {
|
||||
var gl = ctx || cc._renderContext;
|
||||
|
||||
var state = this._previousState;
|
||||
if (!state.stencilEnabled) {
|
||||
gl.disable(gl.STENCIL_TEST);
|
||||
}
|
||||
else {
|
||||
gl.stencilFunc(state.stencilFunc, state.stencilRef, state.stencilValueMask);
|
||||
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
|
||||
gl.stencilMask(state.stencilWriteMask);
|
||||
}
|
||||
|
||||
// restore _currentState, so self states won't affect other nodes any longer
|
||||
_currentState = this._previousState;
|
||||
this._previousState = null;
|
||||
|
||||
// we are done using this layer, decrement
|
||||
cc.ClippingNode.WebGLRenderCmd._layer--;
|
||||
};
|
||||
+109
-119
@@ -23,12 +23,10 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
require('../../clipping-nodes/CCClippingNode');
|
||||
require('../../clipping-nodes/CCClippingNodeCanvasRenderCmd');
|
||||
require('../../clipping-nodes/CCClippingNodeWebGLRenderCmd');
|
||||
|
||||
require('../../shape-nodes/CCDrawNode');
|
||||
|
||||
const renderEngine = require('../renderer/render-engine');
|
||||
const gfx = renderEngine.gfx;
|
||||
const StencilMaterial = renderEngine.StencilMaterial;
|
||||
const RenderComponent = require('./CCRenderComponent');
|
||||
const affineTrans = require('../value-types/CCAffineTransform');
|
||||
var _trans = affineTrans.make();
|
||||
|
||||
@@ -69,7 +67,7 @@ const SEGEMENTS_MAX = 10000;
|
||||
*/
|
||||
var Mask = cc.Class({
|
||||
name: 'cc.Mask',
|
||||
extends: cc.Component,
|
||||
extends: RenderComponent,
|
||||
|
||||
editor: CC_EDITOR && {
|
||||
menu: 'i18n:MAIN_MENU.component.renderers/Mask',
|
||||
@@ -78,9 +76,9 @@ var Mask = cc.Class({
|
||||
},
|
||||
|
||||
properties: {
|
||||
_clippingStencil: {
|
||||
_spriteFrame: {
|
||||
default: null,
|
||||
serializable: false,
|
||||
type: cc.SpriteFrame
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -98,7 +96,15 @@ var Mask = cc.Class({
|
||||
},
|
||||
set: function (value) {
|
||||
this._type = value;
|
||||
this._refreshStencil();
|
||||
if (this._type !== MaskType.IMAGE_STENCIL) {
|
||||
this.spriteFrame = null;
|
||||
this.alphaThreshold = 0;
|
||||
}
|
||||
if (this._renderData) {
|
||||
this._renderData.uvDirty = true;
|
||||
this._renderData.vertDirty = true;
|
||||
}
|
||||
this._updateMaterial();
|
||||
},
|
||||
type: MaskType,
|
||||
tooltip: CC_DEV && 'i18n:COMPONENT.mask.type',
|
||||
@@ -114,12 +120,27 @@ var Mask = cc.Class({
|
||||
* mask.spriteFrame = newSpriteFrame;
|
||||
*/
|
||||
spriteFrame: {
|
||||
default: null,
|
||||
type: cc.SpriteFrame,
|
||||
tooltip: CC_DEV && 'i18n:COMPONENT.mask.spriteFrame',
|
||||
notify: function () {
|
||||
this._refreshStencil();
|
||||
}
|
||||
get: function () {
|
||||
return this._spriteFrame;
|
||||
},
|
||||
set: function (value) {
|
||||
var lastSprite = this._spriteFrame;
|
||||
if (CC_EDITOR) {
|
||||
if ((lastSprite && lastSprite._uuid) === (value && value._uuid)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (lastSprite === value) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
this._spriteFrame = value;
|
||||
this._applySpriteFrame(lastSprite);
|
||||
},
|
||||
type: cc.SpriteFrame,
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -127,17 +148,20 @@ var Mask = cc.Class({
|
||||
* The alpha threshold.(Not supported Canvas Mode) <br/>
|
||||
* The content is drawn only where the stencil have pixel with alpha greater than the alphaThreshold. <br/>
|
||||
* Should be a float between 0 and 1. <br/>
|
||||
* This default to 1 (so alpha test is disabled).
|
||||
* This default to 0 (so alpha test is disabled).
|
||||
* When it's set to 1, the stencil will discard all pixels, nothing will be shown,
|
||||
* In previous version, it act as if the alpha test is disabled, which is incorrect.
|
||||
* !#zh
|
||||
* Alpha 阈值(不支持 Canvas 模式)<br/>
|
||||
* 只有当模板的像素的 alpha 大于 alphaThreshold 时,才会绘制内容。<br/>
|
||||
* 该数值 0 ~ 1 之间的浮点数,默认值为 1(因此禁用 alpha)
|
||||
* 该数值 0 ~ 1 之间的浮点数,默认值为 0(因此禁用 alpha 测试)
|
||||
* 当被设置为 1 时,会丢弃所有蒙版像素,所以不会显示任何内容,在之前的版本中,设置为 1 等同于 0,这种效果其实是不正确的
|
||||
* @property alphaThreshold
|
||||
* @type {Number}
|
||||
* @default 1
|
||||
* @default 0
|
||||
*/
|
||||
alphaThreshold: {
|
||||
default: 1,
|
||||
default: 0,
|
||||
type: cc.Float,
|
||||
range: [0, 1, 0.1],
|
||||
slide: true,
|
||||
@@ -147,7 +171,9 @@ var Mask = cc.Class({
|
||||
cc.warnID(4201);
|
||||
return;
|
||||
}
|
||||
this._sgNode.setAlphaThreshold(this.alphaThreshold);
|
||||
if (this._frontMaterial) {
|
||||
this._frontMaterial.alphaThreshold = this.alphaThreshold;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -167,25 +193,24 @@ var Mask = cc.Class({
|
||||
cc.warnID(4202);
|
||||
return;
|
||||
}
|
||||
this._sgNode.setInverted(this.inverted);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* TODO: remove segments, not supported by graphics
|
||||
* !#en The segements for ellipse mask.
|
||||
* !#zh 椭圆遮罩的曲线细分数
|
||||
* @property segements
|
||||
* @type {Number}
|
||||
* @default 64
|
||||
*/
|
||||
_segements: 64,
|
||||
_segments: 64,
|
||||
segements: {
|
||||
get: function () {
|
||||
return this._segements;
|
||||
return this._segments;
|
||||
},
|
||||
set: function (value) {
|
||||
this._segements = cc.clampf(value, SEGEMENTS_MIN, SEGEMENTS_MAX);
|
||||
this._refreshStencil();
|
||||
this._segments = cc.clampf(value, SEGEMENTS_MIN, SEGEMENTS_MAX);
|
||||
},
|
||||
tooltip: CC_DEV && 'i18n:COMPONENT.mask.segements',
|
||||
},
|
||||
@@ -211,10 +236,61 @@ var Mask = cc.Class({
|
||||
}
|
||||
},
|
||||
|
||||
_initSgNode: function () {},
|
||||
_onTextureLoaded: function (event) {
|
||||
// Mark render data dirty
|
||||
if (this._renderData) {
|
||||
this._renderData.uvDirty = true;
|
||||
this._renderData.vertDirty = true;
|
||||
}
|
||||
// Reactivate material
|
||||
if (this.enabledInHierarchy) {
|
||||
this._updateMaterial();
|
||||
}
|
||||
},
|
||||
|
||||
_createSgNode: function () {
|
||||
return new cc.ClippingNode();
|
||||
_applySpriteFrame: function (oldFrame) {
|
||||
if (oldFrame && oldFrame.off) {
|
||||
oldFrame.off('load', this._onTextureLoaded, this);
|
||||
}
|
||||
var spriteFrame = this._spriteFrame;
|
||||
if (spriteFrame) {
|
||||
if (spriteFrame.textureLoaded) {
|
||||
this._onTextureLoaded(null);
|
||||
}
|
||||
else {
|
||||
spriteFrame.once('load', this._onTextureLoaded, this);
|
||||
spriteFrame.ensureLoadTexture();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_updateMaterial: function () {
|
||||
// cannot be activated if texture not loaded yet
|
||||
if (this._type === MaskType.IMAGE_STENCIL && (!this.spriteFrame || !this.spriteFrame.textureLoaded)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Init material
|
||||
if (!this._material) {
|
||||
this._frontMaterial = new StencilMaterial();
|
||||
this._endMaterial = new StencilMaterial();
|
||||
this._material = this._frontMaterial;
|
||||
}
|
||||
|
||||
// Reset material
|
||||
if (this._type === MaskType.IMAGE_STENCIL) {
|
||||
var texture = this.spriteFrame.getTexture();
|
||||
this._frontMaterial.useTexture = true;
|
||||
this._frontMaterial.texture = texture.getImpl();
|
||||
this._frontMaterial.alphaThreshold = this.alphaThreshold;
|
||||
this._endMaterial.useTexture = true;
|
||||
this._endMaterial.texture = texture.getImpl();
|
||||
this._endMaterial.alphaThreshold = this.alphaThreshold;
|
||||
}
|
||||
else {
|
||||
this._frontMaterial.useTexture = false;
|
||||
this._endMaterial.useTexture = false;
|
||||
}
|
||||
},
|
||||
|
||||
_hitTest: function (point) {
|
||||
@@ -244,101 +320,15 @@ var Mask = cc.Class({
|
||||
},
|
||||
|
||||
onEnable: function () {
|
||||
if (this.spriteFrame) {
|
||||
this.spriteFrame.ensureLoadTexture();
|
||||
}
|
||||
this._refreshStencil();
|
||||
this.node.on('size-changed', this._refreshStencil, this);
|
||||
this.node.on('anchor-changed', this._refreshStencil, this);
|
||||
this._super();
|
||||
this._updateMaterial();
|
||||
},
|
||||
|
||||
onDisable: function () {
|
||||
this.node.off('size-changed', this._refreshStencil, this);
|
||||
this.node.off('anchor-changed', this._refreshStencil, this);
|
||||
onDisable () {
|
||||
this._super();
|
||||
this._frontMaterial = null;
|
||||
this._endMaterial = null;
|
||||
},
|
||||
|
||||
_calculateCircle: function (center, radius, segements) {
|
||||
var polies =[];
|
||||
var anglePerStep = Math.PI * 2 / segements;
|
||||
for(var step = 0; step < segements; ++ step) {
|
||||
polies.push(cc.v2(radius.x * Math.cos(anglePerStep * step) + center.x,
|
||||
radius.y * Math.sin(anglePerStep * step) + center.y));
|
||||
}
|
||||
|
||||
return polies;
|
||||
},
|
||||
|
||||
_refreshStencil: function () {
|
||||
// Check whether the conditions are met
|
||||
if (this.type === MaskType.IMAGE_STENCIL &&
|
||||
cc._renderType !== cc.game.RENDER_TYPE_WEBGL && !CC_JSB) {
|
||||
cc.warnID(4200);
|
||||
return;
|
||||
}
|
||||
|
||||
var contentSize = this.node.getContentSize();
|
||||
var anchorPoint = this.node.getAnchorPoint();
|
||||
var stencil = this._clippingStencil;
|
||||
if (this._type === MaskType.IMAGE_STENCIL) {
|
||||
var isSgSprite = stencil instanceof cc.Scale9Sprite;
|
||||
if (!isSgSprite || stencil._spriteFrame !== this.spriteFrame) {
|
||||
stencil = new cc.Scale9Sprite();
|
||||
stencil.setSpriteFrame(this.spriteFrame);
|
||||
this._sgNode.setStencil(stencil);
|
||||
}
|
||||
stencil.setContentSize(contentSize);
|
||||
stencil.setAnchorPoint(anchorPoint);
|
||||
this._sgNode.setAlphaThreshold(this.alphaThreshold);
|
||||
}
|
||||
else {
|
||||
var isDrawNode = stencil instanceof cc.DrawNode;
|
||||
if (!isDrawNode) {
|
||||
stencil = new cc.DrawNode();
|
||||
if (CC_JSB) {
|
||||
stencil.retain();
|
||||
}
|
||||
this._sgNode.setStencil(stencil);
|
||||
}
|
||||
var width = contentSize.width;
|
||||
var height = contentSize.height;
|
||||
var x = - width * anchorPoint.x;
|
||||
var y = - height * anchorPoint.y;
|
||||
var color = cc.color(255, 255, 255, 0);
|
||||
stencil.clear();
|
||||
if (this._type === MaskType.RECT) {
|
||||
var rectangle = [cc.v2(x, y),
|
||||
cc.v2(x + width, y),
|
||||
cc.v2(x + width, y + height),
|
||||
cc.v2(x, y + height)];
|
||||
stencil.drawPoly(rectangle, color, 0, color);
|
||||
}
|
||||
else if (this._type === MaskType.ELLIPSE) {
|
||||
var center = cc.v2(x + width / 2, y + height / 2);
|
||||
var radius = {
|
||||
x: width / 2,
|
||||
y: height / 2
|
||||
};
|
||||
stencil.drawPoly(this._calculateCircle(center, radius, this._segements), color, 0, color);
|
||||
}
|
||||
}
|
||||
this._sgNode.setInverted(this.inverted);
|
||||
this._clippingStencil = stencil;
|
||||
if (!CC_JSB) {
|
||||
cc.renderer.childrenOrderDirty = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (CC_JSB) {
|
||||
// override onDestroy
|
||||
Mask.prototype.__superOnDestroy = Base.prototype.onDestroy;
|
||||
Mask.prototype.onDestroy = function () {
|
||||
this.__superOnDestroy();
|
||||
if (this._clippingStencil) {
|
||||
this._clippingStencil.release();
|
||||
this._clippingStencil = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
cc.Mask = module.exports = Mask;
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
|
||||
const Component = require('./CCComponent');
|
||||
const defaultVertexFormat = require('../renderer/vertex-format');
|
||||
const renderEngine = require('../renderer/render-engine');
|
||||
const RenderData = renderEngine.RenderData;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -51,6 +53,19 @@ var RenderComponent = cc.Class({
|
||||
this._toPostHandle = false;
|
||||
},
|
||||
|
||||
onEnable () {
|
||||
this.node._renderComponent = this;
|
||||
},
|
||||
|
||||
onDisable () {
|
||||
this.node._renderComponent = null;
|
||||
this._material = null;
|
||||
if (this._renderData) {
|
||||
RenderData.free(this._renderData);
|
||||
this._renderData = null;
|
||||
}
|
||||
},
|
||||
|
||||
getEffect () {
|
||||
if (this._material) {
|
||||
return this._material.effect;
|
||||
|
||||
@@ -268,7 +268,7 @@ var Sprite = cc.Class({
|
||||
if (value !== this._fillType) {
|
||||
if (value === FillType.RADIAL || this._fillType === FillType.RADIAL) {
|
||||
RenderData.free(this._renderData);
|
||||
this._renderData = null;
|
||||
this._renderData = null;
|
||||
}
|
||||
else if (this._renderData) {
|
||||
this._renderData.uvDirty = true;
|
||||
@@ -474,15 +474,8 @@ var Sprite = cc.Class({
|
||||
},
|
||||
|
||||
onEnable: function () {
|
||||
this._super();
|
||||
this._activateMaterial();
|
||||
this.node._renderComponent = this;
|
||||
},
|
||||
|
||||
onDisable: function () {
|
||||
RenderData.free(this._renderData);
|
||||
this._material = null;
|
||||
this._renderData = null;
|
||||
this.node._renderComponent = null;
|
||||
},
|
||||
|
||||
_activateMaterial: function () {
|
||||
@@ -557,6 +550,7 @@ var Sprite = cc.Class({
|
||||
// Mark render data dirty
|
||||
if (this._renderData) {
|
||||
this._renderData.uvDirty = true;
|
||||
this._renderData.vertDirty = true;
|
||||
}
|
||||
// Reactivate material
|
||||
if (this.enabledInHierarchy) {
|
||||
@@ -566,7 +560,6 @@ var Sprite = cc.Class({
|
||||
},
|
||||
|
||||
_applySpriteFrame: function (oldFrame) {
|
||||
var sgNode = this._sgNode;
|
||||
if (oldFrame && oldFrame.off) {
|
||||
oldFrame.off('load', this._onTextureLoaded, this);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ let graphicsAssembler = {
|
||||
updateRenderData (graphics) {
|
||||
// Create render data if needed
|
||||
if (!graphics._renderData) {
|
||||
graphics._renderData = new RenderData();
|
||||
graphics._renderData = RenderData.alloc();
|
||||
}
|
||||
|
||||
let renderData = graphics._renderData;
|
||||
@@ -84,7 +84,7 @@ let graphicsAssembler = {
|
||||
|
||||
stroke (graphics) {
|
||||
if (!graphics._renderData) {
|
||||
graphics._renderData = new RenderData();
|
||||
graphics._renderData = RenderData.alloc();
|
||||
}
|
||||
|
||||
this._curColor = graphics._strokeColor._val;
|
||||
@@ -97,7 +97,7 @@ let graphicsAssembler = {
|
||||
|
||||
fill (graphics) {
|
||||
if (!graphics._renderData) {
|
||||
graphics._renderData = new RenderData();
|
||||
graphics._renderData = RenderData.alloc();
|
||||
}
|
||||
|
||||
this._curColor = graphics._fillColor._val;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require('./sprite-assembler');
|
||||
require('./mask-assembler');
|
||||
require('./motion-streak-assembler');
|
||||
require('./graphics/graphics-assembler');
|
||||
require('./label/label-assembler');
|
||||
@@ -0,0 +1,136 @@
|
||||
const StencilManager = require('../stencil-manager');
|
||||
const spriteUtil = require('./sprite-assembler').simpleRenderUtil;
|
||||
const Node = require('../../CCNode');
|
||||
const Graphics = require('../../graphics/graphics');
|
||||
const Mask = require('../../components/CCMask');
|
||||
const renderEngine = require('../render-engine');
|
||||
const RenderData = renderEngine.RenderData;
|
||||
|
||||
let _stencilMgr = StencilManager.sharedManager;
|
||||
let _color = cc.color(255, 255, 255, 0);
|
||||
let _graphicsNode = new Node();
|
||||
let _graphics = _graphicsNode.addComponent(Graphics);
|
||||
_graphics.lineWidth = 0;
|
||||
|
||||
let maskFrontAssembler = {
|
||||
update (mask) {
|
||||
let renderData = mask._renderData;
|
||||
if (renderData.vertDirty) {
|
||||
// Share render data with graphics content
|
||||
_graphics._renderData = renderData;
|
||||
_graphics.clear(false);
|
||||
let width = renderData._width;
|
||||
let height = renderData._height;
|
||||
let x = -width * renderData._pivotX;
|
||||
let y = -height * renderData._pivotY;
|
||||
if (mask._type === Mask.Type.RECT) {
|
||||
_graphics.rect(x, y, width, height);
|
||||
}
|
||||
else if (mask._type === Mask.Type.ELLIPSE) {
|
||||
let cx = x + width / 2,
|
||||
cy = y + height / 2,
|
||||
rx = width / 2,
|
||||
ry = height / 2;
|
||||
_graphics.ellipse(cx, cy, rx, ry);
|
||||
}
|
||||
_graphics.fill();
|
||||
}
|
||||
},
|
||||
|
||||
updateRenderData (mask) {
|
||||
if (!mask._renderData) {
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL) {
|
||||
mask._renderData = spriteUtil.createData(mask);
|
||||
}
|
||||
else {
|
||||
mask._renderData = RenderData.alloc();
|
||||
}
|
||||
}
|
||||
let renderData = mask._renderData;
|
||||
let size = mask.node._contentSize;
|
||||
let anchor = mask.node._anchorPoint;
|
||||
renderData.updateSizeNPivot(size.width, size.height, anchor.x, anchor.y);
|
||||
|
||||
mask._material = mask._frontMaterial;
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL) {
|
||||
if (mask.spriteFrame) {
|
||||
renderData.dataLength = 4;
|
||||
spriteUtil.update(mask);
|
||||
}
|
||||
else {
|
||||
mask._material = null;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.update(mask);
|
||||
}
|
||||
},
|
||||
|
||||
fillVertexBuffer (mask, index, vbuf, uintbuf) {
|
||||
// Invalid state
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL && !mask.spriteFrame) {
|
||||
return;
|
||||
}
|
||||
// HACK: Must push mask after batch, so we can only put this logic in fillVertexBuffer or fillIndexBuffer
|
||||
_stencilMgr.pushMask(mask);
|
||||
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL) {
|
||||
spriteUtil.fillVertexBuffer(mask, index, vbuf, uintbuf);
|
||||
}
|
||||
else {
|
||||
// Share node for correct global matrix
|
||||
_graphics.node = mask.node;
|
||||
_graphics._renderData = mask._renderData;
|
||||
Graphics._assembler.fillVertexBuffer(_graphics, index, vbuf, uintbuf);
|
||||
}
|
||||
},
|
||||
fillIndexBuffer (mask, offset, vertexId, ibuf) {
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL) {
|
||||
if (mask.spriteFrame) {
|
||||
spriteUtil.fillIndexBuffer(mask, offset, vertexId, ibuf);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Graphics._assembler.fillIndexBuffer(_graphics, offset, vertexId, ibuf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let maskEndAssembler = {
|
||||
updateRenderData (mask) {
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL && !mask.spriteFrame) {
|
||||
mask._material = null;
|
||||
}
|
||||
else {
|
||||
mask._material = mask._endMaterial;
|
||||
}
|
||||
},
|
||||
|
||||
fillVertexBuffer (mask, index, vbuf, uintbuf) {
|
||||
// Invalid state
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL && !mask.spriteFrame) {
|
||||
return;
|
||||
}
|
||||
// HACK: Must pop mask after batch, so we can only put this logic in fillVertexBuffer or fillIndexBuffer
|
||||
_stencilMgr.popMask();
|
||||
|
||||
if (mask._type === Mask.Type.IMAGE_STENCIL) {
|
||||
spriteUtil.fillVertexBuffer(mask, index, vbuf, uintbuf);
|
||||
}
|
||||
else {
|
||||
// Share node for correct global matrix
|
||||
_graphics.node = mask.node;
|
||||
_graphics._renderData = mask._renderData;
|
||||
Graphics._assembler.fillVertexBuffer(_graphics, index, vbuf, uintbuf);
|
||||
}
|
||||
},
|
||||
fillIndexBuffer: maskFrontAssembler.fillIndexBuffer
|
||||
}
|
||||
|
||||
Mask._assembler = maskFrontAssembler;
|
||||
Mask._postAssembler = maskEndAssembler;
|
||||
|
||||
module.exports = {
|
||||
front: maskFrontAssembler,
|
||||
end: maskEndAssembler
|
||||
}
|
||||
@@ -116,28 +116,44 @@ let simpleRenderUtil = {
|
||||
updateVerts (sprite) {
|
||||
let renderData = sprite._renderData,
|
||||
data = renderData._data,
|
||||
frame = sprite.spriteFrame,
|
||||
width, height;
|
||||
cw = renderData._width,
|
||||
ch = renderData._height,
|
||||
appx = renderData._pivotX * cw,
|
||||
appy = renderData._pivotY * ch,
|
||||
l, b, r, t;
|
||||
if (sprite.trim) {
|
||||
width = frame._rect.width;
|
||||
height = frame._rect.height;
|
||||
l = -appx;
|
||||
b = -appy;
|
||||
r = cw - appx;
|
||||
t = ch - appy;
|
||||
}
|
||||
else {
|
||||
width = frame._originalSize.width;
|
||||
height = frame._originalSize.height;
|
||||
let frame = sprite.spriteFrame,
|
||||
ow = frame._originalSize.width,
|
||||
oh = frame._originalSize.height,
|
||||
rw = frame._rect.width,
|
||||
rh = frame._rect.height,
|
||||
offset = frame._offset,
|
||||
scaleX = cw / ow,
|
||||
scaleY = ch / oh;
|
||||
let trimLeft = offset.x + (ow - rw) / 2;
|
||||
let trimRight = offset.x - (ow - rw) / 2;
|
||||
let trimBottom = offset.y + (oh - rh) / 2;
|
||||
let trimTop = offset.y - (oh - rh) / 2;
|
||||
l = trimLeft * scaleX - appx;
|
||||
b = trimBottom * scaleY - appy;
|
||||
r = cw + trimRight * scaleX - appx;
|
||||
t = ch + trimTop * scaleY - appy;
|
||||
}
|
||||
|
||||
let appx = renderData._pivotX * width,
|
||||
appy = renderData._pivotY * height;
|
||||
|
||||
data[0].x = -appx;
|
||||
data[0].y = -appy;
|
||||
data[1].x = width - appx;
|
||||
data[1].y = -appy;
|
||||
data[2].x = -appx;
|
||||
data[2].y = height - appy;
|
||||
data[3].x = width - appx;
|
||||
data[3].y = height - appy;
|
||||
data[0].x = l;
|
||||
data[0].y = b;
|
||||
data[1].x = r;
|
||||
data[1].y = b;
|
||||
data[2].x = l;
|
||||
data[2].y = t;
|
||||
data[3].x = r;
|
||||
data[3].y = t;
|
||||
|
||||
renderData.vertDirty = false;
|
||||
},
|
||||
@@ -995,6 +1011,11 @@ let filledRenderUtil = {
|
||||
|
||||
// Inline all type switch to avoid jit deoptimization during inlined function change
|
||||
var spriteAssembler = {
|
||||
simpleRenderUtil: simpleRenderUtil,
|
||||
slicedRenderUtil: slicedRenderUtil,
|
||||
tiledRenderUtil: tiledRenderUtil,
|
||||
filledRenderUtil: filledRenderUtil,
|
||||
|
||||
updateRenderData (sprite) {
|
||||
// Create render data if needed
|
||||
if (!sprite._renderData) {
|
||||
|
||||
@@ -23,9 +23,10 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
const macro = require('../platform/CCMacro');
|
||||
const renderEngine = require('./render-engine');
|
||||
const defaultVertexFormat = require('./vertex-format');
|
||||
const macro = require('../platform/CCMacro');
|
||||
const StencilManager = require('./stencil-manager');
|
||||
const gfx = renderEngine.gfx;
|
||||
const RecyclePool = renderEngine.RecyclePool;
|
||||
const InputAssembler = renderEngine.InputAssembler;
|
||||
@@ -40,6 +41,7 @@ var _queue = null;
|
||||
var RenderComponentWalker = function (device, renderScene) {
|
||||
this._renderScene = renderScene;
|
||||
this._device = device;
|
||||
this._stencilMgr = StencilManager.sharedManager;
|
||||
|
||||
// Buffers
|
||||
let verts = new Float32Array(MAX_VERTEX * FLOATS_PER_VERT);
|
||||
@@ -167,6 +169,9 @@ RenderComponentWalker.prototype = {
|
||||
ia._indexBuffer = ib;
|
||||
ia._start = 0;
|
||||
ia._count = indiceCount;
|
||||
|
||||
// Check stencil state and modify pass
|
||||
this._stencilMgr.handleEffect(effect);
|
||||
|
||||
// Generate model
|
||||
let model = this._modelPool.add();
|
||||
@@ -201,7 +206,7 @@ RenderComponentWalker.prototype = {
|
||||
_checkBatchBroken (vertexFormat, currEffect, vertexOffset, indiceOffset, needNewBuf) {
|
||||
let vertexCount = vertexOffset - this._vOffset,
|
||||
indiceCount = indiceOffset - this._iOffset;
|
||||
if (vertexFormat && currEffect) {
|
||||
if (vertexFormat && currEffect && vertexCount > 0 && indiceCount > 0) {
|
||||
this._flush(vertexFormat, currEffect, vertexCount, indiceCount);
|
||||
if (needNewBuf) {
|
||||
this._switchBuffer();
|
||||
@@ -224,13 +229,13 @@ RenderComponentWalker.prototype = {
|
||||
uintVerts = this._uintVerts,
|
||||
indices = this._indices,
|
||||
currEffect = null,
|
||||
vertexFormat = null,
|
||||
vertexOffset = this._vOffset,
|
||||
indiceOffset = this._iOffset,
|
||||
vertexId = 0,
|
||||
needNewBuf = false,
|
||||
broken = false;
|
||||
comp = null,
|
||||
broken = false,
|
||||
comp = this._queue[0],
|
||||
prevComp = null,
|
||||
effect = null,
|
||||
assembler = null,
|
||||
data = null;
|
||||
@@ -247,8 +252,7 @@ RenderComponentWalker.prototype = {
|
||||
comp._toPostHandle = true;
|
||||
}
|
||||
}
|
||||
effect = comp.getEffect();
|
||||
if (!assembler || !effect) {
|
||||
if (!assembler) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -256,11 +260,12 @@ RenderComponentWalker.prototype = {
|
||||
assembler.updateRenderData(comp);
|
||||
data = comp._renderData;
|
||||
|
||||
// Must get effect after updateRenderData, material maybe modified during updateRenderData
|
||||
effect = comp.getEffect();
|
||||
// breaking batch
|
||||
needNewBuf = (vertexOffset + data.vertexCount > MAX_VERTEX) || (indiceOffset + data.indiceCount > MAX_INDICE);
|
||||
if (currEffect != effect || needNewBuf) {
|
||||
vertexFormat = comp._vertexFormat;
|
||||
broken = this._checkBatchBroken(vertexFormat, currEffect, vertexOffset, indiceOffset, needNewBuf);
|
||||
if (prevComp && currEffect != effect || needNewBuf) {
|
||||
broken = this._checkBatchBroken(prevComp._vertexFormat, currEffect, vertexOffset, indiceOffset, needNewBuf);
|
||||
if (broken) {
|
||||
vertexOffset = this._vOffset;
|
||||
indiceOffset = this._iOffset;
|
||||
@@ -272,17 +277,22 @@ RenderComponentWalker.prototype = {
|
||||
}
|
||||
currEffect = effect;
|
||||
}
|
||||
// Init effect
|
||||
else if (!currEffect) {
|
||||
currEffect = effect;
|
||||
}
|
||||
|
||||
vertexId = vertexOffset - this._vOffset;
|
||||
assembler.fillVertexBuffer(comp, vertexOffset, verts, uintVerts);
|
||||
assembler.fillIndexBuffer(comp, indiceOffset, vertexId, indices);
|
||||
|
||||
|
||||
vertexOffset += data.vertexCount;
|
||||
indiceOffset += data.indiceCount;
|
||||
prevComp = comp;
|
||||
}
|
||||
|
||||
// last batch
|
||||
this._checkBatchBroken(comp && comp._vertexFormat, currEffect, vertexOffset, indiceOffset, false);
|
||||
this._checkBatchBroken(prevComp && prevComp._vertexFormat, currEffect, vertexOffset, indiceOffset, false);
|
||||
this._queue.length = 0;
|
||||
},
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,134 @@
|
||||
const Enums = require('../platform/CCEnum');
|
||||
const gfx = require('./render-engine').gfx;
|
||||
|
||||
// Stage types
|
||||
var Stage = cc.Enum({
|
||||
// Stencil disabled
|
||||
DISABLED: 0,
|
||||
// Entering a new level, should handle new stencil
|
||||
ENTER_LEVEL: 1,
|
||||
// In stencil
|
||||
ENABLED: 2,
|
||||
// Exiting a level, should restore old stencil or disable
|
||||
EXIT_LEVEL: 3,
|
||||
});
|
||||
|
||||
function StencilManager () {
|
||||
// todo: 8 is least Stencil depth supported by webGL device, it could be adjusted to vendor implementation value
|
||||
this._maxLevel = 8;
|
||||
// Current mask
|
||||
this._maskStack = [];
|
||||
// Current stage of process, DISABLED, ENTER_LEVEL, ENABLED, EXIT_LEVEL
|
||||
this.stage = Stage.DISABLED;
|
||||
}
|
||||
|
||||
StencilManager.prototype = {
|
||||
constructor: StencilManager,
|
||||
|
||||
handleEffect (effect) {
|
||||
let technique = effect.getTechnique('transparent');
|
||||
if (this.stage === Stage.DISABLED) {
|
||||
this.stage = Stage.DISABLED;
|
||||
for (let i = 0; i < technique._passes.length; ++i) {
|
||||
let pass = technique._passes[i];
|
||||
pass._stencilTest = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let mask, func, ref, stencilMask, writeMask, failOp,
|
||||
zFailOp = gfx.STENCIL_OP_KEEP,
|
||||
zPassOp = gfx.STENCIL_OP_KEEP;
|
||||
|
||||
if (this.stage === Stage.ENABLED) {
|
||||
func = gfx.DS_FUNC_EQUAL;
|
||||
mask = this._maskStack[this._maskStack.length - 1];
|
||||
if (mask.inverted) {
|
||||
ref = this.getInvertedRef();
|
||||
stencilMask = this.getStencilRef();
|
||||
}
|
||||
else {
|
||||
ref = this.getStencilRef();
|
||||
stencilMask = ref;
|
||||
}
|
||||
failOp = gfx.STENCIL_OP_KEEP;
|
||||
writeMask = 0;
|
||||
}
|
||||
else {
|
||||
func = gfx.DS_FUNC_NEVER;
|
||||
failOp = gfx.STENCIL_OP_REPLACE;
|
||||
|
||||
if (this.stage === Stage.ENTER_LEVEL) {
|
||||
this.stage = Stage.ENABLED;
|
||||
// Fill stencil mask
|
||||
ref = this.getStencilRef();
|
||||
stencilMask = this.getWriteMask();
|
||||
writeMask = stencilMask;
|
||||
}
|
||||
else if (this.stage === Stage.EXIT_LEVEL) {
|
||||
// Pop mask after getting the correct stencil mask
|
||||
if (this._maskStack.length === 0) {
|
||||
this.stage = Stage.DISABLED;
|
||||
}
|
||||
else {
|
||||
this.stage = Stage.ENABLED;
|
||||
}
|
||||
// Clear stencil mask
|
||||
ref = 0;
|
||||
stencilMask = this.getExitWriteMask();
|
||||
writeMask = stencilMask;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < technique._passes.length; ++i) {
|
||||
let pass = technique._passes[i];
|
||||
pass.setStencilFront(func, ref, stencilMask, failOp, zFailOp, zPassOp, writeMask);
|
||||
pass.setStencilBack(func, ref, stencilMask, failOp, zFailOp, zPassOp, writeMask);
|
||||
}
|
||||
},
|
||||
|
||||
pushMask (mask) {
|
||||
if (this._maskStack.length + 1 > this._maxLevel) {
|
||||
cc.errorID(9000, this._maxLevel);
|
||||
}
|
||||
this._maskStack.push(mask);
|
||||
this.stage = Stage.ENTER_LEVEL;
|
||||
},
|
||||
|
||||
popMask () {
|
||||
if (this._maskStack.length === 0) {
|
||||
cc.errorID(9001);
|
||||
}
|
||||
this._maskStack.pop();
|
||||
this.stage = Stage.EXIT_LEVEL;
|
||||
},
|
||||
|
||||
getWriteMask () {
|
||||
return 0x01 << (this._maskStack.length - 1);
|
||||
},
|
||||
|
||||
getExitWriteMask () {
|
||||
return 0x01 << this._maskStack.length;
|
||||
},
|
||||
|
||||
getStencilRef () {
|
||||
let result = 0;
|
||||
for (let i = 0; i < this._maskStack.length; ++i) {
|
||||
result += (0x01 << i);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
getInvertedRef () {
|
||||
let result = 0;
|
||||
for (let i = 0; i < this._maskStack.length - 1; ++i) {
|
||||
result += (0x01 << i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
StencilManager.sharedManager = new StencilManager();
|
||||
StencilManager.Stage = Stage;
|
||||
|
||||
module.exports = StencilManager;
|
||||
@@ -249,7 +249,7 @@ var Color = (function () {
|
||||
* @return {Number} red value.
|
||||
*/
|
||||
proto.getR = function () {
|
||||
return (this._val & 0xff000000) >>> 24;
|
||||
return this._val & 0x000000ff;
|
||||
};
|
||||
/**
|
||||
* !#en Sets red value and return the current color object
|
||||
@@ -272,7 +272,7 @@ var Color = (function () {
|
||||
* @return {Number} green value.
|
||||
*/
|
||||
proto.getG = function () {
|
||||
return (this._val & 0x00ff0000) >> 16;
|
||||
return (this._val & 0x0000ff00) >> 8;
|
||||
};
|
||||
/**
|
||||
* !#en Sets green value and return the current color object
|
||||
@@ -295,7 +295,7 @@ var Color = (function () {
|
||||
* @return {Number} blue value.
|
||||
*/
|
||||
proto.getB = function () {
|
||||
return (this._val & 0x0000ff00) >> 8;
|
||||
return (this._val & 0x00ff0000) >> 16;
|
||||
};
|
||||
/**
|
||||
* !#en Sets blue value and return the current color object
|
||||
@@ -318,7 +318,7 @@ var Color = (function () {
|
||||
* @return {Number} alpha value.
|
||||
*/
|
||||
proto.getA = function () {
|
||||
return this._val & 0x000000ff;
|
||||
return (this._val & 0xff000000) >>> 24;
|
||||
};
|
||||
/**
|
||||
* !#en Sets alpha value and return the current color object
|
||||
@@ -331,7 +331,7 @@ var Color = (function () {
|
||||
* color.setA(0); // Color {r: 0, g: 0, b: 0, a: 0}
|
||||
*/
|
||||
proto.setA = function (alpha) {
|
||||
this._val = ((this._val & 0x00ffffff) | (~~alpha<<24) >>> 0) >>> 0;
|
||||
this._val = ((this._val & 0x00ffffff) | ((~~alpha << 24) >>> 0)) >>> 0;
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
@@ -62,9 +62,6 @@ var jsbSkipModules = [
|
||||
'../../cocos2d/core/graphics/helper.js',
|
||||
'../../cocos2d/audio/CCAudio',
|
||||
'../../cocos2d/shape-nodes/CCDrawNode.js',
|
||||
'../../cocos2d/clipping-nodes/CCClippingNode.js',
|
||||
'../../cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js',
|
||||
'../../cocos2d/clipping-nodes/CCClippingNodeWebGLRenderCmd.js',
|
||||
'../../cocos2d/particle/CCSGParticleSystem.js',
|
||||
'../../cocos2d/particle/CCSGParticleSystemCanvasRenderCmd.js',
|
||||
'../../cocos2d/particle/CCSGParticleSystemWebGLRenderCmd.js',
|
||||
@@ -75,8 +72,6 @@ var jsbSkipModules = [
|
||||
'../../cocos2d/tilemap/CCSGTMXLayer.js',
|
||||
'../../cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js',
|
||||
'../../cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js',
|
||||
'../../cocos2d/motion-streak/CCSGMotionStreak.js',
|
||||
'../../cocos2d/motion-streak/CCSGMotionStreakWebGLRenderCmd.js',
|
||||
'../../cocos2d/render-texture/CCRenderTexture.js',
|
||||
'../../cocos2d/render-texture/CCRenderTextureCanvasRenderCmd.js',
|
||||
'../../cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js',
|
||||
|
||||
Reference in New Issue
Block a user