difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
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
/****************************************************************************
 Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
 
 https://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.
 Xiamen Yaji Software Co., Ltd. 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.
 ****************************************************************************/
 
const AffineTrans = require('../core/utils/affine-transform');
const js = require('../core/platform/js');
const misc = require('../core/utils/misc');
 
const ZERO_VEC2 = cc.v2(0, 0);
 
let _trans = AffineTrans.create();
let _pos = cc.v2();
let _tpa = cc.v2();
let _tpb = cc.v2();
let _tpc = cc.v2();
 
let Particle = function () {
    this.pos = cc.v2(0, 0);
    this.startPos = cc.v2(0, 0);
    this.color = cc.color(0, 0, 0, 255);
    this.deltaColor = {r: 0, g: 0, b: 0, a: 255};
    this.size = 0;
    this.deltaSize = 0;
    this.rotation = 0;
    this.deltaRotation = 0;
    this.timeToLive = 0;
    this.drawPos = cc.v2(0, 0);
    // Mode A
    this.dir = cc.v2(0, 0);
    this.radialAccel = 0;
    this.tangentialAccel = 0;
    // Mode B
    this.angle = 0;
    this.degreesPerSecond = 0;
    this.radius = 0;
    this.deltaRadius = 0;
}
 
let pool = new js.Pool(function (par) {
    par.pos.set(ZERO_VEC2);
    par.startPos.set(ZERO_VEC2);
    par.color._val = 0xFF000000;
    par.deltaColor.r = par.deltaColor.g = par.deltaColor.b = 0;
    par.deltaColor.a = 255;
    par.size = 0;
    par.deltaSize = 0;
    par.rotation = 0;
    par.deltaRotation = 0;
    par.timeToLive = 0;
    par.drawPos.set(ZERO_VEC2);
    // Mode A
    par.dir.set(ZERO_VEC2);
    par.radialAccel = 0;
    par.tangentialAccel = 0;
    // Mode B
    par.angle = 0;
    par.degreesPerSecond = 0;
    par.radius = 0;
    par.deltaRadius = 0;
}, 1024);
pool.get = function () {
    return this._get() || new Particle();
}
 
let Simulator = function (system) {
    this.sys = system;
    this.particles = [];
    this.active = false;
    this.finished = false;
    this.elapsed = 0;
    this.emitCounter = 0;
    this._uvFilled = 0;
}
 
Simulator.prototype.stop = function () {
    this.active = false;
    this.elapsed = this.sys.duration;
    this.emitCounter = 0;
}
 
Simulator.prototype.reset = function () {
    this.active = true;
    this.elapsed = 0;
    this.emitCounter = 0;
    this.finished = false;
    let particles = this.particles;
    for (let id = 0; id < particles.length; ++id)
        pool.put(particles[id]);
    particles.length = 0;
}
 
