Repository navigation
Expand file tree
/
Copy pathParticleModifierLibrary.cpp
More file actions
432 lines (390 loc) · 14 KB
/
Copy pathParticleModifierLibrary.cpp
File metadata and controls
432 lines (390 loc) · 14 KB
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
// Runtime behaviour (ParticleModifierOps) for the built-in modifiers.
//
// The data structs live in ParticleNodes.h; this file supplies each one's
// callbacks and registers them by node-name hash (REGISTER_PARTICLE_MODIFIER).
// Per-emitter state goes in the chain's zeroed blob (ops.stateSize), never in
// the shared data struct: many emitters may run the same graph asset, and a
// rate accumulator in the struct would be one counter they all fight over.
//
// The graph is walked once, when the chain is built, and not touched again.
#include "ParticleNodes.h"
#include "ParticleModifierRegistry.h"
#include "ParticleEmitterComponent.h"
#include "ParticleMath.h"
#include <deki/Object.h>
#include <cmath>
#include <cstdint>
namespace DekiParticles
{
namespace
{
// The `enabled` reader every modifier node shares.
template <typename T>
bool NodeEnabled(const void* data)
{
return static_cast<const T*>(data)->enabled;
}
// ---------------------------------------------------------------------------
// Emission
// ---------------------------------------------------------------------------
// Per-emitter spawn bookkeeping. Plain data: the blob starts zeroed, and zero
// is the right starting value for all three.
struct EmissionState
{
float rateAccumulator;
float burstTimer;
bool firedFirstBurst;
};
void EmissionAttach(const void* /*data*/, void* state, ParticleEmitterComponent& /*emitter*/)
{
auto* s = static_cast<EmissionState*>(state);
s->rateAccumulator = 0.0f;
s->burstTimer = 0.0f;
s->firedFirstBurst = false;
}
void EmissionEmit(const void* data, void* /*state*/, ParticleEmitterComponent& emitter, int i)
{
const auto* d = static_cast<const ParticleEmissionNode*>(data);
// 1. Lifetime: random in [min, max], with a small positive minimum.
float tN = emitter.rng.NextFloat01();
float life = d->lifetimeMin + (d->lifetimeMax - d->lifetimeMin) * tN;
static const float kMinLife = 0.001f;
if (life < kMinLife)
{
life = kMinLife;
}
emitter.pool.lifetime[i] = life;
// 2. Spawn position: sample the shape in emitter-local space, then add
// the emitter's world origin when worldSpace is on, so the particle
// starts at the emitter.
float ox = 0.0f, oy = 0.0f;
switch (d->shape)
{
case EmitterShapeKind::Point: break;
case EmitterShapeKind::Circle:
{
// Uniform sample inside a disc: r = R*sqrt(u), theta = 2pi*v, in
// radians (engine convention).
float u = emitter.rng.NextFloat01();
float v = emitter.rng.NextFloat01();
float r = d->radius * std::sqrt(u);
float theta = v * Deki::Math::kTwoPi;
ox = r * std::cos(theta);
oy = r * std::sin(theta);
break;
}
case EmitterShapeKind::Rect:
{
float rx = emitter.rng.NextFloat01() - 0.5f;
float ry = emitter.rng.NextFloat01() - 0.5f;
ox = rx * d->width;
oy = ry * d->height;
break;
}
}
if (emitter.worldSpace && emitter.GetOwner())
{
Deki::Object* o = emitter.GetOwner();
emitter.pool.posX[i] = o->GetWorldX() + ox;
emitter.pool.posY[i] = o->GetWorldY() + oy;
}
else
{
emitter.pool.posX[i] = ox;
emitter.pool.posY[i] = oy;
}
}
void EmissionSimulate(const void* data, void* state, ParticleEmitterComponent& emitter, float dt)
{
const auto* d = static_cast<const ParticleEmissionNode*>(data);
auto* s = static_cast<EmissionState*>(state);
// Continuous emission, with an accumulator so fractional rates work.
if (d->emissionRate > 0.0f)
{
s->rateAccumulator += d->emissionRate * dt;
while (s->rateAccumulator >= 1.0f)
{
s->rateAccumulator -= 1.0f;
if (emitter.Spawn() < 0)
{
s->rateAccumulator = 0.0f; // pool full: drop pending spawns
break;
}
}
}
// Burst emission, independent of `emissionRate`. Both can run together
// for "ambient plus the occasional puff" (the inspector shows them as
// separate groups to make that clear).
if (d->burstCount > 0)
{
if (!s->firedFirstBurst)
{
for (int n = 0; n < d->burstCount; ++n)
{
if (emitter.Spawn() < 0)
{
break;
}
}
s->firedFirstBurst = true;
s->burstTimer = 0.0f;
}
else if (d->burstInterval > 0.0f)
{
s->burstTimer += dt;
while (s->burstTimer >= d->burstInterval)
{
s->burstTimer -= d->burstInterval;
for (int n = 0; n < d->burstCount; ++n)
{
if (emitter.Spawn() < 0)
{
break;
}
}
}
}
}
}
// ---------------------------------------------------------------------------
// Initial velocity
// ---------------------------------------------------------------------------
void InitialVelocityEmit(const void* data, void* /*state*/, ParticleEmitterComponent& emitter, int i)
{
const auto* d = static_cast<const ParticleInitialVelocityNode*>(data);
float speed = emitter.rng.NextFloatRange(d->speedMin, d->speedMax);
float angle = emitter.rng.NextFloatRange(d->angleMin, d->angleMax);
// Angle in radians (engine convention).
emitter.pool.velX[i] = speed * std::cos(angle);
emitter.pool.velY[i] = speed * std::sin(angle);
}
// ---------------------------------------------------------------------------
// Initial rotation
// ---------------------------------------------------------------------------
void InitialRotationAttach(const void* /*data*/, void* /*state*/, ParticleEmitterComponent& emitter)
{
emitter.pool.EnsureRotation();
}
void InitialRotationEmit(const void* data, void* /*state*/, ParticleEmitterComponent& emitter, int i)
{
const auto* d = static_cast<const ParticleInitialRotationNode*>(data);
// rotation/rotationSpeed are radians (engine convention).
emitter.pool.rotation[i] = emitter.rng.NextFloatRange(d->rotationMin, d->rotationMax);
emitter.pool.rotationSpeed[i] = emitter.rng.NextFloatRange(d->spinSpeedMin, d->spinSpeedMax);
}
void InitialRotationSimulate(const void* /*data*/, void* /*state*/, ParticleEmitterComponent& emitter, float dt)
{
// Integrate spin, so per-particle rotationSpeed works even without a
// Rotation over Lifetime node in the chain.
int n = emitter.pool.AliveCount();
float* rot = emitter.pool.rotation.Data();
float* spd = emitter.pool.rotationSpeed.Data();
for (int i = 0; i < n; ++i)
{
rot[i] += spd[i] * dt;
}
}
// ---------------------------------------------------------------------------
// Gravity
// ---------------------------------------------------------------------------
void GravitySimulate(const void* data, void* /*state*/, ParticleEmitterComponent& emitter, float dt)
{
const auto* d = static_cast<const ParticleGravityNode*>(data);
int n = emitter.pool.AliveCount();
float* vx = emitter.pool.velX.Data();
float* vy = emitter.pool.velY.Data();
// Fold dt into the delta once, outside the hot loop.
float dvx = d->gravityX * dt;
float dvy = d->gravityY * dt;
for (int i = 0; i < n; ++i)
{
vx[i] += dvx;
vy[i] += dvy;
}
}
// ---------------------------------------------------------------------------
// Drag
// ---------------------------------------------------------------------------
void DragSimulate(const void* data, void* /*state*/, ParticleEmitterComponent& emitter, float dt)
{
const auto* d = static_cast<const ParticleDragNode*>(data);
// k = max(0, 1 - drag*dt)
float k = 1.0f - d->drag * dt;
if (k < 0.0f)
{
k = 0.0f;
}
int n = emitter.pool.AliveCount();
float* vx = emitter.pool.velX.Data();
float* vy = emitter.pool.velY.Data();
for (int i = 0; i < n; ++i)
{
vx[i] *= k;
vy[i] *= k;
}
}
// ---------------------------------------------------------------------------
// Size over lifetime
// ---------------------------------------------------------------------------
void SizeOverLifetimeAttach(const void* /*data*/, void* /*state*/, ParticleEmitterComponent& emitter)
{
emitter.pool.EnsureScale();
}
void SizeOverLifetimeSimulate(const void* data, void* /*state*/, ParticleEmitterComponent& emitter, float /*dt*/)
{
const auto* d = static_cast<const ParticleSizeOverLifetimeNode*>(data);
int n = emitter.pool.AliveCount();
float* age = emitter.pool.age.Data();
float* life = emitter.pool.lifetime.Data();
float* sc = emitter.pool.scale.Data();
float s0 = d->sizeAt0;
float ds = d->sizeAt1 - d->sizeAt0;
for (int i = 0; i < n; ++i)
{
float t = (life[i] > 0.0f) ? (age[i] / life[i]) : 0.0f;
if (t > 1.0f)
{
t = 1.0f;
}
sc[i] = s0 + ds * t;
}
}
// ---------------------------------------------------------------------------
// Color over lifetime
// ---------------------------------------------------------------------------
void ColorOverLifetimeAttach(const void* /*data*/, void* /*state*/, ParticleEmitterComponent& emitter)
{
emitter.pool.EnsureTint();
}
void ColorOverLifetimeSimulate(const void* data, void* /*state*/, ParticleEmitterComponent& emitter, float /*dt*/)
{
const auto* d = static_cast<const ParticleColorOverLifetimeNode*>(data);
int n = emitter.pool.AliveCount();
float* age = emitter.pool.age.Data();
float* life = emitter.pool.lifetime.Data();
uint8_t* tR = emitter.pool.tintR.Data();
uint8_t* tG = emitter.pool.tintG.Data();
uint8_t* tB = emitter.pool.tintB.Data();
uint8_t* tA = emitter.pool.tintA.Data();
int r0 = d->colorAt0.r, g0 = d->colorAt0.g, b0 = d->colorAt0.b, a0 = d->colorAt0.a;
int dr = (int)d->colorAt1.r - r0;
int dg = (int)d->colorAt1.g - g0;
int db = (int)d->colorAt1.b - b0;
int da = (int)d->colorAt1.a - a0;
// 256 in float for the 8.8 lerp factor.
static const float k256 = 256.0f;
for (int i = 0; i < n; ++i)
{
float t = (life[i] > 0.0f) ? (age[i] / life[i]) : 0.0f;
if (t > 1.0f)
{
t = 1.0f;
}
// 8.8 fixed-point lerp, which keeps float-to-int conversions out of
// the loop on MCUs where they are slow.
int ti = static_cast<int>(t * k256);
tR[i] = (uint8_t)(r0 + ((dr * ti) >> 8));
tG[i] = (uint8_t)(g0 + ((dg * ti) >> 8));
tB[i] = (uint8_t)(b0 + ((db * ti) >> 8));
tA[i] = (uint8_t)(a0 + ((da * ti) >> 8));
}
}
// ---------------------------------------------------------------------------
// Rotation over lifetime
// ---------------------------------------------------------------------------
void RotationOverLifetimeAttach(const void* /*data*/, void* /*state*/, ParticleEmitterComponent& emitter)
{
emitter.pool.EnsureRotation();
}
void RotationOverLifetimeSimulate(const void* data, void* /*state*/, ParticleEmitterComponent& emitter, float dt)
{
const auto* d = static_cast<const ParticleRotationOverLifetimeNode*>(data);
int n = emitter.pool.AliveCount();
// rotation is in radians (engine convention) and speeds in radians/sec,
// so integrating is a plain accumulate.
float a0 = d->spinSpeedAt0;
float da = d->spinSpeedAt1 - a0;
float* age = emitter.pool.age.Data();
float* life = emitter.pool.lifetime.Data();
float* rot = emitter.pool.rotation.Data();
for (int i = 0; i < n; ++i)
{
float t = (life[i] > 0.0f) ? (age[i] / life[i]) : 0.0f;
if (t > 1.0f)
{
t = 1.0f;
}
float spd = a0 + da * t;
rot[i] += spd * dt;
}
}
} // namespace
// ---------------------------------------------------------------------------
// Registration (typeId = hash of the node name, as the graph loader stores it)
//
// Called from DekiParticlesInitSystem, not done by REGISTER_PARTICLE_MODIFIER
// statics: a firmware links the game from an archive, and the linker drops an
// object nothing references, registrars and all, which left every modifier
// without runtime ops on a device, so no effect could be built.
// ---------------------------------------------------------------------------
namespace
{
template <typename Node>
void Add(ParticleModifierOps ops)
{
ops.isEnabled = &NodeEnabled<Node>;
ParticleModifierRegistry::Instance().Register(::Deki::HashString(Node::StaticNodeName), ops);
}
} // namespace
void RegisterModifierLibrary()
{
{
ParticleModifierOps ops;
ops.stateSize = sizeof(EmissionState);
ops.onAttach = &EmissionAttach;
ops.onEmit = &EmissionEmit;
ops.onSimulate = &EmissionSimulate;
Add<ParticleEmissionNode>(ops);
}
{
ParticleModifierOps ops;
ops.onEmit = &InitialVelocityEmit;
Add<ParticleInitialVelocityNode>(ops);
}
{
ParticleModifierOps ops;
ops.onAttach = &InitialRotationAttach;
ops.onEmit = &InitialRotationEmit;
ops.onSimulate = &InitialRotationSimulate;
Add<ParticleInitialRotationNode>(ops);
}
{
ParticleModifierOps ops;
ops.onSimulate = &GravitySimulate;
Add<ParticleGravityNode>(ops);
}
{
ParticleModifierOps ops;
ops.onSimulate = &DragSimulate;
Add<ParticleDragNode>(ops);
}
{
ParticleModifierOps ops;
ops.onAttach = &SizeOverLifetimeAttach;
ops.onSimulate = &SizeOverLifetimeSimulate;
Add<ParticleSizeOverLifetimeNode>(ops);
}
{
ParticleModifierOps ops;
ops.onAttach = &ColorOverLifetimeAttach;
ops.onSimulate = &ColorOverLifetimeSimulate;
Add<ParticleColorOverLifetimeNode>(ops);
}
{
ParticleModifierOps ops;
ops.onAttach = &RotationOverLifetimeAttach;
ops.onSimulate = &RotationOverLifetimeSimulate;
Add<ParticleRotationOverLifetimeNode>(ops);
}
}
} // namespace DekiParticles