From ed145df2282675cb5547dd266fa702fa72d1dfc0 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 20 Nov 2017 14:40:33 +0800 Subject: [PATCH] Improve ortho tilemap culling calculation --- cocos2d/core/base-nodes/CCSGNode.js | 8 ++-- cocos2d/core/value-types/CCAffineTransform.js | 6 +-- cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js | 42 ++++++++++++++----- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/cocos2d/core/base-nodes/CCSGNode.js b/cocos2d/core/base-nodes/CCSGNode.js index 76782a42bb..5c06277a94 100644 --- a/cocos2d/core/base-nodes/CCSGNode.js +++ b/cocos2d/core/base-nodes/CCSGNode.js @@ -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; }, /** diff --git a/cocos2d/core/value-types/CCAffineTransform.js b/cocos2d/core/value-types/CCAffineTransform.js index 27c2c4fdf5..d02b7c86be 100644 --- a/cocos2d/core/value-types/CCAffineTransform.js +++ b/cocos2d/core/value-types/CCAffineTransform.js @@ -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); }; diff --git a/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js b/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js index e896954868..f3a2555cff 100644 --- a/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js +++ b/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js @@ -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) {