Simulator.prototype.emitParticle = function (pos) {
    let psys = this.sys;
    let clampf = misc.clampf;
    let particle = pool.get();
    this.particles.push(particle);
 
    // Init particle
    // timeToLive
    // no negative life. prevent division by 0
    particle.timeToLive = psys.life + psys.lifeVar * (Math.random() - 0.5) * 2;
    let timeToLive = particle.timeToLive = Math.max(0, particle.timeToLive);
 
    // position
    particle.pos.x = psys.sourcePos.x + psys.posVar.x * (Math.random() - 0.5) * 2;
    particle.pos.y = psys.sourcePos.y + psys.posVar.y * (Math.random() - 0.5) * 2;
 
    // Color
    let sr, sg, sb, sa;
    let startColor = psys._startColor, startColorVar = psys._startColorVar;
    let endColor = psys._endColor, endColorVar = psys._endColorVar;
    particle.color.r = sr = clampf(startColor.r + startColorVar.r * (Math.random() - 0.5) * 2, 0, 255);
    particle.color.g = sg = clampf(startColor.g + startColorVar.g * (Math.random() - 0.5) * 2, 0, 255);
    particle.color.b = sb = clampf(startColor.b + startColorVar.b * (Math.random() - 0.5) * 2, 0, 255);
    particle.color.a = sa = clampf(startColor.a + startColorVar.a * (Math.random() - 0.5) * 2, 0, 255);
    particle.deltaColor.r = (clampf(endColor.r + endColorVar.r * (Math.random() - 0.5) * 2, 0, 255) - sr) / timeToLive;
    particle.deltaColor.g = (clampf(endColor.g + endColorVar.g * (Math.random() - 0.5) * 2, 0, 255) - sg) / timeToLive;
    particle.deltaColor.b = (clampf(endColor.b + endColorVar.b * (Math.random() - 0.5) * 2, 0, 255) - sb) / timeToLive;
    particle.deltaColor.a = (clampf(endColor.a + endColorVar.a * (Math.random() - 0.5) * 2, 0, 255) - sa) / timeToLive;
 
    // size
    let startS = psys.startSize + psys.startSizeVar * (Math.random() - 0.5) * 2;
    startS = Math.max(0, startS); // No negative value
    particle.size = startS;
    if (psys.endSize === cc.ParticleSystem.START_SIZE_EQUAL_TO_END_SIZE) {
        particle.deltaSize = 0;
    } else {
        var endS = psys.endSize + psys.endSizeVar * (Math.random() - 0.5) * 2;
        endS = Math.max(0, endS); // No negative values
        particle.deltaSize = (endS - startS) / timeToLive;
    }
 
    // rotation
    var startA = psys.startSpin + psys.startSpinVar * (Math.random() - 0.5) * 2;
    var endA = psys.endSpin + psys.endSpinVar * (Math.random() - 0.5) * 2;
    particle.rotation = startA;
    particle.deltaRotation = (endA - startA) / timeToLive;
 
    // position
    particle.startPos.x = pos.x;
    particle.startPos.y = pos.y;
 
    // direction
    let a = misc.degreesToRadians(psys.angle + psys.angleVar * (Math.random() - 0.5) * 2); 
    // Mode Gravity: A
    if (psys.emitterMode === cc.ParticleSystem.EmitterMode.GRAVITY) {
        let s = psys.speed + psys.speedVar * (Math.random() - 0.5) * 2;
        // direction
        particle.dir.x = Math.cos(a);
        particle.dir.y = Math.sin(a);
        particle.dir.mulSelf(s);
        // radial accel
        particle.radialAccel = psys.radialAccel + psys.radialAccelVar * (Math.random() - 0.5) * 2;
        // tangential accel
        particle.tangentialAccel = psys.tangentialAccel + psys.tangentialAccelVar * (Math.random() - 0.5) * 2;
        // rotation is dir
        if (psys.rotationIsDir) {
            particle.rotation = -misc.radiansToDegrees(Math.atan2(particle.dir.y, particle.dir.x));
        }
    }
    // Mode Radius: B
    else {
        // Set the default diameter of the particle from the source position
        var startRadius = psys.startRadius + psys.startRadiusVar * (Math.random() - 0.5) * 2;
        var endRadius = psys.endRadius + psys.endRadiusVar * (Math.random() - 0.5) * 2;
        particle.radius = startRadius;
        particle.deltaRadius = (psys.endRadius === cc.ParticleSystem.START_RADIUS_EQUAL_TO_END_RADIUS) ? 0 : (endRadius - startRadius) / timeToLive;
        particle.angle = a;
        particle.degreesPerSecond = misc.degreesToRadians(psys.rotatePerS + psys.rotatePerSVar * (Math.random() - 0.5) * 2);
    }
};
 
