Merge pull request #2304 from 2youyou2/next

optimize motion streak
This commit is contained in:
pandamicro
2018-02-02 12:52:46 +08:00
committed by GitHub
2 changed files with 162 additions and 109 deletions
@@ -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,
+5 -109
View File
@@ -59,9 +59,8 @@ var MotionStreak = cc.Class({
executeInEditMode: true
},
ctor: function() {
this._root = null;
this._motionStreak = null;
ctor () {
this._points = [];
},
properties: {
@@ -95,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'
@@ -212,7 +211,7 @@ var MotionStreak = cc.Class({
onEnable: function () {
this._super();
this._activateMaterial();
this._applyFadeTime();
this.reset();
},
_activateMaterial: function () {
@@ -233,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();
@@ -261,8 +253,7 @@ var MotionStreak = cc.Class({
* myParticleSystem.stopSystem();
*/
reset: function () {
this._numPoints = 0;
this._lastPoint = null;
this._points.length = 0;
let renderData = this._renderData;
if (renderData) {
renderData.dataLength = 0;
@@ -272,101 +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 lastPoint = this._lastPoint;
if (!lastPoint) {
lastPoint = this._lastPoint = cc.v2();
lastPoint.x = tx;
lastPoint.y = ty;
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) | 0;
if (seg === 0) {
return;
}
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 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 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;
}
let color = this._color,
cr = color.r,
cg = color.g,
cb = color.b,
ca = color.a;
for (let i = 0; i < curLength; i++) {
let u = 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;
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;
}
});