diff --git a/cocos2d/core/base-nodes/CCSGNodeBaseRenderCmd.js b/cocos2d/core/base-nodes/CCSGNodeBaseRenderCmd.js new file mode 100644 index 0000000000..1d2d9a5bec --- /dev/null +++ b/cocos2d/core/base-nodes/CCSGNodeBaseRenderCmd.js @@ -0,0 +1,573 @@ +/**************************************************************************** + 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. + ****************************************************************************/ + + +//---------------------- Customer render cmd -------------------- +cc.CustomRenderCmd = function (target, func) { + this._needDraw = true; + this._target = target; + this._callback = func; +}; +cc.CustomRenderCmd.prototype.rendering = function (ctx, scaleX, scaleY) { + if (!this._callback) + return; + this._callback.call(this._target, ctx, scaleX, scaleY); +}; + +var dirtyFlags = _ccsg.Node._dirtyFlags = { + transformDirty: 1 << 0, + visibleDirty: 1 << 1, + colorDirty: 1 << 2, + opacityDirty: 1 << 3, + cacheDirty: 1 << 4, + orderDirty: 1 << 5, + textDirty: 1 << 6, + gradientDirty: 1 << 7, + textureDirty: 1 << 8, + contentDirty: 1 << 9, + cullingDirty: 1 << 10, + COUNT: 9 +}; +cc.js.get(dirtyFlags, 'all', function () { + var count = dirtyFlags.COUNT; + return (1 << count) - 1; +}, false); +_ccsg.Node._requestDirtyFlag = function (key) { + cc.assertID(!dirtyFlags[key], 1622, key); + + var count = dirtyFlags.COUNT; + var value = 1 << count; + dirtyFlags[key] = value; + dirtyFlags.COUNT++; + return value; +}; + +var ONE_DEGREE = Math.PI / 180; + +function walkChildTree (root, funcName) { + var index = 1; + var children, child, curr, parentCmd, i, len; + var stack = _ccsg.Node._performStacks[_ccsg.Node._performing]; + if (!stack) { + stack = []; + _ccsg.Node._performStacks.push(stack); + } + stack.length = 0; + _ccsg.Node._performing++; + stack[0] = root; + + var childChildren; + while (index) { + index--; + curr = stack[index]; + // Avoid memory leak + stack[index] = null; + if (!curr) continue; + children = curr._children; + if (children && children.length > 0) { + parentCmd = curr._renderCmd; + for (i = 0, len = children.length; i < len; ++i) { + child = children[i]; + stack[index] = child; + index++; + child._renderCmd[funcName](parentCmd); + } + } + } + _ccsg.Node._performing--; +} + +//-------------------------Base ------------------------- +_ccsg.Node.RenderCmd = function (renderable) { + this._node = renderable; + this._anchorPointInPoints = new cc.Vec2(0, 0); + + this._needDraw = false; + this._dirtyFlag = 1; + this._curLevel = -1; + + this._cameraFlag = 0; + + this._displayedColor = new cc.Color(255, 255, 255, 255); + this._displayedOpacity = 255; + this._cascadeColorEnabledDirty = false; + this._cascadeOpacityEnabledDirty = false; + + this._transform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0}; + this._worldTransform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0}; + this._inverse = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0}; + this._transformUpdated = false; + + cc.renderer.pushDirtyNode(this); +}; + +_ccsg.Node.RenderCmd.prototype = { + constructor: _ccsg.Node.RenderCmd, + + _ctor: _ccsg.Node.RenderCmd, + + getAnchorPointInPoints: function(){ + return cc.p(this._anchorPointInPoints); + }, + + getDisplayedColor: function(){ + var tmpColor = this._displayedColor; + return cc.color(tmpColor.r, tmpColor.g, tmpColor.b, tmpColor.a); + }, + + getDisplayedOpacity: function(){ + return this._displayedOpacity; + }, + + setCascadeColorEnabledDirty: function(){ + this._cascadeColorEnabledDirty = true; + this.setDirtyFlag(dirtyFlags.colorDirty); + }, + + setCascadeOpacityEnabledDirty:function(){ + this._cascadeOpacityEnabledDirty = true; + this.setDirtyFlag(dirtyFlags.opacityDirty); + }, + + getParentToNodeTransform: function(){ + if (this._dirtyFlag & dirtyFlags.transformDirty) { + cc.affineTransformInvertOut(this.getNodeToParentTransform(), this._inverse); + } + return this._inverse; + }, + + detachFromParent: function(){}, + + _updateAnchorPointInPoint: function() { + var locAPP = this._anchorPointInPoints, locSize = this._node._contentSize, locAnchorPoint = this._node._anchorPoint; + locAPP.x = locSize.width * locAnchorPoint.x; + locAPP.y = locSize.height * locAnchorPoint.y; + this.setDirtyFlag(dirtyFlags.transformDirty); + }, + + setDirtyFlag: function(dirtyFlag){ + if (this._dirtyFlag === 0 && dirtyFlag !== 0) + cc.renderer.pushDirtyNode(this); + this._dirtyFlag |= dirtyFlag; + }, + + getParentRenderCmd: function(){ + if(this._node && this._node._parent && this._node._parent._renderCmd) + return this._node._parent._renderCmd; + return null; + }, + + updateTransform: function (parentCmd, recursive) { + var node = this._node, + pt = parentCmd ? parentCmd._worldTransform : null, + t = this._transform, + wt = this._worldTransform; //get the world transform + + if (!this._transformUpdated) { + var hasRotation = node._rotationX || node._rotationY; + var hasSkew = node._skewX || node._skewY; + var sx = node._scaleX, sy = node._scaleY; + var appX = this._anchorPointInPoints.x, appY = this._anchorPointInPoints.y; + var a = 1, b = 0, c = 0, d = 1; + if (hasRotation || hasSkew) { + // position + t.tx = node._position.x; + t.ty = node._position.y; + + // rotation + if (hasRotation) { + var rotationRadiansX = node._rotationX * ONE_DEGREE; + c = Math.sin(rotationRadiansX); + d = Math.cos(rotationRadiansX); + if (node._rotationY === node._rotationX) { + a = d; + b = -c; + } + else { + var rotationRadiansY = node._rotationY * ONE_DEGREE; + a = Math.cos(rotationRadiansY); + b = -Math.sin(rotationRadiansY); + } + } + + // scale + t.a = a *= sx; + t.b = b *= sx; + t.c = c *= sy; + t.d = d *= sy; + + // skew + if (hasSkew) { + var skx = Math.tan(node._skewX * ONE_DEGREE); + var sky = Math.tan(node._skewY * ONE_DEGREE); + if (skx === Infinity) + skx = 99999999; + if (sky === Infinity) + sky = 99999999; + t.a = a + c * sky; + t.b = b + d * sky; + t.c = c + a * skx; + t.d = d + b * skx; + } + + if (appX || appY) { + t.tx -= t.a * appX + t.c * appY; + t.ty -= t.b * appX + t.d * appY; + // adjust anchorPoint + if (node._ignoreAnchorPointForPosition) { + t.tx += appX; + t.ty += appY; + } + } + } + else { + t.a = sx; + t.b = 0; + t.c = 0; + t.d = sy; + t.tx = node._position.x; + t.ty = node._position.y; + + if (appX || appY) { + t.tx -= t.a * appX; + t.ty -= t.d * appY; + // adjust anchorPoint + if (node._ignoreAnchorPointForPosition) { + t.tx += appX; + t.ty += appY; + } + } + } + } + + // update world transform + if (pt) { + wt.a = t.a * pt.a + t.b * pt.c; + wt.b = t.a * pt.b + t.b * pt.d; + wt.c = t.c * pt.a + t.d * pt.c; + wt.d = t.c * pt.b + t.d * pt.d; + wt.tx = t.tx * pt.a + t.ty * pt.c + pt.tx; + wt.ty = t.tx * pt.b + t.ty * pt.d + pt.ty; + } else { + wt.a = t.a; + wt.b = t.b; + wt.c = t.c; + wt.d = t.d; + wt.tx = t.tx; + wt.ty = t.ty; + } + }, + + transform: function (parentCmd, recursive) { + this.updateTransform(parentCmd); + + if (this._currentRegion) { + this._updateCurrentRegions(); + this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.DirtyDouble); + } + + if (cc.macro.ENABLE_CULLING) { + this._updateCameraFlag(parentCmd); + + if (this._doCulling) { + this._doCulling(); + } + } + else if (this._doCulling) { + this._needDraw = true; + } + + if (recursive) { + walkChildTree(this._node, 'transform'); + } + }, + + _updateCameraFlag: function (parentCmd) { + var Camera = cc.Camera; + + if (cc._renderType === cc.game.RENDER_TYPE_WEBGL && Camera) { + if (parentCmd && this._cameraFlag != Camera.flags.InCamera) { + this._cameraFlag = parentCmd._cameraFlag > 0 ? Camera.flags.ParentInCamera : 0; + } + } + + }, + + culling: function (parentCmd, recursive) { + if (!cc.macro.ENABLE_CULLING) { + if (this._doCulling) { + this._needDraw = true; + } + return; + } + + this._updateCameraFlag(parentCmd); + + if (this._doCulling) { + this._doCulling(); + } + + if (recursive) { + walkChildTree(this._node, 'culling'); + } + }, + + getNodeToParentTransform: function () { + if (this._dirtyFlag & dirtyFlags.transformDirty) { + this.transform(); + } + return this._transform; + }, + + setNodeToParentTransform: function(transform) { + if (transform) { + // use specified transform + this._transform = transform; + this._transformUpdated = true; + } else { + // not use the specified transform + this._transformUpdated = false; + } + this.setDirtyFlag(dirtyFlags.transformDirty); + }, + + _propagateFlagsDown: function(parentCmd) { + if (!parentCmd) return; + + //return; + var locFlag = this._dirtyFlag; + var parentNode = parentCmd._node; + var parentFlag = parentCmd._dirtyFlag; + + // There is a possibility: + // The parent element changed color, child element not change + // This will cause the parent element changed color + // But while the child element does not enter the circulation + // Here will be reset state in last + // In order the child elements get the parent state + if(parentNode._cascadeColorEnabled && (parentFlag & dirtyFlags.colorDirty)) + locFlag |= dirtyFlags.colorDirty; + + if(parentNode._cascadeOpacityEnabled && (parentFlag & dirtyFlags.opacityDirty)) + locFlag |= dirtyFlags.opacityDirty; + + if (parentFlag & dirtyFlags.transformDirty) { + locFlag |= dirtyFlags.transformDirty; + } + + if (parentFlag & dirtyFlags.cullingDirty) { + locFlag |= dirtyFlags.cullingDirty; + } + + this._dirtyFlag = locFlag; + }, + + visit: function (parentCmd) { + var node = this._node, renderer = cc.renderer; + + parentCmd = parentCmd || this.getParentRenderCmd(); + if (parentCmd) { + this._curLevel = parentCmd._curLevel + 1; + } + this._propagateFlagsDown(parentCmd); + + if (isNaN(node._customZ)) { + node._vertexZ = renderer.assignedZ; + renderer.assignedZ += renderer.assignedZStep; + } + + this._syncStatus(parentCmd); + }, + + _updateDisplayColor: function (parentColor) { + var node = this._node; + var locDispColor = this._displayedColor, locRealColor = node._realColor; + var i, len, selChildren, item; + this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.Dirty); + if (this._cascadeColorEnabledDirty && !node._cascadeColorEnabled) { + locDispColor.r = locRealColor.r; + locDispColor.g = locRealColor.g; + locDispColor.b = locRealColor.b; + var whiteColor = new cc.Color(255, 255, 255, 255); + selChildren = node._children; + for (i = 0, len = selChildren.length; i < len; i++) { + item = selChildren[i]; + if (item && item._renderCmd) + item._renderCmd._updateDisplayColor(whiteColor); + } + this._cascadeColorEnabledDirty = false; + } else { + if (parentColor === undefined) { + var locParent = node._parent; + if (locParent && locParent._cascadeColorEnabled) + parentColor = locParent.getDisplayedColor(); + else + parentColor = cc.Color.WHITE; + } + locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0); + locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0); + locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0); + if (node._cascadeColorEnabled) { + selChildren = node._children; + for (i = 0, len = selChildren.length; i < len; i++) { + item = selChildren[i]; + if (item && item._renderCmd){ + item._renderCmd._updateDisplayColor(locDispColor); + item._renderCmd._updateColor(); + } + } + } + } + this._dirtyFlag &= ~dirtyFlags.colorDirty; + }, + + _updateDisplayOpacity: function (parentOpacity) { + var node = this._node; + var i, len, selChildren, item; + this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.Dirty); + if (this._cascadeOpacityEnabledDirty && !node._cascadeOpacityEnabled) { + this._displayedOpacity = node._realOpacity; + selChildren = node._children; + for (i = 0, len = selChildren.length; i < len; i++) { + item = selChildren[i]; + if (item && item._renderCmd) + item._renderCmd._updateDisplayOpacity(255); + } + this._cascadeOpacityEnabledDirty = false; + } else { + if (parentOpacity === undefined) { + var locParent = node._parent; + parentOpacity = 255; + if (locParent && locParent._cascadeOpacityEnabled) + parentOpacity = locParent.getDisplayedOpacity(); + } + this._displayedOpacity = node._realOpacity * parentOpacity / 255.0; + if (node._cascadeOpacityEnabled) { + selChildren = node._children; + for (i = 0, len = selChildren.length; i < len; i++) { + item = selChildren[i]; + if (item && item._renderCmd){ + item._renderCmd._updateDisplayOpacity(this._displayedOpacity); + item._renderCmd._updateColor(); + } + } + } + } + this._dirtyFlag &= ~dirtyFlags.opacityDirty; + }, + + _syncDisplayColor : function (parentColor) { + var node = this._node, locDispColor = this._displayedColor, locRealColor = node._realColor; + if (parentColor === undefined) { + var locParent = node._parent; + if (locParent && locParent._cascadeColorEnabled) + parentColor = locParent.getDisplayedColor(); + else + parentColor = cc.Color.WHITE; + } + locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0); + locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0); + locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0); + }, + + _syncDisplayOpacity : function (parentOpacity) { + var node = this._node; + if (parentOpacity === undefined) { + var locParent = node._parent; + parentOpacity = 255; + if (locParent && locParent._cascadeOpacityEnabled) + parentOpacity = locParent.getDisplayedOpacity(); + } + this._displayedOpacity = node._realOpacity * parentOpacity / 255.0; + }, + + _updateColor: function(){}, + + updateStatus: function () { + var locFlag = this._dirtyFlag; + var colorDirty = locFlag & dirtyFlags.colorDirty, + opacityDirty = locFlag & dirtyFlags.opacityDirty; + + if (locFlag & dirtyFlags.contentDirty) { + this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.Dirty); + this._dirtyFlag &= ~dirtyFlags.contentDirty; + } + + if (colorDirty) + this._updateDisplayColor(); + + if (opacityDirty) + this._updateDisplayOpacity(); + + if (colorDirty || opacityDirty) + this._updateColor(); + + if (locFlag & dirtyFlags.transformDirty) { + var parentCmd = this.getParentRenderCmd(); + + //update the transform + this.transform(parentCmd, true); + this._dirtyFlag &= ~dirtyFlags.transformDirty; + this._dirtyFlag &= ~dirtyFlags.cullingDirty; + } + else if (locFlag & dirtyFlags.cullingDirty) { + this.culling(parentCmd, true); + this._dirtyFlag &= ~dirtyFlags.cullingDirty; + } + }, + + _syncStatus: function (parentCmd) { + // In the visit logic does not restore the _dirtyFlag + // Because child elements need parent's _dirtyFlag to change himself + var locFlag = this._dirtyFlag; + + var colorDirty = locFlag & dirtyFlags.colorDirty, + opacityDirty = locFlag & dirtyFlags.opacityDirty; + + if (colorDirty) + //update the color + this._syncDisplayColor(); + + if (opacityDirty) + //update the opacity + this._syncDisplayOpacity(); + + if (colorDirty || opacityDirty) + this._updateColor(); + + if (locFlag & dirtyFlags.transformDirty) { + //update the transform + this.transform(parentCmd); + } + else if (locFlag & dirtyFlags.cullingDirty) { + this.culling(parentCmd); + } + } +}; + +_ccsg.Node.RenderCmd.prototype.originUpdateTransform = _ccsg.Node.RenderCmd.prototype.updateTransform; +_ccsg.Node.RenderCmd.prototype.originTransform = _ccsg.Node.RenderCmd.prototype.transform; +_ccsg.Node.RenderCmd.prototype.originCulling = _ccsg.Node.RenderCmd.prototype.culling; +_ccsg.Node.RenderCmd.prototype.originUpdateStatus = _ccsg.Node.RenderCmd.prototype.updateStatus; +_ccsg.Node.RenderCmd.prototype._originSyncStatus = _ccsg.Node.RenderCmd.prototype._syncStatus; diff --git a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js index 9bf3d76552..09463e6c54 100644 --- a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js +++ b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js @@ -22,494 +22,6 @@ THE SOFTWARE. ****************************************************************************/ -//---------------------- Customer render cmd -------------------- -cc.CustomRenderCmd = function (target, func) { - this._needDraw = true; - this._target = target; - this._callback = func; -}; -cc.CustomRenderCmd.prototype.rendering = function (ctx, scaleX, scaleY) { - if (!this._callback) - return; - this._callback.call(this._target, ctx, scaleX, scaleY); -}; - -var dirtyFlags = _ccsg.Node._dirtyFlags = { - transformDirty: 1 << 0, - visibleDirty: 1 << 1, - colorDirty: 1 << 2, - opacityDirty: 1 << 3, - cacheDirty: 1 << 4, - orderDirty: 1 << 5, - textDirty: 1 << 6, - gradientDirty: 1 << 7, - textureDirty: 1 << 8, - contentDirty: 1 << 9, - COUNT: 9 -}; -cc.js.get(dirtyFlags, 'all', function () { - var count = dirtyFlags.COUNT; - return (1 << count) - 1; -}, false); -_ccsg.Node._requestDirtyFlag = function (key) { - cc.assertID(!dirtyFlags[key], 1622, key); - - var count = dirtyFlags.COUNT; - var value = 1 << count; - dirtyFlags[key] = value; - dirtyFlags.COUNT++; - return value; -}; - -var ONE_DEGREE = Math.PI / 180; - -function transformChildTree (root) { - var index = 1; - var children, child, curr, parentCmd, i, len; - var stack = _ccsg.Node._performStacks[_ccsg.Node._performing]; - if (!stack) { - stack = []; - _ccsg.Node._performStacks.push(stack); - } - stack.length = 0; - _ccsg.Node._performing++; - stack[0] = root; - while (index) { - index--; - curr = stack[index]; - // Avoid memory leak - stack[index] = null; - if (!curr) continue; - children = curr._children; - if (children && children.length > 0) { - parentCmd = curr._renderCmd; - for (i = 0, len = children.length; i < len; ++i) { - child = children[i]; - stack[index] = child; - index++; - child._renderCmd.transform(parentCmd); - } - } - } - _ccsg.Node._performing--; -} - -//-------------------------Base ------------------------- -_ccsg.Node.RenderCmd = function (renderable) { - this._node = renderable; - this._anchorPointInPoints = new cc.Vec2(0, 0); - - this._needDraw = false; - this._dirtyFlag = 1; - this._curLevel = -1; - - this._cameraFlag = 0; - - this._displayedColor = new cc.Color(255, 255, 255, 255); - this._displayedOpacity = 255; - this._cascadeColorEnabledDirty = false; - this._cascadeOpacityEnabledDirty = false; - - this._transform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0}; - this._worldTransform = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0}; - this._inverse = {a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0}; - this._transformUpdated = false; - - cc.renderer.pushDirtyNode(this); -}; - -_ccsg.Node.RenderCmd.prototype = { - constructor: _ccsg.Node.RenderCmd, - - _ctor: _ccsg.Node.RenderCmd, - - getAnchorPointInPoints: function(){ - return cc.p(this._anchorPointInPoints); - }, - - getDisplayedColor: function(){ - var tmpColor = this._displayedColor; - return cc.color(tmpColor.r, tmpColor.g, tmpColor.b, tmpColor.a); - }, - - getDisplayedOpacity: function(){ - return this._displayedOpacity; - }, - - setCascadeColorEnabledDirty: function(){ - this._cascadeColorEnabledDirty = true; - this.setDirtyFlag(dirtyFlags.colorDirty); - }, - - setCascadeOpacityEnabledDirty:function(){ - this._cascadeOpacityEnabledDirty = true; - this.setDirtyFlag(dirtyFlags.opacityDirty); - }, - - getParentToNodeTransform: function(){ - if (this._dirtyFlag & dirtyFlags.transformDirty) { - cc.affineTransformInvertOut(this.getNodeToParentTransform(), this._inverse); - } - return this._inverse; - }, - - detachFromParent: function(){}, - - _updateAnchorPointInPoint: function() { - var locAPP = this._anchorPointInPoints, locSize = this._node._contentSize, locAnchorPoint = this._node._anchorPoint; - locAPP.x = locSize.width * locAnchorPoint.x; - locAPP.y = locSize.height * locAnchorPoint.y; - this.setDirtyFlag(dirtyFlags.transformDirty); - }, - - setDirtyFlag: function(dirtyFlag){ - if (this._dirtyFlag === 0 && dirtyFlag !== 0) - cc.renderer.pushDirtyNode(this); - this._dirtyFlag |= dirtyFlag; - }, - - getParentRenderCmd: function(){ - if(this._node && this._node._parent && this._node._parent._renderCmd) - return this._node._parent._renderCmd; - return null; - }, - - transform: function (parentCmd, recursive) { - var node = this._node, - pt = parentCmd ? parentCmd._worldTransform : null, - t = this._transform, - wt = this._worldTransform; //get the world transform - - if (!this._transformUpdated) { - var hasRotation = node._rotationX || node._rotationY; - var hasSkew = node._skewX || node._skewY; - var sx = node._scaleX, sy = node._scaleY; - var appX = this._anchorPointInPoints.x, appY = this._anchorPointInPoints.y; - var a = 1, b = 0, c = 0, d = 1; - if (hasRotation || hasSkew) { - // position - t.tx = node._position.x; - t.ty = node._position.y; - - // rotation - if (hasRotation) { - var rotationRadiansX = node._rotationX * ONE_DEGREE; - c = Math.sin(rotationRadiansX); - d = Math.cos(rotationRadiansX); - if (node._rotationY === node._rotationX) { - a = d; - b = -c; - } - else { - var rotationRadiansY = node._rotationY * ONE_DEGREE; - a = Math.cos(rotationRadiansY); - b = -Math.sin(rotationRadiansY); - } - } - - // scale - t.a = a *= sx; - t.b = b *= sx; - t.c = c *= sy; - t.d = d *= sy; - - // skew - if (hasSkew) { - var skx = Math.tan(node._skewX * ONE_DEGREE); - var sky = Math.tan(node._skewY * ONE_DEGREE); - if (skx === Infinity) - skx = 99999999; - if (sky === Infinity) - sky = 99999999; - t.a = a + c * sky; - t.b = b + d * sky; - t.c = c + a * skx; - t.d = d + b * skx; - } - - if (appX || appY) { - t.tx -= t.a * appX + t.c * appY; - t.ty -= t.b * appX + t.d * appY; - // adjust anchorPoint - if (node._ignoreAnchorPointForPosition) { - t.tx += appX; - t.ty += appY; - } - } - } - else { - t.a = sx; - t.b = 0; - t.c = 0; - t.d = sy; - t.tx = node._position.x; - t.ty = node._position.y; - - if (appX || appY) { - t.tx -= t.a * appX; - t.ty -= t.d * appY; - // adjust anchorPoint - if (node._ignoreAnchorPointForPosition) { - t.tx += appX; - t.ty += appY; - } - } - } - } - - // update world transform - if (pt) { - wt.a = t.a * pt.a + t.b * pt.c; - wt.b = t.a * pt.b + t.b * pt.d; - wt.c = t.c * pt.a + t.d * pt.c; - wt.d = t.c * pt.b + t.d * pt.d; - wt.tx = t.tx * pt.a + t.ty * pt.c + pt.tx; - wt.ty = t.tx * pt.b + t.ty * pt.d + pt.ty; - } else { - wt.a = t.a; - wt.b = t.b; - wt.c = t.c; - wt.d = t.d; - wt.tx = t.tx; - wt.ty = t.ty; - } - - if (this._currentRegion) { - this._updateCurrentRegions(); - this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.DirtyDouble); - } - - var Camera = cc.Camera; - if (cc._renderType === cc.game.RENDER_TYPE_WEBGL && Camera) { - // check if node is in camera - var camera = Camera.main; - if (parentCmd && this._cameraFlag != Camera.flags.InCamera) { - this._cameraFlag = parentCmd._cameraFlag > 0 ? Camera.flags.ParentInCamera : 0; - } - } - - if (recursive) { - transformChildTree(node); - } - }, - - getNodeToParentTransform: function () { - if (this._dirtyFlag & dirtyFlags.transformDirty) { - this.transform(); - } - return this._transform; - }, - - setNodeToParentTransform: function(transform) { - if (transform) { - // use specified transform - this._transform = transform; - this._transformUpdated = true; - } else { - // not use the specified transform - this._transformUpdated = false; - } - this.setDirtyFlag(dirtyFlags.transformDirty); - }, - - _propagateFlagsDown: function(parentCmd) { - //return; - var locFlag = this._dirtyFlag; - var parentNode = parentCmd ? parentCmd._node : null; - - // There is a possibility: - // The parent element changed color, child element not change - // This will cause the parent element changed color - // But while the child element does not enter the circulation - // Here will be reset state in last - // In order the child elements get the parent state - if(parentNode && parentNode._cascadeColorEnabled && (parentCmd._dirtyFlag & dirtyFlags.colorDirty)) - locFlag |= dirtyFlags.colorDirty; - - if(parentNode && parentNode._cascadeOpacityEnabled && (parentCmd._dirtyFlag & dirtyFlags.opacityDirty)) - locFlag |= dirtyFlags.opacityDirty; - - if(parentCmd && (parentCmd._dirtyFlag & dirtyFlags.transformDirty)) - locFlag |= dirtyFlags.transformDirty; - - this._dirtyFlag = locFlag; - }, - - visit: function (parentCmd) { - var node = this._node, renderer = cc.renderer; - - parentCmd = parentCmd || this.getParentRenderCmd(); - if (parentCmd) { - this._curLevel = parentCmd._curLevel + 1; - } - this._propagateFlagsDown(parentCmd); - - if (isNaN(node._customZ)) { - node._vertexZ = renderer.assignedZ; - renderer.assignedZ += renderer.assignedZStep; - } - - this._syncStatus(parentCmd); - }, - - _updateDisplayColor: function (parentColor) { - var node = this._node; - var locDispColor = this._displayedColor, locRealColor = node._realColor; - var i, len, selChildren, item; - this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.Dirty); - if (this._cascadeColorEnabledDirty && !node._cascadeColorEnabled) { - locDispColor.r = locRealColor.r; - locDispColor.g = locRealColor.g; - locDispColor.b = locRealColor.b; - var whiteColor = new cc.Color(255, 255, 255, 255); - selChildren = node._children; - for (i = 0, len = selChildren.length; i < len; i++) { - item = selChildren[i]; - if (item && item._renderCmd) - item._renderCmd._updateDisplayColor(whiteColor); - } - this._cascadeColorEnabledDirty = false; - } else { - if (parentColor === undefined) { - var locParent = node._parent; - if (locParent && locParent._cascadeColorEnabled) - parentColor = locParent.getDisplayedColor(); - else - parentColor = cc.Color.WHITE; - } - locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0); - locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0); - locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0); - if (node._cascadeColorEnabled) { - selChildren = node._children; - for (i = 0, len = selChildren.length; i < len; i++) { - item = selChildren[i]; - if (item && item._renderCmd){ - item._renderCmd._updateDisplayColor(locDispColor); - item._renderCmd._updateColor(); - } - } - } - } - this._dirtyFlag &= ~dirtyFlags.colorDirty; - }, - - _updateDisplayOpacity: function (parentOpacity) { - var node = this._node; - var i, len, selChildren, item; - this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.Dirty); - if (this._cascadeOpacityEnabledDirty && !node._cascadeOpacityEnabled) { - this._displayedOpacity = node._realOpacity; - selChildren = node._children; - for (i = 0, len = selChildren.length; i < len; i++) { - item = selChildren[i]; - if (item && item._renderCmd) - item._renderCmd._updateDisplayOpacity(255); - } - this._cascadeOpacityEnabledDirty = false; - } else { - if (parentOpacity === undefined) { - var locParent = node._parent; - parentOpacity = 255; - if (locParent && locParent._cascadeOpacityEnabled) - parentOpacity = locParent.getDisplayedOpacity(); - } - this._displayedOpacity = node._realOpacity * parentOpacity / 255.0; - if (node._cascadeOpacityEnabled) { - selChildren = node._children; - for (i = 0, len = selChildren.length; i < len; i++) { - item = selChildren[i]; - if (item && item._renderCmd){ - item._renderCmd._updateDisplayOpacity(this._displayedOpacity); - item._renderCmd._updateColor(); - } - } - } - } - this._dirtyFlag &= ~dirtyFlags.opacityDirty; - }, - - _syncDisplayColor : function (parentColor) { - var node = this._node, locDispColor = this._displayedColor, locRealColor = node._realColor; - if (parentColor === undefined) { - var locParent = node._parent; - if (locParent && locParent._cascadeColorEnabled) - parentColor = locParent.getDisplayedColor(); - else - parentColor = cc.Color.WHITE; - } - locDispColor.r = 0 | (locRealColor.r * parentColor.r / 255.0); - locDispColor.g = 0 | (locRealColor.g * parentColor.g / 255.0); - locDispColor.b = 0 | (locRealColor.b * parentColor.b / 255.0); - }, - - _syncDisplayOpacity : function (parentOpacity) { - var node = this._node; - if (parentOpacity === undefined) { - var locParent = node._parent; - parentOpacity = 255; - if (locParent && locParent._cascadeOpacityEnabled) - parentOpacity = locParent.getDisplayedOpacity(); - } - this._displayedOpacity = node._realOpacity * parentOpacity / 255.0; - }, - - _updateColor: function(){}, - - updateStatus: function () { - var locFlag = this._dirtyFlag; - var colorDirty = locFlag & dirtyFlags.colorDirty, - opacityDirty = locFlag & dirtyFlags.opacityDirty; - - if (locFlag & dirtyFlags.contentDirty) { - this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.Dirty); - this._dirtyFlag &= ~dirtyFlags.contentDirty; - } - - if (colorDirty) - this._updateDisplayColor(); - - if (opacityDirty) - this._updateDisplayOpacity(); - - if (colorDirty || opacityDirty) - this._updateColor(); - - if (locFlag & dirtyFlags.transformDirty) { - //update the transform - this.transform(this.getParentRenderCmd(), true); - this._dirtyFlag &= ~dirtyFlags.transformDirty; - } - }, - - _syncStatus: function (parentCmd) { - // In the visit logic does not restore the _dirtyFlag - // Because child elements need parent's _dirtyFlag to change himself - var locFlag = this._dirtyFlag; - - var colorDirty = locFlag & dirtyFlags.colorDirty, - opacityDirty = locFlag & dirtyFlags.opacityDirty; - - if (colorDirty) - //update the color - this._syncDisplayColor(); - - if (opacityDirty) - //update the opacity - this._syncDisplayOpacity(); - - if (colorDirty || opacityDirty) - this._updateColor(); - - if (locFlag & dirtyFlags.transformDirty) - //update the transform - this.transform(parentCmd); - } -}; - -_ccsg.Node.RenderCmd.prototype.originTransform = _ccsg.Node.RenderCmd.prototype.transform; -_ccsg.Node.RenderCmd.prototype.originUpdateStatus = _ccsg.Node.RenderCmd.prototype.updateStatus; -_ccsg.Node.RenderCmd.prototype._originSyncStatus = _ccsg.Node.RenderCmd.prototype._syncStatus; //-----------------------Canvas --------------------------- diff --git a/cocos2d/core/camera/CCCamera.js b/cocos2d/core/camera/CCCamera.js index a77cbe58d0..6952143cc1 100644 --- a/cocos2d/core/camera/CCCamera.js +++ b/cocos2d/core/camera/CCCamera.js @@ -23,10 +23,10 @@ THE SOFTWARE. ****************************************************************************/ -var transformDirtyFlag; +var cullingDirtyFlag; if (!CC_JSB) { - transformDirtyFlag = _ccsg.Node._dirtyFlags.transformDirty; + cullingDirtyFlag = _ccsg.Node._dirtyFlags.cullingDirty; require('./CCSGCameraNode'); } @@ -131,7 +131,7 @@ let Camera = cc.Class({ if (!CC_JSB) { var cmd = sgNode._renderCmd; - cmd.setDirtyFlag(transformDirtyFlag); + cmd.setDirtyFlag(cullingDirtyFlag); cmd._cameraFlag = Camera.flags.InCamera; cc.renderer.childrenOrderDirty = true; @@ -156,7 +156,7 @@ let Camera = cc.Class({ if (!CC_JSB) { var cmd = sgNode._renderCmd; - cmd.setDirtyFlag(transformDirtyFlag); + cmd.setDirtyFlag(cullingDirtyFlag); cmd._cameraFlag = 0; cc.renderer.childrenOrderDirty = true; @@ -292,14 +292,14 @@ let Camera = cc.Class({ return false; }, - _setSgNodesTransformDirty: function () { + _setSgNodesCullingDirty: function () { let sgTarges = this._sgTarges; for (let i = 0; i < sgTarges.length; i++) { if (CC_JSB) { sgTarges[i].markTransformUpdated(); } else { - sgTarges[i]._renderCmd.setDirtyFlag(transformDirtyFlag); + sgTarges[i]._renderCmd.setDirtyFlag(cullingDirtyFlag); } } }, @@ -400,7 +400,7 @@ let Camera = cc.Class({ lvm.tx !== m.tx || lvm.ty !== m.ty ) { - this._setSgNodesTransformDirty(); + this._setSgNodesCullingDirty(); lvm.a = m.a; lvm.b = m.b; diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 9c587c73fe..3dab23bc1b 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -501,19 +501,13 @@ proto.constructor = _ccsg.Label.CanvasRenderCmd; - proto.transform = function (parentCmd, recursive) { - this.originTransform(parentCmd, recursive); - - var bb = this._currentRegion, + proto._doCulling = function () { + var rect = cc.visibleRect, + bb = this._currentRegion, l = bb._minX, r = bb._maxX, b = bb._minY, t = bb._maxY, - rect = cc.visibleRect, vl = rect.left.x, vr = rect.right.x, vt = rect.top.y, vb = rect.bottom.y; - if (r < vl || l > vr || t < vb || b > vt) { - this._needDraw = false; - } - else { - this._needDraw = true; - } + + this._needDraw = !(r < vl || l > vr || t < vb || b > vt); }; proto.rendering = function (ctx, scaleX, scaleY) { diff --git a/cocos2d/core/label/CCSGLabelWebGLRenderCmd.js b/cocos2d/core/label/CCSGLabelWebGLRenderCmd.js index 7d7ab32640..680c467594 100644 --- a/cocos2d/core/label/CCSGLabelWebGLRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelWebGLRenderCmd.js @@ -61,8 +61,8 @@ cc.js.mixin(proto, _ccsg.Label.TTFLabelBaker.prototype); proto.constructor = _ccsg.Label.WebGLRenderCmd; -proto.transform = function (parentCmd, recursive) { - this.originTransform(parentCmd, recursive); +proto.updateTransform = function (parentCmd) { + this.originUpdateTransform(parentCmd); var node = this._node, lx = 0, rx = this._labelCanvas.width, @@ -78,24 +78,27 @@ proto.transform = function (parentCmd, recursive) { vert[2].y = rx * wt.b + ty * wt.d + wt.ty; vert[3].x = rx * wt.a + by * wt.c + wt.tx; // br vert[3].y = rx * wt.b + by * wt.d + wt.ty; +}; +proto._doCulling = function () { + var node = this._node; if (!node._string || (node._labelType !== _ccsg.Label.Type.TTF && - node._labelType !== _ccsg.Label.Type.SystemFont)) { + node._labelType !== _ccsg.Label.Type.SystemFont)) { // No culling for bmfont return; } - var vl, vr, vb, vt; var rect = cc.visibleRect; if (this._cameraFlag > 0) { rect = cc.Camera.main.visibleRect; } - vl = rect.left.x; - vr = rect.right.x; - vt = rect.top.y; - vb = rect.bottom.y; + var vl = rect.left.x; + var vr = rect.right.x; + var vt = rect.top.y; + var vb = rect.bottom.y; + var vert = this._vertices; if (((vert[0].x-vl) & (vert[1].x-vl) & (vert[2].x-vl) & (vert[3].x-vl)) >> 31 || // All outside left ((vr-vert[0].x) & (vr-vert[1].x) & (vr-vert[2].x) & (vr-vert[3].x)) >> 31 || // All outside right ((vert[0].y-vb) & (vert[1].y-vb) & (vert[2].y-vb) & (vert[3].y-vb)) >> 31 || // All outside bottom diff --git a/cocos2d/core/platform/CCMacro.js b/cocos2d/core/platform/CCMacro.js index 4fb5266144..98d577ec54 100644 --- a/cocos2d/core/platform/CCMacro.js +++ b/cocos2d/core/platform/CCMacro.js @@ -1885,6 +1885,21 @@ cc.macro = { */ ENABLE_GC_FOR_NATIVE_OBJECTS: true, + + /** + * !#en + * Whether or not enable auto culling. + * If your game have more dynamic objects, we suggest to disable auto culling. + * If your game have more static objects, we suggest to enable auto culling. + * !#zh + * 是否开启自动裁减功能,开启裁减功能将会把在屏幕外的物体从渲染队列中去除掉。 + * 如果游戏中的动态物体比较多的话,建议将此选项关闭。 + * 如果游戏中的静态物体比较多的话,建议将此选项打开。 + * @property {Boolean} ENABLE_CULLING + * @default true + */ + ENABLE_CULLING: true, + /** * !#en * Whether or not enabled tiled map auto culling. diff --git a/cocos2d/core/renderer/RendererWebGL.js b/cocos2d/core/renderer/RendererWebGL.js index 32cdc49f78..73480a54ad 100644 --- a/cocos2d/core/renderer/RendererWebGL.js +++ b/cocos2d/core/renderer/RendererWebGL.js @@ -468,7 +468,7 @@ cc.rendererWebGL = { // Reset buffer for rendering context.bindBuffer(context.ARRAY_BUFFER, null); - + for (i = 0, len = locCmds.length; i < len; ++i) { cmd = locCmds[i]; if (!cmd._needDraw) continue; diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index fcc6ddf1f5..72fefaa378 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -61,8 +61,7 @@ var dataPool = { var macro = cc.macro, webgl, - vl, vb, vt, vr, - cornerId = []; + vl, vb, vt, vr; /* *

