From 54de7ce4b2711cdff9873e878f865da7abd833d4 Mon Sep 17 00:00:00 2001
From: yinkaile <501251991@qq.com>
Date: Tue, 30 Jan 2018 15:55:10 +0800
Subject: [PATCH 1/4] remove redefined property
---
cocos2d/core/platform/attribute.js | 7 -------
1 file changed, 7 deletions(-)
diff --git a/cocos2d/core/platform/attribute.js b/cocos2d/core/platform/attribute.js
index e3542aa139..b050a9ebfc 100644
--- a/cocos2d/core/platform/attribute.js
+++ b/cocos2d/core/platform/attribute.js
@@ -183,13 +183,6 @@ cc.Integer = 'Integer';
*/
cc.Float = 'Float';
-if (CC_EDITOR) {
- JS.get(cc, 'Number', function () {
- cc.warnID(3603);
- return cc.Float;
- });
-}
-
/**
* Indicates that the elements in array should be type boolean.
* @property {string} Boolean
From 5fa710d7297f26dd9e34d3607a7c3ce0f8507c97 Mon Sep 17 00:00:00 2001
From: yinkaile <501251991@qq.com>
Date: Thu, 1 Feb 2018 17:10:15 +0800
Subject: [PATCH 2/4] upgrade motion streak
---
cocos2d/motion-streak/CCMotionStreak.js | 133 ++++++++++++++++++------
1 file changed, 100 insertions(+), 33 deletions(-)
diff --git a/cocos2d/motion-streak/CCMotionStreak.js b/cocos2d/motion-streak/CCMotionStreak.js
index 6e9d3f1af8..185680cd86 100644
--- a/cocos2d/motion-streak/CCMotionStreak.js
+++ b/cocos2d/motion-streak/CCMotionStreak.js
@@ -29,6 +29,48 @@ const renderEngine = require('../core/renderer/render-engine');
const SpriteMaterial = renderEngine.SpriteMaterial;
const RenderData = renderEngine.RenderData;
+let _tangent = cc.v2();
+let _miter = cc.v2();
+let _normal = cc.v2();
+let _vec2 = cc.v2();
+
+let _lineA = cc.v2();
+let _lineB = cc.v2();
+
+function normal (out, dir) {
+ //get perpendicular
+ out.x = -dir.y;
+ out.y = dir.x;
+ return out
+}
+
+//get unit dir of two lines
+function direction (out, a, b) {
+ a.sub(b, out);
+ out.normalizeSelf();
+ return out
+}
+
+function computeMiter (tangent, miter, lineA, lineB, halfThick, maxMultiple) {
+ //get tangent line
+ lineA.add(lineB, tangent);
+ tangent.normalizeSelf();
+
+ //get miter as a unit vector
+ miter.x = -tangent.y;
+ miter.y = tangent.x;
+ _vec2.x = -lineA.y;
+ _vec2.y = lineA.x;
+
+ //get the necessary length of our miter
+ let multiple = 1 / miter.dot(_vec2);
+ if (maxMultiple) {
+ multiple = Math.min(multiple, maxMultiple);
+ }
+ return halfThick * multiple;
+}
+
+
/**
* !#en
* cc.MotionStreak manages a Ribbon based on it's motion in absolute space.
@@ -59,9 +101,12 @@ var MotionStreak = cc.Class({
executeInEditMode: true
},
- ctor: function() {
- this._root = null;
- this._motionStreak = null;
+ ctor () {
+ this._points = [];
+
+ this._fadeDelta = 0;
+ this._maxPoints = 0;
+ this._numPoints = 0;
},
properties: {
@@ -263,6 +308,7 @@ var MotionStreak = cc.Class({
reset: function () {
this._numPoints = 0;
this._lastPoint = null;
+ this._points.length = 0;
let renderData = this._renderData;
if (renderData) {
renderData.dataLength = 0;
@@ -295,11 +341,14 @@ var MotionStreak = cc.Class({
ty = matrix.m13;
let stroke = this._stroke / 2;
- let lastPoint = this._lastPoint;
- if (!lastPoint) {
- lastPoint = this._lastPoint = cc.v2();
- lastPoint.x = tx;
- lastPoint.y = ty;
+
+ let points = this._points;
+ if (points.length >= 3) {
+ points.length -= 1;
+ }
+ points.splice(0, 0, cc.v2(tx, ty));
+
+ if (points.length === 1) {
this._numPoints = 1;
renderData.dataLength = 2;
data[0].x = tx;
@@ -312,11 +361,10 @@ var MotionStreak = cc.Class({
let maxPoints = this._maxPoints;
let numPoints = this._numPoints;
- let seg = Math.min(dt/this._fadeDelta, maxPoints) | 0;
+ let seg = Math.min(dt/this._fadeDelta, maxPoints - 1) | 0;
- if (seg === 0) {
- return;
- }
+ // at least move one seg to ensure follow the newest position
+ seg = Math.max(seg, 1);
let curLength = Math.min(numPoints + seg, maxPoints);
renderData.dataLength = renderData.vertexCount = curLength * 2;
@@ -329,21 +377,42 @@ var MotionStreak = cc.Class({
data[dest].y = data[i].y;
}
- let lastx = lastPoint.x, lasty = lastPoint.y;
- let angle = Math.PI - Math.atan2(ty - lasty, tx - lastx);
- let difx = Math.sin(angle) * stroke,
- dify = Math.cos(angle) * stroke;
- let lasttdx = data[seg*2].x;
- let lastbdx = data[seg*2+1].x;
- let lasttdy = data[seg*2].y;
- let lastbdy = data[seg*2+1].y;
- for (let i = 0; i < seg; i++) {
- let cur = i*2;
+ let last = [];
+ let cur = [];
+
+ direction(_lineA, points[0], points[1]);
+ normal(_normal, _lineA);
+
+ if (points.length === 2) {
+ last[0] = points[1].x + _normal.x * stroke;
+ last[1] = points[1].y + _normal.y * stroke;
+ last[2] = points[1].x - _normal.x * stroke;
+ last[3] = points[1].y - _normal.y * stroke;
+ }
+ else {
+ //get unit dir of next line
+ direction(_lineB, points[1], points[2]);
+
+ //stores tangent & miter
+ let miterLen = computeMiter(_tangent, _miter, _lineA, _lineB, stroke);
+ last[0] = points[1].x + _miter.x * miterLen;
+ last[1] = points[1].y + _miter.y * miterLen;
+ last[2] = points[1].x - _miter.x * miterLen;
+ last[3] = points[1].y - _miter.y * miterLen;
+ }
+
+ cur[0] = points[0].x + _normal.x * stroke;
+ cur[1] = points[0].y + _normal.y * stroke;
+ cur[2] = points[0].x - _normal.x * stroke;
+ cur[3] = points[0].y - _normal.y * stroke;
+
+ for (let i = 0; i < seg+1; i++) {
+ let index = i*2;
let step = 1 - 1 / seg * i;
- data[cur].x = lasttdx + (tx + difx - lasttdx) * step;
- data[cur].y = lasttdy + (ty + dify - lasttdy) * step;
- data[cur+1].x = lastbdx + (tx - difx - lastbdx) * step;
- data[cur+1].y = lastbdy + (ty - dify - lastbdy) * step;
+ data[index].x = last[0] + (cur[0] - last[0]) * step;
+ data[index].y = last[1] + (cur[1] - last[1]) * step;
+ data[index+1].x = last[2] + (cur[2] - last[2]) * step;
+ data[index+1].y = last[3] + (cur[3] - last[3]) * step;
}
let color = this._color,
@@ -353,20 +422,18 @@ var MotionStreak = cc.Class({
ca = color.a;
for (let i = 0; i < curLength; i++) {
- let u = 1/(curLength-1)*i;
+ let v = 1/(curLength-1)*i;
let dest = i*2;
- data[dest].u = u;
- data[dest].v = 0;
- data[dest+1].u = u;
- data[dest+1].v = 1;
+ data[dest].u = 0;
+ data[dest].v = v;
+ data[dest+1].u = 1;
+ data[dest+1].v = v;
let da = (1-1/(maxPoints-1)*i)*ca;
data[dest].color = data[dest+1].color = ((da<<24) >>> 0) + (cb<<16) + (cg<<8) + cr;
}
this._numPoints = curLength;
- lastPoint.x = tx;
- lastPoint.y = ty;
}
});
From 4a5b95f87314e28525d34d5bdc6717b529224c65 Mon Sep 17 00:00:00 2001
From: yinkaile <501251991@qq.com>
Date: Fri, 2 Feb 2018 11:19:11 +0800
Subject: [PATCH 3/4] optimize motion streak
---
.../assemblers/motion-streak-assembler.js | 157 ++++++++++++++++
cocos2d/motion-streak/CCMotionStreak.js | 175 +-----------------
2 files changed, 159 insertions(+), 173 deletions(-)
diff --git a/cocos2d/core/renderer/assemblers/motion-streak-assembler.js b/cocos2d/core/renderer/assemblers/motion-streak-assembler.js
index ac0d3ff67f..3cf791924b 100644
--- a/cocos2d/core/renderer/assemblers/motion-streak-assembler.js
+++ b/cocos2d/core/renderer/assemblers/motion-streak-assembler.js
@@ -26,9 +26,63 @@
const js = require('../../platform/js');
const assembler = require('./assembler');
const MotionStreak = require('../../../motion-streak/CCMotionStreak');
+const renderEngine = require('../render-engine');
+const RenderData = renderEngine.RenderData;
+
+function Point (point, dir) {
+ this.point = point || cc.v2();
+ this.dir = dir || cc.v2();
+ this.distance = 0
+ this.time = 0;
+}
+
+Point.prototype.setPoint = function (x, y) {
+ this.point.x = x;
+ this.point.y = y;
+};
+
+Point.prototype.setDir = function (x, y) {
+ this.dir.x = x;
+ this.dir.y = y;
+};
+
+let _tangent = cc.v2();
+let _miter = cc.v2();
+let _normal = cc.v2();
+let _vec2 = cc.v2();
+
+function normal (out, dir) {
+ //get perpendicular
+ out.x = -dir.y;
+ out.y = dir.x;
+ return out
+}
+
+function computeMiter (miter, lineA, lineB, halfThick, maxMultiple) {
+ //get tangent line
+ lineA.add(lineB, _tangent);
+ _tangent.normalizeSelf();
+
+ //get miter as a unit vector
+ miter.x = -_tangent.y;
+ miter.y = _tangent.x;
+ _vec2.x = -lineA.y;
+ _vec2.y = lineA.x;
+
+ //get the necessary length of our miter
+ let multiple = 1 / miter.dot(_vec2);
+ if (maxMultiple) {
+ multiple = Math.min(multiple, maxMultiple);
+ }
+ return halfThick * multiple;
+}
+
var motionStreakAssembler = js.addon({
updateRenderData (comp) {
+ let dt = cc.director.getDeltaTime();
+ this.update(comp, dt);
+
let renderData = comp._renderData;
let size = comp.node._contentSize;
let anchor = comp.node._anchorPoint;
@@ -39,6 +93,109 @@ var motionStreakAssembler = js.addon({
return this.datas;
},
+ update (comp, dt) {
+ let renderData = comp._renderData;
+ if (!renderData) {
+ renderData = comp._renderData = RenderData.alloc();
+ }
+
+ if (CC_EDITOR && !comp.preview) return;
+
+ let stroke = comp._stroke / 2;
+
+ let node = comp.node;
+ node._updateWorldMatrix();
+ let matrix = node._worldMatrix;
+ let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
+ tx = matrix.m12, ty = matrix.m13;
+
+ let points = comp._points;
+
+ let cur;
+ if (points.length > 1) {
+ let difx = points[0].x - tx;
+ let dify = points[0].y - ty;
+ if ((difx*difx + dify*dify) < comp.minSeg) {
+ cur = points[0];
+ }
+ }
+
+ if (!cur) {
+ cur = new Point();
+ points.splice(0, 0, cur);
+ }
+
+ cur.setPoint(tx, ty);
+ cur.time = comp._fadeTime;
+
+ renderData.dataLength = 0;
+ if (points.length < 2) {
+ return;
+ }
+
+ let data = renderData._data;
+
+ let color = comp._color,
+ cr = color.r, cg = color.g, cb = color.b, ca = color.a;
+
+ let prev = points[1];
+ prev.distance = cur.point.sub(prev.point, _vec2).mag();
+ _vec2.normalizeSelf();
+ prev.setDir(_vec2.x, _vec2.y);
+ cur.setDir(_vec2.x, _vec2.y);
+
+ let fadeTime = comp._fadeTime;
+ let findLast = false;
+ for (let i = points.length - 1; i >=0 ; i--) {
+ let p = points[i];
+ let point = p.point;
+ let dir = p.dir;
+ p.time -= dt;
+
+ if (p.time < 0) {
+ points.splice(i, 1);
+ continue;
+ }
+
+ let progress = p.time / fadeTime;
+
+ let next = points[i - 1];
+ if (!findLast) {
+ if (!next) {
+ points.splice(i, 1);
+ continue;
+ }
+
+ point.x = next.point.x - dir.x * progress;
+ point.y = next.point.y - dir.y * progress;
+ }
+ findLast = true;
+
+ normal(_normal, dir);
+
+ renderData.dataLength += 2;
+
+ let da = progress*ca;
+ let c = ((da<<24) >>> 0) + (cb<<16) + (cg<<8) + cr;
+
+ let dataIndex = data.length - 1;
+ data[dataIndex].x = point.x - _normal.x * stroke;
+ data[dataIndex].y = point.y - _normal.y * stroke;
+ data[dataIndex].u = 0;
+ data[dataIndex].v = progress;
+ data[dataIndex].color = c;
+ dataIndex--;
+ data[dataIndex].x = point.x + _normal.x * stroke;
+ data[dataIndex].y = point.y + _normal.y * stroke;
+ data[dataIndex].u = 1;
+ data[dataIndex].v = progress;
+ data[dataIndex].color = c;
+ }
+
+ renderData.vertexCount = renderData.dataLength;
+ renderData.indiceCount = (renderData.vertexCount - 2)*3;
+ },
+
fillBuffers (batchData, vertexId, vbuf, uintbuf, ibuf) {
let comp = batchData.comp,
offset = batchData.byteOffset / 4,
diff --git a/cocos2d/motion-streak/CCMotionStreak.js b/cocos2d/motion-streak/CCMotionStreak.js
index 185680cd86..1d59f909d0 100644
--- a/cocos2d/motion-streak/CCMotionStreak.js
+++ b/cocos2d/motion-streak/CCMotionStreak.js
@@ -29,48 +29,6 @@ const renderEngine = require('../core/renderer/render-engine');
const SpriteMaterial = renderEngine.SpriteMaterial;
const RenderData = renderEngine.RenderData;
-let _tangent = cc.v2();
-let _miter = cc.v2();
-let _normal = cc.v2();
-let _vec2 = cc.v2();
-
-let _lineA = cc.v2();
-let _lineB = cc.v2();
-
-function normal (out, dir) {
- //get perpendicular
- out.x = -dir.y;
- out.y = dir.x;
- return out
-}
-
-//get unit dir of two lines
-function direction (out, a, b) {
- a.sub(b, out);
- out.normalizeSelf();
- return out
-}
-
-function computeMiter (tangent, miter, lineA, lineB, halfThick, maxMultiple) {
- //get tangent line
- lineA.add(lineB, tangent);
- tangent.normalizeSelf();
-
- //get miter as a unit vector
- miter.x = -tangent.y;
- miter.y = tangent.x;
- _vec2.x = -lineA.y;
- _vec2.y = lineA.x;
-
- //get the necessary length of our miter
- let multiple = 1 / miter.dot(_vec2);
- if (maxMultiple) {
- multiple = Math.min(multiple, maxMultiple);
- }
- return halfThick * multiple;
-}
-
-
/**
* !#en
* cc.MotionStreak manages a Ribbon based on it's motion in absolute space.
@@ -103,10 +61,6 @@ var MotionStreak = cc.Class({
ctor () {
this._points = [];
-
- this._fadeDelta = 0;
- this._maxPoints = 0;
- this._numPoints = 0;
},
properties: {
@@ -140,7 +94,7 @@ var MotionStreak = cc.Class({
},
set: function (value) {
this._fadeTime = value;
- this._applyFadeTime();
+ this.reset();
},
animatable: false,
tooltip: CC_DEV && 'i18n:COMPONENT.motionStreak.fadeTime'
@@ -257,7 +211,7 @@ var MotionStreak = cc.Class({
onEnable: function () {
this._super();
this._activateMaterial();
- this._applyFadeTime();
+ this.reset();
},
_activateMaterial: function () {
@@ -278,13 +232,6 @@ var MotionStreak = cc.Class({
this._material = material;
},
- _applyFadeTime: function () {
- this._fadeDelta = 1/60;
- this._maxPoints = (0 | (this._fadeTime * 60));
- this._numPoints = 0;
- this.reset();
- },
-
onFocusInEditor: CC_EDITOR && function () {
if (this.preview) {
this.reset();
@@ -306,8 +253,6 @@ var MotionStreak = cc.Class({
* myParticleSystem.stopSystem();
*/
reset: function () {
- this._numPoints = 0;
- this._lastPoint = null;
this._points.length = 0;
let renderData = this._renderData;
if (renderData) {
@@ -318,122 +263,6 @@ var MotionStreak = cc.Class({
if (CC_EDITOR) {
cc.engine.repaintInEditMode();
}
- },
-
- update: function (dt) {
- let renderData = this._renderData;
- if (!renderData) {
- renderData = this._renderData = RenderData.alloc();
- }
-
- if (CC_EDITOR && !this.preview) return;
-
- let data = renderData._data;
-
- let node = this.node;
- node._updateWorldMatrix();
- let matrix = node._worldMatrix;
- let a = matrix.m00,
- b = matrix.m01,
- c = matrix.m04,
- d = matrix.m05,
- tx = matrix.m12,
- ty = matrix.m13;
-
- let stroke = this._stroke / 2;
-
- let points = this._points;
- if (points.length >= 3) {
- points.length -= 1;
- }
- points.splice(0, 0, cc.v2(tx, ty));
-
- if (points.length === 1) {
- this._numPoints = 1;
- renderData.dataLength = 2;
- data[0].x = tx;
- data[0].y = ty+stroke;
- data[1].x = tx;
- data[1].y = ty-stroke;
- return;
- }
-
- let maxPoints = this._maxPoints;
- let numPoints = this._numPoints;
-
- let seg = Math.min(dt/this._fadeDelta, maxPoints - 1) | 0;
-
- // at least move one seg to ensure follow the newest position
- seg = Math.max(seg, 1);
-
- let curLength = Math.min(numPoints + seg, maxPoints);
- renderData.dataLength = renderData.vertexCount = curLength * 2;
- renderData.indiceCount = (curLength - 1) * 6;
-
- for (let i = numPoints * 2 - 1; i >= 0; i--) {
- let dest = i + seg*2;
- if (dest >= maxPoints*2) continue;
- data[dest].x = data[i].x;
- data[dest].y = data[i].y;
- }
-
- let last = [];
- let cur = [];
-
- direction(_lineA, points[0], points[1]);
- normal(_normal, _lineA);
-
- if (points.length === 2) {
- last[0] = points[1].x + _normal.x * stroke;
- last[1] = points[1].y + _normal.y * stroke;
- last[2] = points[1].x - _normal.x * stroke;
- last[3] = points[1].y - _normal.y * stroke;
- }
- else {
- //get unit dir of next line
- direction(_lineB, points[1], points[2]);
-
- //stores tangent & miter
- let miterLen = computeMiter(_tangent, _miter, _lineA, _lineB, stroke);
- last[0] = points[1].x + _miter.x * miterLen;
- last[1] = points[1].y + _miter.y * miterLen;
- last[2] = points[1].x - _miter.x * miterLen;
- last[3] = points[1].y - _miter.y * miterLen;
- }
-
- cur[0] = points[0].x + _normal.x * stroke;
- cur[1] = points[0].y + _normal.y * stroke;
- cur[2] = points[0].x - _normal.x * stroke;
- cur[3] = points[0].y - _normal.y * stroke;
-
- for (let i = 0; i < seg+1; i++) {
- let index = i*2;
- let step = 1 - 1 / seg * i;
- data[index].x = last[0] + (cur[0] - last[0]) * step;
- data[index].y = last[1] + (cur[1] - last[1]) * step;
- data[index+1].x = last[2] + (cur[2] - last[2]) * step;
- data[index+1].y = last[3] + (cur[3] - last[3]) * step;
- }
-
- let color = this._color,
- cr = color.r,
- cg = color.g,
- cb = color.b,
- ca = color.a;
-
- for (let i = 0; i < curLength; i++) {
- let v = 1/(curLength-1)*i;
- let dest = i*2;
- data[dest].u = 0;
- data[dest].v = v;
- data[dest+1].u = 1;
- data[dest+1].v = v;
-
- let da = (1-1/(maxPoints-1)*i)*ca;
- data[dest].color = data[dest+1].color = ((da<<24) >>> 0) + (cb<<16) + (cg<<8) + cr;
- }
-
- this._numPoints = curLength;
}
});
From 632121daff89a31c69b632995f8524301a6b1dca Mon Sep 17 00:00:00 2001
From: yinkaile <501251991@qq.com>
Date: Fri, 2 Feb 2018 11:46:37 +0800
Subject: [PATCH 4/4] Revert "remove redefined property"
This reverts commit 54de7ce4b2711cdff9873e878f865da7abd833d4.
---
cocos2d/core/platform/attribute.js | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/cocos2d/core/platform/attribute.js b/cocos2d/core/platform/attribute.js
index b050a9ebfc..e3542aa139 100644
--- a/cocos2d/core/platform/attribute.js
+++ b/cocos2d/core/platform/attribute.js
@@ -183,6 +183,13 @@ cc.Integer = 'Integer';
*/
cc.Float = 'Float';
+if (CC_EDITOR) {
+ JS.get(cc, 'Number', function () {
+ cc.warnID(3603);
+ return cc.Float;
+ });
+}
+
/**
* Indicates that the elements in array should be type boolean.
* @property {string} Boolean