From e25078e7f4c9f1e621bc369fe4f15140d0a226b8 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 20 Jun 2017 10:50:20 +0800 Subject: [PATCH 01/29] fixed physics api doc --- cocos2d/core/physics/CCRigidBody.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/physics/CCRigidBody.js b/cocos2d/core/physics/CCRigidBody.js index d2e2bf2274..57ababd75b 100644 --- a/cocos2d/core/physics/CCRigidBody.js +++ b/cocos2d/core/physics/CCRigidBody.js @@ -137,7 +137,7 @@ var RigidBody = cc.Class({ * 碰撞回调。 * 如果你的脚本中实现了这个函数,那么它将会在接触更新完后被调用。 * 你可以在这个回调中从接触信息中获取到冲量信息。 - * @method onEndSolve + * @method onPostSolve * @param {PhysicsContact} contact - contact information * @param {PhysicsCollider} selfCollider - the collider belong to this rigidbody * @param {PhysicsCollider} otherCollider - the collider belong to another rigidbody From aa190b503e45e2c88e48707755709542a5b03ce1 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 20 Jun 2017 18:14:37 +0800 Subject: [PATCH 02/29] create new buffer when graphics verts number is bigger than 65535 --- cocos2d/core/graphics/graphics-webgl-cmd.js | 265 +++++++++++--------- 1 file changed, 151 insertions(+), 114 deletions(-) diff --git a/cocos2d/core/graphics/graphics-webgl-cmd.js b/cocos2d/core/graphics/graphics-webgl-cmd.js index ebe873c325..809597c18c 100644 --- a/cocos2d/core/graphics/graphics-webgl-cmd.js +++ b/cocos2d/core/graphics/graphics-webgl-cmd.js @@ -41,6 +41,8 @@ var INIT_VERTS_SIZE = 32; var VERTS_FLOAT_LENGTH = 2; var VERTS_BYTE_LENGTH = 8; +var MAX_BUFFER_SIZE = 65535; + var PI = Math.PI; var min = Math.min; var max = Math.max; @@ -102,6 +104,79 @@ Path.prototype.reset = function () { } }; +// GraphicsBuffer +function GraphicsBuffer () { + this.vertsOffset = 0; + this.vertsVBO = gl.createBuffer(); + this.vertsBuffer = null; + this.vertsDirty = false; + + this.indicesOffset = 0; + this.indicesVBO = gl.createBuffer(); + this.indicesBuffer = null; + this.indicesDirty = false; +} + +GraphicsBuffer.prototype.clear = function () { + this.vertsOffset = 0; + this.indicesOffset = 0; +} + +GraphicsBuffer.prototype.alloc = function (cverts, cindices) { + var dnverts = this.vertsOffset + cverts; + if (dnverts > MAX_BUFFER_SIZE) { + return false; + } + + var verts = this.vertsBuffer; + var nverts = verts ? verts.length / VERTS_FLOAT_LENGTH : 0; + + if (dnverts > nverts) { + if (nverts === 0) { + nverts = INIT_VERTS_SIZE; + } + + while (dnverts > nverts) { + nverts *= 2; + } + + var newBuffer = new Float32Array(nverts * VERTS_FLOAT_LENGTH); + + if (verts) { + for (var i = 0, l = verts.length; i < l; i++) { + newBuffer[i] = verts[i]; + } + } + + this.vertsBuffer = newBuffer; + } + + var indices = this.indicesBuffer; + var dnindices = this.indicesOffset + cindices; + var nindices = indices ? indices.length : 0; + + if (dnindices > nindices) { + if (nindices === 0) { + nindices = INIT_VERTS_SIZE * 3; + } + + while (dnindices > nindices) { + nindices *= 2; + } + + var newIndices = new Uint16Array(nindices); + + if (indices) { + for (var i = 0, l = indices.length; i < l; i++) { + newIndices[i] = indices[i]; + } + } + this.indicesBuffer = newIndices; + } + + return true; +} + // webgl render command function WebGLRenderCmd (renderable) { this._rootCtor(renderable); @@ -109,15 +184,9 @@ function WebGLRenderCmd (renderable) { var gl = cc._renderContext; - this._vertsOffset = 0; - this._vertsVBO = gl.createBuffer(); - this._vertsBuffer = null; - this._vertsDirty = false; - - this._indicesOffset = 0; - this._indicesVBO = gl.createBuffer(); - this._indicesBuffer = null; - this._indicesDirty = false; + this._buffers = []; + this._buffer = null; + this._allocBuffer(); this._matrix = new cc.math.Matrix4(); this._matrix.identity(); @@ -246,7 +315,6 @@ _p.stroke = function () { this._expandStroke(); - this._vertsDirty = true; this._updatePathOffset = true; }; @@ -255,7 +323,6 @@ _p.fill = function () { this._expandFill(); - this._vertsDirty = true; this._updatePathOffset = true; this._filling = false; }; @@ -303,45 +370,46 @@ Js.getset(_p, 'strokeColor', _p.getStrokeColor, _p.setStrokeColor); Js.getset(_p, 'fillColor', _p.getFillColor, _p.setFillColor); _p._render = function () { - var vertsBuffer = this._vertsBuffer; - if (!vertsBuffer || vertsBuffer.length === 0 || this._cmds.length === 0) return; + if (this._cmds.length === 0) return; var gl = cc._renderContext; - gl.bindBuffer(gl.ARRAY_BUFFER, this._vertsVBO); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesVBO); - - if (this._vertsDirty) { - gl.bufferData(gl.ARRAY_BUFFER, vertsBuffer, gl.STREAM_DRAW); - this._vertsDirty = false; - } - - if (this._indicesDirty && this._indicesBuffer) { - gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer, gl.STREAM_DRAW); - this._indicesDirty = false; - } - - if (this._vertsOffset > 65536) { - cc.warnID(2401); - } - - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, VERTS_BYTE_LENGTH, 0); - var shader = this._shader; var colorLocation = shader.getUniformLocationForName('color'); // draw paths var cmds = this._cmds; + var lastBuffer = null; for (var i = 0, l = cmds.length; i < l; i++) { var cmd = cmds[i]; + + var buffer = cmd.buffer; + if (buffer !== lastBuffer) { + gl.bindBuffer(gl.ARRAY_BUFFER, buffer.vertsVBO); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer.indicesVBO); + + if (buffer.vertsDirty) { + gl.bufferData(gl.ARRAY_BUFFER, buffer.vertsBuffer, gl.STREAM_DRAW); + buffer.vertsDirty = false; + } + + if (buffer.indicesDirty && buffer.indicesBuffer) { + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, buffer.indicesBuffer, gl.STREAM_DRAW); + buffer.indicesDirty = false; + } + + gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, VERTS_BYTE_LENGTH, 0); + + lastBuffer = buffer; + } if (cmd.nIndices) { var color = cmd.color; gl.uniform4f(colorLocation, color.r / 255, color.g / 255, color.b / 255, color.a / 255); gl.drawElements(gl.TRIANGLES, cmd.nIndices, gl.UNSIGNED_SHORT, cmd.indicesOffset * 2); - cc.incrementGLDraws(cmd.nverts); + cc.g_NumberOfDraws++; } } @@ -368,9 +436,6 @@ _p.rendering = function () { // clear _p.clear = function (clean) { - this._vertsOffset = 0; - this._indicesOffset = 0; - this._pathLength = 0; this._pathOffset = 0; this._pointsOffset = 0; @@ -383,8 +448,14 @@ _p.clear = function (clean) { this._paths.length = 0; this._points.length = 0; - this._vertsBuffer = null; - this._indicesBuffer = null; + this._buffer = null; + this._buffers = []; + } + else { + var buffers = this._buffers; + for(var i = 0; i < buffers.length; i++) { + buffers[i].clear(); + } } }; @@ -473,55 +544,19 @@ _p._flattenPaths = function () { } }; -_p._allocVerts = function (cverts) { - var dnverts = this._vertsOffset + cverts; - var buffer = this._vertsBuffer; - var nverts = buffer ? buffer.length / VERTS_FLOAT_LENGTH : 0; - - if (dnverts > nverts) { - if (nverts === 0) { - nverts = INIT_VERTS_SIZE; - } - - while (dnverts > nverts) { - nverts *= 2; - } - - var newBuffer = new Float32Array(nverts * VERTS_FLOAT_LENGTH); - - if (buffer) { - for (var i = 0, l = buffer.length; i < l; i++) { - newBuffer[i] = buffer[i]; - } - } - - this._vertsBuffer = newBuffer; - } +_p._allocBuffer = function () { + var buffer = new GraphicsBuffer(); + this._buffers.push(buffer); + this._buffer = buffer; }; -_p._allocIndices = function (cindices) { - var indices = this._indicesBuffer; - var dnindices = this._indicesOffset + cindices; - var nindices = indices ? indices.length : 0; - - if (dnindices > nindices) { - if (nindices === 0) { - nindices = INIT_VERTS_SIZE * 3; - } - - while (dnindices > nindices) { - nindices *= 2; - } - - var newIndices = new Uint16Array(nindices); - - if (indices) { - for (var i = 0, l = indices.length; i < l; i++) { - newIndices[i] = indices[i]; - } - } - this._indicesBuffer = newIndices; +_p._allocVerts = function (cverts) { + var nIndices = (cverts - 2*(this._pathLength-this._pathOffset)) * 3; + if (!this._buffer.alloc(cverts, nIndices)) { + this._allocBuffer(); + this._buffer.alloc(cverts, nIndices); } + this._buffer.vertsDirty = true; }; _p._pushCmd = function (cmd) { @@ -580,8 +615,8 @@ _p._expandStroke = function () { } this._allocVerts(cverts); - this._allocIndices((cverts - 2*(this._pathLength-this._pathOffset)) * 3); + var buffer = this._buffer; for (var i = this._pathOffset, l = this._pathLength; i < l; i++) { var path = paths[i]; var pts = path.points; @@ -591,7 +626,7 @@ _p._expandStroke = function () { var s, e, loop; loop = path.closed; - var offset = this._vertsOffset; + var offset = buffer.vertsOffset; if (loop) { // Looping @@ -662,10 +697,10 @@ _p._expandStroke = function () { } // stroke indices - var indicesOffset = this._indicesOffset; - var indicesBuffer = this._indicesBuffer; + var indicesOffset = buffer.indicesOffset; + var indicesBuffer = buffer.indicesBuffer; - for (var start = offset+2, end = this._vertsOffset; start < end; start++) { + for (var start = offset+2, end = buffer.vertsOffset; start < end; start++) { indicesBuffer[indicesOffset++] = start - 2; indicesBuffer[indicesOffset++] = start - 1; indicesBuffer[indicesOffset++] = start; @@ -673,14 +708,15 @@ _p._expandStroke = function () { this._pushCmd({ color: this._strokeColor, - indicesOffset: this._indicesOffset, - nIndices: indicesOffset - this._indicesOffset, + indicesOffset: buffer.indicesOffset, + nIndices: indicesOffset - buffer.indicesOffset, vertsOffset: offset, - nverts: this._vertsOffset - offset + nverts: buffer.vertsOffset - offset, + buffer: buffer }); - this._indicesOffset = indicesOffset; - this._indicesDirty = true; + buffer.indicesOffset = indicesOffset; + buffer.indicesDirty = true; } }; @@ -699,8 +735,8 @@ _p._expandFill = function () { } this._allocVerts(cverts); - this._allocIndices((cverts - 2*(this._pathLength-this._pathOffset)) * 3); + var buffer = this._buffer; for (var i = this._pathOffset, l = this._pathLength; i < l; i++) { var path = paths[i]; var pts = path.points; @@ -711,18 +747,18 @@ _p._expandFill = function () { } // Calculate shape vertices. - var offset = this._vertsOffset; + var offset = buffer.vertsOffset; for (var j = 0; j < pointsLength; ++j) { this._vset(pts[j].x, pts[j].y, 0.5, 1); } - var indicesOffset = path.indicesOffset = this._indicesOffset; - var indicesBuffer = this._indicesBuffer; + var indicesOffset = path.indicesOffset = buffer.indicesOffset; + var indicesBuffer = buffer.indicesBuffer; var nIndices = 0; if (path.complex) { - var data = new Float32Array(this._vertsBuffer.buffer, offset * 8, (this._vertsOffset - offset) * 2); + var data = new Float32Array(buffer.vertsBuffer.buffer, offset * 8, (buffer.vertsOffset - offset) * 2); var newIndices = Earcut(data, null, 2); if (!newIndices || newIndices.length === 0) { @@ -737,25 +773,26 @@ _p._expandFill = function () { } else { var first = offset; - for (var start = offset+2, end = this._vertsOffset; start < end; start++) { + for (var start = offset+2, end = buffer.vertsOffset; start < end; start++) { indicesBuffer[indicesOffset++] = first; indicesBuffer[indicesOffset++] = start - 1; indicesBuffer[indicesOffset++] = start; } - nIndices = indicesOffset - this._indicesOffset; + nIndices = indicesOffset - buffer.indicesOffset; } this._pushCmd({ color: this._fillColor, - indicesOffset: this._indicesOffset, + indicesOffset: buffer.indicesOffset, nIndices: nIndices, vertsOffset: offset, - nverts: this._vertsOffset - offset + nverts: buffer.vertsOffset - offset, + buffer: buffer }); - this._indicesOffset += nIndices; - this._indicesDirty = true; + buffer.indicesOffset += nIndices; + buffer.indicesDirty = true; } }; @@ -838,21 +875,21 @@ _p._calculateJoins = function (w, lineJoin, miterLimit) { }; _p._vset = function (x, y) { - var offset = this._vertsOffset * VERTS_FLOAT_LENGTH; - var buffer = this._vertsBuffer; + var offset = this._buffer.vertsOffset * VERTS_FLOAT_LENGTH; + var vertsBuffer = this._buffer.vertsBuffer; - buffer[offset] = x; - buffer[offset + 1] = y; + vertsBuffer[offset] = x; + vertsBuffer[offset + 1] = y; - this._vertsOffset++; + this._buffer.vertsOffset++; }; _p._vget = function (index) { - var buffer = this._vertsBuffer; + var vertsBuffer = this._buffer.vertsBuffer; var offset = index * VERTS_FLOAT_LENGTH; return { - x: buffer[offset], - y: buffer[offset + 1] + x: vertsBuffer[offset], + y: vertsBuffer[offset + 1] }; }; From a11299ab3713e23f4425097223bfdda50923770a Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 21 Jun 2017 11:14:59 +0800 Subject: [PATCH 03/29] fixed alloc buffer --- cocos2d/core/graphics/graphics-webgl-cmd.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cocos2d/core/graphics/graphics-webgl-cmd.js b/cocos2d/core/graphics/graphics-webgl-cmd.js index 809597c18c..c1b3140b53 100644 --- a/cocos2d/core/graphics/graphics-webgl-cmd.js +++ b/cocos2d/core/graphics/graphics-webgl-cmd.js @@ -456,6 +456,8 @@ _p.clear = function (clean) { for(var i = 0; i < buffers.length; i++) { buffers[i].clear(); } + + this._buffer = buffers[0]; } }; @@ -545,6 +547,14 @@ _p._flattenPaths = function () { }; _p._allocBuffer = function () { + if (this._buffer) { + var index = this._buffers.indexOf(this._buffer); + if (index < (this._buffers.length - 1)) { + this._buffer = this._buffers[index + 1]; + return; + } + } + var buffer = new GraphicsBuffer(); this._buffers.push(buffer); this._buffer = buffer; From dc8914db9cf346fa5e7150c2344e95b10b5c56ad Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 21 Jun 2017 11:27:05 +0800 Subject: [PATCH 04/29] fixed clean graphics --- cocos2d/core/graphics/graphics-webgl-cmd.js | 4 ++++ cocos2d/core/graphics/graphics.js | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/graphics/graphics-webgl-cmd.js b/cocos2d/core/graphics/graphics-webgl-cmd.js index c1b3140b53..e1d13e379c 100644 --- a/cocos2d/core/graphics/graphics-webgl-cmd.js +++ b/cocos2d/core/graphics/graphics-webgl-cmd.js @@ -561,6 +561,10 @@ _p._allocBuffer = function () { }; _p._allocVerts = function (cverts) { + if (!this._buffer) { + this._allocBuffer(); + } + var nIndices = (cverts - 2*(this._pathLength-this._pathOffset)) * 3; if (!this._buffer.alloc(cverts, nIndices)) { this._allocBuffer(); diff --git a/cocos2d/core/graphics/graphics.js b/cocos2d/core/graphics/graphics.js index 556b2bbd2e..ff6d5c0177 100644 --- a/cocos2d/core/graphics/graphics.js +++ b/cocos2d/core/graphics/graphics.js @@ -317,9 +317,10 @@ var Graphics = cc.Class({ * !#en Erasing any previously drawn content. * !#zh 擦除之前绘制的所有内容的方法。 * @method clear + * @param {Boolean} [clean] Whether to clean the graphics inner cache. */ - clear: function () { - this._sgNode.clear(); + clear: function (clean) { + this._sgNode.clear(clean); }, /** From e8ab15f124e20ccf65de7d081d58366a9c0336bb Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Wed, 21 Jun 2017 14:06:33 +0800 Subject: [PATCH 05/29] fix CCRect api error --- cocos2d/core/value-types/CCRect.js | 90 +++++++++++++++--------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/cocos2d/core/value-types/CCRect.js b/cocos2d/core/value-types/CCRect.js index 6af6f2d363..1071d79ad3 100644 --- a/cocos2d/core/value-types/CCRect.js +++ b/cocos2d/core/value-types/CCRect.js @@ -105,9 +105,9 @@ Rect.fromMinMax = function (v1, v2) { * @param b {Rect} Rect b * @return {Number} The contains result, 1 is a contains b, -1 is b contains a, 0 is no contains. * @example - * var a = new cc.rect(0, 0, 10, 10); - * var b = new cc.rect(5, 5, 5, 5); - * var c = new cc.rect(20, 20, 10, 10); + * var a = new cc.Rect(0, 0, 10, 10); + * var b = new cc.Rect(5, 5, 5, 5); + * var c = new cc.Rect(20, 20, 10, 10); * cc.Rect.contain(a, b); // 1; * cc.Rect.contain(b, a); // -1; * cc.Rect.contain(a, c); // 0; @@ -138,7 +138,7 @@ var proto = Rect.prototype; * @method clone * @return {Rect} * @example - * var a = new cc.rect(0, 0, 10, 10); + * var a = new cc.Rect(0, 0, 10, 10); * a.clone();// Rect {x: 0, y: 0, width: 10, height: 10} */ proto.clone = function () { @@ -152,8 +152,8 @@ proto.clone = function () { * @param {Rect} other * @return {Boolean} * @example - * var a = new cc.rect(0, 0, 10, 10); - * var b = new cc.rect(0, 0, 10, 10); + * var a = new cc.Rect(0, 0, 10, 10); + * var b = new cc.Rect(0, 0, 10, 10); * a.equals(b);// true; */ proto.equals = function (other) { @@ -173,8 +173,8 @@ proto.equals = function (other) { * @param {Rect} [out] - optional, the receiving vector. * @return {Rect} * @example - * var a = new cc.rect(0, 0, 10, 10); - * var b = new cc.rect(50, 50, 100, 100); + * var a = new cc.Rect(0, 0, 10, 10); + * var b = new cc.Rect(50, 50, 100, 100); * update (dt) { * // method 1; * var c = a.lerp(b, dt * 0.1); @@ -201,7 +201,7 @@ proto.lerp = function (to, ratio, out) { * @method toString * @return {String} * @example - * var a = new cc.rect(0, 0, 10, 10); + * var a = new cc.Rect(0, 0, 10, 10); * a.toString();// "(0.00, 0.00, 10.00, 10.00)"; */ proto.toString = function () { @@ -312,8 +312,8 @@ JS.getset(proto, 'size', * @param {Rect} rect * @type {Boolean} * @example - * var a = new cc.rect(0, 0, 10, 10); - * var b = new cc.rect(0, 0, 20, 20); + * var a = new cc.Rect(0, 0, 10, 10); + * var b = new cc.Rect(0, 0, 20, 20); * a.intersects(b);// true */ proto.intersects = function (rect) { @@ -328,15 +328,15 @@ proto.intersects = function (rect) { * @param {Vec2} point * @type {Boolean} * @example - * var a = new cc.rect(0, 0, 10, 10); - * var b = new cc.v2(0, 5); + * var a = new cc.Rect(0, 0, 10, 10); + * var b = new cc.Vec2(0, 5); * a.contains(b);// true */ proto.contains = function (point) { - return (this.x < point.x && - this.x + this.width > point.x && - this.y < point.y && - this.y + this.height > point.y); + return (this.x <= point.x && + this.x + this.width >= point.x && + this.y <= point.y && + this.y + this.height >= point.y); }; /** @@ -346,15 +346,15 @@ proto.contains = function (point) { * @param {Rect} rect * @type {Boolean} * @example - * var a = new cc.rect(0, 0, 10, 10); - * var b = new cc.rect(0, 0, 20, 20); + * var a = new cc.Rect(0, 0, 10, 10); + * var b = new cc.Rect(0, 0, 20, 20); * a.containsRect(b);// true */ proto.containsRect = function (rect) { - return (this.x < rect.x && - this.x + this.width > rect.x + rect.width && - this.y < rect.y && - this.y + this.height > rect.y + rect.height); + return (this.x <= rect.x && + this.x + this.width >= rect.x + rect.width && + this.y <= rect.y && + this.y + this.height >= rect.y + rect.height); }; cc.Rect = Rect; @@ -376,7 +376,7 @@ cc.Rect = Rect; * @param {Number} [h=0] * @return {Rect} * @example - * var a = new cc.rect(0 , 0, 10, 0); + * var a = new cc.Rect(0 , 0, 10, 0); */ cc.rect = function rect (x, y, w, h) { return new Rect(x, y, w, h); @@ -393,10 +393,10 @@ cc.rect = function rect (x, y, w, h) { * @param {Rect} rect2 * @return {Boolean} * @example - * var a = new cc.rect(0, 0, 10, 10); - * var b = new cc.rect(0, 0, 5, 5); + * var a = new cc.Rect(0, 0, 10, 10); + * var b = new cc.Rect(0, 0, 5, 5); * cc.rectEqualToRect(a, b); // false; - * var c = new cc.rect(0, 0, 5, 5); + * var c = new cc.Rect(0, 0, 5, 5); * cc.rectEqualToRect(b, c); // true; */ cc.rectEqualToRect = function (rect1, rect2) { @@ -417,8 +417,8 @@ cc._rectEqualToZero = function(rect){ * @param {Rect} rect2 * @return {Boolean} * @example - * var a = new cc.rect(0, 0, 20, 20); - * var b = new cc.rect(10, 10, 20, 20); + * var a = new cc.Rect(0, 0, 20, 20); + * var b = new cc.Rect(10, 10, 20, 20); * cc.rectContainsRect(a, b); // true; */ cc.rectContainsRect = function (rect1, rect2) { @@ -436,7 +436,7 @@ cc.rectContainsRect = function (rect1, rect2) { * @param {Rect} rect * @return {Number} The rightmost x value. * @example - * var a = new cc.rect(10, 0, 20, 20); + * var a = new cc.Rect(10, 0, 20, 20); * cc.rectGetMaxX(a); // 30; */ cc.rectGetMaxX = function (rect) { @@ -450,7 +450,7 @@ cc.rectGetMaxX = function (rect) { * @param {Rect} rect * @return {Number} The midpoint x value. * @example - * var a = new cc.rect(10, 0, 20, 20); + * var a = new cc.Rect(10, 0, 20, 20); * cc.rectGetMidX(a); // 20; */ cc.rectGetMidX = function (rect) { @@ -463,7 +463,7 @@ cc.rectGetMidX = function (rect) { * @param {Rect} rect * @return {Number} The leftmost x value. * @example - * var a = new cc.rect(10, 0, 20, 20); + * var a = new cc.Rect(10, 0, 20, 20); * cc.rectGetMinX(a); // 10; */ cc.rectGetMinX = function (rect) { @@ -477,7 +477,7 @@ cc.rectGetMinX = function (rect) { * @param {Rect} rect * @return {Number} The topmost y value. * @example - * var a = new cc.rect(0, 10, 20, 20); + * var a = new cc.Rect(0, 10, 20, 20); * cc.rectGetMaxY(a); // 30; */ cc.rectGetMaxY = function (rect) { @@ -491,7 +491,7 @@ cc.rectGetMaxY = function (rect) { * @param {Rect} rect * @return {Number} The midpoint y value. * @example - * var a = new cc.rect(0, 10, 20, 20); + * var a = new cc.Rect(0, 10, 20, 20); * cc.rectGetMidY(a); // 20; */ cc.rectGetMidY = function (rect) { @@ -505,7 +505,7 @@ cc.rectGetMidY = function (rect) { * @param {Rect} rect * @return {Number} The bottommost y value. * @example - * var a = new cc.rect(0, 10, 20, 20); + * var a = new cc.Rect(0, 10, 20, 20); * cc.rectGetMinY(a); // 10; */ cc.rectGetMinY = function (rect) { @@ -520,8 +520,8 @@ cc.rectGetMinY = function (rect) { * @param {Vec2} point * @return {Boolean} * @example - * var a = new cc.rect(0, 10, 20, 20); - * var b = cc.v2(0, 10, 10, 10); + * var a = new cc.Rect(0, 10, 20, 20); + * var b = new cc.Vec2(0, 10, 10, 10); * cc.rectContainsPoint(a, b); // true; */ cc.rectContainsPoint = function (rect, point) { @@ -537,8 +537,8 @@ cc.rectContainsPoint = function (rect, point) { * @param {Rect} rectB * @return {Boolean} * @example - * var a = new cc.rect(0, 10, 20, 20); - * var b = new cc.rect(0, 10, 10, 10); + * var a = new cc.Rect(0, 10, 20, 20); + * var b = new cc.Rect(0, 10, 10, 10); * cc.rectIntersectsRect(a, b); // true; */ cc.rectIntersectsRect = function (ra, rb) { @@ -557,8 +557,8 @@ cc.rectIntersectsRect = function (ra, rb) { * @param {Rect} rectB * @return {Boolean} * @example - * var a = new cc.rect(0, 10, 20, 20); - * var b = new cc.rect(0, 10, 10, 10); + * var a = new cc.Rect(0, 10, 20, 20); + * var b = new cc.Rect(0, 10, 10, 10); * cc.rectOverlapsRect(a, b); // true; */ cc.rectOverlapsRect = function (rectA, rectB) { @@ -576,8 +576,8 @@ cc.rectOverlapsRect = function (rectA, rectB) { * @param {Rect} rectB * @return {Rect} * @example - * var a = new cc.rect(0, 10, 20, 20); - * var b = new cc.rect(0, 10, 10, 10); + * var a = new cc.Rect(0, 10, 20, 20); + * var b = new cc.Rect(0, 10, 10, 10); * cc.rectUnion(a, b); // Rect {x: 0, y: 10, width: 20, height: 20}; */ cc.rectUnion = function (rectA, rectB) { @@ -597,8 +597,8 @@ cc.rectUnion = function (rectA, rectB) { * @param {Rect} rectB * @return {Rect} * @example - * var a = new cc.rect(0, 10, 20, 20); - * var b = new cc.rect(0, 10, 10, 10); + * var a = new cc.Rect(0, 10, 20, 20); + * var b = new cc.Rect(0, 10, 10, 10); * cc.rectIntersection(a, b); // Rect {x: 0, y: 10, width: 10, height: 10}; */ cc.rectIntersection = function (rectA, rectB) { From a1ef7c398de8c33e797b68b800e35c5ddca03e36 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 21 Jun 2017 14:36:25 +0800 Subject: [PATCH 06/29] remove unneeded api in particle sgNode --- cocos2d/particle/CCSGParticleSystem.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/cocos2d/particle/CCSGParticleSystem.js b/cocos2d/particle/CCSGParticleSystem.js index 24e1f00f5e..5ef6aca390 100644 --- a/cocos2d/particle/CCSGParticleSystem.js +++ b/cocos2d/particle/CCSGParticleSystem.js @@ -1477,16 +1477,7 @@ _ccsg.ParticleSystem = _ccsg.Node.extend({ this._renderCmd._initWithTotalParticles(numberOfParticles); return true; }, - - /** - * Unschedules the "update" method. - * @function - * @see scheduleUpdate(); - */ - destroyParticleSystem:function () { - this.unscheduleUpdate(); - }, - + /** * Add a particle to the emitter * @return {Boolean} From 8423864d819d4768ab1091bcc2eb529d284ea088 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Thu, 22 Jun 2017 10:59:04 +0800 Subject: [PATCH 07/29] fix web and native node object inconsistencies --- jsb/jsb-action.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/jsb/jsb-action.js b/jsb/jsb-action.js index 3a299da115..b79985b470 100644 --- a/jsb/jsb-action.js +++ b/jsb/jsb-action.js @@ -79,6 +79,12 @@ var actionArr = [ 'TintBy', ]; +cc.Action.prototype._getSgTarget = cc.Action.prototype.getTarget; +cc.Action.prototype.getTarget = function () { + var sgNode = this._getSgTarget(); + return sgNode._owner || sgNode; +}; + function setCtorReplacer (proto) { var ctor = proto._ctor; proto._ctor = function (...args) { @@ -126,7 +132,7 @@ cc.follow = function (followedNode, rect) { }; cc.Follow.prototype.update = function(dt) { - var target = this.getTarget(); + var target = this._getSgTarget(); if (target._owner) { target._owner.setPosition(target.getPosition()); } @@ -147,7 +153,7 @@ cc.FlipX = _FlipX.extend({ }, update:function (dt) { - var target = this.getTarget(); + var target = this._getSgTarget(); target.scaleX = Math.abs(target.scaleX) * (this._flippedX ? -1 : 1); }, @@ -179,7 +185,7 @@ cc.FlipY = _FlipY.extend({ }, update:function (dt) { - var target = this.getTarget(); + var target = this._getSgTarget(); target.scaleY = Math.abs(target.scaleY) * (this._flippedY ? -1 : 1); }, @@ -206,15 +212,15 @@ function setRendererVisibility (sgNode, toggleVisible, visible) { } cc.Show.prototype.update = function (dt) { - setRendererVisibility(this.getTarget(), false, true); + setRendererVisibility(this._getSgTarget(), false, true); }; cc.Hide.prototype.update = function (dt) { - setRendererVisibility(this.getTarget(), false, false); + setRendererVisibility(this._getSgTarget(), false, false); }; cc.ToggleVisibility.prototype.update = function (dt) { - setRendererVisibility(this.getTarget(), true); + setRendererVisibility(this._getSgTarget(), true); }; // Special call func @@ -359,7 +365,7 @@ cc.ActionManager.prototype.pauseTargets = function (targetsToPause) { function syncPositionUpdate (dt) { this._jsbUpdate(dt); - var target = this.getTarget(); + var target = this._getSgTarget(); if (target._owner) { target._owner.x = target.getPositionX(); target._owner.y = target.getPositionY(); @@ -368,7 +374,7 @@ function syncPositionUpdate (dt) { function syncRotationUpdate (dt) { this._jsbUpdate(dt); - var target = this.getTarget(); + var target = this._getSgTarget(); if (target._owner) { target._owner.rotationX = target.getRotationX(); target._owner.rotationY = target.getRotationY(); @@ -377,7 +383,7 @@ function syncRotationUpdate (dt) { function syncScaleUpdate (dt) { this._jsbUpdate(dt); - var target = this.getTarget(); + var target = this._getSgTarget(); if (target._owner) { target._owner.scaleX = target.getScaleX(); target._owner.scaleY = target.getScaleY(); @@ -386,7 +392,7 @@ function syncScaleUpdate (dt) { function syncRemoveSelfUpdate (dt) { this._jsbUpdate(dt); - var target = this.getTarget(); + var target = this._getSgTarget(); if (target._owner) { target._owner.removeFromParent(); } @@ -394,7 +400,7 @@ function syncRemoveSelfUpdate (dt) { function syncSkewUpdate (dt) { this._jsbUpdate(dt); - var target = this.getTarget(); + var target = this._getSgTarget(); if (target._owner) { target._owner.skewX = target.getSkewX(); target._owner.skewY = target.getSkewY(); @@ -403,7 +409,7 @@ function syncSkewUpdate (dt) { function syncOpacityUpdate (dt) { this._jsbUpdate(dt); - var target = this.getTarget(); + var target = this._getSgTarget(); if (target._owner) { target._owner.opacity = target.getOpacity(); } @@ -411,7 +417,7 @@ function syncOpacityUpdate (dt) { function syncColorUpdate (dt) { this._jsbUpdate(dt); - var target = this.getTarget(); + var target = this._getSgTarget(); if (target._owner) { target._owner.color = target.getColor(); } From 0f14c7d8ea9c2b479692c8106207d82df80b3f59 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Thu, 22 Jun 2017 11:41:32 +0800 Subject: [PATCH 08/29] recalculate targets culling when camera view matrix changed --- cocos2d/core/camera/CCCamera.js | 37 +++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/camera/CCCamera.js b/cocos2d/core/camera/CCCamera.js index 724a4b9678..9c8e036b3f 100644 --- a/cocos2d/core/camera/CCCamera.js +++ b/cocos2d/core/camera/CCCamera.js @@ -23,7 +23,10 @@ THE SOFTWARE. ****************************************************************************/ +var transformDirtyFlag; + if (!CC_JSB) { + transformDirtyFlag = _ccsg.Node._dirtyFlags.transformDirty; require('./CCSGCameraNode'); } @@ -45,6 +48,8 @@ let Camera = cc.Class({ this.viewMatrix = cc.affineTransformMake(); this.invertViewMatrix = cc.affineTransformMake(); + this._lastViewMatrix = cc.affineTransformMake(); + this._sgTarges = []; this.visibleRect = { @@ -123,7 +128,7 @@ let Camera = cc.Class({ if (!CC_JSB) { var cmd = sgNode._renderCmd; - cmd.setDirtyFlag(_ccsg.Node._dirtyFlags.transformDirty); + cmd.setDirtyFlag(transformDirtyFlag); cmd._cameraFlag = Camera.flags.InCamera; cc.renderer.childrenOrderDirty = true; @@ -146,7 +151,7 @@ let Camera = cc.Class({ if (!CC_JSB) { var cmd = sgNode._renderCmd; - cmd.setDirtyFlag(_ccsg.Node._dirtyFlags.transformDirty); + cmd.setDirtyFlag(transformDirtyFlag); cmd._cameraFlag = 0; cc.renderer.childrenOrderDirty = true; @@ -281,6 +286,13 @@ let Camera = cc.Class({ return false; }, + _setSgNodesTransformDirty: !CC_JSB && function () { + let sgTarges = this._sgTarges; + for (let i = 0; i < sgTarges.length; i++) { + sgTarges[i]._renderCmd.setDirtyFlag(transformDirtyFlag); + } + }, + lateUpdate: !CC_EDITOR && function () { let m = this.viewMatrix; let im = this.invertViewMatrix; @@ -335,6 +347,27 @@ let Camera = cc.Class({ selfVisibleRect.top.y = viewPort.yMax; this._sgNode.setTransform(a, b, c, d, m.tx, m.ty); + + // if view transform changed, then need recalculate whether targets need culling + if (!CC_JSB) { + var lvm = this._lastViewMatrix; + if (lvm.a !== m.a || + lvm.b !== m.b || + lvm.c !== m.c || + lvm.d !== m.d || + lvm.tx !== m.tx || + lvm.ty !== m.ty + ) { + this._setSgNodesTransformDirty(); + + lvm.a = m.a; + lvm.b = m.b; + lvm.c = m.c; + lvm.d = m.d; + lvm.tx = m.tx; + lvm.ty = m.ty; + } + } } }); From c82db2821fda6f841388272969080367ac500836 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Thu, 22 Jun 2017 16:40:34 +0800 Subject: [PATCH 09/29] remove command from graphics --- cocos2d/core/graphics/graphics-webgl-cmd.js | 171 +++++++------------- cocos2d/core/graphics/shader.js | 50 ------ 2 files changed, 62 insertions(+), 159 deletions(-) delete mode 100644 cocos2d/core/graphics/shader.js diff --git a/cocos2d/core/graphics/graphics-webgl-cmd.js b/cocos2d/core/graphics/graphics-webgl-cmd.js index e1d13e379c..d069090c0c 100644 --- a/cocos2d/core/graphics/graphics-webgl-cmd.js +++ b/cocos2d/core/graphics/graphics-webgl-cmd.js @@ -23,8 +23,6 @@ THE SOFTWARE. ****************************************************************************/ -var Shader = require('./shader'); - var LineCap = require('./types').LineCap; var LineJoin = require('./types').LineJoin; @@ -38,8 +36,8 @@ var Js = cc.js; // Math var INIT_VERTS_SIZE = 32; -var VERTS_FLOAT_LENGTH = 2; -var VERTS_BYTE_LENGTH = 8; +var VERTS_FLOAT_LENGTH = 3; +var VERTS_BYTE_LENGTH = 12; var MAX_BUFFER_SIZE = 65535; @@ -109,6 +107,7 @@ function GraphicsBuffer () { this.vertsOffset = 0; this.vertsVBO = gl.createBuffer(); this.vertsBuffer = null; + this.uint32VertsBuffer = null; this.vertsDirty = false; this.indicesOffset = 0; @@ -149,6 +148,7 @@ GraphicsBuffer.prototype.alloc = function (cverts, cindices) { } this.vertsBuffer = newBuffer; + this.uint32VertsBuffer = new Uint32Array(this.vertsBuffer.buffer); } var indices = this.indicesBuffer; @@ -194,17 +194,20 @@ function WebGLRenderCmd (renderable) { this._paths = []; this._points = []; - this._cmds = []; + this._curColorValue = 0; this._blendFunc = new cc.BlendFunc(cc.macro.BLEND_SRC, cc.macro.BLEND_DST); // init shader - this._shader = new cc.GLProgram(); - this._shader.initWithVertexShaderByteArray(Shader.vert, Shader.frag); - this._shader.retain(); - this._shader.addAttribute(cc.macro.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION); - this._shader.link(); - this._shader.updateUniforms(); + var shader = new cc.GLProgram(); + shader.initWithVertexShaderByteArray(cc.PresetShaders.POSITION_COLOR_VERT, cc.PresetShaders.POSITION_COLOR_FRAG); + shader.retain(); + shader.addAttribute(cc.macro.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION); + shader.addAttribute(cc.macro.ATTRIBUTE_NAME_COLOR, cc.macro.VERTEX_ATTRIB_COLOR); + shader.link(); + shader.updateUniforms(); + + this._shaderProgram = shader; this._allocVerts(INIT_VERTS_SIZE); } @@ -313,6 +316,9 @@ _p.close = function () { _p.stroke = function () { this._flattenPaths(); + var color = this._strokeColor; + this._curColorValue = ((color.a<<24) >>> 0) + (color.b<<16) + (color.g<<8) + color.r; + this._expandStroke(); this._updatePathOffset = true; @@ -321,6 +327,9 @@ _p.stroke = function () { _p.fill = function () { // this._flattenPaths(); + var color = this._fillColor; + this._curColorValue = ((color.a<<24) >>> 0) + (color.b<<16) + (color.g<<8) + color.r; + this._expandFill(); this._updatePathOffset = true; @@ -370,47 +379,36 @@ Js.getset(_p, 'strokeColor', _p.getStrokeColor, _p.setStrokeColor); Js.getset(_p, 'fillColor', _p.getFillColor, _p.setFillColor); _p._render = function () { - if (this._cmds.length === 0) return; + let buffers = this._buffers; + if (buffers.length === 0) return; - var gl = cc._renderContext; - - var shader = this._shader; - var colorLocation = shader.getUniformLocationForName('color'); + let gl = cc._renderContext; // draw paths - var cmds = this._cmds; - var lastBuffer = null; - for (var i = 0, l = cmds.length; i < l; i++) { - var cmd = cmds[i]; + for (let i = 0, l = buffers.length; i < l; i++) { + let buffer = buffers[i]; - var buffer = cmd.buffer; - if (buffer !== lastBuffer) { - gl.bindBuffer(gl.ARRAY_BUFFER, buffer.vertsVBO); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer.indicesVBO); + gl.bindBuffer(gl.ARRAY_BUFFER, buffer.vertsVBO); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer.indicesVBO); - if (buffer.vertsDirty) { - gl.bufferData(gl.ARRAY_BUFFER, buffer.vertsBuffer, gl.STREAM_DRAW); - buffer.vertsDirty = false; - } - - if (buffer.indicesDirty && buffer.indicesBuffer) { - gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, buffer.indicesBuffer, gl.STREAM_DRAW); - buffer.indicesDirty = false; - } - - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, VERTS_BYTE_LENGTH, 0); - - lastBuffer = buffer; + if (buffer.vertsDirty) { + gl.bufferData(gl.ARRAY_BUFFER, buffer.vertsBuffer, gl.STREAM_DRAW); + buffer.vertsDirty = false; } - if (cmd.nIndices) { - var color = cmd.color; - gl.uniform4f(colorLocation, color.r / 255, color.g / 255, color.b / 255, color.a / 255); - gl.drawElements(gl.TRIANGLES, cmd.nIndices, gl.UNSIGNED_SHORT, cmd.indicesOffset * 2); - - cc.g_NumberOfDraws++; + if (buffer.indicesDirty && buffer.indicesBuffer) { + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, buffer.indicesBuffer, gl.STREAM_DRAW); + buffer.indicesDirty = false; } + + gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, VERTS_BYTE_LENGTH, 0); + gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, VERTS_BYTE_LENGTH, 8); + + gl.drawElements(gl.TRIANGLES, buffer.indicesOffset, gl.UNSIGNED_SHORT, 0); + + cc.g_NumberOfDraws++; } cc.checkGLErrorDebug(); @@ -419,7 +417,7 @@ _p._render = function () { _p.rendering = function () { cc.gl.blendFunc(this._blendFunc.src, this._blendFunc.dst); - var wt = this._worldTransform, mat = this._matrix.mat; + let wt = this._worldTransform, mat = this._matrix.mat; mat[0] = wt.a; mat[4] = wt.c; mat[12] = wt.tx; @@ -427,7 +425,7 @@ _p.rendering = function () { mat[5] = wt.d; mat[13] = wt.ty; - var shader = this._shader; + let shader = this._shaderProgram; shader.use(); shader._setUniformForMVPMatrixWithMat4(this._matrix); @@ -442,8 +440,6 @@ _p.clear = function (clean) { this._curPath = null; - this._cmds.length = 0; - if (clean) { this._paths.length = 0; this._points.length = 0; @@ -564,7 +560,7 @@ _p._allocVerts = function (cverts) { if (!this._buffer) { this._allocBuffer(); } - + var nIndices = (cverts - 2*(this._pathLength-this._pathOffset)) * 3; if (!this._buffer.alloc(cverts, nIndices)) { this._allocBuffer(); @@ -573,31 +569,6 @@ _p._allocVerts = function (cverts) { this._buffer.vertsDirty = true; }; -_p._pushCmd = function (cmd) { - var cmds = this._cmds; - var lastCmd = cmds[cmds.length - 1]; - - if (lastCmd) { - var lastColor = lastCmd.color; - var color = cmd.color; - - if (lastColor.r === color.r && - lastColor.g === color.g && - lastColor.b === color.b && - lastColor.a === color.a && - (lastCmd.indicesOffset + lastCmd.nIndices === cmd.indicesOffset)) { - lastCmd.nIndices += cmd.nIndices; - lastCmd.nverts += cmd.nverts; - } - else { - cmds.push(cmd); - } - } - else { - cmds.push(cmd); - } -}; - _p._expandStroke = function () { var w = this.lineWidth * 0.5, lineCap = this.lineCap, @@ -689,11 +660,11 @@ _p._expandStroke = function () { } if (loop) { - var v0 = this._vget(offset); - var v1 = this._vget(offset + 1); + let vertsBuffer = buffer.vertsBuffer; + // Loop it - this._vset(v0.x, v0.y); - this._vset(v1.x, v1.y); + this._vset(vertsBuffer[offset*VERTS_FLOAT_LENGTH], vertsBuffer[offset*VERTS_FLOAT_LENGTH+1]); + this._vset(vertsBuffer[(offset+1)*VERTS_FLOAT_LENGTH], vertsBuffer[(offset+1)*VERTS_FLOAT_LENGTH+1]); } else { // Add cap var dPos = p1.sub(p0); @@ -720,15 +691,6 @@ _p._expandStroke = function () { indicesBuffer[indicesOffset++] = start; } - this._pushCmd({ - color: this._strokeColor, - indicesOffset: buffer.indicesOffset, - nIndices: indicesOffset - buffer.indicesOffset, - vertsOffset: offset, - nverts: buffer.vertsOffset - offset, - buffer: buffer - }); - buffer.indicesOffset = indicesOffset; buffer.indicesDirty = true; } @@ -772,7 +734,13 @@ _p._expandFill = function () { var nIndices = 0; if (path.complex) { - var data = new Float32Array(buffer.vertsBuffer.buffer, offset * 8, (buffer.vertsOffset - offset) * 2); + var data = []; + var start = offset*VERTS_FLOAT_LENGTH, end = buffer.vertsOffset*VERTS_FLOAT_LENGTH; + for (var i = start; i < end; i+=VERTS_FLOAT_LENGTH) { + data.push(buffer.vertsBuffer[i]); + data.push(buffer.vertsBuffer[i+1]); + } + var newIndices = Earcut(data, null, 2); if (!newIndices || newIndices.length === 0) { @@ -796,15 +764,6 @@ _p._expandFill = function () { nIndices = indicesOffset - buffer.indicesOffset; } - this._pushCmd({ - color: this._fillColor, - indicesOffset: buffer.indicesOffset, - nIndices: nIndices, - vertsOffset: offset, - nverts: buffer.vertsOffset - offset, - buffer: buffer - }); - buffer.indicesOffset += nIndices; buffer.indicesDirty = true; } @@ -889,23 +848,17 @@ _p._calculateJoins = function (w, lineJoin, miterLimit) { }; _p._vset = function (x, y) { - var offset = this._buffer.vertsOffset * VERTS_FLOAT_LENGTH; - var vertsBuffer = this._buffer.vertsBuffer; + let buffer = this._buffer; + let offset = buffer.vertsOffset * VERTS_FLOAT_LENGTH; + let vertsBuffer = buffer.vertsBuffer; vertsBuffer[offset] = x; vertsBuffer[offset + 1] = y; - this._buffer.vertsOffset++; -}; + buffer.uint32VertsBuffer[offset + 2] = this._curColorValue; -_p._vget = function (index) { - var vertsBuffer = this._buffer.vertsBuffer; - var offset = index * VERTS_FLOAT_LENGTH; - return { - x: vertsBuffer[offset], - y: vertsBuffer[offset + 1] - }; - }; + buffer.vertsOffset++; +}; // _p._chooseBevel = function (bevel, p0, p1, w) { diff --git a/cocos2d/core/graphics/shader.js b/cocos2d/core/graphics/shader.js deleted file mode 100644 index cb80571c96..0000000000 --- a/cocos2d/core/graphics/shader.js +++ /dev/null @@ -1,50 +0,0 @@ -/**************************************************************************** - 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. - ****************************************************************************/ - -var vert = [ - 'attribute vec4 a_position;', - '', - 'void main()', - '{', - 'gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position;', - '}' -]; - -var frag = [ - '#ifdef GL_ES', - 'precision mediump float;', - '#endif', - '', - 'uniform vec4 color;', - '', - 'void main(void) {', - 'gl_FragColor = color;', - '}' -]; - -module.exports = { - vert: vert.join(' \n'), - frag: frag.join(' \n') -}; From c077be8f8d39c5861cba8ab93cdb43fce2ea2a4b Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 23 Jun 2017 14:21:42 +0800 Subject: [PATCH 10/29] fix webview border on iOS platform --- cocos2d/core/webview/CCSGWebView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/webview/CCSGWebView.js b/cocos2d/core/webview/CCSGWebView.js index f80f2d3ce6..3be92317ff 100644 --- a/cocos2d/core/webview/CCSGWebView.js +++ b/cocos2d/core/webview/CCSGWebView.js @@ -356,7 +356,7 @@ _ccsg.WebView.EventType = { this._div.style.height = contentSize.height + "px"; this._div.style.width = contentSize.width + "px"; this._div.style.overflow = "scroll"; - this._div.style.border = "none"; + this._iframe.style.border = "none"; cc.game.container.appendChild(this._div); this.updateVisibility(); }; From 01b18fd269659d330c3eb192f4a39f8a40df1e1e Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 23 Jun 2017 14:58:27 +0800 Subject: [PATCH 11/29] fix api link --- cocos2d/core/platform/CCClass.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index cc9f56a00e..8918e8b65e 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -750,8 +750,8 @@ function declareProperties (cls, className, properties, baseClass, mixins, es6) */ /** - * !#en Defines a CCClass using the given specification, please see [Class](/docs/editors_and_tools/creator-chapters/scripting/class-es5/) for details. - * !#zh 定义一个 CCClass,传入参数必须是一个包含类型参数的字面量对象,具体用法请查阅[类型定义](/docs/creator/scripting/class-es5/)。 + * !#en Defines a CCClass using the given specification, please see [Class](/docs/editors_and_tools/creator-chapters/scripting/class.html) for details. + * !#zh 定义一个 CCClass,传入参数必须是一个包含类型参数的字面量对象,具体用法请查阅[类型定义](/docs/creator/scripting/class.html)。 * * @method Class * From 4dd3158de5a73a806d62f8adbb96ed1123882587 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Fri, 23 Jun 2017 15:20:17 +0800 Subject: [PATCH 12/29] set transform updated for native --- cocos2d/core/camera/CCCamera.js | 43 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/cocos2d/core/camera/CCCamera.js b/cocos2d/core/camera/CCCamera.js index 9c8e036b3f..c09888fe8b 100644 --- a/cocos2d/core/camera/CCCamera.js +++ b/cocos2d/core/camera/CCCamera.js @@ -286,10 +286,15 @@ let Camera = cc.Class({ return false; }, - _setSgNodesTransformDirty: !CC_JSB && function () { + _setSgNodesTransformDirty: function () { let sgTarges = this._sgTarges; for (let i = 0; i < sgTarges.length; i++) { - sgTarges[i]._renderCmd.setDirtyFlag(transformDirtyFlag); + if (CC_JSB) { + sgTarges[i].markTransformUpdated(); + } + else { + sgTarges[i]._renderCmd.setDirtyFlag(transformDirtyFlag); + } } }, @@ -349,24 +354,22 @@ let Camera = cc.Class({ this._sgNode.setTransform(a, b, c, d, m.tx, m.ty); // if view transform changed, then need recalculate whether targets need culling - if (!CC_JSB) { - var lvm = this._lastViewMatrix; - if (lvm.a !== m.a || - lvm.b !== m.b || - lvm.c !== m.c || - lvm.d !== m.d || - lvm.tx !== m.tx || - lvm.ty !== m.ty - ) { - this._setSgNodesTransformDirty(); - - lvm.a = m.a; - lvm.b = m.b; - lvm.c = m.c; - lvm.d = m.d; - lvm.tx = m.tx; - lvm.ty = m.ty; - } + var lvm = this._lastViewMatrix; + if (lvm.a !== m.a || + lvm.b !== m.b || + lvm.c !== m.c || + lvm.d !== m.d || + lvm.tx !== m.tx || + lvm.ty !== m.ty + ) { + this._setSgNodesTransformDirty(); + + lvm.a = m.a; + lvm.b = m.b; + lvm.c = m.c; + lvm.d = m.d; + lvm.tx = m.tx; + lvm.ty = m.ty; } } }); From f1f733efca9048abfddc1b17d4d240666d75e44f Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 23 Jun 2017 16:24:30 +0800 Subject: [PATCH 13/29] fix deprecated warning --- cocos2d/core/platform/js.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index 92590d0804..dad750f041 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -574,7 +574,7 @@ cc.js.unregisterClass to remove the id of unused class'; * @param {Boolean} [writable=false] */ js.obsolete = function (obj, obsoleted, newPropName, writable) { - var oldName = obsoleted.split('.').slice(-1); + var oldName = obsoleted.split('.').slice(-1)[0]; function get () { if (CC_DEV) { cc.warnID(5400, obsoleted, newPropName); @@ -688,7 +688,7 @@ js.shiftArguments = function () { var len = arguments.length - 1; var args = new Array(len); for(var i = 0; i < len; ++i) { - args[i] = arguments[i + i]; + args[i] = arguments[i + 1]; } return args; }; From f40b2a00bc1e7c78b9ba495fe5553e766b1c4759 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 26 Jun 2017 12:01:55 +0800 Subject: [PATCH 14/29] Strict assert for sequence construction --- DebugInfos.js | 2 ++ cocos2d/actions/CCActionInterval.js | 16 +++++++++ jsb/jsb-action.js | 50 +++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/DebugInfos.js b/DebugInfos.js index 889d3c36ac..5c05246271 100644 --- a/DebugInfos.js +++ b/DebugInfos.js @@ -20,6 +20,8 @@ if (CC_DEV) { "1016": "cc.RotateTo.reverse(): it should be overridden in subclass.", //RotateTo.reverse "1017": "cc.GridAction.getGrid(): it should be overridden in subclass.", //ActionGrid.getGrid "1018": "Grid size must be (1,1)", //ActionGrid3D.initWithSize + "1019": "Failed to construct, Sequence construction needs two or more actions", //ActionInterval.sequence + "1020": "Failed to construct, Spawn construction needs two or more actions", //ActionInterval.spawn //configuration: 1100 "1100": "Expected 'data' dict, but not found. Config file: %s", //loadConfigFile "1101": "Please load the resource first : %s", //loadConfigFile_2 diff --git a/cocos2d/actions/CCActionInterval.js b/cocos2d/actions/CCActionInterval.js index 21ece7466f..531a2f6dde 100644 --- a/cocos2d/actions/CCActionInterval.js +++ b/cocos2d/actions/CCActionInterval.js @@ -313,6 +313,10 @@ cc.Sequence = cc.ActionInterval.extend({ this._actions = []; var paramArray = (tempArray instanceof Array) ? tempArray : arguments; + if (paramArray.length === 1) { + cc.errorID(1019); + return; + } var last = paramArray.length - 1; if ((last >= 0) && (paramArray[last] == null)) cc.logID(1015); @@ -444,6 +448,10 @@ cc.Sequence = cc.ActionInterval.extend({ // todo: It should be use new cc.sequence = function (/*Multiple Arguments*/tempArray) { var paramArray = (tempArray instanceof Array) ? tempArray : arguments; + if (paramArray.length === 1) { + cc.errorID(1019); + return null; + } var last = paramArray.length - 1; if ((last >= 0) && (paramArray[last] == null)) cc.logID(1015); @@ -731,6 +739,10 @@ cc.Spawn = cc.ActionInterval.extend({ this._two = null; var paramArray = (tempArray instanceof Array) ? tempArray : arguments; + if (paramArray.length === 1) { + cc.errorID(1020); + return; + } var last = paramArray.length - 1; if ((last >= 0) && (paramArray[last] == null)) cc.logID(1015); @@ -825,6 +837,10 @@ cc.Spawn = cc.ActionInterval.extend({ */ cc.spawn = function (/*Multiple Arguments*/tempArray) { var paramArray = (tempArray instanceof Array) ? tempArray : arguments; + if (paramArray.length === 1) { + cc.errorID(1020); + return null; + } if ((paramArray.length > 0) && (paramArray[paramArray.length - 1] == null)) cc.logID(1015); diff --git a/jsb/jsb-action.js b/jsb/jsb-action.js index b79985b470..c272a4d874 100644 --- a/jsb/jsb-action.js +++ b/jsb/jsb-action.js @@ -53,8 +53,6 @@ var actionArr = [ 'Place', 'CallFunc', 'DelayTime', - 'Sequence', - 'Spawn', 'Speed', 'Repeat', 'RepeatForever', @@ -117,6 +115,54 @@ if (!ENABLE_GC_FOR_NATIVE_OBJECTS) { } } +cc.Sequence.prototype._ctor = function (...args) { + var paramArray = (args[0] instanceof Array) ? args[0] : args; + if (paramArray.length === 1) { + cc.errorID(1019); + return; + } + var last = paramArray.length - 1; + if ((last >= 0) && (paramArray[last] == null)) + cc.logID(1015); + + if (last >= 0) { + this.init(paramArray); + } + + if (!ENABLE_GC_FOR_NATIVE_OBJECTS) { + this.retain(); + this._retained = true; + } +}; + +cc.sequence = function (...args) { + var paramArray = (args[0] instanceof Array) ? args[0] : args; + return new cc.Sequence(paramArray); +} + +cc.Spawn.prototype._ctor = function (...args) { + var paramArray = (args[0] instanceof Array) ? args[0] : args; + if (paramArray.length === 1) + cc.errorID(1020); + var last = paramArray.length - 1; + if ((last >= 0) && (paramArray[last] == null)) + cc.logID(1015); + + if (last >= 0) { + this.init(paramArray); + } + + if (!ENABLE_GC_FOR_NATIVE_OBJECTS) { + this.retain(); + this._retained = true; + } +}; + +cc.spawn = function (...args) { + var paramArray = (args[0] instanceof Array) ? args[0] : args; + return new cc.Spawn(paramArray); +} + cc.targetedAction = function (target, action) { return new cc.TargetedAction(target, action); }; From 7a4667fddd59159979605840356afac209bbfad2 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Mon, 26 Jun 2017 15:15:58 +0800 Subject: [PATCH 15/29] add camera check for module --- cocos2d/core/CCNode.js | 2 +- cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js | 9 +++++---- cocos2d/core/physics/joint/CCMouseJoint.js | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index e15cdc1542..1db12558b0 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1306,7 +1306,7 @@ var Node = cc.Class({ var rect = cc.rect(0, 0, w, h); var trans; - if (cc.Camera.main) { + if (cc.Camera && cc.Camera.main) { trans = cc.Camera.main.getNodeToCameraTransform(this); } else { diff --git a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js index e7c2f6f57f..9bf3d76552 100644 --- a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js +++ b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js @@ -279,11 +279,12 @@ _ccsg.Node.RenderCmd.prototype = { this._notifyRegionStatus && this._notifyRegionStatus(_ccsg.Node.CanvasRenderCmd.RegionStatus.DirtyDouble); } - if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) { + var Camera = cc.Camera; + if (cc._renderType === cc.game.RENDER_TYPE_WEBGL && Camera) { // 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; + var camera = Camera.main; + if (parentCmd && this._cameraFlag != Camera.flags.InCamera) { + this._cameraFlag = parentCmd._cameraFlag > 0 ? Camera.flags.ParentInCamera : 0; } } diff --git a/cocos2d/core/physics/joint/CCMouseJoint.js b/cocos2d/core/physics/joint/CCMouseJoint.js index ca60c776b1..6516e1c432 100644 --- a/cocos2d/core/physics/joint/CCMouseJoint.js +++ b/cocos2d/core/physics/joint/CCMouseJoint.js @@ -222,7 +222,7 @@ var MouseJoint = cc.Class({ var manager = cc.director.getPhysicsManager(); var target = this._pressPoint = event.touch.getLocation(); - if (cc.Camera.main) { + if (cc.Camera && cc.Camera.main) { target = cc.Camera.main.getCameraToWorldPoint(target); } @@ -259,7 +259,7 @@ var MouseJoint = cc.Class({ }, update: function () { - if (cc.Camera.main && this._pressPoint) { + if (cc.Camera && cc.Camera.main && this._pressPoint) { this.target = cc.Camera.main.getCameraToWorldPoint(this._pressPoint); } } From 93e026ecdc90dded968929bf5e29201414b8d129 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Mon, 26 Jun 2017 15:59:00 +0800 Subject: [PATCH 16/29] add Intersection to modules --- modules.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules.json b/modules.json index 86653ae728..01cf742d2d 100644 --- a/modules.json +++ b/modules.json @@ -230,5 +230,11 @@ "entries": [ "./cocos2d/core/camera/CCCamera.js" ] + }, + { + "name": "Intersection", + "entries": [ + "./cocos2d/core/collider/CCIntersection.js" + ] } ] From 858fb30a1471f11f0570a377ade9174d5960a513 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 26 Jun 2017 17:56:56 +0800 Subject: [PATCH 17/29] Fix circle reference check issue --- cocos2d/core/load-pipeline/CCLoader.js | 8 +------- cocos2d/core/load-pipeline/loading-items.js | 13 ++++++++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index d022891a30..237ef42c10 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -261,10 +261,6 @@ proto.load = function(resources, progressCallback, completeCallback) { }; proto.flowInDeps = function (owner, urlList, callback) { - if (owner && !owner.deps) { - owner.deps = []; - } - _sharedList.length = 0; for (var i = 0; i < urlList.length; ++i) { var res = getResWithUrl(urlList[i]); @@ -273,8 +269,6 @@ proto.flowInDeps = function (owner, urlList, callback) { var item = this.getItem(res.url); if (item) { _sharedList.push(item); - // Collect deps to avoid circle reference - owner && owner.deps.push(item); } else { _sharedList.push(res); @@ -289,7 +283,7 @@ proto.flowInDeps = function (owner, urlList, callback) { callback(errors, items); // Clear deps because it's already done // Each item will only flowInDeps once, so it's still safe here - owner && (owner.deps.length = 0); + owner && owner.deps && (owner.deps.length = 0); items.destroy(); }); if (owner) { diff --git a/cocos2d/core/load-pipeline/loading-items.js b/cocos2d/core/load-pipeline/loading-items.js index b0977ba5d8..baee6ca746 100644 --- a/cocos2d/core/load-pipeline/loading-items.js +++ b/cocos2d/core/load-pipeline/loading-items.js @@ -413,7 +413,7 @@ LoadingItems.initQueueDeps = function (queue) { } }; -LoadingItems.registerDep = function (owner, depId) { +LoadingItems.registerQueueDep = function (owner, depId) { var queueId = owner.queueId || owner; if (!queueId) { return false; @@ -463,6 +463,9 @@ proto.append = function (urlList, owner) { if (!this.active) { return []; } + if (owner && !owner.deps) { + owner.deps = []; + } this._appending = true; var accepted = [], i, url, item; @@ -472,6 +475,8 @@ proto.append = function (urlList, owner) { // Already queued in another items queue, url is actually the item if (url.queueId && !this.map[url.id]) { this.map[url.id] = url; + // Register item deps for circle reference check + owner && owner.deps.push(url); // Queued and completed or Owner circle referenced by dependency if (url.complete || checkCircleReference(owner, url)) { this.totalCount++; @@ -485,7 +490,7 @@ proto.append = function (urlList, owner) { var queue = _queues[url.queueId]; if (queue) { this.totalCount++; - LoadingItems.registerDep(owner || this._id, url.id); + LoadingItems.registerQueueDep(owner || this._id, url.id); // console.log('+++++ Waited ' + url.id); queue.addListener(url.id, function (item) { // console.log('----- Completed by waiting ' + item.id + ', rest: ' + (self.totalCount - self.completedCount-1)); @@ -503,7 +508,9 @@ proto.append = function (urlList, owner) { if (!this.map[key]) { this.map[key] = item; this.totalCount++; - LoadingItems.registerDep(owner || this._id, key); + // Register item deps for circle reference check + owner && owner.deps.push(item); + LoadingItems.registerQueueDep(owner || this._id, key); accepted.push(item); // console.log('+++++ Appended ' + item.id); } From 854d2d8f14a197b3df364b433b193b921a197083 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 26 Jun 2017 18:23:28 +0800 Subject: [PATCH 18/29] Add circle reference test case --- test/qunit/assets/library/65/65535.json | 7 +++++++ test/qunit/assets/library/74/749417.json | 7 +++++++ test/qunit/unit-es5/_init.js | 11 +++++++++++ test/qunit/unit-es5/test-asset-library.js | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 test/qunit/assets/library/65/65535.json create mode 100644 test/qunit/assets/library/74/749417.json diff --git a/test/qunit/assets/library/65/65535.json b/test/qunit/assets/library/65/65535.json new file mode 100644 index 0000000000..a4df3363c9 --- /dev/null +++ b/test/qunit/assets/library/65/65535.json @@ -0,0 +1,7 @@ +{ + "__type__": "TestDependency", + "name": "Prefab A", + "dependency": { + "__uuid__": "749417" + } +} \ No newline at end of file diff --git a/test/qunit/assets/library/74/749417.json b/test/qunit/assets/library/74/749417.json new file mode 100644 index 0000000000..b989312f4a --- /dev/null +++ b/test/qunit/assets/library/74/749417.json @@ -0,0 +1,7 @@ +{ + "__type__": "TestDependency", + "name": "Prefab B", + "dependency": { + "__uuid__": "65535" + } +} \ No newline at end of file diff --git a/test/qunit/unit-es5/_init.js b/test/qunit/unit-es5/_init.js index 4d4c092e3a..58dc51bf54 100644 --- a/test/qunit/unit-es5/_init.js +++ b/test/qunit/unit-es5/_init.js @@ -107,6 +107,17 @@ var TestScript = cc.Class({ } }); +var TestDependency = cc.Class({ + name: 'TestDependency', + extends: cc.Asset, + properties: { + dependency: { + default: '', + url: TestDependency, + } + } +}); + // mocks to test engine extends cc.engine = new (cc.Class({ diff --git a/test/qunit/unit-es5/test-asset-library.js b/test/qunit/unit-es5/test-asset-library.js index d398e2e127..8a93bbf072 100644 --- a/test/qunit/unit-es5/test-asset-library.js +++ b/test/qunit/unit-es5/test-asset-library.js @@ -5,6 +5,7 @@ var grossini_uuid = '748321'; var grossiniSprite_uuid = '1232218'; var selfReferenced_uuid = '123200'; + var circleReferenced_uuid = '65535'; largeModule('AssetLibrary', { setup: function () { @@ -81,4 +82,21 @@ }, 200); }); + asyncTest('load asset with circle referenced dependencies', function () { + AssetLibrary.loadAsset(circleReferenced_uuid, function (err, asset) { + if (err) { + ok(false, err.errorMessage); + return start(); + } + clearTimeout(timerId); + ok(asset.dependency, 'can load circle referenced asset'); + strictEqual(asset.dependency.dependency, asset, 'circle referenced asset should have dependency which equal to self'); + start(); + }); + var timerId = setTimeout(function () { + ok(false, 'time out!'); + start(); + }, 200); + }); + })(); From 0101ff31375bc78a8df98f339f46bdf75dc37532 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 27 Jun 2017 10:12:21 +0800 Subject: [PATCH 19/29] fixed mouse joint --- cocos2d/core/physics/joint/CCMouseJoint.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/physics/joint/CCMouseJoint.js b/cocos2d/core/physics/joint/CCMouseJoint.js index 6516e1c432..ad6bb3e7b4 100644 --- a/cocos2d/core/physics/joint/CCMouseJoint.js +++ b/cocos2d/core/physics/joint/CCMouseJoint.js @@ -259,9 +259,16 @@ var MouseJoint = cc.Class({ }, update: function () { - if (cc.Camera && cc.Camera.main && this._pressPoint) { + if (!this._pressPoint) { + return; + } + + if (cc.Camera && cc.Camera.main) { this.target = cc.Camera.main.getCameraToWorldPoint(this._pressPoint); } + else { + this.target = this._pressPoint; + } } }); From 3a4942e6c47627646152a54e622692c5bdc52bff Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 27 Jun 2017 21:38:08 +0800 Subject: [PATCH 20/29] Add event touch api doc for touch property --- cocos2d/core/event-manager/CCEvent.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/event-manager/CCEvent.js b/cocos2d/core/event-manager/CCEvent.js index 0c9dc869bf..d14fcb20a8 100644 --- a/cocos2d/core/event-manager/CCEvent.js +++ b/cocos2d/core/event-manager/CCEvent.js @@ -330,7 +330,13 @@ var EventTouch = function (touchArr, bubbles) { cc.Event.call(this, cc.Event.TOUCH, bubbles); this._eventCode = 0; this._touches = touchArr || []; - this.currentTouch = null; + /** + * !#en The current touch object + * !#zh 当前触点对象 + * @property touch + * @type {Touch} + */ + this.touch = null; }; JS.extend(EventTouch, cc.Event); From 08b5c224d7f24c9d7d2c4bd741d44b8b223543fe Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 27 Jun 2017 21:40:56 +0800 Subject: [PATCH 21/29] Fix director.convertToGL & convertToUI --- cocos2d/core/CCDirector.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index 781883ce66..2e894439a5 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -252,11 +252,11 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ * @return {Vec2} */ convertToGL: function (uiPoint) { - var docElem = document.documentElement; + var container = cc.game.container; var view = cc.view; - var box = docElem.getBoundingClientRect(); - var left = box.left + window.pageXOffset - docElem.clientLeft; - var top = box.top + window.pageYOffset - docElem.clientTop; + var box = container.getBoundingClientRect(); + var left = box.left + window.pageXOffset - container.clientLeft; + var top = box.top + window.pageYOffset - container.clientTop; var x = view._devicePixelRatio * (uiPoint.x - left); var y = view._devicePixelRatio * (top + box.height - uiPoint.y); return view._isRotated ? {x: view._viewPortRect.width - y, y: x} : {x: x, y: y}; @@ -273,19 +273,19 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ * @return {Vec2} */ convertToUI: function (glPoint) { - var docElem = document.documentElement; + var container = cc.game.container; var view = cc.view; - var box = docElem.getBoundingClientRect(); - var left = box.left + window.pageXOffset - docElem.clientLeft; - var top = box.top + window.pageYOffset - docElem.clientTop; + var box = container.getBoundingClientRect(); + var left = box.left + window.pageXOffset - container.clientLeft; + var top = box.top + window.pageYOffset - container.clientTop; var uiPoint = {x: 0, y: 0}; if (view._isRotated) { uiPoint.x = left + glPoint.y / view._devicePixelRatio; uiPoint.y = top + box.height - (view._viewPortRect.width - glPoint.x) / view._devicePixelRatio; } else { - uiPoint.x = left + glPoint.x / view._devicePixelRatio; - uiPoint.y = top + box.height - glPoint.y / view._devicePixelRatio; + uiPoint.x = left + glPoint.x * view._devicePixelRatio; + uiPoint.y = top + box.height - glPoint.y * view._devicePixelRatio; } return uiPoint; }, From a6d10e780426006f6484b803656c58be69f72a15 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 19 May 2017 10:02:21 +0800 Subject: [PATCH 22/29] Add pool for recycling _ccsg.Label objects --- cocos2d/core/components/CCLabel.js | 16 ++++-- cocos2d/core/components/CCRichText.js | 10 ++-- cocos2d/core/editbox/CCSGEditBox.js | 4 +- cocos2d/core/label/CCSGLabel.js | 70 +++++++++++++++++++++++---- cocos2d/core/platform/js.js | 11 +++++ jsb/jsb-label.js | 2 + 6 files changed, 94 insertions(+), 19 deletions(-) diff --git a/cocos2d/core/components/CCLabel.js b/cocos2d/core/components/CCLabel.js index 2b06739073..357499da22 100644 --- a/cocos2d/core/components/CCLabel.js +++ b/cocos2d/core/components/CCLabel.js @@ -528,22 +528,22 @@ var Label = cc.Class({ if (font.spriteFrame) { if (CC_JSB) { if (font.spriteFrame.textureLoaded()) { - sgNode = this._sgNode = new _ccsg.Label(this.string, font.fntDataStr, font.spriteFrame); + sgNode = this._sgNode = _ccsg.Label.pool.get(this.string, font.fntDataStr, font.spriteFrame); } else { cc.warnID(4012, font.name); - sgNode = this._sgNode = new _ccsg.Label(this.string); + sgNode = this._sgNode = _ccsg.Label.pool.get(this.string); } } else { - sgNode = this._sgNode = new _ccsg.Label(this.string, font.fntDataStr, font.spriteFrame, font); + sgNode = this._sgNode = _ccsg.Label.pool.get(this.string, font.fntDataStr, font.spriteFrame, font); } } else { cc.warnID(4011, font.name); - sgNode = this._sgNode = new _ccsg.Label(this.string); + sgNode = this._sgNode = _ccsg.Label.pool.get(this.string); } } else { var isAsset = font instanceof cc.Font; var ttfName = isAsset ? font.rawUrl : ''; - sgNode = this._sgNode = new _ccsg.Label(this.string, ttfName); + sgNode = this._sgNode = _ccsg.Label.pool.get(this.string, ttfName); } if (CC_JSB) { @@ -592,6 +592,12 @@ var Label = cc.Class({ this.node.setContentSize(this._sgNode.getContentSize()); } } + }, + + onDestroy: function () { + if (this._sgNode) { + _ccsg.Label.pool.put(this._sgNode); + } } }); diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index cf2bf9ccf3..c8e3b3c652 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -250,7 +250,7 @@ var RichText = cc.Class({ }, _createFontLabel: function (string) { - return new _ccsg.Label(string, this._getFontRawUrl()); + return _ccsg.Label.pool.get(string, this._getFontRawUrl()); }, _getFontRawUrl: function() { @@ -427,7 +427,6 @@ var RichText = cc.Class({ this._addLabelSegment(labelString, styleIndex); } - }, _isLastComponentCR: function(stringToken) { @@ -764,8 +763,13 @@ var RichText = cc.Class({ label._clickHandler = textStyle.event.click; } } - } + }, + onDestroy: function () { + for (var i = 0; i < this._labelSegments.length; ++i) { + _ccsg.Label.pool.put(this._labelSegments[i]); + } + } }); cc.RichText = module.exports = RichText; diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 869b03ac76..cb4fbb43a8 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -853,14 +853,14 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; proto._createLabels = function () { var editBoxSize = this._editBox.getContentSize(); if(!this._textLabel) { - this._textLabel = new _ccsg.Label(); + this._textLabel = _ccsg.Label.pool.get(); this._textLabel.setAnchorPoint(cc.p(0, 1)); this._textLabel.setOverflow(_ccsg.Label.Overflow.CLAMP); this._editBox.addChild(this._textLabel, 100); } if(!this._placeholderLabel) { - this._placeholderLabel = new _ccsg.Label(); + this._placeholderLabel = _ccsg.Label.pool.get(); this._placeholderLabel.setAnchorPoint(cc.p(0, 1)); this._placeholderLabel.setColor(cc.Color.GRAY); this._editBox.addChild(this._placeholderLabel, 100); diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 4202c2f5ca..b5ed74f7e3 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -25,6 +25,7 @@ ****************************************************************************/ require('./CCTextUtils.js'); var EventTarget = require("../event/event-target"); +var JS = require("../platform/js"); var FntLoader = { INFO_EXP: /info [^\n]*(\n|$)/gi, @@ -244,33 +245,41 @@ _ccsg.Label = _ccsg.Node.extend({ this._string = string; _ccsg.Node.prototype.ctor.call(this); - this.setAnchorPoint(cc.p(0.5, 0.5)); - _ccsg.Node.prototype.setContentSize.call(this, cc.size(128, 128)); + this.setAnchorPoint(0.5, 0.5); + _ccsg.Node.prototype.setContentSize.call(this, 128, 128); this._blendFunc = cc.BlendFunc._alphaNonPremultiplied(); + this._imageOffset = cc.p(0, 0); + this._numberOfLines = 0; + this._lettersInfo = []; + this._linesWidth = []; + this._linesOffsetX = []; + this._horizontalKernings = []; + this._reusedRect = cc.rect(0, 0, 0, 0); + this.setFontFileOrFamily(fontHandle, spriteFrame, fontAsset); this.setString(this._string); }, _resetBMFont: function() { - this._imageOffset = cc.p(0, 0); + this._imageOffset.x = this._imageOffset.y = 0; this._cascadeColorEnabled = true; this._cascadeOpacityEnabled = true; this._fontAtlas = null; this._config = null; this._numberOfLines = 0; - this._lettersInfo = []; - this._linesWidth = []; - this._linesOffsetX = []; + this._lettersInfo.length = 0; + this._linesWidth.length = 0; + this._linesOffsetX.length = 0; this._textDesiredHeight = 0; this._letterOffsetY = 0; this._tailoredTopY = 0; this._tailoredBottomY = 0; this._bmfontScale = 1.0; - this._horizontalKernings = []; + this._horizontalKernings.length = 0; this._lineBreakWithoutSpaces = false; - this._reusedRect = cc.rect(0, 0, 0, 0); + this._reusedRect.x = this._reusedRect.y = this._reusedRect.width = this._reusedRect.height = 0; this._textureLoaded = false; if (this._spriteBatchNode) { @@ -669,7 +678,7 @@ _ccsg.Label = _ccsg.Node.extend({ } return _ccsg.Node.prototype._getHeight.call(this); }, - _alignText: function() { + _alignText: function() { var ret = true; do { @@ -1327,6 +1336,49 @@ _ccsg.Label = _ccsg.Node.extend({ } }); +_ccsg.Label.pool = new JS.Pool(function (label) { + // return false; + if (!label instanceof _ccsg.Label) { + return false; + } + label._string = ""; + label._fontAsset = null; + label._fontHandle = ""; + label._labelType = 0; + label._resetBMFont(); + label._renderCmd._labelCanvas.width = 1; + label._renderCmd._labelCanvas.height = 1; + label._parent = null; + return true; +}, 20); + +_ccsg.Label.pool.get = function (string, fontHandle, spriteFrame, fontAsset) { + var label = this._get(); + if (label) { + fontHandle = fontHandle || ""; + label._fontHandle = fontHandle; + if (typeof string !== 'string') { + string = '' + string; + } + label._string = string; + + label._position.x = 0; + label._position.y = 0; + label.setAnchorPoint(0.5, 0.5); + _ccsg.Node.prototype.setContentSize.call(label, 128, 128); + + label._blendFunc.src = cc.macro.SRC_ALPHA; + label._blendFunc.dst = cc.macro.ONE_MINUS_SRC_ALPHA; + + label.setFontFileOrFamily(fontHandle, spriteFrame, fontAsset); + label.setString(label._string); + return label; + } + else { + return new _ccsg.Label(string, fontHandle, spriteFrame, fontAsset); + } +}; + var _p = _ccsg.Label.prototype; cc.js.addon(_p, EventTarget.prototype); diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index 9c03dfe6e5..ffcd68eab0 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -988,6 +988,17 @@ Pool.prototype.put = function (obj) { } }; +/** + * !#en Resize the pool. + * !#zh 设置对象池容量。 + * @method resize + */ +Pool.prototype.resize = function (length) { + if (length > 0) { + this._pool.length = length; + } +}; + js.Pool = Pool; // diff --git a/jsb/jsb-label.js b/jsb/jsb-label.js index 3e9cb5e94f..b74e590a9e 100644 --- a/jsb/jsb-label.js +++ b/jsb/jsb-label.js @@ -225,6 +225,8 @@ jsbLabel.prototype.getOutlineColor = function() { return this._outlineColor || cc.color(255,255,255,255); }; +jsbLabel.pool = new JS.Pool(0); + cc.Label = function (string, fontHandle, spriteFrame) { fontHandle = fontHandle || "Arial"; From a77a23b9b8e14c9298c77dc6695f0929c90892ed Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 19 May 2017 11:14:29 +0800 Subject: [PATCH 23/29] fix sglabel pool cache issue --- cocos2d/core/label/CCSGLabel.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index b5ed74f7e3..c93a4af040 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -1338,7 +1338,7 @@ _ccsg.Label = _ccsg.Node.extend({ _ccsg.Label.pool = new JS.Pool(function (label) { // return false; - if (!label instanceof _ccsg.Label) { + if (!(label instanceof _ccsg.Label)) { return false; } label._string = ""; @@ -1366,12 +1366,18 @@ _ccsg.Label.pool.get = function (string, fontHandle, spriteFrame, fontAsset) { label._position.y = 0; label.setAnchorPoint(0.5, 0.5); _ccsg.Node.prototype.setContentSize.call(label, 128, 128); - - label._blendFunc.src = cc.macro.SRC_ALPHA; - label._blendFunc.dst = cc.macro.ONE_MINUS_SRC_ALPHA; label.setFontFileOrFamily(fontHandle, spriteFrame, fontAsset); - label.setString(label._string); + label.setString(string); + label.setHorizontalAlign( cc.TextAlignment.LEFT); + label.setVerticalAlign( cc.VerticalTextAlignment.TOP); + label.setFontSize(40); + label.setOverflow(0); + label.enableWrapText(true); + label.setVisible(true); + label.setLineHeight(40); + label.setOutlined(false); + return label; } else { From 788589564c35d4ede747b0f2e7197b317c7854f4 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 19 May 2017 11:23:01 +0800 Subject: [PATCH 24/29] Fix Label reset issues with RichText --- cocos2d/core/label/CCSGLabel.js | 7 ++++--- cocos2d/core/platform/js.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index c93a4af040..bb6e6a5a00 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -1337,7 +1337,6 @@ _ccsg.Label = _ccsg.Node.extend({ }); _ccsg.Label.pool = new JS.Pool(function (label) { - // return false; if (!(label instanceof _ccsg.Label)) { return false; } @@ -1349,6 +1348,7 @@ _ccsg.Label.pool = new JS.Pool(function (label) { label._renderCmd._labelCanvas.width = 1; label._renderCmd._labelCanvas.height = 1; label._parent = null; + label._updateLabel(); return true; }, 20); @@ -1369,14 +1369,15 @@ _ccsg.Label.pool.get = function (string, fontHandle, spriteFrame, fontAsset) { label.setFontFileOrFamily(fontHandle, spriteFrame, fontAsset); label.setString(string); - label.setHorizontalAlign( cc.TextAlignment.LEFT); - label.setVerticalAlign( cc.VerticalTextAlignment.TOP); + label.setHorizontalAlign(cc.TextAlignment.LEFT); + label.setVerticalAlign(cc.VerticalTextAlignment.TOP); label.setFontSize(40); label.setOverflow(0); label.enableWrapText(true); label.setVisible(true); label.setLineHeight(40); label.setOutlined(false); + label.setBlendFunc(cc.macro.SRC_ALPHA, cc.macro.ONE_MINUS_SRC_ALPHA); return label; } diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index ffcd68eab0..d06b2a5228 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -994,7 +994,7 @@ Pool.prototype.put = function (obj) { * @method resize */ Pool.prototype.resize = function (length) { - if (length > 0) { + if (length >= 0) { this._pool.length = length; } }; From 93539c1772bf51a5d9fa083ab51a00e518093c0b Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 27 Jun 2017 22:30:13 +0800 Subject: [PATCH 25/29] Fix Pool.resize --- cocos2d/core/platform/js.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index d06b2a5228..ce2b973649 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -996,6 +996,9 @@ Pool.prototype.put = function (obj) { Pool.prototype.resize = function (length) { if (length >= 0) { this._pool.length = length; + if (this.count > length) { + this.count = length; + } } }; From abe32eda2f94c2f660de183ce4d088a76f566845 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 23 Jun 2017 14:53:06 +0800 Subject: [PATCH 26/29] make base64 speed +20% --- cocos2d/compression/base64.js | 13 +++++++------ cocos2d/core/utils/misc.js | 8 ++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cocos2d/compression/base64.js b/cocos2d/compression/base64.js index f0960c8577..388a605d45 100644 --- a/cocos2d/compression/base64.js +++ b/cocos2d/compression/base64.js @@ -4,13 +4,14 @@ You can choose between MIT and BSD-3-Clause license. License file will be added later. --*/ +var Misc = require('../core/utils/misc'); +var strValue = Misc.BASE64_VALUES; + /** * mixin cc.Codec.Base64 */ var Base64 = {name:'Jacob__Codec__Base64'}; -Base64._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - /** *

* cc.Codec.Base64.decode(input[, unicode=false]) -> String (http://en.wikipedia.org/wiki/Base64). @@ -31,10 +32,10 @@ Base64.decode = function Jacob__Codec__Base64__decode(input) { input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); while (i < input.length) { - enc1 = this._keyStr.indexOf(input.charAt(i++)); - enc2 = this._keyStr.indexOf(input.charAt(i++)); - enc3 = this._keyStr.indexOf(input.charAt(i++)); - enc4 = this._keyStr.indexOf(input.charAt(i++)); + enc1 = strValue[input.charCodeAt(i++)]; + enc2 = strValue[input.charCodeAt(i++)]; + enc3 = strValue[input.charCodeAt(i++)]; + enc4 = strValue[input.charCodeAt(i++)]; chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); diff --git a/cocos2d/core/utils/misc.js b/cocos2d/core/utils/misc.js index 0479f01400..c3be311c89 100644 --- a/cocos2d/core/utils/misc.js +++ b/cocos2d/core/utils/misc.js @@ -128,3 +128,11 @@ misc.isBuiltinClassId = function (id) { return id.startsWith('cc.') || id.startsWith('dragonBones.') || id.startsWith('sp.') || id.startsWith('ccsg.'); }; + +var BASE64_KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; +var BASE64_VALUES = new Array(123); // max char code in base64Keys +for (let i = 0; i < 123; ++i) BASE64_VALUES[i] = 64; // fill with placeholder('=') index +for (let i = 0; i < 64; ++i) BASE64_VALUES[BASE64_KEYS.charCodeAt(i)] = i; + +// decoded value indexed by base64 char code +misc.BASE64_VALUES = BASE64_VALUES; From a0930171b12cd51ed4cb091c7cd05541db5d3fef Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Mon, 26 Jun 2017 15:50:32 +0800 Subject: [PATCH 27/29] add decode uuid algorithm --- cocos2d/core/utils/decode-uuid.js | 50 +++++++++++++++++++++++++ test/qunit/unit-es5/test-decode-uuid.js | 8 ++++ 2 files changed, 58 insertions(+) create mode 100644 cocos2d/core/utils/decode-uuid.js create mode 100644 test/qunit/unit-es5/test-decode-uuid.js diff --git a/cocos2d/core/utils/decode-uuid.js b/cocos2d/core/utils/decode-uuid.js new file mode 100644 index 0000000000..9b18e168da --- /dev/null +++ b/cocos2d/core/utils/decode-uuid.js @@ -0,0 +1,50 @@ +/**************************************************************************** + 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. + ****************************************************************************/ + +var Base64Values = require('./misc').BASE64_VALUES; + +var HexChars = '0123456789abcdef'.split(''); + +var _t = ['', '', '', '']; +var UuidTemplate = _t.concat(_t, '-', _t, '-', _t, '-', _t, '-', _t, _t, _t); +var Indices = UuidTemplate.map(function (x, i) { return x === '-' ? NaN : i; }).filter(isFinite); + +// fcmR3XADNLgJ1ByKhqcC5Z -> fc991dd7-0033-4b80-9d41-c8a86a702e59 +module.exports = function (base64) { + UuidTemplate[0] = base64[0]; + UuidTemplate[1] = base64[1]; + for (var i = 2, j = 2; i < 22; i += 2) { + var lhs = Base64Values[base64.charCodeAt(i)]; + var rhs = Base64Values[base64.charCodeAt(i + 1)]; + UuidTemplate[Indices[j++]] = HexChars[lhs >> 2]; + UuidTemplate[Indices[j++]] = HexChars[((lhs & 3) << 2) | rhs >> 4]; + UuidTemplate[Indices[j++]] = HexChars[rhs & 0xF]; + } + return UuidTemplate.join(''); +}; + +if (CC_TEST) { + cc._Test.decodeUuid = module.exports; +} diff --git a/test/qunit/unit-es5/test-decode-uuid.js b/test/qunit/unit-es5/test-decode-uuid.js new file mode 100644 index 0000000000..2620116318 --- /dev/null +++ b/test/qunit/unit-es5/test-decode-uuid.js @@ -0,0 +1,8 @@ +module('decode uuid'); + +test('decode uuid', function () { + var encoded = 'fcmR3XADNLgJ1ByKhqcC5Z'; + var decoded = 'fc991dd7-0033-4b80-9d41-c8a86a702e59'; + var uuid = cc._Test.decodeUuid(encoded); + strictEqual(uuid, decoded, 'uuid could be decoded'); +}); \ No newline at end of file From a78bd81a627a229c835da639124609d906213c9c Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Mon, 26 Jun 2017 18:24:32 +0800 Subject: [PATCH 28/29] refine AssetLibrary api --- cocos2d/core/assets/CCAsset.js | 4 ++-- cocos2d/core/load-pipeline/CCLoader.js | 4 ---- cocos2d/core/load-pipeline/pack-downloader.js | 2 +- cocos2d/core/platform/CCAssetLibrary.js | 9 ++++----- test/qunit/unit-es5/test-decode-uuid.js | 2 +- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/cocos2d/core/assets/CCAsset.js b/cocos2d/core/assets/CCAsset.js index 23ceaf5115..f0be42f050 100644 --- a/cocos2d/core/assets/CCAsset.js +++ b/cocos2d/core/assets/CCAsset.js @@ -60,7 +60,7 @@ cc.Asset = cc.Class({ get: function () { if (this._rawFiles) { if (cc.AssetLibrary) { - return cc.AssetLibrary.getImportedDir(this._uuid) + '/' + this._uuid + '/' + this._rawFiles[0]; + return cc.AssetLibrary.getImportedDir(this._uuid) + '/' + this._rawFiles[0]; } else { cc.errorID(6400); @@ -84,7 +84,7 @@ cc.Asset = cc.Class({ get: function () { if (this._rawFiles) { if (cc.AssetLibrary) { - var dir = cc.AssetLibrary.getImportedDir(this._uuid) + '/' + this._uuid + '/'; + var dir = cc.AssetLibrary.getImportedDir(this._uuid) + '/'; return this._rawFiles.map(function (filename) { return dir + filename; }); diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 237ef42c10..dfca92742b 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -59,8 +59,6 @@ function getResWithUrl (res) { id = res; } isUuid = result.type ? result.type === 'uuid' : cc.AssetLibrary._getAssetUrl(id); - _info.url = null; - _info.raw = false; cc.AssetLibrary._getAssetInfoInRuntime(id, _info); result.url = !isUuid ? id : _info.url; if (_info.url && result.type === 'uuid' && _info.raw) { @@ -332,8 +330,6 @@ proto._getReferenceKey = function (assetOrUrlOrUuid) { cc.warnID(4800, assetOrUrlOrUuid); return key; } - _info.url = null; - _info.raw = false; cc.AssetLibrary._getAssetInfoInRuntime(key, _info); return this._cache[_info.url] ? _info.url : key; }; diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index d797f8f0be..03a885badc 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -81,7 +81,7 @@ module.exports = { _loadNewPack: function (uuid, packUuid, callback) { var self = this; - var packUrl = cc.AssetLibrary.getImportedDir(packUuid) + '/' + packUuid + '.json'; + var packUrl = cc.AssetLibrary.getImportedDir(packUuid) + '.json'; cc.loader.load({ url: packUrl, ignoreMaxConcurrency: true }, function (err, packJson) { if (err) { cc.errorID(4916, uuid); diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index df21512950..a7117700b9 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -111,7 +111,7 @@ var AssetLibrary = { }, getImportedDir: function (uuid) { - return _libraryBase + uuid.slice(0, 2)/* + cc.path.sep + uuid*/; + return _libraryBase + uuid.slice(0, 2) + '/' + uuid; }, _queryAssetInfoInEditor: function (uuid, callback) { @@ -138,16 +138,15 @@ var AssetLibrary = { }, _getAssetInfoInRuntime: function (uuid, result) { - if (!result) { - result = {url: null, raw: false}; - } + result = result || {url: null, raw: false}; var info = _uuidToRawAsset[uuid]; if (info && !cc.isChildClassOf(info.type, cc.Asset)) { result.url = _rawAssetsBase + info.url; result.raw = true; } else { - result.url = this.getImportedDir(uuid) + '/' + uuid + '.json'; + result.url = this.getImportedDir(uuid) + '.json'; + result.raw = false; } return result; }, diff --git a/test/qunit/unit-es5/test-decode-uuid.js b/test/qunit/unit-es5/test-decode-uuid.js index 2620116318..71d1675fa5 100644 --- a/test/qunit/unit-es5/test-decode-uuid.js +++ b/test/qunit/unit-es5/test-decode-uuid.js @@ -5,4 +5,4 @@ test('decode uuid', function () { var decoded = 'fc991dd7-0033-4b80-9d41-c8a86a702e59'; var uuid = cc._Test.decodeUuid(encoded); strictEqual(uuid, decoded, 'uuid could be decoded'); -}); \ No newline at end of file +}); From 82a69bddedc937e797877e281fe00fdb576bc0e1 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 27 Jun 2017 10:00:00 +0800 Subject: [PATCH 29/29] decode uuid for all imported assets --- cocos2d/core/platform/CCAssetLibrary.js | 4 ++++ cocos2d/core/sprites/CCSpriteFrame.js | 3 +++ cocos2d/core/utils/decode-uuid.js | 3 +++ 3 files changed, 10 insertions(+) diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index a7117700b9..549abf303a 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -28,6 +28,7 @@ var callInNextTick = require('./utils').callInNextTick; var Loader = require('../load-pipeline/CCLoader'); var PackDownloader = require('../load-pipeline/pack-downloader'); var AutoReleaseUtils = require('../load-pipeline/auto-release-utils'); +var decodeUuid = require('../utils/decode-uuid'); /** * The asset library which managing loading/unloading assets in project. @@ -111,6 +112,9 @@ var AssetLibrary = { }, getImportedDir: function (uuid) { + if (CC_BUILD) { + uuid = decodeUuid(uuid); + } return _libraryBase + uuid.slice(0, 2) + '/' + uuid; }, diff --git a/cocos2d/core/sprites/CCSpriteFrame.js b/cocos2d/core/sprites/CCSpriteFrame.js index acfe73ee51..83906cd6c2 100644 --- a/cocos2d/core/sprites/CCSpriteFrame.js +++ b/cocos2d/core/sprites/CCSpriteFrame.js @@ -492,6 +492,9 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{ else { uuid = Editor.Utils.UuidCache.urlToUuid(url); } + if (exporting) { + uuid = Editor.Utils.UuidUtils.compressUuid(uuid, true); + } } var capInsets; if (this.insetLeft !== 0 || diff --git a/cocos2d/core/utils/decode-uuid.js b/cocos2d/core/utils/decode-uuid.js index 9b18e168da..b74f862a2d 100644 --- a/cocos2d/core/utils/decode-uuid.js +++ b/cocos2d/core/utils/decode-uuid.js @@ -33,6 +33,9 @@ var Indices = UuidTemplate.map(function (x, i) { return x === '-' ? NaN : i; }). // fcmR3XADNLgJ1ByKhqcC5Z -> fc991dd7-0033-4b80-9d41-c8a86a702e59 module.exports = function (base64) { + if (base64.length !== 22) { + return base64; + } UuidTemplate[0] = base64[0]; UuidTemplate[1] = base64[1]; for (var i = 2, j = 2; i < 22; i += 2) {