diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 725e01794e..2623bfdcdf 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -940,6 +940,9 @@ var Node = cc.Class({ this._worldMatrix = mathPools.mat4.get(); this._localMatDirty = true; this._worldMatDirty = true; + + this._cullingMask = 1; + this._inheritMask = 1; }, statics: { @@ -1306,9 +1309,9 @@ var Node = cc.Class({ h = this.height; var testPt; - var Camera = cc.Camera; - if (Camera && Camera.main && Camera.main.containsNode(this)) { - testPt = Camera.main.getCameraToWorldPoint(point); + let camera = cc.Camera.findCamera(this); + if (camera) { + testPt = camera.getCameraToWorldPoint(point); } else { testPt = cc.v2(point); diff --git a/cocos2d/core/camera/CCCamera.js b/cocos2d/core/camera/CCCamera.js index d969a9cf8a..7b51c80e9f 100644 --- a/cocos2d/core/camera/CCCamera.js +++ b/cocos2d/core/camera/CCCamera.js @@ -24,14 +24,21 @@ ****************************************************************************/ const affineTrans = require('../value-types/CCAffineTransform'); +const renderEngine = require('../renderer/render-engine'); +const renderer = require('../renderer/index'); -var cullingDirtyFlag; -var _wt = affineTrans.make(); +const mat4 = cc.vmath.mat4; +const vec2 = cc.vmath.vec2; +const vec3 = cc.vmath.vec3; -if (!CC_JSB) { - cullingDirtyFlag = _ccsg.Node._dirtyFlags.cullingDirty; - require('./CCSGCameraNode'); -} +let _static_culling_mask = 1; + +let _mat4_temp_1 = mat4.create(); +let _mat4_temp_2 = mat4.create(); +let _vec3_temp_1 = vec3.create(); +let _vec3_temp_2 = vec3.create(); + +let _cameras = []; /** * !#en @@ -47,23 +54,28 @@ let Camera = cc.Class({ name: 'cc.Camera', extends: cc.Component, - ctor: function () { - this.viewMatrix = cc.affineTransformMake(); - this.invertViewMatrix = cc.affineTransformMake(); + ctor () { + let camera = new renderEngine.Camera(); - this._lastViewMatrix = cc.affineTransformMake(); + camera.setStages([ + 'transparent' + ]); - this._sgTarges = []; + camera.setClearFlags(0); - this._checkedTimes = 0; + this._fov = Math.PI * 60 / 180; + camera.setFov(this._fov); + camera.setNear(0.1); + camera.setFar(1024); - this.visibleRect = { - left: cc.v2(), - right: cc.v2(), - top: cc.v2(), - bottom: cc.v2() - }; - this.viewPort = cc.rect(); + let view = new renderEngine.View(); + camera.view = view; + camera.dirty = true; + + this.cullingMask = 1 << _static_culling_mask++; + camera._cullingMask = view._cullingMask = this.cullingMask; + + this._camera = camera; }, editor: CC_EDITOR && { @@ -97,111 +109,52 @@ let Camera = cc.Class({ * @property {Camera} main * @static */ - main: null - }, + main: null, - _createSgNode: function () { - if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) { - cc.errorID(8301); - var sgNode = new _ccsg.Node(); - sgNode.setTransform = sgNode.addTarget = sgNode.removeTarget = function () {}; - return sgNode; - } - else { - return new _ccsg.CameraNode(); + cameras: _cameras, + + findCamera (node) { + for (let i = 0, l = _cameras.length; i < l; i++) { + let camera = _cameras[i]; + if (camera.containsNode(node)) { + return camera; + } + } + + return null; } }, - _initSgNode: function () { - // sgNode is the sizeProvider of the node so we should sync its size with the node, - // otherwise the node size will become zero. - this._sgNode.setContentSize(this.node.getContentSize(true)); + onLoad () { + this._camera.setNode(this.node); }, - _addSgTargetInSg: function (target) { - var sgNode; - if (target instanceof cc.Node) { - sgNode = target._sgNode; - } - else if (target instanceof _ccsg.Node) { - sgNode = target; - } - - if (!sgNode || sgNode._cameraInfo) return; - - sgNode._cameraInfo = { - touched: this._checkedTimes - }; - this._sgNode.addTarget(sgNode); - - this._sgTarges.push(sgNode); - - if (!CC_JSB) { - var cmd = sgNode._renderCmd; - cmd.setDirtyFlag(cullingDirtyFlag); - cmd._cameraFlag = Camera.flags.InCamera; - - cc.rendererWebGL.childrenOrderDirty = true; - } - }, - - _removeTargetInSg: function (target) { - var sgNode; - if (target instanceof cc.Node) { - sgNode = target._sgNode; - } - else if (target instanceof _ccsg.Node) { - sgNode = target; - } - - if (!sgNode || !sgNode._cameraInfo) return; - - this._sgNode.removeTarget(sgNode); - delete sgNode._cameraInfo; - - cc.js.array.remove(this._sgTarges, sgNode); - - if (!CC_JSB) { - var cmd = sgNode._renderCmd; - cmd.setDirtyFlag(cullingDirtyFlag); - cmd._cameraFlag = 0; - - cc.rendererWebGL.childrenOrderDirty = true; - } - }, - - onEnable: function () { - if (Camera.main) { - cc.errorID(8300); - return; - } - - Camera.main = this; - if (CC_JSB) { - this._sgNode.setEnable(true); + onEnable () { + if (!Camera.main) { + Camera.main = this; } let targets = this._targets; for (let i = 0, l = targets.length; i < l; i++) { - this._addSgTargetInSg(targets[i]); + targets[i]._cullingMask = this.cullingMask; } + + renderer.scene.addCamera(this._camera); + _cameras.push(this); }, - onDisable: function () { - if (Camera.main !== this) { - return; + onDisable () { + if (Camera.main === this) { + Camera.main = null; } - Camera.main = null; - if (CC_JSB) { - this._sgNode.setEnable(false); + let targets = this._targets; + for (let i = 0, l = targets.length; i < l; i++) { + targets[i]._cullingMask = 1; } - // target sgNode may changed, so directly remove sgTargets here. - let sgTargets = this._sgTarges; - for (let i = sgTargets.length - 1; i >= 0; i--) { - this._removeTargetInSg(sgTargets[i]); - } + renderer.scene.removeCamera(this._camera); + cc.js.array.remove(_cameras, this); }, /** @@ -212,12 +165,12 @@ let Camera = cc.Class({ * @method addTarget * @param {Node} target */ - addTarget: function (target) { + addTarget (target) { if (this._targets.indexOf(target) !== -1) { return; } - this._addSgTargetInSg(target); + target._cullingMask = this.cullingMask; this._targets.push(target); }, @@ -229,12 +182,12 @@ let Camera = cc.Class({ * @method removeTarget * @param {Node} target */ - removeTarget: function (target) { + removeTarget (target) { if (this._targets.indexOf(target) === -1) { return; } - this._removeTargetInSg(target); + target._cullingMask = 1; cc.js.array.remove(this._targets, target); }, @@ -246,7 +199,7 @@ let Camera = cc.Class({ * @method getTargets * @return {[Node]} */ - getTargets: function () { + getTargets () { return this._targets; }, @@ -260,12 +213,14 @@ let Camera = cc.Class({ * @return {AffineTransform} */ getNodeToCameraTransform (node) { - let t = affineTrans.make(); - node.getNodeToWorldTransform(t); + let out = affineTrans.makeIdentity(); + node.getWorldMatrix(_mat4_temp_2); if (this.containsNode(node)) { - t = affineTrans.concatIn(t, cc.Camera.main.viewMatrix); + this.getWorldToCameraMatrix(_mat4_temp_1); + mat4.mul(_mat4_temp_2, _mat4_temp_2, _mat4_temp_1); } - return t; + affineTrans.fromMatrix(_mat4_temp_2, out); + return out; }, /** @@ -274,14 +229,76 @@ let Camera = cc.Class({ * !#zh * 将一个摄像机坐标系下的点转换到世界坐标系下。 * @method getCameraToWorldPoint - * @param {Node} point - the point which should transform + * @param {Vec2} point - the point which should transform + * @param {Vec2} out - the point to receive the result * @return {Vec2} */ - getCameraToWorldPoint (point) { - if (cc.Camera.main) { - point = cc.pointApplyAffineTransform(point, cc.Camera.main.invertViewMatrix); + getCameraToWorldPoint (point, out) { + out = out || cc.v2(); + this.getCameraToWorldMatrix(_mat4_temp_1); + vec2.transformMat4(out, point, _mat4_temp_1); + return out; + }, + + /** + * !#en + * Conver a world coordinates point to camera coordinates. + * !#zh + * 将一个世界坐标系下的点转换到摄像机坐标系下。 + * @param {Vec2} point + * @param {Vec2} out - the point to receive the result + * @return {Vec2} + */ + getWorldToCameraPoint (point, out) { + out = out || cc.v2(); + this.getWorldToCameraMatrix(_mat4_temp_1); + vec2.transformMat4(out, point, _mat4_temp_1); + return out; + }, + + /** + * !#en + * Get the camera to world matrix + * !#zh + * 获取摄像机坐标系到世界坐标系的矩阵 + * @param {Mat4} out - the matrix to receive the result + * @return {Mat4} + */ + getCameraToWorldMatrix (out) { + this.getWorldToCameraMatrix(out); + mat4.invert(out, out); + return out; + }, + + + /** + * !#en + * Get the world to camera matrix + * !#zh + * 获取世界坐标系到摄像机坐标系的矩阵 + * @param {Mat4} out - the matrix to receive the result + * @return {Mat4} + */ + getWorldToCameraMatrix (out) { + this.node.getWorldRT(_mat4_temp_1); + + let zoomRatio = this.zoomRatio; + _mat4_temp_1.m00 *= zoomRatio; + _mat4_temp_1.m01 *= zoomRatio; + _mat4_temp_1.m04 *= zoomRatio; + _mat4_temp_1.m05 *= zoomRatio; + + let m12 = _mat4_temp_1.m12; + let m13 = _mat4_temp_1.m13; + + let center = cc.visibleRect.center; + _mat4_temp_1.m12 = center.x - (_mat4_temp_1.m00 * m12 + _mat4_temp_1.m04 * m13); + _mat4_temp_1.m13 = center.y - (_mat4_temp_1.m01 * m12 + _mat4_temp_1.m05 * m13); + + if (out !== _mat4_temp_1) { + mat4.copy(out, _mat4_temp_1); } - return point; + return out; }, /** @@ -293,145 +310,27 @@ let Camera = cc.Class({ * @param {Node} node - the node which need to check * @return {Boolean} */ - containsNode: function (node) { - if (node instanceof cc.Node) { - node = node._sgNode; - } - - let targets = this._sgTarges; - while (node) { - if (targets.indexOf(node) !== -1) { - return true; - } - node = node.parent; - } - - return false; - }, - - _setSgNodesCullingDirty: function () { - let sgTarges = this._sgTarges; - for (let i = 0; i < sgTarges.length; i++) { - if (CC_JSB) { - sgTarges[i].markCullingDirty(); - } - else { - sgTarges[i]._renderCmd.setDirtyFlag(cullingDirtyFlag); - } - } - }, - - _checkSgTargets: function () { - let targets = this._targets; - let sgTarges = this._sgTarges; - - let checkedTimes = ++this._checkedTimes; - - for (let i = 0, l = targets.length; i < l; i++) { - let target = targets[i]; - let sgNode = target; - - if (target instanceof cc.Node) { - sgNode = target._sgNode; - if (sgNode && !sgNode._cameraInfo) { - this._addSgTargetInSg(sgNode); - } - } - - if (sgNode) { - sgNode._cameraInfo.touched = checkedTimes; - } - } - - for (let i = sgTarges.length - 1; i >= 0; i--) { - let sgTarget = sgTarges[i]; - if (sgTarget._cameraInfo.touched !== checkedTimes) { - this._removeTargetInSg(sgTarget); - } - } + containsNode (node) { + return node._inheritMask === this.cullingMask; }, lateUpdate: !CC_EDITOR && function () { - this._checkSgTargets(); - - let m = this.viewMatrix; - let im = this.invertViewMatrix; - let viewPort = this.viewPort; - let visibleRect = cc.visibleRect; - let selfVisibleRect = this.visibleRect; let node = this.node; - - node.getNodeToWorldTransformAR(_wt); - let rotation = -(Math.atan2(_wt.b, _wt.a) + Math.atan2(-_wt.c, _wt.d)) * 0.5; - let a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0; - - // rotation - if (rotation) { - c = Math.sin(rotation); - d = Math.cos(rotation); - a = d; - b = -c; - } - - // scale let zoomRatio = this.zoomRatio; - a *= zoomRatio; - b *= zoomRatio; - c *= zoomRatio; - d *= zoomRatio; + let min = -(zoomRatio/2-0.5); + this._camera.setRect(min,min,zoomRatio,zoomRatio); - m.a = a; - m.b = b; - m.c = c; - m.d = d; + node.z = renderer.canvas.height / cc.view.getScaleY() / 1.1566; + node.getWorldMatrix(_mat4_temp_1); - // move camera to center - let center = visibleRect.center; - m.tx = center.x - (a * _wt.tx + c * _wt.ty); - m.ty = center.y - (b * _wt.tx + d * _wt.ty); + _vec3_temp_1.x = _mat4_temp_1.m12; + _vec3_temp_1.y = _mat4_temp_1.m13; + _vec3_temp_1.z = 0; + node.lookAt(_vec3_temp_1); - // calculate ivert view matrix - cc.affineTransformInvertOut(m, im); - - // calculate view port - viewPort.x = visibleRect.bottomLeft.x; - viewPort.y = visibleRect.bottomLeft.y; - viewPort.width = visibleRect.width; - viewPort.height = visibleRect.height; - cc._rectApplyAffineTransformIn(viewPort, im); - - selfVisibleRect.left.x = viewPort.xMin; - selfVisibleRect.right.x = viewPort.xMax; - selfVisibleRect.bottom.y = viewPort.yMin; - selfVisibleRect.top.y = viewPort.yMax; - - this._sgNode.setTransform(a, b, c, d, m.tx, m.ty); - - // if view transform changed, then need recalculate whether targets need culling - var lvm = this._lastViewMatrix; - if (lvm.a !== m.a || - lvm.b !== m.b || - lvm.c !== m.c || - lvm.d !== m.d || - lvm.tx !== m.tx || - lvm.ty !== m.ty - ) { - this._setSgNodesCullingDirty(); - - lvm.a = m.a; - lvm.b = m.b; - lvm.c = m.c; - lvm.d = m.d; - lvm.tx = m.tx; - lvm.ty = m.ty; - } + this._camera.dirty = true; } }); -Camera.flags = cc.Enum({ - InCamera: 1, - ParentInCamera: 2 -}); - module.exports = cc.Camera = Camera; diff --git a/cocos2d/core/camera/CCSGCameraNode.js b/cocos2d/core/camera/CCSGCameraNode.js deleted file mode 100644 index 6cbaa8fddf..0000000000 --- a/cocos2d/core/camera/CCSGCameraNode.js +++ /dev/null @@ -1,65 +0,0 @@ -var tempMat = new cc.math.Matrix4(); - -let CameraNode = _ccsg.Node.extend({ - ctor: function () { - this._super(); - - this._mat = new cc.math.Matrix4(); - this._mat.identity(); - - this._beforeVisitCmd = new cc.CustomRenderCmd(this, this._onBeforeVisit); - this._afterVisitCmd = new cc.CustomRenderCmd(this, this._onAfterVisit); - }, - - setTransform: function (a, b, c, d, tx, ty) { - let mat = this._mat.mat; - - mat[0] = a; - mat[1] = b; - mat[4] = c; - mat[5] = d; - mat[12] = tx; - mat[13] = ty; - }, - - addTarget: function (target) { - let info = target._cameraInfo; - info.sgCameraNode = this; - info.originVisit = target.visit; - - target.visit = this._visit; - }, - - removeTarget: function (target) { - target.visit = target._cameraInfo.originVisit; - }, - - _visit: function (parent) { - let info = this._cameraInfo; - let sgCameraNode = info.sgCameraNode; - - cc.rendererWebGL.pushRenderCommand(sgCameraNode._beforeVisitCmd); - info.originVisit.call(this, parent); - cc.rendererWebGL.pushRenderCommand(sgCameraNode._afterVisitCmd); - }, - - _onBeforeVisit: function () { - cc.rendererWebGL._breakBatch(); - - cc.math.glMatrixMode(cc.math.KM_GL_PROJECTION); - - tempMat.assignFrom(cc.current_stack.top); - tempMat.multiply(this._mat); - cc.current_stack.push(tempMat); - }, - - _onAfterVisit: function () { - cc.rendererWebGL._breakBatch(); - - cc.math.glMatrixMode(cc.math.KM_GL_PROJECTION); - cc.current_stack.pop(); - }, - -}); - -module.exports = _ccsg.CameraNode = CameraNode; diff --git a/cocos2d/core/collider/CCCollisionManager.js b/cocos2d/core/collider/CCCollisionManager.js index 4e89d60bad..e6f0605a45 100644 --- a/cocos2d/core/collider/CCCollisionManager.js +++ b/cocos2d/core/collider/CCCollisionManager.js @@ -340,14 +340,12 @@ let CollisionManager = cc.Class({ }, attachDebugDrawToCamera: function (camera) { - // TODO: support camera debug draw - // if (!this._debugDrawer) return; - // camera.addTarget(this._debugDrawer); + if (!this._debugDrawer) return; + camera.addTarget(this._debugDrawer); }, detachDebugDrawFromCamera: function (camera) { - // TODO: support camera debug draw - // if (!this._debugDrawer) return; - // camera.removeTarget(this._debugDrawer); + if (!this._debugDrawer) return; + camera.removeTarget(this._debugDrawer); }, onNodeGroupChanged: function (event) { diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index ccea9cbc28..7315b320d4 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -765,10 +765,10 @@ var Layout = cc.Class({ }); if (allChildrenBoundingBox) { - var leftBottomInParentSpace = this.node.parent.convertToNodeSpace(cc.p(allChildrenBoundingBox.x, allChildrenBoundingBox.y)); + var leftBottomInParentSpace = this.node.parent.convertToNodeSpaceAR(cc.p(allChildrenBoundingBox.x, allChildrenBoundingBox.y)); leftBottomInParentSpace = cc.pAdd(leftBottomInParentSpace, cc.p(-this.paddingLeft, -this.paddingBottom)); - var rightTopInParentSpace = this.node.parent.convertToNodeSpace(cc.p(allChildrenBoundingBox.x + allChildrenBoundingBox.width, + var rightTopInParentSpace = this.node.parent.convertToNodeSpaceAR(cc.p(allChildrenBoundingBox.x + allChildrenBoundingBox.width, allChildrenBoundingBox.y + allChildrenBoundingBox.height)); rightTopInParentSpace = cc.pAdd(rightTopInParentSpace, cc.p(this.paddingRight, this.paddingTop)); diff --git a/cocos2d/core/index.js b/cocos2d/core/index.js index 117da12f4e..2e5f351c44 100644 --- a/cocos2d/core/index.js +++ b/cocos2d/core/index.js @@ -36,7 +36,7 @@ if (!CC_EDITOR || !Editor.isMainProcess) { // CCIntersection can be used separately. require('./collider/CCIntersection'); require('./physics'); - // require('./camera/CCCamera'); + require('./camera/CCCamera'); } require('./base-ui/CCWidgetManager'); diff --git a/cocos2d/core/physics/joint/CCMouseJoint.js b/cocos2d/core/physics/joint/CCMouseJoint.js index 4f1046d3c4..b88576392e 100644 --- a/cocos2d/core/physics/joint/CCMouseJoint.js +++ b/cocos2d/core/physics/joint/CCMouseJoint.js @@ -263,8 +263,9 @@ var MouseJoint = cc.Class({ return; } - if (cc.Camera && cc.Camera.main) { - this.target = cc.Camera.main.getCameraToWorldPoint(this._pressPoint); + var camera = cc.Camera.find(this.node); + if (camera) { + this.target = camera.getCameraToWorldPoint(this._pressPoint); } else { this.target = this._pressPoint; diff --git a/cocos2d/core/renderer/assemblers/tmx-layer-assembler.js b/cocos2d/core/renderer/assemblers/tmx-layer-assembler.js index 1ba73ca8e8..97554747f1 100644 --- a/cocos2d/core/renderer/assemblers/tmx-layer-assembler.js +++ b/cocos2d/core/renderer/assemblers/tmx-layer-assembler.js @@ -30,7 +30,6 @@ const js = require('../../platform/js'); const assembler = require('./assembler'); const renderEngine = require('../render-engine'); const RenderData = renderEngine.RenderData; -const math = renderEngine.math; const Orientation = TiledMap.Orientation; const TileFlag = TiledMap.TileFlag; @@ -38,8 +37,12 @@ const FLIPPED_MASK = TileFlag.FLIPPED_MASK; const StaggerAxis = TiledMap.StaggerAxis; const StaggerIndex = TiledMap.StaggerIndex; -let _matrix = math.mat4.create(); -let _v3 = cc.v3(); +const math = renderEngine.math; +const mat4 = math.mat4; +const vec3 = math.vec3; + +let _mat4_temp = mat4.create(); +let _vec3_temp = vec3.create(); let tmxAssembler = js.addon({ createData (comp) { @@ -145,23 +148,24 @@ let tmxAssembler = js.addon({ let enabledCulling = cc.macro.ENABLE_TILEDMAP_CULLING; if (enabledCulling) { - // TODO: support camera - // if (this._cameraFlag > 0) { - // let tmpt = cc.affineTransformConcat(wt, cc.Camera.main.viewMatrix); - // cullingA = tmpt.a; - // cullingD = tmpt.d; - // cullingMapx = ox * cullingA + oy * tmpt.c + tmpt.tx; - // cullingMapy = ox * tmpt.b + oy * cullingD + tmpt.ty; - // cullingW = tilew * cullingA; - // cullingH = tileh * cullingD; - // } - + let camera = cc.Camera.findCamera(comp.node); + if (camera) { + camera.getWorldToCameraMatrix(_mat4_temp); + mat4.mul(_mat4_temp, matrix, _mat4_temp); + cullingA = _mat4_temp.m00; + cullingD = _mat4_temp.m05; + cullingMapx = ox * cullingA + oy * _mat4_temp.m04 + _mat4_temp.m12; + cullingMapy = ox * _mat4_temp.m01 + oy * cullingD + _mat4_temp.m13; + cullingW = tilew * cullingA; + cullingH = tileh * cullingD; + } + if (layerOrientation === Orientation.ORTHO) { - cc.vmath.mat4.invert(_matrix, matrix); + cc.vmath.mat4.invert(_mat4_temp, matrix); let rect = cc.visibleRect; - let a = _matrix.m00, b = _matrix.m01, c = _matrix.m04, d = _matrix.m05, - tx = _matrix.m12, ty = _matrix.m13; + let a = _mat4_temp.m00, b = _mat4_temp.m01, c = _mat4_temp.m04, d = _mat4_temp.m05, + tx = _mat4_temp.m12, ty = _mat4_temp.m13; let v0x = rect.topLeft.x * a + rect.topLeft.y * c + tx; let v0y = rect.topLeft.x * b + rect.topLeft.y * d + ty; let v1x = rect.bottomLeft.x * a + rect.bottomLeft.y * c + tx; @@ -251,12 +255,12 @@ let tmxAssembler = js.addon({ // transform a2 = a; b2 = b; c2 = c; d2 = d; tx2 = tx; ty2 = ty; tiledNode._updateLocalMatrix(); - cc.vmath.mat4.copy(_matrix, tiledNode._matrix); - _v3.x = -left; _v3.y = -bottom; _v3.z = 0; - cc.vmath.mat4.translate(_matrix, _matrix, _v3); - cc.vmath.mat4.multiply(_matrix, matrix, _matrix); - a = _matrix.m00; b = _matrix.m01; c = _matrix.m04; d = _matrix.m05; - tx = _matrix.m12; ty = _matrix.m13; + cc.vmath.mat4.copy(_mat4_temp, tiledNode._mat4_temp); + _vec3_temp.x = -left; _vec3_temp.y = -bottom; _vec3_temp.z = 0; + cc.vmath.mat4.translate(_mat4_temp, _mat4_temp, _vec3_temp); + cc.vmath.mat4.multiply(_mat4_temp, matrix, _mat4_temp); + a = _mat4_temp.m00; b = _mat4_temp.m01; c = _mat4_temp.m04; d = _mat4_temp.m05; + tx = _mat4_temp.m12; ty = _mat4_temp.m13; } left -= appx; diff --git a/cocos2d/core/renderer/index.js b/cocos2d/core/renderer/index.js index be49c12b0a..ce92f719be 100644 --- a/cocos2d/core/renderer/index.js +++ b/cocos2d/core/renderer/index.js @@ -80,11 +80,13 @@ module.exports = { this._walker = new RenderComponentWalker(this.device, this.scene); this._cameraNode = new cc.Node(); + this._camera = new renderEngine.Camera(); this._camera.setFov(Math.PI * 60 / 180); this._camera.setNear(0.1); this._camera.setFar(1024); this._camera.setNode(this._cameraNode); + let view = new renderEngine.View(); this._camera.view = view; this._camera.dirty = true; @@ -107,6 +109,7 @@ module.exports = { var ecScene = cc.director.getScene(); ecScene.scaleX = ecScene.scaleY = 1; } + let node = this._cameraNode; let canvas = this.canvas; let scaleX = cc.view.getScaleX(); diff --git a/cocos2d/core/renderer/render-component-walker.js b/cocos2d/core/renderer/render-component-walker.js index 25f9181273..a3f7144b41 100644 --- a/cocos2d/core/renderer/render-component-walker.js +++ b/cocos2d/core/renderer/render-component-walker.js @@ -35,6 +35,7 @@ const FLOATS_PER_VERT = defaultVertexFormat._bytes / 4; const MAX_VERTEX = macro.BATCH_VERTEX_COUNT; const MAX_INDICE = MAX_VERTEX * 2; + var _queue = null; var _batchData = { vfmt: null, @@ -44,6 +45,7 @@ var _batchData = { indiceOffset: 0, comp: null, data: null, + cullingMask: 1, MAX_VERTEX: MAX_VERTEX, MAX_INDICE: MAX_INDICE }; @@ -135,6 +137,17 @@ RenderComponentWalker.prototype = { }, _handleRender (node) { + if (node._cullingMask !== 1) { + this._curCameraNode = node; + } + + if (this._curCameraNode) { + node._inheritMask = this._curCameraNode._cullingMask; + } + else { + node._inheritMask = 1; + } + let comp = node._renderComponent; if (comp) { _queue.push(comp); @@ -146,9 +159,13 @@ RenderComponentWalker.prototype = { if (comp && comp.constructor._postAssembler) { _queue.push(comp); } + + if (this._curCameraNode === node) { + this._curCameraNode = null; + } }, - _flush (vertexFormat, effect, lastByte, vertexCount, indiceCount) { + _flush (vertexFormat, effect, lastByte, vertexCount, indiceCount, cullingMask) { let vertexByte = vertexCount * vertexFormat._bytes, byteOffset = lastByte - vertexByte, vertexsData = null, @@ -191,7 +208,7 @@ RenderComponentWalker.prototype = { let model = this._modelPool.add(); this._batchedModels.push(model); model.sortKey = this._sortKey++; - model._viewID = -1; + model._cullingMask = CC_EDITOR ? 1 : cullingMask; model.setNode(this._node); model.addEffect(effect); model.addInputAssembler(ia); @@ -219,13 +236,14 @@ RenderComponentWalker.prototype = { _batchData.vertexOffset = 0; _batchData.byteOffset = 0; _batchData.indiceOffset = 0; + _batchData.cullingMask = 1; }, _checkBatchBroken (batchData, needNewBuf) { let vertexCount = batchData.vertexOffset - this._vOffset, indiceCount = batchData.indiceOffset - this._iOffset; if (batchData.vfmt && batchData.effect && vertexCount > 0 && indiceCount > 0) { - this._flush(batchData.vfmt, batchData.effect, batchData.byteOffset, vertexCount, indiceCount); + this._flush(batchData.vfmt, batchData.effect, batchData.byteOffset, vertexCount, indiceCount, batchData.cullingMask); if (needNewBuf) { this._switchBuffer(); } @@ -267,6 +285,8 @@ RenderComponentWalker.prototype = { _batchData.comp = comp; datas = assembler.updateRenderData(comp, _batchData); + let cullingMask = comp.node._inheritMask; + for (let id = 0; id < datas.length; id ++) { data = datas[id]; _batchData.data = data; @@ -277,11 +297,12 @@ RenderComponentWalker.prototype = { } // breaking batch needNewBuf = (_batchData.vertexOffset + data.vertexCount > MAX_VERTEX) || (_batchData.indiceOffset + data.indiceCount > MAX_INDICE); - if (_batchData.vfmt && (_batchData.effect != effect || needNewBuf)) { + if (_batchData.vfmt && (_batchData.effect != effect || _batchData.cullingMask !== cullingMask || needNewBuf)) { this._checkBatchBroken(_batchData, needNewBuf); this._node = this._dummyNode; _batchData.effect = effect; _batchData.vfmt = comp._vertexFormat; + _batchData.cullingMask = cullingMask; } // Init effect else if (!_batchData.effect) { diff --git a/cocos2d/deprecated.js b/cocos2d/deprecated.js index 99bf2940d5..f3b3c1700b 100644 --- a/cocos2d/deprecated.js +++ b/cocos2d/deprecated.js @@ -464,6 +464,10 @@ if (CC_DEV) { 'getWorldToNodeTransform': 'getWorldMatrix', }); + markFunctionWarning(cc.Camera.prototype, { + 'getNodeToCameraTransform': 'getWorldToCameraMatrix' + }); + provideClearError(cc.Node.prototype, { arrivalOrder: 'getSiblingIndex, setSiblingIndex', _visible: '_activeInHierarchy, active',