Merge pull request #1720 from 2youyou2/culling

support camera culling
This commit is contained in:
Wang Nan
2017-06-12 10:12:27 +08:00
committed by GitHub
6 changed files with 206 additions and 47 deletions
+9 -1
View File
@@ -1306,7 +1306,15 @@ var Node = cc.Class({
var w = this.width,
h = this.height;
var rect = cc.rect(0, 0, w, h);
var trans = this.getNodeToWorldTransform();
var trans;
if (cc.Camera.main) {
trans = cc.Camera.main.getNodeToCameraTransform(this);
}
else {
trans = this.getNodeToWorldTransform();
}
cc._rectApplyAffineTransformIn(rect, trans);
var left = point.x - rect.x,
right = rect.x + rect.width - point.x,
@@ -103,6 +103,8 @@ _ccsg.Node.RenderCmd = function (renderable) {
this._dirtyFlag = 1;
this._curLevel = -1;
this._cameraFlag = 0;
this._displayedColor = new cc.Color(255, 255, 255, 255);
this._displayedOpacity = 255;
this._cascadeColorEnabledDirty = false;
@@ -277,6 +279,14 @@ _ccsg.Node.RenderCmd.prototype = {
this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.DirtyDouble);
}
if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) {
// check if node is in camera
var camera = cc.Camera.main;
if (parentCmd && this._cameraFlag != cc.Camera.flags.InCamera) {
this._cameraFlag = parentCmd._cameraFlag > 0 ? cc.Camera.flags.ParentInCamera : 0;
}
}
if (recursive) {
transformChildTree(node);
}
+138 -22
View File
@@ -27,8 +27,6 @@ if (!CC_JSB) {
require('./CCSGCameraNode');
}
let tempTransform = cc.affineTransformMake();
/**
* !#en
* Camera is usefull when making reel game or other games which need scroll screen.
@@ -43,6 +41,21 @@ let Camera = cc.Class({
name: 'cc.Camera',
extends: cc._RendererUnderSG,
ctor: function () {
this.viewMatrix = cc.affineTransformMake();
this.invertViewMatrix = cc.affineTransformMake();
this._sgTarges = [];
this.visibleRect = {
left: cc.v2(),
right: cc.v2(),
top: cc.v2(),
bottom: cc.v2()
};
this.viewPort = cc.rect();
},
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.others/Camera',
executeInEditMode: false
@@ -92,20 +105,47 @@ let Camera = cc.Class({
_initSgNode: function () {},
_addSgTargetInSg: function (target) {
var sgNode;
if (target instanceof cc.Node) {
this._sgNode.addTarget(target._sgNode);
sgNode = target._sgNode;
}
else if (target instanceof _ccsg.Node) {
this._sgNode.addTarget(target);
sgNode = target;
}
if (!sgNode) return;
this._sgNode.addTarget(sgNode);
if (this._sgTarges.indexOf(sgNode) === -1) {
this._sgTarges.push(sgNode);
}
if (!CC_JSB) {
var cmd = sgNode._renderCmd;
cmd.setDirtyFlag(_ccsg.Node._dirtyFlags.transformDirty);
cmd._cameraFlag = Camera.flags.InCamera;
}
},
_removeTargetInSg: function (target) {
var sgNode;
if (target instanceof cc.Node) {
this._sgNode.removeTarget(target._sgNode);
sgNode = target._sgNode;
}
else if (target instanceof _ccsg.Node) {
this._sgNode.removeTarget(target);
sgNode = target;
}
if (!sgNode) return;
this._sgNode.removeTarget(sgNode);
cc.js.array.remove(this._sgTarges, sgNode);
if (!CC_JSB) {
var cmd = sgNode._renderCmd;
cmd.setDirtyFlag(_ccsg.Node._dirtyFlags.transformDirty);
cmd._cameraFlag = 0;
}
},
@@ -182,13 +222,73 @@ let Camera = cc.Class({
return this._targets;
},
calculateCameraTransformIn: function (transform) {
/**
* !#en
* Returns the matrix that transform the node's (local) space coordinates into the camera's space coordinates.
* !#zh
* 返回一个将节点坐标系转换到摄像机坐标系下的矩阵
* @param {Node} node - the node which should transform
* @return {AffineTransform}
*/
getNodeToCameraTransform (node) {
var t = node.getNodeToWorldTransform();
if (this.containsNode(node)) {
t = cc.affineTransformConcatIn(t, cc.Camera.main.viewMatrix);
}
return t;
},
/**
* !#en
* Conver a camera coordinates point to world coordinates.
* !#zh
* 讲一个摄像机坐标系下的点转换到世界坐标系下。
* @param {Node} point - the point which should transform
* @return {Vec2}
*/
getCameraToWorldPoint (point) {
if (cc.Camera.main) {
point = cc.pointApplyAffineTransform(point, cc.Camera.main.invertViewMatrix);
}
return point;
},
/**
* !#en
* Check whether the node is in the camera.
* !#zh
* 检测节点是否被此摄像机影响
* @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;
},
lateUpdate: !CC_EDITOR && function () {
let m = this.viewMatrix;
let im = this.invertViewMatrix;
let viewPort = this.viewPort;
let visibleRect = cc.visibleRect;
let selfVisibleRect = this.visibleRect;
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 wt = node.getNodeToWorldTransformAR();
let a = 1, b = 0, c = 0, d = 1;
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) {
@@ -205,22 +305,38 @@ let Camera = cc.Class({
c *= zoomRatio;
d *= zoomRatio;
m.a = a;
m.b = b;
m.c = c;
m.d = d;
// 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);
let center = visibleRect.center;
m.tx = center.x - (a * wt.tx + c * wt.ty);
m.ty = center.y - (b * wt.tx + d * wt.ty);
transform.a = a;
transform.b = b;
transform.c = c;
transform.d = d;
},
// calculate ivert view matrix
cc.affineTransformInvertOut(m, im);
lateUpdate: !CC_EDITOR && function () {
let t = tempTransform;
this.calculateCameraTransformIn(t);
this._sgNode.setTransform(t.a, t.b, t.c, t.d, t.tx, t.ty);
// 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);
}
});
Camera.flags = cc.Enum({
InCamera: 1,
ParentInCamera: 2
});
module.exports = cc.Camera = Camera;
+11 -2
View File
@@ -85,8 +85,17 @@ proto.transform = function (parentCmd, recursive) {
return;
}
var rect = cc.visibleRect,
vl = rect.left.x, vr = rect.right.x, vt = rect.top.y, vb = rect.bottom.y;
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;
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
+14 -2
View File
@@ -220,7 +220,12 @@ var MouseJoint = cc.Class({
onTouchBegan: function (event) {
var manager = cc.director.getPhysicsManager();
var target = event.touch.getLocation();
var target = this._pressPoint = event.touch.getLocation();
if (cc.Camera.main) {
target = cc.Camera.main.getCameraToWorldPoint(target);
}
var collider = manager.testPoint( target );
if (!collider) return;
@@ -234,11 +239,12 @@ var MouseJoint = cc.Class({
},
onTouchMove: function (event) {
this.target = event.touch.getLocation();
this._pressPoint = event.touch.getLocation();
},
onTouchEnd: function (event) {
this._destroy();
this._pressPoint = null;
},
_createJointDef: function () {
@@ -250,6 +256,12 @@ var MouseJoint = cc.Class({
def.dampingRatio = this.dampingRatio;
def.frequencyHz = this.frequency;
return def;
},
update: function () {
if (cc.Camera.main && this._pressPoint) {
this.target = cc.Camera.main.getCameraToWorldPoint(this._pressPoint);
}
}
});
+24 -20
View File
@@ -1050,10 +1050,6 @@ cc.Scale9Sprite = _ccsg.Node.extend({
if (webgl === undefined) {
webgl = cc._renderType === cc.game.RENDER_TYPE_WEBGL;
vl = cc.visibleRect.left;
vr = cc.visibleRect.right;
vt = cc.visibleRect.top;
vb = cc.visibleRect.bottom;
}
},
@@ -1407,32 +1403,40 @@ cc.Scale9Sprite = _ccsg.Node.extend({
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) {
// 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) & (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.x || l > vr.x || t < vb.y || b > vt.y) {
if (r < vl || l > vr || t < vb || b > vt) {
this._renderCmd._needDraw = false;
}
else {