forked from stemkoski/aframe-spe-particles-component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
544 lines (510 loc) · 18.7 KB
/
index.js
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
// Copyright 2018 harlyq
// MIT license
// A-frame component for the Shader Particle Engine
let SPE = require("./lib/SPE.js")
const toVector3 = a => new THREE.Vector3(a.x, a.y, a.z)
const toColor = a => new THREE.Color(a)
const toVector2 = a => new THREE.Vector2(a.x, a.y)
const parseFloatArray = a => typeof a === "string" ? a.split(",").map(y => parseFloat(y)) : a
const degToRad = a => parseFloat(a)/180*Math.PI
const radToDeg = a => parseFloat(a)*180/Math.PI
let uniqueEmitterID = 1
AFRAME.registerComponent("spe-particles", {
schema: {
enableInEditor: {
default: false,
description: "enable the emitter while the editor is active (i.e. while scene is paused)",
},
enabled: {
default: true,
description: "enable/disable the emitter",
},
frustumCulled: {
default: false,
description: "enable/disable frustum culling",
},
// GROUP ATTRIBUTES
texture: {
type: "map",
description: "texture to be used for each particle, may be a spritesheet",
},
textureFrames: {
type: "vec2",
default: {x: 1, y: 1},
description: "x and y frames for a spritesheet. each particle will transition through every frame of the spritesheet over its lifetime (see textureFramesLoop)",
},
textureFrameCount: {
type: "int",
default: -1,
description: "number of frames in the spritesheet, negative numbers default to textureFrames.x * textureFrames.y",
},
textureFrameLoop: {
type: "int",
default: 1,
description: "number of times the spritesheet should be looped over the lifetime of a particle",
},
// maxParticleCount: {
// default: 1000,
// description: "maximum number of particles for all emitters in this group (currently only one emitter per group)",
// },
blending: {
default: "normal",
oneOf: ["no", "normal", "additive", "subtractive", "multiply", "custom"],
description: "blending mode, when drawing particles",
parse: x => (x || "no").toLowerCase(),
},
hasPerspective: {
default: true,
description: "if true, particles will be larger the closer they are to the camera. setting this to false cancels the effect of emitterScale",
},
useTransparency: {
default: true,
description: "should the particles be rendered with transparency?",
},
alphaTest: {
default: 0,
min: 0,
max: 1,
description: "alpha values below the alphaTest threshold are considered invisible",
},
depthWrite: {
default: false,
description: "if true, particles write their depth into the depth buffer. this should be false if we use transparent particles",
},
depthTest: {
default: true,
description: "if true, don't render a particle's pixels if something is closer in the depth buffer",
},
affectedByFog: {
default: true,
description: "if true, the particles are affected by THREE js fog",
},
emitterScale: {
default: 100,
description: "global scaling factor for all particles from the emitter",
},
// EMITTER ATTRIBUTES
relative: {
default: "local",
oneOf: ["local", "world"],
description: "world relative particles move relative to the world, while local particles move relative to the emitter (i.e. if the emitter moves, all particles move with it)",
parse: x => (x || "local").toLowerCase()
},
particleCount: {
type: "int",
default: 100,
description: "maximum number of particles for this emitter",
},
duration: {
default: -1,
description: "duration of the emitter (seconds), if less than 0 then continuously emit particles",
},
distribution: {
default: "BOX",
oneOf: ["BOX", "SPHERE", "DISC"],
description: "distribution for particle positions, velocities and acceleration. will be overriden by specific '...Distribution' attributes",
parse: x => (x || "BOX").toUpperCase(),
},
activeMultiplier: {
default: 1,
min: 0,
description: "multiply the rate of particles emission, if larger than 1 then the particles will be emitted in bursts. note, very large numbers will emit all particles at once",
},
direction: {
default: "forward",
oneOf: ["forward", "backward"],
description: "make the emitter operate forward or backward in time",
parse: x => (x || "forward").toLowerCase(),
},
maxAge: {
default: 1,
description: "maximum age of a particle before reusing",
},
maxAgeSpread: {
default: 0,
description: "variance for the 'maxAge' attribute",
},
positionDistribution: {
default: "NONE",
oneOf: ["NONE", "BOX", "SPHERE", "DISC"],
description: "distribution of particle positions, disc and sphere will use the radius attributes. For box particles emit at 0,0,0, for sphere they emit on the surface of the sphere and for disc on the edge of a 2D disc on the XY plane",
parse: x => (x || "NONE").toUpperCase(),
},
positionSpread: {
type: "vec3",
description: "particles are positioned within +- of these local bounds. for sphere and disc distributions only the x axis is used",
},
positionOffset: {
type: "vec3",
description: "fixed offset to the apply to the emitter relative to its parent entity",
},
randomizePosition: {
default: false,
description: "if true, re-randomize position when re-spawning a particle, can incur a performance hit",
},
radius: {
default: 1,
min: 0,
description: "radius of the disc or sphere emitter (ignored for box). note radius of 0 will prevent velocity and acceleration if they use a sphere or disc distribution",
},
radiusScale: {
type: "vec3",
default: { x: 1, y: 1, z: 1 },
description: "scales the emitter for sphere and disc shapes to form oblongs and ellipses",
},
velocityDistribution: {
default: "NONE",
oneOf: ["NONE", "BOX", "SPHERE", "DISC"],
description: "distribution of particle velocities, for disc and sphere, only the x component will be used. if set to NONE use the 'distribution' attribute for velocityDistribution",
parse: x => (x || "NONE").toUpperCase(),
},
velocity: {
type: "vec3",
description: "for sphere and disc distributions, only the x axis is used",
},
velocitySpread: {
type: "vec3",
description: "variance for the velocity",
},
randomizeVelocity: {
default: false,
description: "if true, re-randomize velocity when re-spawning a particle, can incur a performance hit",
},
accelerationDistribution: {
default: "NONE",
oneOf: ["NONE", "BOX", "SPHERE", "DISC"],
description: "distribution of particle acceleration, for disc and sphere, only the x component will be used. if set to NONE use the 'distribution' attribute for accelerationDistribution",
parse: x => (x || "NONE").toUpperCase(),
},
acceleration: {
type: "vec3",
description: "for sphere and disc distributions, only the x axis is used",
},
accelerationSpread: {
type: "vec3",
description: "spread of the particle's acceleration. for sphere and disc distributions, only the x axis is used",
},
randomizeAcceleration: {
default: false,
description: "if true, re-randomize acceleration when re-spawning a particle, can incur a performance hit",
},
drag: {
default: 0,
min: 0,
max: 1,
description: "apply resistance to moving the particle, 0 is no resistance, 1 is full resistance, particle will stop moving at half of it's maxAge"
},
dragSpread: {
default: 0,
description: "spread to apply to the drag attribute"
},
randomizeDrag: {
default: false,
description: "if true, re-randomize drag when re-spawning a particle, can incur a performance hit",
},
wiggle: {
default: 0,
description: "extra distance the particle moves over its lifetime",
},
wiggleSpread: {
default: 0,
description: "+- spread for the wiggle attribute",
},
rotation: {
default: 0,
description: "rotation in degrees",
parse: x => degToRad(x),
stringify: x => radToDeg(x),
},
rotationSpread: {
default: 0,
description: "rotation variance in degrees",
parse: x => degToRad(x),
stringify: x => radToDeg(x),
},
rotationAxis: {
type: "vec3",
description: "local axis when using rotation",
},
rotationAxisSpread: {
type: "vec3",
description: "variance in the axis of rotation",
},
rotationStatic: {
default: false,
description: "if true, the particles are fixed at their initial rotation value. if false, the particle will rotate from 0 to the 'rotation' value",
},
// rotationPivot: {
// default: {x: Number.MAX_VALUE, y: Number.MAX_VALUE, z: Number.MAX_VALUE, },
// description: "if set rotate about this pivot, otherwise rotate about the emitter",
// },
randomizeRotation: {
default: false,
description: "if true, re-randomize rotation when re-spawning a particle, can incur a performance hit",
},
color: {
type: "array",
default: ["#fff"],
description: "array of colors over the particle's lifetime, max 4 elements",
},
colorSpread: {
type: "array",
default: [],
description: "spread to apply to colors, spread an array of vec3 (r g b) with 0 for no spread. note the spread will be re-applied through-out the lifetime of the particle",
parse: x => typeof x === "string" ? x.split(",").map(AFRAME.utils.coordinates.parse) : x,
stringify: x => x.map(AFRAME.utils.coordinates.stringify).join(","),
},
randomizeColor: {
default: false,
description: "if true, re-randomize colour when re-spawning a particle, can incur a performance hit",
},
opacity: {
type: "array",
default: [1],
description: "opacity over the particle's lifetime, max 4 elements",
parse: parseFloatArray,
},
opacitySpread: {
type: "array",
default: [0],
description: "spread in opacity over the particle's lifetime, max 4 elements",
parse: parseFloatArray,
},
randomizeOpacity: {
default: false,
description: "if true, re-randomize opacity when re-spawning a particle, can incur a performance hit",
},
size: {
type: "array",
default: [1],
description: "array of sizes over the particle's lifetime, max 4 elements",
parse: parseFloatArray,
},
sizeSpread: {
type: "array",
default: [0],
description: "spread in size over the particle's lifetime, max 4 elements",
parse: parseFloatArray,
},
randomizeSize: {
default: false,
description: "if true, re-randomize size when re-spawning a particle, can incur a performance hit",
},
angle: {
type: "array",
default: [0],
description: "2D rotation of the particle over the particle's lifetime, max 4 elements",
parse: parseFloatArray,
},
angleSpread: {
type: "array",
default: [0],
description: "spread in angle over the particle's lifetime, max 4 elements",
parse: parseFloatArray,
},
randomizeAngle: {
default: false,
description: "if true, re-randomize angle when re-spawning a particle, can incur a performance hit",
},
},
multiple: true,
init: function () {
this.particleGroup = null
this.referenceEl = null
this.pauseTickId = undefined
this.emitterID = uniqueEmitterID++
this.pauseTick = this.pauseTick.bind(this)
this.defaultTexture = new THREE.DataTexture(new Uint8Array(4).fill(255), 1, 1, THREE.RGBAFormat)
this.defaultTexture.needsUpdate = true
},
update: function (oldData) {
// don't recreate the effect if just changing enabled or enableInEditor
const delta = diff(oldData, this.data)
if (Object.keys(delta).some(x => !["enabled", "enableInEditor"].includes(x))) {
this.removeParticleSystem()
this.addParticleSystem()
}
if (this.data.enabled) {
this.startParticles()
} else {
this.stopParticles()
}
// HACK - assume editor is up if isPlaying on this component is false
if (!this.isPlaying && this.data.enableInEditor) {
this.setupPauseTick()
} else {
this.shutdownPauseTick()
}
},
remove: function () {
this.removeParticleSystem()
},
tick: function (time, timeDelta) {
this.tickParticleSystem(timeDelta)
},
pause: function () {
this.setupPauseTick()
},
play: function () {
this.shutdownPauseTick()
},
setupPauseTick: function () {
if (!this.pauseTickId && this.data.enableInEditor) {
this.pauseTick(true)
}
},
shutdownPauseTick: function () {
if (this.pauseTickId) {
clearTimeout(this.pauseTickId)
this.pauseTickId = undefined
}
},
pauseTick: function() {
this.tickParticleSystem(33)
this.pauseTickId = setTimeout(this.pauseTick, 33)
},
tickParticleSystem: function (dt) {
if (this.data.relative === "world") {
let newPosition = toVector3(this.data.positionOffset).applyMatrix4(this.el.object3D.matrixWorld)
this.particleGroup.emitters[0].position.value = newPosition
}
this.particleGroup.tick(dt/1000)
},
addParticleSystem: function() {
let data = this.data
let textureLoader = new THREE.TextureLoader()
let particleTexture = data.texture ? textureLoader.load(data.texture) : this.defaultTexture
let blending = data.blending || "No"
blending = blending.charAt(0).toUpperCase() + blending.substring(1).toLowerCase() + "Blending"
if(!blending in THREE) console.error(`unknown blending mode "${data.blending}"`)
console.assert(this.particleGroup === null)
let groupOptions = {
texture: {
value: particleTexture,
frames: toVector2(data.textureFrames),
frameCount: data.textureFrameCount >= 0 ? data.textureFrameCount : undefined,
loop: data.textureFrameLoop,
},
maxParticleCount: data.particleCount, //data.maxParticleCount,
blending: THREE[blending],
hasPerspective: data.hasPerspective,
transparent: data.useTransparency,
alphaTest: data.alphaTest,
depthWrite: data.depthWrite,
depthTest: data.depthTest,
fog: data.affectedByFog,
scale: data.emitterScale,
}
this.particleGroup = new SPE.Group(groupOptions)
let emitterOptions = {
type: SPE.distributions[data.distribution in SPE.distributions ? data.distribution : "BOX"],
particleCount: data.particleCount,
duration: data.duration >= 0 ? data.duration : null,
// isStatic: true,
activeMultiplier: data.activeMultiplier,
direction: data.direction === "forward" ? 1 : -1,
maxAge: {
value: data.maxAge,
spread: data.maxAgeSpread,
},
position: {
value: data.relative === "World" ? toVector3(this.data.positionOffset).applyMatrix4(this.el.object3D.matrixWorld) : toVector3(data.positionOffset).applyMatrix4(this.el.object3D.matrix),
radius: data.radius,
radiusScale: toVector3(data.radiusScale),
spread: toVector3(data.positionSpread),
distribution: SPE.distributions[data.positionDistribution in SPE.distributions ? data.positionDistribution : data.distribution], // default to the base distribution
randomise: data.randomizePosition,
},
velocity: {
value: toVector3(data.velocity),
spread: toVector3(data.velocitySpread),
distribution: SPE.distributions[data.velocityDistribution in SPE.distributions ? data.velocityDistribution : data.distribution], // default to the base distribution
randomise: data.randomizeVelocity,
},
acceleration: {
value: toVector3(data.acceleration),
spread: toVector3(data.accelerationSpread),
distribution: SPE.distributions[data.accelerationDistribution in SPE.distributions ? data.accelerationDistribution : data.distribution], // default to the base distribution
randomise: data.randomizeAcceleration,
},
drag: {
value: data.drag,
spread: data.dragSpread,
randomise: data.randomizeDrag,
},
wiggle: {
value: data.wiggle,
spread: data.wiggleSpread,
},
rotation: {
axis: toVector3(data.rotationAxis),
axisSpread: toVector3(data.rotationAxisSpread),
angle: data.rotation,
angleSpread: data.rotationSpread,
static: data.rotationStatic,
// center: data.rotationPivot,
randomise: data.randomizeRotation,
},
color: {
value: data.color.length > 0 ? data.color.map(x => toColor(x)) : [toColor("#fff")],
spread: data.colorSpread.length > 0 ? data.colorSpread.map(x => toVector3(x)) : undefined, // doesn't accept an empty array
randomise: data.randomizeColor,
},
opacity: {
value: data.opacity,
spread: data.opacitySpread,
randomise: data.randomizeOpacity,
},
size: {
value: data.size,
spread: data.sizeSpread,
randomise: data.randomizeSize,
},
angle: {
value: data.angle,
spread: data.angleSpread,
randomise: data.randomizeAngle,
},
}
let emitter = new SPE.Emitter(emitterOptions)
this.particleGroup.addEmitter(emitter)
this.particleGroup.mesh.frustumCulled = data.frustumCulled // TODO verify this
// World emitters are parented to the world and we set their position each frame.
// Local emitters are parented to the DOM entity
this.referenceEl = data.relative === "world" ? this.el.sceneEl : this.el
this.referenceEl.setObject3D(this.getEmitterName(), this.particleGroup.mesh)
},
removeParticleSystem: function() {
if (this.particleGroup) {
this.referenceEl.removeObject3D(this.getEmitterName())
this.particleGroup = null
}
},
startParticles: function() {
this.particleGroup.emitters.forEach(em => em.enable())
},
stopParticles: function() {
this.particleGroup.emitters.forEach(em => em.disable())
},
getEmitterName: function() {
return "spe-particles" + this.emitterID
}
})
// Use a custom diff because AFRAME.utils.diff() does not correctly diff arrays (https://github.com/aframevr/aframe/issues/3591)
function diff(a, b) {
let delta = {}
let keys = Object.keys(a)
for (let k in b) {
if (!keys.includes[k]) { keys.push(k) }
}
for (let k of keys) {
let av = a[k]
let bv = b[k]
let isObj = av && bv && ( (av.constructor == Object && bv.constructor === Object) ||
(Array.isArray(av) && Array.isArray(bv)) )
if ((isObj && !AFRAME.utils.deepEqual(av, bv)) || (!isObj && av !== bv)) {
delta[k] = bv;
}
}
return delta
}