@@ -86,10 +85,14 @@ var macro = cc.macro, * @property {Number} insetBottom - The bottom inset of the 9-slice sprite */ var simpleQuadGenerator = { - _rebuildQuads_base: function (sprite, spriteFrame, contentSize, isTrimmedContentSize) { + _rebuildQuads_base: function (sprite) { + var spriteFrame = sprite._spriteFrame, + contentSize = sprite._contentSize, + isTrimmedContentSize = sprite._isTrimmedContentSize; + //build vertices var vertices = sprite._vertices, - wt = sprite._renderCmd._worldTransform, + corner = sprite._corner, l, b, r, t; if (isTrimmedContentSize) { l = 0; @@ -100,17 +103,22 @@ var simpleQuadGenerator = { var originalSize = spriteFrame._originalSize; var rect = spriteFrame._rect; var offset = spriteFrame._offset; - var scaleX = contentSize.width / originalSize.width; - var scaleY = contentSize.height / originalSize.height; - var trimmLeft = offset.x + (originalSize.width - rect.width) / 2; - var trimmRight = offset.x - (originalSize.width - rect.width) / 2; - var trimmedBottom = offset.y + (originalSize.height - rect.height) / 2; - var trimmedTop = offset.y - (originalSize.height - rect.height) / 2; + + var cw = contentSize.width, ch = contentSize.height; + var ow = originalSize.width, oh = originalSize.height; + var rw = rect.width, rh = rect.height; + + var scaleX = cw / ow; + var scaleY = ch / oh; + var trimmLeft = offset.x + (ow - rw) / 2; + var trimmRight = offset.x - (ow - rw) / 2; + var trimmedBottom = offset.y + (oh - rh) / 2; + var trimmedTop = offset.y - (oh - rh) / 2; l = trimmLeft * scaleX; b = trimmedBottom * scaleY; - r = contentSize.width + trimmRight * scaleX; - t = contentSize.height + trimmedTop * scaleY; + r = cw + trimmRight * scaleX; + t = ch + trimmedTop * scaleY; } if (vertices.length < 8) { @@ -120,9 +128,12 @@ var simpleQuadGenerator = { } // bl, br, tl, tr if (webgl) { - var la = l * wt.a, lb = l * wt.b, ra = r * wt.a, rb = r * wt.b, - tcx = t * wt.c + wt.tx, tdy = t * wt.d + wt.ty, - bcx = b * wt.c + wt.tx, bdy = b * wt.d + wt.ty; + var wt = sprite._renderCmd._worldTransform; + var wta = wt.a, wtb = wt.b, wtc = wt.c, wtd = wt.d, wtx = wt.tx, wty = wt.ty; + + var la = l * wta, lb = l * wtb, ra = r * wta, rb = r * wtb, + tcx = t * wtc + wtx, tdy = t * wtd + wty, + bcx = b * wtc + wtx, bdy = b * wtd + wty; vertices[0] = la + bcx; vertices[1] = lb + bdy; vertices[2] = ra + bcx; @@ -143,10 +154,10 @@ var simpleQuadGenerator = { vertices[7] = t; } - cornerId[0] = 0; // bl - cornerId[1] = 2; // br - cornerId[2] = 4; // tl - cornerId[3] = 6; // tr + corner[0] = 0; // bl + corner[1] = 2; // br + corner[2] = 4; // tl + corner[3] = 6; // tr //build uvs if (sprite._uvsDirty) { @@ -198,13 +209,21 @@ var simpleQuadGenerator = { var scale9QuadGenerator = { x: new Array(4), y: new Array(4), - _rebuildQuads_base: function (sprite, spriteFrame, contentSize, insetLeft, insetRight, insetTop, insetBottom) { + _rebuildQuads_base: function (sprite) { + var spriteFrame = sprite._spriteFrame, + contentSize = sprite._contentSize, + insetLeft = sprite._insetLeft, + insetRight = sprite._insetRight, + insetTop = sprite._insetTop, + insetBottom = sprite._insetBottom; + //build vertices var vertices = sprite._vertices; var wt = sprite._renderCmd._worldTransform; var leftWidth, centerWidth, rightWidth; var topHeight, centerHeight, bottomHeight; var rect = spriteFrame._rect; + var corner = sprite._corner; leftWidth = insetLeft; rightWidth = insetRight; @@ -258,10 +277,10 @@ var scale9QuadGenerator = { } } - cornerId[0] = 0; // bl - cornerId[1] = 6; // br - cornerId[2] = 24; // tl - cornerId[3] = 30; // tr + corner[0] = 0; // bl + corner[1] = 6; // br + corner[2] = 24; // tl + corner[3] = 30; // tr //build uvs if (sprite._uvsDirty) { @@ -342,7 +361,11 @@ var scale9QuadGenerator = { var tiledQuadGenerator = { _rebuildQuads_base: function (sprite, spriteFrame, contentSize) { - var vertices = sprite._vertices, + + var spriteFrame = sprite._spriteFrame, + contentSize = sprite._contentSize, + vertices = sprite._vertices, + corner = sprite._corner, wt = sprite._renderCmd._worldTransform, uvs = sprite._uvs; //build uvs @@ -446,17 +469,39 @@ var tiledQuadGenerator = { } } - cornerId[0] = 0; // bl - cornerId[1] = (col-1) * 8 + 2; // br - cornerId[2] = (row-1) * col * 8 + 4; // tl - cornerId[3] = dataLength - 2; // tr + corner[0] = 0; // bl + corner[1] = (col-1) * 8 + 2; // br + corner[2] = (row-1) * col * 8 + 4; // tl + corner[3] = dataLength - 2; // tr } }; var fillQuadGeneratorBar = { //percentage from 0 to 1; - _rebuildQuads_base : function (sprite, spriteFrame, contentSize, fillType, fillStart, fillRange) { + _rebuildQuads_base : function (sprite) { + var spriteFrame = sprite._spriteFrame, + contentSize = sprite._contentSize; + + var fillStart = sprite._fillStart; + var fillRange = sprite._fillRange; + + if (fillRange < 0) { + fillStart += fillRange; + fillRange = -fillRange; + } + + fillRange = fillStart + fillRange; + fillStart = fillStart > 1.0 ? 1.0 : fillStart; + fillStart = fillStart < 0.0 ? 0.0 : fillStart; + + fillRange = fillRange > 1.0 ? 1.0 : fillRange; + fillRange = fillRange < 0.0 ? 0.0 : fillRange; + fillRange = fillRange - fillStart; + + var fillType = sprite._fillType; + var vertices = sprite._vertices, + corner = sprite._corner, wt = sprite._renderCmd._worldTransform, uvs = sprite._uvs; var fillEnd; @@ -583,10 +628,10 @@ var fillQuadGeneratorBar = { sprite._vertCount = 4; - cornerId[0] = 0; // bl - cornerId[1] = 2; // br - cornerId[2] = 4; // tl - cornerId[3] = 6; // tr + corner[0] = 0; // bl + corner[1] = 2; // br + corner[2] = 4; // tl + corner[3] = 6; // tr } }; @@ -601,8 +646,27 @@ var fillQuadGeneratorRadial = { rawVerts: null, rawUvs: null, - _rebuildQuads_base : function (sprite, spriteFrame, contentSize, fillCenter, fillStart, fillRange) { + _rebuildQuads_base : function (sprite) { + var spriteFrame = sprite._spriteFrame, + contentSize = sprite._contentSize; + + var fillStart = sprite._fillStart; + var fillRange = sprite._fillRange; + if (fillRange < 0) { + fillStart += fillRange; + fillRange = -fillRange; + } + + sprite._isTriangle = true; + if (!sprite._rawVerts) { + sprite._rawVerts = dataPool.get(8) || new Float32Array(8); + sprite._rawUvs = dataPool.get(8) || new Float32Array(8); + } + + var fillCenter = sprite._fillCenter; + var vertices = sprite._vertices, + corner = sprite._corner, uvs = sprite._uvs, rawVerts = sprite._rawVerts, rawUvs = sprite._rawUvs, @@ -753,10 +817,10 @@ var fillQuadGeneratorRadial = { } sprite._vertCount = count; - cornerId[0] = 0; // bl - cornerId[1] = 2; // br - cornerId[2] = 4; // tl - cornerId[3] = 6; // tr + corner[0] = 0; // bl + corner[1] = 2; // br + corner[2] = 4; // tl + corner[3] = 6; // tr }, _generateTriangle: function(wt, offset, vert0, vert1, vert2) { @@ -940,6 +1004,9 @@ var meshQuadGenerator = { return; } + var spriteFrame = sprite._spriteFrame, + polygonInfo = sprite._meshPolygonInfo + if (!polygonInfo) { return; } @@ -949,6 +1016,7 @@ var meshQuadGenerator = { var vertices = sprite._vertices; var uvs = sprite._uvs; var count = srcVerts.length; + var corner = sprite._corner; var dataLength = count * 2; if (vertices.length < dataLength) { @@ -974,22 +1042,22 @@ var meshQuadGenerator = { if (x < l) { l = x; - cornerId[0] = i * 2; // left + corner[0] = i * 2; // left } if (x > r) { r = x; - cornerId[1] = i * 2; // right + corner[1] = i * 2; // right } if (y < b) { b = y; - cornerId[2] = i * 2; // bottom + corner[2] = i * 2; // bottom } if (y > t) { t = y; - cornerId[3] = i * 2; // top + corner[3] = i * 2; // top } } @@ -1055,6 +1123,8 @@ cc.Scale9Sprite = _ccsg.Node.extend({ if (webgl === undefined) { webgl = cc._renderType === cc.game.RENDER_TYPE_WEBGL; } + + this._corner = []; }, loaded: function () { @@ -1361,91 +1431,39 @@ cc.Scale9Sprite = _ccsg.Node.extend({ return; } this._isTriangle = false; + + var quadGenerator; switch (this._renderingType) { case RenderingType.SIMPLE: - simpleQuadGenerator._rebuildQuads_base(this, this._spriteFrame, this._contentSize, this._isTrimmedContentSize); + quadGenerator = simpleQuadGenerator; break; case RenderingType.SLICED: - scale9QuadGenerator._rebuildQuads_base(this, this._spriteFrame, this._contentSize, this._insetLeft, this._insetRight, this._insetTop, this._insetBottom); + quadGenerator = scale9QuadGenerator; break; case RenderingType.TILED: - tiledQuadGenerator._rebuildQuads_base(this, this._spriteFrame, this._contentSize); + quadGenerator = tiledQuadGenerator; break; case RenderingType.FILLED: - var fillstart = this._fillStart; - var fillRange = this._fillRange; - if(fillRange < 0) { - fillstart += fillRange; - fillRange = -fillRange; + if (this._fillType === FillType.RADIAL) { + quadGenerator = fillQuadGeneratorRadial; } - if (this._fillType !== FillType.RADIAL) { - fillRange = fillstart + fillRange; - fillstart = fillstart > 1.0 ? 1.0 : fillstart; - fillstart = fillstart < 0.0 ? 0.0 : fillstart; - - fillRange = fillRange > 1.0 ? 1.0 : fillRange; - fillRange = fillRange < 0.0 ? 0.0 : fillRange; - fillRange = fillRange - fillstart; - fillQuadGeneratorBar._rebuildQuads_base(this, this._spriteFrame, this._contentSize, this._fillType, fillstart, fillRange); - } else { - this._isTriangle = true; - if (!this._rawVerts) { - this._rawVerts = dataPool.get(8) || new Float32Array(8); - this._rawUvs = dataPool.get(8) || new Float32Array(8); - } - fillQuadGeneratorRadial._rebuildQuads_base(this, this._spriteFrame, this._contentSize, this._fillCenter,fillstart, fillRange); + else { + quadGenerator = fillQuadGeneratorBar; } break; case RenderingType.MESH: - meshQuadGenerator._rebuildQuads_base(this, this._spriteFrame, this._meshPolygonInfo); + quadGenerator = meshQuadGenerator; break; - default: + } + + if (quadGenerator) { + quadGenerator._rebuildQuads_base(this); + } + else { this._quadsDirty = false; this._uvsDirty = false; this._renderCmd._needDraw = false; cc.errorID(2627); - return; - } - - var rect = cc.visibleRect; - if (webgl && this._renderCmd._cameraFlag > 0) { - rect = cc.Camera.main.visibleRect; - } - - vl = rect.left.x; - vr = rect.right.x; - vt = rect.top.y; - vb = rect.bottom.y; - - // Culling - if (webgl) { - // x1, y1 leftBottom - // x2, y2 rightBottom - // x3, y3 leftTop - // x4, y4 rightTop - var vert = this._isTriangle ? this._rawVerts : this._vertices, - x0 = vert[cornerId[0]], x1 = vert[cornerId[1]], x2 = vert[cornerId[2]], x3 = vert[cornerId[3]], - y0 = vert[cornerId[0] + 1], y1 = vert[cornerId[1] + 1], y2 = vert[cornerId[2] + 1], y3 = vert[cornerId[3] + 1]; - if (((x0-vl) & (x1-vl) & (x2-vl) & (x3-vl)) >> 31 || // All outside left - ((vr-x0) & (vr-x1) & (vr-x2) & (vr-x3)) >> 31 || // All outside right - ((y0-vb) & (y1-vb) & (y2-vb) & (y3-vb)) >> 31 || // All outside bottom - ((vt-y0) & (vt-y1) & (vt-y2) & (vt-y3)) >> 31) // All outside top - { - this._renderCmd._needDraw = false; - } - else { - this._renderCmd._needDraw = true; - } - } - else { - var bb = this._renderCmd._currentRegion, - l = bb._minX, r = bb._maxX, b = bb._minY, t = bb._maxY; - if (r < vl || l > vr || t < vb || b > vt) { - this._renderCmd._needDraw = false; - } - else { - this._renderCmd._needDraw = true; - } } this._quadsDirty = false; diff --git a/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js b/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js index 113db3db11..041040ef46 100644 --- a/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js +++ b/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js @@ -37,11 +37,20 @@ cc.Scale9Sprite.CanvasRenderCmd = function (renderable) { var proto = cc.Scale9Sprite.CanvasRenderCmd.prototype = Object.create(_ccsg.Node.CanvasRenderCmd.prototype); proto.constructor = cc.Scale9Sprite.CanvasRenderCmd; -proto.transform = function (parentCmd, recursive) { - this.originTransform(parentCmd, recursive); +proto.updateTransform = function (parentCmd) { + this.originUpdateTransform(parentCmd); this._node._rebuildQuads(); }; +proto._doCulling = function () { + var rect = cc.visibleRect, + bb = this._currentRegion, + l = bb._minX, r = bb._maxX, b = bb._minY, t = bb._maxY, + vl = rect.left.x, vr = rect.right.x, vt = rect.top.y, vb = rect.bottom.y; + + this._needDraw = !(r < vl || l > vr || t < vb || b > vt); +}; + proto._updateDisplayColor = function(parentColor){ _ccsg.Node.WebGLRenderCmd.prototype._updateDisplayColor.call(this, parentColor); this._originalTexture = this._textureToRender = null; diff --git a/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js b/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js index 19dd05322c..2d5c1f19f2 100644 --- a/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js +++ b/cocos2d/core/sprites/CCScale9SpriteWebGLRenderCmd.js @@ -84,11 +84,45 @@ proto._uploadSliced = function (vertices, uvs, color, z, f32buffer, ui32buffer, return 36; }; -proto.transform = function (parentCmd, recursive) { - this.originTransform(parentCmd, recursive); +proto.updateTransform = function (parentCmd) { + this.originUpdateTransform(parentCmd); this._node._rebuildQuads(); }; +proto._doCulling = function () { + var node = this._node; + var rect = cc.visibleRect; + + if (this._cameraFlag > 0) { + rect = cc.Camera.main.visibleRect; + } + + vl = rect.left.x; + vr = rect.right.x; + vt = rect.top.y; + vb = rect.bottom.y; + + // x1, y1 leftBottom + // x2, y2 rightBottom + // x3, y3 leftTop + // x4, y4 rightTop + var vert = node._isTriangle ? node._rawVerts : node._vertices, + corner = node._corner, + c0 = corner[0], c1 = corner[1], c2 = corner[2], c3 = corner[3], + x0 = vert[c0], x1 = vert[c1], x2 = vert[c2], x3 = vert[c3], + y0 = vert[c0 + 1], y1 = vert[c1 + 1], y2 = vert[c2 + 1], y3 = vert[c3 + 1]; + if (((x0-vl) & (x1-vl) & (x2-vl) & (x3-vl)) >> 31 || // All outside left + ((vr-x0) & (vr-x1) & (vr-x2) & (vr-x3)) >> 31 || // All outside right + ((y0-vb) & (y1-vb) & (y2-vb) & (y3-vb)) >> 31 || // All outside bottom + ((vt-y0) & (vt-y1) & (vt-y2) & (vt-y3)) >> 31) // All outside top + { + this._needDraw = false; + } + else { + this._needDraw = true; + } +}; + proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset){ var node = this._node; if (this._displayedOpacity === 0) { diff --git a/cocos2d/index.js b/cocos2d/index.js index e51e1bd471..6d0d79d2bf 100644 --- a/cocos2d/index.js +++ b/cocos2d/index.js @@ -4,6 +4,7 @@ require('./core/CCGame'); require('./actions'); require('./core/base-nodes/CCSGNode'); +require('./core/base-nodes/CCSGNodeBaseRenderCmd'); require('./core/base-nodes/CCSGNodeCanvasRenderCmd'); require('./core/base-nodes/CCSGNodeWebGLRenderCmd'); require('./core/scenes/CCSGScene');