Improve ortho tilemap culling calculation

This commit is contained in:
pandamicro
2017-11-20 14:40:33 +08:00
parent a595b8b6db
commit ed145df228
3 changed files with 39 additions and 17 deletions
+5 -3
View File
@@ -1695,9 +1695,9 @@ _ccsg.Node = cc.Class({
* @return {cc.AffineTransform}
*/
getNodeToWorldTransform: function () {
var t = this.getNodeToParentTransform();
var t = cc.affineTransformClone(this.getNodeToParentTransform());
for (var p = this._parent; p !== null; p = p.parent)
t = cc.affineTransformConcat(t, p.getNodeToParentTransform());
t = cc.affineTransformConcatIn(t, p.getNodeToParentTransform());
return t;
},
@@ -1715,7 +1715,9 @@ _ccsg.Node = cc.Class({
* @return {cc.AffineTransform}
*/
getWorldToNodeTransform: function () {
return cc.affineTransformInvert(this.getNodeToWorldTransform());
var trans = this.getNodeToWorldTransform();
cc.affineTransformInvertOut(trans, trans);
return trans;
},
/**
@@ -382,12 +382,12 @@ cc.affineTransformInvert = function (t) {
* @param {AffineTransform} out
*/
cc.affineTransformInvertOut = function (t, out) {
var a = t.a, b = t.b, c = t.c, d = t.d;
var a = t.a, b = t.b, c = t.c, d = t.d, tx = t.tx, ty = t.ty;
var determinant = 1 / (a * d - b * c);
out.a = determinant * d;
out.b = -determinant * b;
out.c = -determinant * c;
out.d = determinant * a;
out.tx = determinant * (c * t.ty - d * t.tx);
out.ty = determinant * (b * t.tx - a * t.ty);
out.tx = determinant * (c * ty - d * tx);
out.ty = determinant * (b * tx - a * ty);
};
+31 -11
View File
@@ -118,24 +118,45 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset) {
var cullingA = a, cullingD = d,
cullingMapx = mapx, cullingMapy = mapy,
cullingW = w, cullingH = h;
var enabledCulling = cc.macro.ENABLE_TILEDMAP_CULLING;
var enabledCulling = cc.macro.ENABLE_TILEDMAP_CULLING,
vertices = this._vertices;
if (enabledCulling) {
var trans;
if (this._cameraFlag > 0) {
var 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;
trans = cc.affineTransformConcat(wt, cc.Camera.main.viewMatrix);
cullingA = trans.a;
cullingD = trans.d;
cullingMapx = ox * cullingA + oy * trans.c + trans.tx;
cullingMapy = ox * trans.b + oy * cullingD + trans.ty;
cullingW = tilew * cullingA;
cullingH = tileh * cullingD;
}
if (layerOrientation === Orientation.ORTHO) {
startCol = Math.floor(-(cullingMapx - extw * cullingA) / (maptw * cullingA));
startRow = Math.floor((cullingMapy - exth * cullingD + mapth * rows * cullingD - winh) / (mapth * cullingD));
maxCol = Math.ceil((winw - cullingMapx + extw * cullingA) / (maptw * cullingA));
maxRow = rows - Math.floor(-(cullingMapy + exth * cullingD) / (mapth * cullingD));
// In case trans is not created yet
if (this._cameraFlag <= 0) {
trans = cc.affineTransformClone(wt);
}
cc.affineTransformInvertOut(trans, trans);
var world = cc.visibleRect;
vertices[0].x = world.topLeft.x * trans.a + world.topLeft.y * trans.c + trans.tx;
vertices[0].y = world.topLeft.x * trans.b + world.topLeft.y * trans.d + trans.ty;
vertices[1].x = world.bottomLeft.x * trans.a + world.bottomLeft.y * trans.c + trans.tx;
vertices[1].y = world.bottomLeft.x * trans.b + world.bottomLeft.y * trans.d + trans.ty;
vertices[2].x = world.topRight.x * trans.a + world.topRight.y * trans.c + trans.tx;
vertices[2].y = world.topRight.x * trans.b + world.topRight.y * trans.d + trans.ty;
vertices[3].x = world.bottomRight.x * trans.a + world.bottomRight.y * trans.c + trans.tx;
vertices[3].y = world.bottomRight.x * trans.b + world.bottomRight.y * trans.d + trans.ty;
var minx = Math.min(vertices[0].x, vertices[1].x, vertices[2].x, vertices[3].x),
maxx = Math.max(vertices[0].x, vertices[1].x, vertices[2].x, vertices[3].x),
miny = Math.min(vertices[0].y, vertices[1].y, vertices[2].y, vertices[3].y),
maxy = Math.max(vertices[0].y, vertices[1].y, vertices[2].y, vertices[3].y);
startCol = Math.floor(minx / maptw);
startRow = rows - Math.ceil(maxy / mapth);
maxCol = Math.ceil((maxx + extw) / maptw);
maxRow = rows - Math.floor((miny - exth) / mapth);
// Adjustment
if (startCol < 0) startCol = 0;
@@ -153,7 +174,6 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset) {
wa = a, wb = b, wc = c, wd = d,
wtx = Math.floor(tx) + 0.5, wty = Math.floor(ty) + 0.5, // world
flagged = false, flippedX = false, flippedY = false,
vertices = this._vertices,
axis, tileOffset, diffX1, diffY1, odd_even;
if (layerOrientation === Orientation.HEX) {