Simulator.prototype.updateUVs = function (force) {
    let particleCount = this.particles.length;
    if (this.sys._buffer && this.sys._renderSpriteFrame) {
        const FLOAT_PER_PARTICLE = 4 * this.sys._vertexFormat._bytes / 4;
        let vbuf = this.sys._buffer._vData;
        let uv = this.sys._renderSpriteFrame.uv;
 
        let start = force ? 0 : this._uvFilled;
        for (let i = start; i < particleCount; i++) {
            let offset = i * FLOAT_PER_PARTICLE;
            vbuf[offset+2] = uv[0];
            vbuf[offset+3] = uv[1];
            vbuf[offset+7] = uv[2];
            vbuf[offset+8] = uv[3];
            vbuf[offset+12] = uv[4];
            vbuf[offset+13] = uv[5];
            vbuf[offset+17] = uv[6];
            vbuf[offset+18] = uv[7];
        }
        this._uvFilled = particleCount;
    }
}
 
Simulator.prototype.updateParticleBuffer = function (particle, pos, buffer, offset) {
    let vbuf = buffer._vData;
    let uintbuf = buffer._uintVData;
    
    let x = pos.x, y = pos.y;
    let size_2 = particle.size / 2;
    // pos
    if (particle.rotation) {
        let x1 = -size_2, y1 = -size_2;
        let x2 = size_2, y2 = size_2;
        let rad = -misc.degreesToRadians(particle.rotation);
        let cr = Math.cos(rad), sr = Math.sin(rad);
        // bl
        vbuf[offset] = x1 * cr - y1 * sr + x;
        vbuf[offset+1] = x1 * sr + y1 * cr + y;
        // br
        vbuf[offset+5] = x2 * cr - y1 * sr + x;
        vbuf[offset+6] = x2 * sr + y1 * cr + y;
        // tl
        vbuf[offset+10] = x1 * cr - y2 * sr + x;
        vbuf[offset+11] = x1 * sr + y2 * cr + y;
        // tr
        vbuf[offset+15] = x2 * cr - y2 * sr + x;
        vbuf[offset+16] = x2 * sr + y2 * cr + y;
    }
    else {
        // bl
        vbuf[offset] = x - size_2;
        vbuf[offset+1] = y - size_2;
        // br
        vbuf[offset+5] = x + size_2;
        vbuf[offset+6] = y - size_2;
        // tl
        vbuf[offset+10] = x - size_2;
        vbuf[offset+11] = y + size_2;
        // tr
        vbuf[offset+15] = x + size_2;
        vbuf[offset+16] = y + size_2;
    }
    // color
    uintbuf[offset+4] = particle.color._val;
    uintbuf[offset+9] = particle.color._val;
    uintbuf[offset+14] = particle.color._val;
    uintbuf[offset+19] = particle.color._val;
};
 
