From 55820e02eb2a51c9a5249584cb70ad64b4e00778 Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Fri, 30 Mar 2018 18:37:32 +0800 Subject: [PATCH] optimize graphics --- cocos2d/core/graphics/graphics.js | 176 ++------------- .../core/renderer/assemblers/graphics/impl.js | 208 ++++++++++++++++++ .../renderer/assemblers/graphics/index.js | 103 +++++---- .../renderer/assemblers/mask-assembler.js | 2 +- 4 files changed, 287 insertions(+), 202 deletions(-) create mode 100644 cocos2d/core/renderer/assemblers/graphics/impl.js diff --git a/cocos2d/core/graphics/graphics.js b/cocos2d/core/graphics/graphics.js index 4eb331fb3f..487d3367a7 100644 --- a/cocos2d/core/graphics/graphics.js +++ b/cocos2d/core/graphics/graphics.js @@ -33,41 +33,6 @@ const Helper = require('./helper'); const Types = require('./types'); const LineCap = Types.LineCap; const LineJoin = Types.LineJoin; -const PointFlags = Types.PointFlags; - -// Point -function Point (x, y) { - cc.Vec2.call(this, x, y); - this.reset(); -} -cc.js.extend(Point, cc.Vec2); - -Point.prototype.reset = function () { - this.dx = 0; - this.dy = 0; - this.dmx = 0; - this.dmy = 0; - this.flags = 0; - this.len = 0; -}; - -// Path -function Path () { - this.reset(); -} -Path.prototype.reset = function () { - this.closed = false; - this.nbevel = 0; - this.complex = true; - - if (this.points) { - this.points.length = 0; - } - else { - this.points = []; - } -}; - /** * @class Graphics @@ -82,25 +47,7 @@ let Graphics = cc.Class({ }, ctor () { - // inner properties - this._tessTol = 0.25; - this._distTol = 0.01; - this._updatePathOffset = false; - - this._paths = null; - this._pathLength = 0; - this._pathOffset = 0; - - this._points = null; - this._pointsOffset = 0; - - this._commandx = 0; - this._commandy = 0; - - this._paths = []; - this._points = []; - - this._renderDatas = []; + this._impl = Graphics._assembler.createImpl(); }, properties: { @@ -228,8 +175,7 @@ let Graphics = cc.Class({ onDestroy () { this._super(); - // all requested render data will be destroyed by RenderComponent - this._renderDatas.length = 0; + this._impl = null; }, _activateMaterial () { @@ -248,16 +194,7 @@ let Graphics = cc.Class({ * @param {Number} [y] The y axis of the coordinate for the end point. */ moveTo (x, y) { - if (this._updatePathOffset) { - this._pathOffset = this._pathLength; - this._updatePathOffset = false; - } - - this._addPath(); - this._addPoint(x, y, PointFlags.PT_CORNER); - - this._commandx = x; - this._commandy = y; + this._impl.moveTo(x, y); }, /** @@ -268,10 +205,7 @@ let Graphics = cc.Class({ * @param {Number} [y] The y axis of the coordinate for the end point. */ lineTo (x, y) { - this._addPoint(x, y, PointFlags.PT_CORNER); - - this._commandx = x; - this._commandy = y; + this._impl.lineTo(x, y); }, /** @@ -286,18 +220,7 @@ let Graphics = cc.Class({ * @param {Number} [y] The y axis of the coordinate for the end point. */ bezierCurveTo (c1x, c1y, c2x, c2y, x, y) { - var path = this._curPath; - var last = path.points[path.points.length - 1]; - - if (last.x === c1x && last.y === c1y && c2x === x && c2y === y) { - this.lineTo(x, y); - return; - } - - Helper.tesselateBezier(this, last.x, last.y, c1x, c1y, c2x, c2y, x, y, 0, PointFlags.PT_CORNER); - - this._commandx = x; - this._commandy = y; + this._impl.bezierCurveTo(c1x, c1y, c2x, c2y, x, y); }, /** @@ -310,9 +233,7 @@ let Graphics = cc.Class({ * @param {Number} [y] The y axis of the coordinate for the end point. */ quadraticCurveTo (cx, cy, x, y) { - var x0 = this._commandx; - var y0 = this._commandy; - this.bezierCurveTo(x0 + 2.0 / 3.0 * (cx - x0), y0 + 2.0 / 3.0 * (cy - y0), x + 2.0 / 3.0 * (cx - x), y + 2.0 / 3.0 * (cy - y), x, y); + this._impl.quadraticCurveTo(cx, cy, x, y); }, /** @@ -327,7 +248,7 @@ let Graphics = cc.Class({ * @param {Number} [counterclockwise] An optional Boolean which, if true, causes the arc to be drawn counter-clockwise between the two angles. By default it is drawn clockwise. */ arc (cx, cy, r, startAngle, endAngle, counterclockwise) { - Helper.arc(this, cx, cy, r, startAngle, endAngle, counterclockwise); + this._impl.arc(cx, cy, r, startAngle, endAngle, counterclockwise); }, /** @@ -340,8 +261,7 @@ let Graphics = cc.Class({ * @param {Number} [ry] The ellipse's y-axis radius. */ ellipse (cx, cy, rx, ry) { - Helper.ellipse(this, cx, cy, rx, ry); - this._curPath.complex = false; + this._impl.ellipse(cx, cy, rx, ry); }, /** @@ -353,8 +273,7 @@ let Graphics = cc.Class({ * @param {Number} [r] The circle's radius. */ circle (cx, cy, r) { - Helper.ellipse(this, cx, cy, r, r); - this._curPath.complex = false; + this._impl.circle(cx, cy, r); }, /** @@ -367,12 +286,7 @@ let Graphics = cc.Class({ * @param {Number} [h] The rectangle's height. */ rect (x, y, w, h) { - this.moveTo(x, y); - this.lineTo(x, y + h); - this.lineTo(x + w, y + h); - this.lineTo(x + w, y); - this.close(); - this._curPath.complex = false; + this._impl.rect(x, y, w, h); }, /** @@ -386,8 +300,7 @@ let Graphics = cc.Class({ * @param {Number} [r] The radius of the rectangle. */ roundRect (x, y, w, h, r) { - Helper.roundRect(this, x, y, w, h, r); - this._curPath.complex = false; + this._impl.roundRect(x, y, w, h, r); }, /** @@ -411,29 +324,7 @@ let Graphics = cc.Class({ * @param {Boolean} [clean] Whether to clean the graphics inner cache. */ clear (clean) { - this._pathLength = 0; - this._pathOffset = 0; - this._pointsOffset = 0; - - this._curPath = null; - - let datas = this._renderDatas; - if (clean) { - this._paths.length = 0; - this._points.length = 0; - // manually destroy render datas - for (let i = 0, l = datas.length; i < l; i++) { - this.destroyRenderData(datas[i]); - } - datas.length = 0; - } - else { - for (let i = 0, l = datas.length; i < l; i++) { - let data = datas[i]; - data.vertexCount = data.indiceCount = data.dataLength = 0; - data._indices.length = 0; - } - } + this._impl.clear(this, clean); }, /** @@ -442,7 +333,7 @@ let Graphics = cc.Class({ * @method close */ close () { - this._curPath.closed = true; + this._impl.close(); }, /** @@ -461,47 +352,6 @@ let Graphics = cc.Class({ */ fill () { Graphics._assembler.fill(this); - }, - - _addPath () { - var offset = this._pathLength; - var path = this._paths[offset]; - - if (!path) { - path = new Path(); - - this._paths.push(path); - } else { - path.reset(); - } - - this._pathLength++; - this._curPath = path; - - return path; - }, - - _addPoint (x, y, flags) { - var path = this._curPath; - if (!path) return; - - var pt; - var points = this._points; - var pathPoints = path.points; - - var offset = this._pointsOffset++; - pt = points[offset]; - - if (!pt) { - pt = new Point(x, y); - points.push(pt); - } else { - pt.x = x; - pt.y = y; - } - - pt.flags = flags; - pathPoints.push(pt); } }); diff --git a/cocos2d/core/renderer/assemblers/graphics/impl.js b/cocos2d/core/renderer/assemblers/graphics/impl.js new file mode 100644 index 0000000000..b8775ca96a --- /dev/null +++ b/cocos2d/core/renderer/assemblers/graphics/impl.js @@ -0,0 +1,208 @@ + +const Helper = require('../../../graphics/helper'); +const PointFlags = require('../../../graphics/types').PointFlags; + +class Point extends cc.Vec2 { + constructor (x, y) { + super(x, y); + this.reset(); + } + + reset () { + this.dx = 0; + this.dy = 0; + this.dmx = 0; + this.dmy = 0; + this.flags = 0; + this.len = 0; + } +} + +class Path { + constructor () { + this.reset(); + } + + reset () { + this.closed = false; + this.nbevel = 0; + this.complex = true; + + if (this.points) { + this.points.length = 0; + } + else { + this.points = []; + } + } +} + +class Impl { + constructor () { + // inner properties + this._tessTol = 0.25; + this._distTol = 0.01; + this._updatePathOffset = false; + + this._paths = null; + this._pathLength = 0; + this._pathOffset = 0; + + this._points = null; + this._pointsOffset = 0; + + this._commandx = 0; + this._commandy = 0; + + this._paths = []; + this._points = []; + + this._renderDatas = []; + + this._dataOffset = 0; + } + + moveTo (x, y) { + if (this._updatePathOffset) { + this._pathOffset = this._pathLength; + this._updatePathOffset = false; + } + + this._addPath(); + this._addPoint(x, y, PointFlags.PT_CORNER); + + this._commandx = x; + this._commandy = y; + } + + lineTo (x, y) { + this._addPoint(x, y, PointFlags.PT_CORNER); + + this._commandx = x; + this._commandy = y; + } + + bezierCurveTo (c1x, c1y, c2x, c2y, x, y) { + var path = this._curPath; + var last = path.points[path.points.length - 1]; + + if (last.x === c1x && last.y === c1y && c2x === x && c2y === y) { + this.lineTo(x, y); + return; + } + + Helper.tesselateBezier(this, last.x, last.y, c1x, c1y, c2x, c2y, x, y, 0, PointFlags.PT_CORNER); + + this._commandx = x; + this._commandy = y; + } + + quadraticCurveTo (cx, cy, x, y) { + var x0 = this._commandx; + var y0 = this._commandy; + this.bezierCurveTo(x0 + 2.0 / 3.0 * (cx - x0), y0 + 2.0 / 3.0 * (cy - y0), x + 2.0 / 3.0 * (cx - x), y + 2.0 / 3.0 * (cy - y), x, y); + } + + arc (cx, cy, r, startAngle, endAngle, counterclockwise) { + Helper.arc(this, cx, cy, r, startAngle, endAngle, counterclockwise); + } + + ellipse (cx, cy, rx, ry) { + Helper.ellipse(this, cx, cy, rx, ry); + this._curPath.complex = false; + } + + circle (cx, cy, r) { + Helper.ellipse(this, cx, cy, r, r); + this._curPath.complex = false; + } + + rect (x, y, w, h) { + this.moveTo(x, y); + this.lineTo(x, y + h); + this.lineTo(x + w, y + h); + this.lineTo(x + w, y); + this.close(); + this._curPath.complex = false; + } + + roundRect (x, y, w, h, r) { + Helper.roundRect(this, x, y, w, h, r); + this._curPath.complex = false; + } + + clear (comp, clean) { + this._pathLength = 0; + this._pathOffset = 0; + this._pointsOffset = 0; + + this._dataOffset = 0; + + this._curPath = null; + + let datas = this._renderDatas; + if (clean) { + this._paths.length = 0; + this._points.length = 0; + // manually destroy render datas + for (let i = 0, l = datas.length; i < l; i++) { + comp.destroyRenderData(datas[i]); + } + datas.length = 0; + } + else { + for (let i = 0, l = datas.length; i < l; i++) { + let data = datas[i]; + data.indiceCount = data._indices.length = 0; + data.vertexCount = 0; + } + } + } + + close () { + this._curPath.closed = true; + } + + _addPath () { + var offset = this._pathLength; + var path = this._paths[offset]; + + if (!path) { + path = new Path(); + + this._paths.push(path); + } else { + path.reset(); + } + + this._pathLength++; + this._curPath = path; + + return path; + } + + _addPoint (x, y, flags) { + var path = this._curPath; + if (!path) return; + + var pt; + var points = this._points; + var pathPoints = path.points; + + var offset = this._pointsOffset++; + pt = points[offset]; + + if (!pt) { + pt = new Point(x, y); + points.push(pt); + } else { + pt.x = x; + pt.y = y; + } + + pt.flags = flags; + pathPoints.push(pt); + } +} + +module.exports = Impl; diff --git a/cocos2d/core/renderer/assemblers/graphics/index.js b/cocos2d/core/renderer/assemblers/graphics/index.js index 5f9e8a5c19..752cba56e8 100644 --- a/cocos2d/core/renderer/assemblers/graphics/index.js +++ b/cocos2d/core/renderer/assemblers/graphics/index.js @@ -28,6 +28,7 @@ const PointFlags = require('../../../graphics/types').PointFlags; const LineJoin = Graphics.LineJoin; const LineCap = Graphics.LineCap; const Earcut = require('./earcut'); +const Impl = require('./impl'); const macro = require('../../../platform/CCMacro'); const MAX_VERTEX = macro.BATCH_VERTEX_COUNT; @@ -50,6 +51,7 @@ const abs = Math.abs; let _matrix = math.mat4.create(); let _renderData = null; +let _impl = null; let _curColor = 0; function curveDivs (r, arc, tol) { @@ -68,8 +70,12 @@ function clamp (v, min, max) { } let graphicsAssembler = js.addon({ + createImpl () { + return new Impl(); + }, + updateRenderData (graphics) { - let datas = graphics._renderDatas; + let datas = graphics._impl._renderDatas; if (datas.length === 0) { datas.push(graphics.requestRenderData()); } @@ -98,7 +104,7 @@ let graphicsAssembler = js.addon({ nodeG = nodeColor.g / 255, nodeB = nodeColor.b / 255, nodeA = nodeColor.a / 255; - for (let i = 0, l = data.length; i < l; i++) { + for (let i = 0, l = renderData.vertexCount; i < l; i++) { vbuf[offset++] = data[i].x * a + data[i].y * c + tx; vbuf[offset++] = data[i].x * b + data[i].y * d + ty; vbuf[offset++] = z; @@ -122,38 +128,55 @@ let graphicsAssembler = js.addon({ }, genRenderData (graphics, cverts) { - let renderDatas = graphics._renderDatas; - let renderData = renderDatas[renderDatas.length - 1]; - let maxVertsCount = renderData.dataLength + cverts; + let renderDatas = _impl._renderDatas; + let renderData = renderDatas[_impl._dataOffset]; + + let maxVertsCount = renderData.vertexCount + cverts; if (maxVertsCount > MAX_VERTEX || maxVertsCount * 3 > MAX_INDICE) { - renderData = graphics.requestRenderData(); + ++_impl._dataOffset; + maxVertsCount = cverts; + + if (_impl._dataOffset < renderDatas.length) { + renderData = renderDatas[_impl._dataOffset]; + } + else { + renderData = graphics.requestRenderData(); + renderDatas[_impl._dataOffset] = renderData; + } } + + if (maxVertsCount > renderData.dataLength) { + renderData.dataLength = maxVertsCount; + } + return renderData; }, stroke (graphics) { - if (graphics._renderDatas.length === 0) { - graphics._renderDatas.push(graphics.requestRenderData()); + let renderDatas = graphics._impl._renderDatas; + if (renderDatas.length === 0) { + renderDatas.push(graphics.requestRenderData()); } _curColor = graphics._strokeColor._val; - this._flattenPaths(graphics); + this._flattenPaths(graphics._impl); this._expandStroke(graphics); - graphics._updatePathOffset = true; + graphics._impl._updatePathOffset = true; }, fill (graphics) { - if (graphics._renderDatas.length === 0) { - graphics._renderDatas.push(graphics.requestRenderData()); + let renderDatas = graphics._impl._renderDatas; + if (renderDatas.length === 0) { + renderDatas.push(graphics.requestRenderData()); } _curColor = graphics._fillColor._val; this._expandFill(graphics); - this._updatePathOffset = true; + graphics._impl._updatePathOffset = true; }, _expandStroke (graphics) { @@ -161,16 +184,18 @@ let graphicsAssembler = js.addon({ lineCap = graphics.lineCap, lineJoin = graphics.lineJoin, miterLimit = graphics.miterLimit; + + _impl = graphics._impl; - let ncap = curveDivs(w, PI, graphics._tessTol); + let ncap = curveDivs(w, PI, _impl._tessTol); - this._calculateJoins(graphics, w, lineJoin, miterLimit); + this._calculateJoins(_impl, w, lineJoin, miterLimit); - let paths = graphics._paths; + let paths = _impl._paths; // Calculate max vertex usage. let cverts = 0; - for (let i = graphics._pathOffset, l = graphics._pathLength; i < l; i++) { + for (let i = _impl._pathOffset, l = _impl._pathLength; i < l; i++) { let path = paths[i]; let pointsLength = path.points.length; @@ -186,16 +211,16 @@ let graphicsAssembler = js.addon({ } } } - + let renderData = _renderData = this.genRenderData(graphics, cverts), data = renderData._data, indicesBuffer = renderData._indices; - for (let i = graphics._pathOffset, l = graphics._pathLength; i < l; i++) { + for (let i = _impl._pathOffset, l = _impl._pathLength; i < l; i++) { let path = paths[i]; let pts = path.points; let pointsLength = pts.length; - let offset = data.length; + let offset = renderData.vertexCount; let p0, p1; let start, end, loop; @@ -268,25 +293,27 @@ let graphicsAssembler = js.addon({ // stroke indices let indicesOffset = indicesBuffer.length; - for (let start = offset+2, end = data.length; start < end; start++) { + for (let start = offset+2, end = renderData.vertexCount; start < end; start++) { indicesBuffer[indicesOffset++] = start - 2; indicesBuffer[indicesOffset++] = start - 1; indicesBuffer[indicesOffset++] = start; } } - renderData.vertexCount = data.length; renderData.indiceCount = indicesBuffer.length; _renderData = null; + _impl = null; }, _expandFill (graphics) { - let paths = graphics._paths; + _impl = graphics._impl; + + let paths = _impl._paths; // Calculate max vertex usage. let cverts = 0; - for (let i = graphics._pathOffset, l = graphics._pathLength; i < l; i++) { + for (let i = _impl._pathOffset, l = _impl._pathLength; i < l; i++) { let path = paths[i]; let pointsLength = path.points.length; @@ -297,7 +324,7 @@ let graphicsAssembler = js.addon({ data = renderData._data, indicesBuffer = renderData._indices; - for (let i = graphics._pathOffset, l = graphics._pathLength; i < l; i++) { + for (let i = _impl._pathOffset, l = _impl._pathLength; i < l; i++) { let path = paths[i]; let pts = path.points; let pointsLength = pts.length; @@ -307,7 +334,7 @@ let graphicsAssembler = js.addon({ } // Calculate shape vertices. - let offset = data.length; + let offset = renderData.vertexCount; for (let j = 0; j < pointsLength; ++j) { this._vset(pts[j].x, pts[j].y); @@ -317,7 +344,7 @@ let graphicsAssembler = js.addon({ if (path.complex) { let earcutData = []; - for (let j = offset, end = data.length; j < end; j++) { + for (let j = offset, end = renderData.vertexCount; j < end; j++) { earcutData.push(data[j].x); earcutData.push(data[j].y); } @@ -334,7 +361,7 @@ let graphicsAssembler = js.addon({ } else { let first = offset; - for (let start = offset+2, end = data.length; start < end; start++) { + for (let start = offset+2, end = renderData.vertexCount; start < end; start++) { indicesBuffer[indicesOffset++] = first; indicesBuffer[indicesOffset++] = start - 1; indicesBuffer[indicesOffset++] = start; @@ -342,13 +369,13 @@ let graphicsAssembler = js.addon({ } } - renderData.vertexCount = data.length; renderData.indiceCount = indicesBuffer.length; _renderData = null; + _impl = null; }, - _calculateJoins (graphics, w, lineJoin, miterLimit) { + _calculateJoins (impl, w, lineJoin, miterLimit) { let iw = 0.0; if (w > 0.0) { @@ -356,8 +383,8 @@ let graphicsAssembler = js.addon({ } // Calculate which joins needs extra vertices to append, and gather vertex count. - let paths = graphics._paths; - for (let i = graphics._pathOffset, l = graphics._pathLength; i < l; i++) { + let paths = impl._paths; + for (let i = impl._pathOffset, l = impl._pathLength; i < l; i++) { let path = paths[i]; let pts = path.points; @@ -420,9 +447,9 @@ let graphicsAssembler = js.addon({ } }, - _flattenPaths (graphics) { - let paths = graphics._paths; - for (let i = graphics._pathOffset, l = graphics._pathLength; i < l; i++) { + _flattenPaths (impl) { + let paths = impl._paths; + for (let i = impl._pathOffset, l = impl._pathLength; i < l; i++) { let path = paths[i]; let pts = path.points; @@ -613,13 +640,13 @@ let graphicsAssembler = js.addon({ _vset (x, y) { let data = _renderData._data; - - let offset = data.length; - _renderData.dataLength = offset + 1; + let offset = _renderData.vertexCount; data[offset].x = x; data[offset].y = y; data[offset].color = _curColor; + + _renderData.vertexCount ++; } }, assembler); diff --git a/cocos2d/core/renderer/assemblers/mask-assembler.js b/cocos2d/core/renderer/assemblers/mask-assembler.js index 56365d61e1..c9251e19b5 100644 --- a/cocos2d/core/renderer/assemblers/mask-assembler.js +++ b/cocos2d/core/renderer/assemblers/mask-assembler.js @@ -149,7 +149,7 @@ let maskEndAssembler = js.addon({ datas = mask._renderDatas; } else { - datas = mask._graphics._renderDatas; + datas = mask._graphics._impl._renderDatas; } let material = mask.getMaterial(); for (let i = 0; i < datas.length; i++) {