diff --git a/DebugInfos.js b/DebugInfos.js index 2d2a0c6aa9..a85a68d007 100644 --- a/DebugInfos.js +++ b/DebugInfos.js @@ -579,6 +579,8 @@ if (CC_DEV) { "8105": "cocos2d: cc.shaderCache._loadDefaultShader, error shader type", //_loadDefaultShader //Physics: 8200 "8200": "Please set node\'s active instead of rigidbody\'s enabled.", //cc.RigidBody.enabled + //Camera: 8300 + "8300": "Should only one camera exists, please check your project.", //cc.Camera.enabled }; cc._LogInfos = logs; } diff --git a/cocos2d/core/camera/CCCamera.js b/cocos2d/core/camera/CCCamera.js new file mode 100644 index 0000000000..baab85ea38 --- /dev/null +++ b/cocos2d/core/camera/CCCamera.js @@ -0,0 +1,167 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Chukong Aipu reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +if (!CC_JSB) { + require('./CCSGCameraNode'); +} + +let tempTransform = cc.affineTransformMake(); + +let Camera = cc.Class({ + name: 'cc.Camera', + extends: cc._RendererUnderSG, + + editor: CC_EDITOR && { + menu: 'i18n:MAIN_MENU.component.others/Camera', + executeInEditMode: false + }, + + properties: { + _targets: { + default: [], + type: cc.Node, + visible: true + }, + + zoomRatio: 1, + }, + + _createSgNode: function () { + return new _ccsg.CameraNode(); + }, + + _initSgNode: function () {}, + + _addSgTargetInSg: function (target) { + if (target instanceof cc.Node) { + this._sgNode.addTarget(target._sgNode); + } + else if (target instanceof _ccsg.Node) { + this._sgNode.addTarget(target); + } + }, + + _removeTargetInSg: function (target) { + if (target instanceof cc.Node) { + this._sgNode.removeTarget(target._sgNode); + } + else if (target instanceof _ccsg.Node) { + this._sgNode.removeTarget(target); + } + }, + + onEnable: function () { + if (Camera.main) { + cc.errorID(8300); + return; + } + + Camera.main = this; + + let targets = this._targets; + for (let i = 0, l = targets.length; i < l; i++) { + this._addSgTargetInSg(targets[i]); + } + }, + + onDisable: function () { + if (Camera.main !== this) { + return; + } + + Camera.main = null; + + let targets = this._targets; + for (let i = 0, l = targets.length; i < l; i++) { + this._removeTargetInSg(targets[i]); + } + }, + + addTarget: function (target) { + if (this._targets.indexOf(target) !== -1) { + return; + } + + this._addSgTargetInSg(target); + this._targets.push(target); + }, + + removeTarget: function (target) { + if (this._targets.indexOf(target) === -1) { + return; + } + + this._removeTargetInSg(target); + cc.js.array.remove(this._targets, target); + }, + + getTargets: function () { + return this._targets; + }, + + calculateCaemraTransformIn: function (transform) { + let node = this.node; + + let wt = node.getNodeToWorldTransform(); + 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; + + // 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; + + // move camera to center + let center = cc.visibleRect.center; + transform.tx = center.x - (a * wt.tx + c * wt.ty); + transform.ty = center.y - (b * wt.tx + d * wt.ty); + + transform.a = a; + transform.b = b; + transform.c = c; + transform.d = d; + }, + + lateUpdate: !CC_EDITOR && function () { + let t = tempTransform; + this.calculateCaemraTransformIn(t); + this._sgNode.setTransform(t.a, t.b, t.c, t.d, t.tx, t.ty); + } +}); + +Camera.main = null; + +module.exports = cc.Camera = Camera; diff --git a/cocos2d/core/camera/CCSGCameraNode.js b/cocos2d/core/camera/CCSGCameraNode.js new file mode 100644 index 0000000000..32923935d5 --- /dev/null +++ b/cocos2d/core/camera/CCSGCameraNode.js @@ -0,0 +1,69 @@ + +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) { + if (target._cameraInfo) return; + + target._cameraInfo = { + sgCameraNode: this, + originVisit: target.visit + }; + + target.visit = this._visit; + }, + + removeTarget: function (target) { + let info = target._cameraInfo; + if (!info) return; + + target.visit = info.originVisit; + target._cameraInfo = undefined; + }, + + _visit: function (parent) { + let info = this._cameraInfo; + let sgCameraNode = info.sgCameraNode; + + cc.renderer.pushRenderCommand(sgCameraNode._beforeVisitCmd); + info.originVisit.call(this, parent); + cc.renderer.pushRenderCommand(sgCameraNode._afterVisitCmd); + }, + + _onBeforeVisit: function () { + cc.renderer._breakBatch(); + + cc.math.glMatrixMode(cc.math.KM_GL_PROJECTION) + cc.current_stack.push(); + cc.current_stack.top.multiply(this._mat); + }, + + _onAfterVisit: function () { + cc.renderer._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 9a16b7bb6f..d748778aba 100644 --- a/cocos2d/core/collider/CCCollisionManager.js +++ b/cocos2d/core/collider/CCCollisionManager.js @@ -308,6 +308,15 @@ var CollisionManager = cc.Class({ } }, + attachDebugDrawToCamera: function (camera) { + if (!this._debugDrawer) return; + camera.addTarget(this._debugDrawer); + }, + detachDebugDrawFromCamera: function (camera) { + if (!this._debugDrawer) return; + camera.removeTarget(this._debugDrawer); + }, + onNodeGroupChanged: function (event) { var node = event.currentTarget; var colliders = node.getComponents(cc.Collider); diff --git a/cocos2d/core/index.js b/cocos2d/core/index.js index 1fca2325be..e080d7001a 100644 --- a/cocos2d/core/index.js +++ b/cocos2d/core/index.js @@ -34,6 +34,7 @@ if (!CC_EDITOR || !Editor.isMainProcess) { require('./graphics'); require('./collider'); require('./physics'); + require('./camera/CCCamera'); } require('./base-ui/CCWidgetManager'); diff --git a/cocos2d/core/physics/CCPhysicsDebugDraw.js b/cocos2d/core/physics/CCPhysicsDebugDraw.js deleted file mode 100644 index 784d23b3a2..0000000000 --- a/cocos2d/core/physics/CCPhysicsDebugDraw.js +++ /dev/null @@ -1,90 +0,0 @@ - -var CC_PTM_RATIO = cc.PhysicsManager.CC_PTM_RATIO; -var PhysicsDebugDraw = cc.Class({ - name: 'cc.PhysicsDebugDraw', - mixins: [b2.Draw], - - ctor: function () { - this._drawer = new _ccsg.GraphicsNode(); - this._drawer.retain(); - }, - - AddDrawerToNode: function (node) { - this._drawer.removeFromParent(); - node.addChild(this._drawer); - }, - - ClearDraw: function () { - this._drawer.clear(); - }, - - _DrawPolygon: function (vertices, vertexCount) { - var drawer = this._drawer; - - for (var i=0; i> 31 || // All outside left - ((vr.x-x0) & (vr.x-x1) & (vr.x-x2) & (vr.x-x3)) >> 31 || // All outside right - ((y0-vb.y) & (y1-vb.y) & (y2-vb.y) & (y3-vb.y)) >> 31 || // All outside bottom - ((vt.y-y0) & (vt.y-y1) & (vt.y-y2) & (vt.y-y3)) >> 31) // All outside top - { - this._renderCmd._needDraw = false; - } - else { - this._renderCmd._needDraw = true; - } + // 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.x) & (x1-vl.x) & (x2-vl.x) & (x3-vl.x)) >> 31 || // All outside left + // ((vr.x-x0) & (vr.x-x1) & (vr.x-x2) & (vr.x-x3)) >> 31 || // All outside right + // ((y0-vb.y) & (y1-vb.y) & (y2-vb.y) & (y3-vb.y)) >> 31 || // All outside bottom + // ((vt.y-y0) & (vt.y-y1) & (vt.y-y2) & (vt.y-y3)) >> 31) // All outside top + // { + // this._renderCmd._needDraw = false; + // } + // else { + // this._renderCmd._needDraw = true; + // } } else { var bb = this._renderCmd._currentRegion, diff --git a/cocos2d/kazmath/gl/mat4stack.js b/cocos2d/kazmath/gl/mat4stack.js index 8e2089e0bb..58059665c9 100644 --- a/cocos2d/kazmath/gl/mat4stack.js +++ b/cocos2d/kazmath/gl/mat4stack.js @@ -50,11 +50,17 @@ proto.push = function(item) { this.stack.push(this.top); this.top = new cc.math.Matrix4(item); //this.top = this._getFromPool(item); + this.update(); }; proto.pop = function() { //this._putInPool(this.top); this.top = this.stack.pop(); + this.update(); +}; + +proto.update = function () { + this.lastUpdated ++; }; proto.release = function(){ diff --git a/cocos2d/kazmath/gl/matrix.js b/cocos2d/kazmath/gl/matrix.js index db7e2fc653..3aa868815d 100644 --- a/cocos2d/kazmath/gl/matrix.js +++ b/cocos2d/kazmath/gl/matrix.js @@ -106,7 +106,6 @@ math.glMatrixMode = function (mode) { throw new Error("Invalid matrix mode specified"); //TODO: Proper error handling break; } - cc.current_stack.lastUpdated = cc.director.getTotalFrames(); }; math.glLoadIdentity = function () { diff --git a/gulpfile.js b/gulpfile.js index ce4b17d761..a08f1bb955 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -95,6 +95,7 @@ var jsbSkipModules = [ '../../cocos2d/core/physics/platform/CCPhysicsAABBQueryCallback.js', '../../cocos2d/core/physics/platform/CCPhysicsRayCastCallback.js', '../../cocos2d/core/physics/platform/CCPhysicsContactListner.js', + '../../cocos2d/core/camera/CCSGCameraNode.js', ]; gulp.task('build-jsb-extends-dev', function (done) { diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js index 52fd8a009d..c15e28dfb8 100644 --- a/jsb/jsb-etc.js +++ b/jsb/jsb-etc.js @@ -197,7 +197,8 @@ window._ccsg = { TMXObjectImage: cc.TMXObjetImage, TMXObjectShape: cc.TMXObjectShape, TMXLayer: cc.TMXLayer, - MotionStreak: cc.MotionStreak + MotionStreak: cc.MotionStreak, + CameraNode: cc.CameraNode }; // __errorHandler