Simulator.prototype.step = function (dt) {
    let psys = this.sys;
    let node = psys.node;
    let particles = this.particles;
    const FLOAT_PER_PARTICLE = 4 * psys._vertexFormat._bytes / 4;
 
    // Calculate pos
    node._updateWorldMatrix();
    AffineTrans.fromMat4(_trans, node._worldMatrix);
    if (psys.positionType === cc.ParticleSystem.PositionType.FREE) {
        AffineTrans.transformVec2(_pos, ZERO_VEC2, _trans);
    } else if (psys.positionType === cc.ParticleSystem.PositionType.RELATIVE) {
        _pos.x = node._position.x;
        _pos.y = node._position.y;
    }
 
    // Get world to node trans only once
    AffineTrans.invert(_trans, _trans);
    let worldToNodeTrans = _trans;
 
    // Emission
    if (this.active && psys.emissionRate) {
        var rate = 1.0 / psys.emissionRate;
        //issue #1201, prevent bursts of particles, due to too high emitCounter
        if (particles.length < psys.totalParticles)
            this.emitCounter += dt;
 
        while ((particles.length < psys.totalParticles) && (this.emitCounter > rate)) {
            this.emitParticle(_pos);
            this.emitCounter -= rate;
        }
 
        this.elapsed += dt;
        if (psys.duration !== -1 && psys.duration < this.elapsed) {
            psys.stopSystem();
        }
    }
 
    // Request buffer for particles
    let buffer = psys._buffer;
    let particleCount = particles.length;
    buffer.reset();
    buffer.request(particleCount * 4, particleCount * 6);
 
    // Fill up uvs
    if (particleCount > this._uvFilled) {
        this.updateUVs();
    }
 
    // Used to reduce memory allocation / creation within the loop
    let particleIdx = 0;
    while (particleIdx < particles.length) {
        // Reset temporary vectors
        _tpa.x = _tpa.y = _tpb.x = _tpb.y = _tpc.x = _tpc.y = 0;
 
        let particle = particles[particleIdx];
 
        // life
        particle.timeToLive -= dt;
        if (particle.timeToLive > 0) {
            // Mode A: gravity, direction, tangential accel & radial accel
            if (psys.emitterMode === cc.ParticleSystem.EmitterMode.GRAVITY) {
                let tmp = _tpc, radial = _tpa, tangential = _tpb;
 
                // radial acceleration
                if (particle.pos.x || particle.pos.y) {
                    radial.set(particle.pos);
                    radial.normalizeSelf();
                }
                tangential.set(radial);
                radial.mulSelf(particle.radialAccel);
 
                // tangential acceleration
                let newy = tangential.x;
                tangential.x = -tangential.y;
                tangential.y = newy;
 
                tangential.mulSelf(particle.tangentialAccel);
 
                tmp.set(radial);
                tmp.addSelf(tangential);
                tmp.addSelf(psys.gravity);
                tmp.mulSelf(dt);
                particle.dir.addSelf(tmp);
 
                tmp.set(particle.dir);
                tmp.mulSelf(dt);
                particle.pos.addSelf(tmp);
            }
            // Mode B: radius movement
            else {
                // Update the angle and radius of the particle.
                particle.angle += particle.degreesPerSecond * dt;
                particle.radius += particle.deltaRadius * dt;
 
                particle.pos.x = -Math.cos(particle.angle) * particle.radius;
                particle.pos.y = -Math.sin(particle.angle) * particle.radius;
            }
 
            // color
            particle.color.r += particle.deltaColor.r * dt;
            particle.color.g += particle.deltaColor.g * dt;
            particle.color.b += particle.deltaColor.b * dt;
            particle.color.a += particle.deltaColor.a * dt;
 
            // size
            particle.size += particle.deltaSize * dt;
            if (particle.size < 0) {
                particle.size = 0;
            }
 
            // angle
            particle.rotation += particle.deltaRotation * dt;
 
            // update values in quad buffer
            let newPos = _tpa;
            if (psys.positionType === cc.ParticleSystem.PositionType.FREE || psys.positionType === cc.ParticleSystem.PositionType.RELATIVE) {
                let diff = _tpb, startPos = _tpc;
                // current Position convert To Node Space
                AffineTrans.transformVec2(diff, _pos, worldToNodeTrans);
                // start Position convert To Node Space
                AffineTrans.transformVec2(startPos, particle.startPos, worldToNodeTrans);
                diff.subSelf(startPos);
                newPos.set(particle.pos);
                newPos.subSelf(diff);
            } else {
                newPos.set(particle.pos);
            }
 
            let offset = FLOAT_PER_PARTICLE * particleIdx;
            this.updateParticleBuffer(particle, newPos, buffer, offset);
 
            // update particle counter
            ++particleIdx;
        } else {
            // life < 0
            let deadParticle = particles[particleIdx];
            if (particleIdx !== particles.length - 1) {
                particles[particleIdx] = particles[particles.length - 1];
            }
            pool.put(deadParticle);
            particles.length--;
        }
    }
 
    if (particles.length > 0) {
        buffer.uploadData();
        psys._ia._count = particles.length * 6;
    }
    else if (!this.active) {
        this.finished = true;
        psys._finishedSimulation();
    }
}
 
module.exports = Simulator;