-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
trail.ts
704 lines (639 loc) · 27 KB
/
trail.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/**
* @category particle
*/
import { Material } from '../../core/assets/material';
import { RenderingSubMesh } from '../../core/assets/mesh';
import { ccclass, property, tooltip, displayOrder, type, visible } from '../../core/data/class-decorator';
import { director } from '../../core/director';
import { GFX_DRAW_INFO_SIZE, GFXBuffer, IGFXIndirectBuffer } from '../../core/gfx/buffer';
import { GFXAttributeName, GFXBufferUsageBit, GFXFormat, GFXFormatInfos, GFXMemoryUsageBit, GFXPrimitiveMode } from '../../core/gfx/define';
import { GFXDevice } from '../../core/gfx/device';
import { IGFXAttribute } from '../../core/gfx/input-assembler';
import { Color, Mat4, Quat, toRadian, Vec3 } from '../../core/math';
import { Pool } from '../../core/memop';
import { Model } from '../../core/renderer';
import CurveRange from '../animator/curve-range';
import GradientRange from '../animator/gradient-range';
import { Space, TextureMode, TrailMode } from '../enum';
import { Particle } from '../particle';
import { legacyCC } from '../../core/global-exports';
// tslint:disable: max-line-length
const PRE_TRIANGLE_INDEX = 1;
const NEXT_TRIANGLE_INDEX = 1 << 2;
const DIRECTION_THRESHOLD = Math.cos(toRadian(100));
const _temp_trailEle = { position: new Vec3(), velocity: new Vec3() } as ITrailElement;
const _temp_quat = new Quat();
const _temp_xform = new Mat4();
const _temp_vec3 = new Vec3();
const _temp_vec3_1 = new Vec3();
const _temp_color = new Color();
const barycentric = [1, 0, 0, 0, 1, 0, 0, 0, 1]; // <wireframe debug>
// tslint:disable-next-line: prefer-const
let _bcIdx = 0;
interface ITrailElement {
position: Vec3;
lifetime: number;
width: number;
velocity: Vec3;
direction: number; // if one element's direction differs from the previous one,it means the trail's direction reverse.
color: Color;
}
// the valid element is in [start,end) range.if start equals -1,it represents the array is empty.
class TrailSegment {
public start: number;
public end: number;
public trailElements: ITrailElement[];
constructor (maxTrailElementNum: number) {
this.start = -1;
this.end = -1;
this.trailElements = [];
while (maxTrailElementNum--) {
this.trailElements.push({
position: new Vec3(),
lifetime: 0,
width: 0,
velocity: new Vec3(),
direction: 0,
color: new Color(),
});
}
}
public getElement (idx: number) {
if (this.start === -1) {
return null;
}
if (idx < 0) {
idx = (idx + this.trailElements.length) % this.trailElements.length;
}
if (idx >= this.trailElements.length) {
idx %= this.trailElements.length;
}
return this.trailElements[idx];
}
public addElement (): ITrailElement {
if (this.trailElements.length === 0) {
return null as any;
}
if (this.start === -1) {
this.start = 0;
this.end = 1;
return this.trailElements[0];
}
if (this.start === this.end) {
this.trailElements.splice(this.end, 0, {
position: new Vec3(),
lifetime: 0,
width: 0,
velocity: new Vec3(),
direction: 0,
color: new Color(),
});
this.start++;
this.start %= this.trailElements.length;
}
const newEleLoc = this.end++;
this.end %= this.trailElements.length;
return this.trailElements[newEleLoc];
}
public iterateElement (target: object, f: (target: object, e: ITrailElement, p: Particle, dt: number) => boolean, p: Particle, dt: number) {
const end = this.start >= this.end ? this.end + this.trailElements.length : this.end;
for (let i = this.start; i < end; i++) {
if (f(target, this.trailElements[i % this.trailElements.length], p, dt)) {
this.start++;
this.start %= this.trailElements.length;
}
}
if (this.start === end) {
this.start = -1;
this.end = -1;
}
}
public count () {
if (this.start < this.end) {
return this.end - this.start;
} else {
return this.trailElements.length + this.end - this.start;
}
}
public clear () {
this.start = -1;
this.end = -1;
}
// <debug>
// public _print () {
// let msg = String();
// this.iterateElement(this, (target: object, e: ITrailElement, p: Particle, dt: number) => {
// msg += 'pos:' + e.position.toString() + ' lifetime:' + e.lifetime + ' dir:' + e.direction + ' velocity:' + e.velocity.toString() + '\n';
// return false;
// }, null, 0);
// console.log(msg);
// }
}
@ccclass('cc.TrailModule')
export default class TrailModule {
/**
* 是否启用。
*/
@displayOrder(0)
public get enable () {
return this._enable;
}
public set enable (val) {
if (val === this._enable && this._trailModel) {
return;
}
if (val && !this._enable) {
this._enable = val;
this._particleSystem.processor.updateTrailMaterial();
}
if (val && !this._trailModel) {
this._createModel();
this.rebuild();
}
this._enable = val;
if (this._trailModel) {
this._trailModel.enabled = val;
}
val ? this.onEnable() : this.onDisable();
}
@property
public _enable = false;
/**
* 设定粒子生成轨迹的方式。
*/
@type(TrailMode)
@displayOrder(1)
@tooltip('Particle在每个粒子的运动轨迹上形成拖尾效果')
public mode = TrailMode.Particles;
/**
* 轨迹存在的生命周期。
*/
@type(CurveRange)
@displayOrder(3)
@tooltip('拖尾的生命周期')
public lifeTime = new CurveRange();
@property
public _minParticleDistance = 0.1;
/**
* 每个轨迹粒子之间的最小间距。
*/
@property
@displayOrder(5)
@tooltip('粒子每生成一个拖尾节点所运行的最短距离')
public get minParticleDistance () {
return this._minParticleDistance;
}
public set minParticleDistance (val) {
this._minParticleDistance = val;
this._minSquaredDistance = val * val;
}
@type(Space)
@displayOrder(6)
@tooltip('拖尾所在的坐标系,World在世界坐标系中运行,Local在本地坐标系中运行')
public get space () {
return this._space;
}
public set space (val) {
this._space = val;
if (this._particleSystem) {
this._particleSystem.processor.updateTrailMaterial();
}
}
/**
* 粒子本身是否存在。
*/
@property
@visible(false)
@displayOrder(7)
@tooltip('拖尾是否跟随粒子一起消失')
public existWithParticles = true;
/**
* 设定纹理填充方式。
*/
@type(TextureMode)
@displayOrder(8)
@tooltip('贴图在拖尾上的展开形式,Stretch贴图覆盖在整条拖尾上,Repeat贴图覆盖在一段拖尾上')
public textureMode = TextureMode.Stretch;
@property
@displayOrder(9)
@tooltip('拖尾宽度继承自粒子大小')
public widthFromParticle = true;
/**
* 控制轨迹长度的曲线。
*/
@type(CurveRange)
@displayOrder(10)
@tooltip('拖尾宽度,如果继承自粒子则是粒子大小的比例')
public widthRatio = new CurveRange();
@property
@displayOrder(11)
@tooltip('拖尾颜色是否继承自粒子')
public colorFromParticle = false;
@type(GradientRange)
@displayOrder(12)
@tooltip('拖尾颜色随拖尾自身长度的颜色渐变')
public colorOverTrail = new GradientRange();
@type(GradientRange)
@displayOrder(13)
@tooltip('拖尾颜色随时间的颜色渐变')
public colorOvertime = new GradientRange();
/**
* 轨迹设定时的坐标系。
*/
@type(Space)
private _space = Space.World;
@property
private _particleSystem: any = null;
private _minSquaredDistance: number = 0;
private _vertSize: number;
private _trailNum: number = 0;
private _trailLifetime: number = 0;
private vbOffset: number = 0;
private ibOffset: number = 0;
private _trailSegments: Pool<TrailSegment> | null = null;
private _particleTrail: Map<Particle, TrailSegment>;
private _trailModel: Model | null = null;
private _iaInfo: IGFXIndirectBuffer;
private _iaInfoBuffer: GFXBuffer | null = null;
private _subMeshData: RenderingSubMesh | null = null;
private _vertAttrs: IGFXAttribute[];
private _vbF32: Float32Array | null = null;
private _vbUint32: Uint32Array | null = null;
private _iBuffer: Uint16Array | null = null;
private _needTransform: boolean = false;
private _material: Material | null = null;
constructor () {
this._iaInfo = {
drawInfos: [{
vertexCount: 0,
firstVertex: 0,
indexCount: 0,
firstIndex: 0,
vertexOffset: 0,
instanceCount: 0,
firstInstance: 0,
}],
};
this._vertAttrs = [
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // xyz:position
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGBA32F }, // x:index y:size zw:texcoord
// { name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // <wireframe debug>
{ name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // xyz:velocity
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true },
];
this._vertSize = 0;
for (const a of this._vertAttrs) {
this._vertSize += GFXFormatInfos[a.format].size;
}
this._particleTrail = new Map<Particle, TrailSegment>();
}
public onInit (ps) {
this._particleSystem = ps;
this.minParticleDistance = this._minParticleDistance;
let burstCount = 0;
const psTime = ps.startLifetime.getMax();
const psRate = ps.rateOverTime.getMax();
const duration = ps.duration;
for (let i = 0, len = ps.bursts.length; i < len; i++) {
const b = ps.bursts[i];
burstCount += b.getMaxCount(ps) * Math.ceil(psTime / duration);
}
this._trailNum = Math.ceil(psTime * this.lifeTime.getMax() * 60 * (psRate * duration + burstCount));
this._trailSegments = new Pool(() => new TrailSegment(10), Math.ceil(psRate * duration));
if (this._enable) {
this.enable = this._enable;
}
}
public onEnable () {
this._attachToScene();
}
public onDisable () {
this._particleTrail.clear();
this._detachFromScene();
}
public _attachToScene () {
if (this._trailModel) {
if (this._trailModel.scene) {
this._detachFromScene();
}
this._particleSystem._getRenderScene().addModel(this._trailModel);
}
}
public _detachFromScene () {
if (this._trailModel && this._trailModel.scene) {
this._trailModel.scene.removeModel(this._trailModel);
}
}
public destroy () {
this.destroySubMeshData();
if (this._trailModel) {
director.root!.destroyModel(this._trailModel);
this._trailModel = null;
}
if (this._trailSegments) {
this._trailSegments.destroy((obj: TrailSegment) => { obj.trailElements.length = 0; });
this._trailSegments = null;
}
}
public clear () {
if (this.enable) {
const trailIter = this._particleTrail.values();
let trail = trailIter.next();
while (!trail.done) {
trail.value.clear();
trail = trailIter.next();
}
this._particleTrail.clear();
this.updateRenderData();
}
}
public updateMaterial () {
if (this._particleSystem) {
this._material = this._particleSystem.getMaterialInstance(1) || this._particleSystem.processor._defaultTrailMat;
if (this._trailModel) {
this._trailModel.setSubModelMaterial(0, this._material!);
}
}
}
public update () {
this._trailLifetime = this.lifeTime.evaluate(this._particleSystem._time, 1)!;
if (this.space === Space.World && this._particleSystem._simulationSpace === Space.Local) {
this._needTransform = true;
this._particleSystem.node.getWorldMatrix(_temp_xform);
this._particleSystem.node.getWorldRotation(_temp_quat);
} else {
this._needTransform = false;
}
}
public animate (p: Particle, scaledDt: number) {
if (!this._trailSegments) {
return;
}
let trail = this._particleTrail.get(p);
if (!trail) {
trail = this._trailSegments.alloc();
this._particleTrail.set(p, trail);
// Avoid position and trail are one frame apart at the end of the particle animation.
return;
}
let lastSeg = trail.getElement(trail.end - 1);
if (this._needTransform) {
Vec3.transformMat4(_temp_vec3, p.position, _temp_xform);
} else {
Vec3.copy(_temp_vec3, p.position);
}
if (lastSeg) {
trail.iterateElement(this, this._updateTrailElement, p, scaledDt);
if (Vec3.squaredDistance(lastSeg.position, _temp_vec3) < this._minSquaredDistance) {
return;
}
}
lastSeg = trail.addElement();
if (!lastSeg) {
return;
}
Vec3.copy(lastSeg.position, _temp_vec3);
lastSeg.lifetime = 0;
if (this.widthFromParticle) {
lastSeg.width = p.size.x * this.widthRatio.evaluate(0, 1)!;
} else {
lastSeg.width = this.widthRatio.evaluate(0, 1)!;
}
const trailNum = trail.count();
if (trailNum === 2) {
const lastSecondTrail = trail.getElement(trail.end - 2)!;
Vec3.subtract(lastSecondTrail.velocity, lastSeg.position, lastSecondTrail.position);
} else if (trailNum > 2) {
const lastSecondTrail = trail.getElement(trail.end - 2)!;
const lastThirdTrail = trail.getElement(trail.end - 3)!;
Vec3.subtract(_temp_vec3, lastThirdTrail.position, lastSecondTrail.position);
Vec3.subtract(_temp_vec3_1, lastSeg.position, lastSecondTrail.position);
Vec3.subtract(lastSecondTrail.velocity, _temp_vec3_1, _temp_vec3);
if (Vec3.equals(Vec3.ZERO, lastSecondTrail.velocity)) {
Vec3.copy(lastSecondTrail.velocity, _temp_vec3);
}
Vec3.normalize(lastSecondTrail.velocity, lastSecondTrail.velocity);
this._checkDirectionReverse(lastSecondTrail, lastThirdTrail);
}
if (this.colorFromParticle) {
lastSeg.color.set(p.color);
} else {
lastSeg.color.set(this.colorOvertime.evaluate(0, 1));
}
}
public removeParticle (p: Particle) {
const trail = this._particleTrail.get(p);
if (trail && this._trailSegments) {
trail.clear();
this._trailSegments.free(trail);
this._particleTrail.delete(p);
}
}
public updateRenderData () {
this.vbOffset = 0;
this.ibOffset = 0;
for (const p of this._particleTrail.keys()) {
const trailSeg = this._particleTrail.get(p)!;
if (trailSeg.start === -1) {
continue;
}
const indexOffset = this.vbOffset * 4 / this._vertSize;
const end = trailSeg.start >= trailSeg.end ? trailSeg.end + trailSeg.trailElements.length : trailSeg.end;
const trailNum = end - trailSeg.start;
// const lastSegRatio = vec3.distance(trailSeg.getTailElement()!.position, p.position) / this._minParticleDistance;
const textCoordSeg = 1 / (trailNum /*- 1 + lastSegRatio*/);
const startSegEle = trailSeg.trailElements[trailSeg.start];
this._fillVertexBuffer(startSegEle, this.colorOverTrail.evaluate(1, 1), indexOffset, 1, 0, NEXT_TRIANGLE_INDEX);
for (let i = trailSeg.start + 1; i < end; i++) {
const segEle = trailSeg.trailElements[i % trailSeg.trailElements.length];
const j = i - trailSeg.start;
this._fillVertexBuffer(segEle, this.colorOverTrail.evaluate(1 - j / trailNum, 1), indexOffset, 1 - j * textCoordSeg, j, PRE_TRIANGLE_INDEX | NEXT_TRIANGLE_INDEX);
}
if (this._needTransform) {
Vec3.transformMat4(_temp_trailEle.position, p.position, _temp_xform);
} else {
Vec3.copy(_temp_trailEle.position, p.position);
}
if (trailNum === 1 || trailNum === 2) {
const lastSecondTrail = trailSeg.getElement(trailSeg.end - 1)!;
Vec3.subtract(lastSecondTrail.velocity, _temp_trailEle.position, lastSecondTrail.position);
this._vbF32![this.vbOffset - this._vertSize / 4 - 4] = lastSecondTrail.velocity.x;
this._vbF32![this.vbOffset - this._vertSize / 4 - 3] = lastSecondTrail.velocity.y;
this._vbF32![this.vbOffset - this._vertSize / 4 - 2] = lastSecondTrail.velocity.z;
this._vbF32![this.vbOffset - 4] = lastSecondTrail.velocity.x;
this._vbF32![this.vbOffset - 3] = lastSecondTrail.velocity.y;
this._vbF32![this.vbOffset - 2] = lastSecondTrail.velocity.z;
Vec3.subtract(_temp_trailEle.velocity, _temp_trailEle.position, lastSecondTrail.position);
this._checkDirectionReverse(_temp_trailEle, lastSecondTrail);
} else if (trailNum > 2) {
const lastSecondTrail = trailSeg.getElement(trailSeg.end - 1)!;
const lastThirdTrail = trailSeg.getElement(trailSeg.end - 2)!;
Vec3.subtract(_temp_vec3, lastThirdTrail.position, lastSecondTrail.position);
Vec3.subtract(_temp_vec3_1, _temp_trailEle.position, lastSecondTrail.position);
Vec3.normalize(_temp_vec3, _temp_vec3);
Vec3.normalize(_temp_vec3_1, _temp_vec3_1);
Vec3.subtract(lastSecondTrail.velocity, _temp_vec3_1, _temp_vec3);
Vec3.normalize(lastSecondTrail.velocity, lastSecondTrail.velocity);
this._checkDirectionReverse(lastSecondTrail, lastThirdTrail);
// refresh last trail segment data
this.vbOffset -= this._vertSize / 4 * 2;
this.ibOffset -= 6;
// _bcIdx = (_bcIdx - 6 + 9) % 9; // <wireframe debug>
this._fillVertexBuffer(lastSecondTrail, this.colorOverTrail.evaluate(textCoordSeg, 1), indexOffset, textCoordSeg, trailNum - 1, PRE_TRIANGLE_INDEX | NEXT_TRIANGLE_INDEX);
Vec3.subtract(_temp_trailEle.velocity, _temp_trailEle.position, lastSecondTrail.position);
Vec3.normalize(_temp_trailEle.velocity, _temp_trailEle.velocity);
this._checkDirectionReverse(_temp_trailEle, lastSecondTrail);
}
if (this.widthFromParticle) {
_temp_trailEle.width = p.size.x * this.widthRatio.evaluate(0, 1)!;
} else {
_temp_trailEle.width = this.widthRatio.evaluate(0, 1)!;
}
_temp_trailEle.color = p.color;
if (Vec3.equals(_temp_trailEle.velocity, Vec3.ZERO)) {
this.ibOffset -= 3;
} else {
this._fillVertexBuffer(_temp_trailEle, this.colorOverTrail.evaluate(0, 1), indexOffset, 0, trailNum, PRE_TRIANGLE_INDEX);
}
}
this.updateIA(this.ibOffset);
}
public updateIA (count: number) {
const subModels = this._trailModel && this._trailModel.subModels;
if (subModels && subModels.length > 0) {
const subModel = subModels[0];
subModel.inputAssembler!.vertexBuffers[0].update(this._vbF32!);
subModel.inputAssembler!.indexBuffer!.update(this._iBuffer!);
this._iaInfo.drawInfos[0].firstIndex = 0;
this._iaInfo.drawInfos[0].indexCount = count;
this._iaInfoBuffer!.update(this._iaInfo);
}
}
private _createModel () {
if (this._trailModel) {
return;
}
this._trailModel = legacyCC.director.root.createModel(Model);
}
private rebuild () {
const device: GFXDevice = director.root!.device;
const vertexBuffer = device.createBuffer({
usage: GFXBufferUsageBit.VERTEX | GFXBufferUsageBit.TRANSFER_DST,
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
size: this._vertSize * (this._trailNum + 1) * 2,
stride: this._vertSize,
});
const vBuffer: ArrayBuffer = new ArrayBuffer(this._vertSize * (this._trailNum + 1) * 2);
this._vbF32 = new Float32Array(vBuffer);
this._vbUint32 = new Uint32Array(vBuffer);
vertexBuffer.update(vBuffer);
const indexBuffer = device.createBuffer({
usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST,
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
size: this._trailNum * 6 * Uint16Array.BYTES_PER_ELEMENT,
stride: Uint16Array.BYTES_PER_ELEMENT,
});
this._iBuffer = new Uint16Array(this._trailNum * 6);
indexBuffer.update(this._iBuffer);
this._iaInfoBuffer = device.createBuffer({
usage: GFXBufferUsageBit.INDIRECT,
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
size: GFX_DRAW_INFO_SIZE,
stride: GFX_DRAW_INFO_SIZE,
});
this._iaInfo.drawInfos[0].vertexCount = (this._trailNum + 1) * 2;
this._iaInfo.drawInfos[0].indexCount = this._trailNum * 6;
this._iaInfoBuffer.update(this._iaInfo);
this._subMeshData = new RenderingSubMesh([vertexBuffer], this._vertAttrs!, GFXPrimitiveMode.TRIANGLE_LIST);
this._subMeshData.indexBuffer = indexBuffer;
this._subMeshData.indirectBuffer = this._iaInfoBuffer;
this._trailModel!.initialize(this._particleSystem.node);
this._trailModel!.visFlags = this._particleSystem.visibility;
this._trailModel!.initSubModel(0, this._subMeshData, this._material!);
this._trailModel!.enabled = true;
}
private _updateTrailElement (module: any, trailEle: ITrailElement, p: Particle, dt: number): boolean {
trailEle.lifetime += dt;
if (module.colorFromParticle) {
trailEle.color.set(p.color);
trailEle.color.multiply(module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1));
} else {
trailEle.color.set(module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1));
}
if (module.widthFromParticle) {
trailEle.width = p.size.x * module.widthRatio.evaluate(trailEle.lifetime / module._trailLifetime, 1)!;
} else {
trailEle.width = module.widthRatio.evaluate(trailEle.lifetime / module._trailLifetime, 1)!;
}
return trailEle.lifetime > module._trailLifetime;
}
private _fillVertexBuffer (trailSeg: ITrailElement, colorModifer: Color, indexOffset: number, xTexCoord: number, trailEleIdx: number, indexSet: number) {
this._vbF32![this.vbOffset++] = trailSeg.position.x;
this._vbF32![this.vbOffset++] = trailSeg.position.y;
this._vbF32![this.vbOffset++] = trailSeg.position.z;
this._vbF32![this.vbOffset++] = trailSeg.direction;
this._vbF32![this.vbOffset++] = trailSeg.width;
this._vbF32![this.vbOffset++] = xTexCoord;
this._vbF32![this.vbOffset++] = 0;
// this._vbF32![this.vbOffset++] = barycentric[_bcIdx++]; // <wireframe debug>
// this._vbF32![this.vbOffset++] = barycentric[_bcIdx++];
// this._vbF32![this.vbOffset++] = barycentric[_bcIdx++];
// _bcIdx %= 9;
this._vbF32![this.vbOffset++] = trailSeg.velocity.x;
this._vbF32![this.vbOffset++] = trailSeg.velocity.y;
this._vbF32![this.vbOffset++] = trailSeg.velocity.z;
_temp_color.set(trailSeg.color);
_temp_color.multiply(colorModifer);
this._vbUint32![this.vbOffset++] = _temp_color._val;
this._vbF32![this.vbOffset++] = trailSeg.position.x;
this._vbF32![this.vbOffset++] = trailSeg.position.y;
this._vbF32![this.vbOffset++] = trailSeg.position.z;
this._vbF32![this.vbOffset++] = 1 - trailSeg.direction;
this._vbF32![this.vbOffset++] = trailSeg.width;
this._vbF32![this.vbOffset++] = xTexCoord;
this._vbF32![this.vbOffset++] = 1;
// this._vbF32![this.vbOffset++] = barycentric[_bcIdx++]; // <wireframe debug>
// this._vbF32![this.vbOffset++] = barycentric[_bcIdx++];
// this._vbF32![this.vbOffset++] = barycentric[_bcIdx++];
// _bcIdx %= 9;
this._vbF32![this.vbOffset++] = trailSeg.velocity.x;
this._vbF32![this.vbOffset++] = trailSeg.velocity.y;
this._vbF32![this.vbOffset++] = trailSeg.velocity.z;
this._vbUint32![this.vbOffset++] = _temp_color._val;
if (indexSet & PRE_TRIANGLE_INDEX) {
this._iBuffer![this.ibOffset++] = indexOffset + 2 * trailEleIdx;
this._iBuffer![this.ibOffset++] = indexOffset + 2 * trailEleIdx - 1;
this._iBuffer![this.ibOffset++] = indexOffset + 2 * trailEleIdx + 1;
}
if (indexSet & NEXT_TRIANGLE_INDEX) {
this._iBuffer![this.ibOffset++] = indexOffset + 2 * trailEleIdx;
this._iBuffer![this.ibOffset++] = indexOffset + 2 * trailEleIdx + 1;
this._iBuffer![this.ibOffset++] = indexOffset + 2 * trailEleIdx + 2;
}
}
private _checkDirectionReverse (currElement: ITrailElement, prevElement: ITrailElement) {
if (Vec3.dot(currElement.velocity, prevElement.velocity) < DIRECTION_THRESHOLD) {
currElement.direction = 1 - prevElement.direction;
} else {
currElement.direction = prevElement.direction;
}
}
private destroySubMeshData () {
if (this._subMeshData) {
this._subMeshData.destroy();
this._subMeshData = null;
}
}
// <debug use>
// private _printVB() {
// let log = new String();
// for (let i = 0; i < this.vbOffset; i++) {
// log += 'pos:' + this._vbF32![i++].toFixed(2) + ',' + this._vbF32![i++].toFixed(2) + ',' + this._vbF32![i++].toFixed(2) + ' dir:' + this._vbF32![i++].toFixed(0) + ' ';
// i += 6;
// log += 'vel:' + this._vbF32![i++].toFixed(2) + ',' + this._vbF32![i++].toFixed(2) + ',' + this._vbF32![i++].toFixed(2) + '\n';
// }
// if (log.length > 0) {
// console.log(log);
// }
// }
}