Merge pull request #1568 from 2youyou2/camera

add camera
This commit is contained in:
Wang Nan
2017-03-29 17:29:56 +08:00
committed by GitHub
15 changed files with 290 additions and 110 deletions
+2
View File
@@ -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;
}
+167
View File
@@ -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;
+69
View File
@@ -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;
@@ -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);
+1
View File
@@ -34,6 +34,7 @@ if (!CC_EDITOR || !Editor.isMainProcess) {
require('./graphics');
require('./collider');
require('./physics');
require('./camera/CCCamera');
}
require('./base-ui/CCWidgetManager');
@@ -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<vertexCount; i++) {
if (i === 0)
drawer.moveTo(vertices[i].x * CC_PTM_RATIO, vertices[i].y * CC_PTM_RATIO);
else {
drawer.lineTo(vertices[i].x * CC_PTM_RATIO, vertices[i].y * CC_PTM_RATIO);
}
}
drawer.close();
},
DrawPolygon: function (vertices, vertexCount, color) {
this._applyStrokeColor(color);
this._DrawPolygon(vertices, vertexCount);
this._drawer.stroke();
},
DrawSolidPolygon: function (vertices, vertexCount, color) {
this._applyFillColor(color);
this._DrawPolygon(vertices, vertexCount);
this._drawer.fill();
},
_DrawCircle: function (center, radius) {
this._drawer.circle(center.x * CC_PTM_RATIO, center.y * CC_PTM_RATIO, radius * CC_PTM_RATIO);
},
DrawCircle: function (center, radius, color) {
this._applyStrokeColor(color);
this._DrawCircle(center, radius);
this._drawer.stroke();
},
DrawSolidCircle: function (center, radius, axis, color) {
this._applyFillColor(color);
this._DrawCircle(center, radius);
this._drawer.fill();
},
DrawSegment: function (p1, p2, color) {
var drawer = this._drawer;
if (p1.x === p2.x && p1.y === p2.y) {
this._applyFillColor(color);
this._DrawCircle(p1, 2/CC_PTM_RATIO);
drawer.fill();
return;
}
this._applyStrokeColor(color);
drawer.moveTo(p1.x * CC_PTM_RATIO, p1.y * CC_PTM_RATIO);
drawer.lineTo(p2.x * CC_PTM_RATIO, p2.y * CC_PTM_RATIO);
drawer.stroke();
},
DrawPoint: function (center, radius, color) {
},
_applyStrokeColor: function (color) {
this._drawer.setStrokeColor( cc.color(color.r*255, color.g*255, color.b*255, 150) );
},
_applyFillColor: function (color) {
this._drawer.setFillColor( cc.color(color.r*255, color.g*255, color.b*255, 150) );
}
});
cc.PhysicsDebugDraw = module.exports = PhysicsDebugDraw;
+12 -1
View File
@@ -179,6 +179,15 @@ var PhysicsManager = cc.Class({
}
},
attachDebugDrawToCamera: function (camera) {
if (!this._debugDrawer) return;
camera.addTarget(this._debugDrawer.getDrawer());
},
detachDebugDrawFromCamera: function (camera) {
if (!this._debugDrawer) return;
camera.removeTarget(this._debugDrawer.getDrawer());
},
_registerContactFixture: function (fixture) {
this._contactListener.registerContactFixture(fixture);
},
@@ -259,7 +268,8 @@ var PhysicsManager = cc.Class({
for (var i = 0, l = bodies.length; i < l; i++) {
var body = bodies[i];
var node = body.node;
node._position = node._sgNode.getPosition();
node._position.x = node._sgNode.getPositionX();
node._position.y = node._sgNode.getPositionY();
node._rotationX = node._rotationY = node._sgNode.getRotation();
}
}
@@ -384,6 +394,7 @@ cc.js.getset(PhysicsManager.prototype, 'gravity',
}
);
PhysicsManager.DrawBits = b2.Draw;
PhysicsManager.CC_PTM_RATIO = CC_PTM_RATIO;
PhysicsManager.CC_TO_PHYSICS_ANGLE = CC_TO_PHYSICS_ANGLE;
+1 -1
View File
@@ -12,7 +12,7 @@ var BodyType = cc.Enum({
Dynamic: 2,
Animated: 3
});
cc.PhysicsBodyType = BodyType;
cc.RigidBodyType = BodyType;
var RayCastType = cc.Enum({
Closest: 0,
@@ -9,6 +9,10 @@ var PhysicsDebugDraw = cc.Class({
this._drawer.retain();
},
getDrawer: function () {
return this._drawer;
},
AddDrawerToNode: function (node) {
this._drawer.removeFromParent();
node.addChild(this._drawer);
+1 -3
View File
@@ -268,9 +268,7 @@ cc.rendererWebGL = {
if (cmdList.indexOf(cmd) === -1)
cmdList.push(cmd);
} else {
if (this._renderCmds.indexOf(cmd) === -1) {
this._renderCmds.push(cmd);
}
this._renderCmds.push(cmd);
}
},
+15 -13
View File
@@ -1409,23 +1409,25 @@ cc.Scale9Sprite = _ccsg.Node.extend({
// Culling
if (webgl) {
// disabled for camera for now, find a better way for webgl culling
// 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.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;
}
// 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,
+6
View File
@@ -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(){
-1
View File
@@ -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 () {
+1
View File
@@ -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) {
+2 -1
View File
@@ -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