@needle-tools/engine 2.42.0-pre → 2.43.0-pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/needle-engine.d.ts +130 -40
  3. package/dist/needle-engine.js +3836 -422
  4. package/dist/needle-engine.js.map +4 -4
  5. package/dist/needle-engine.min.js +40 -40
  6. package/dist/needle-engine.min.js.map +4 -4
  7. package/lib/engine/engine_element_loading.js +22 -6
  8. package/lib/engine/engine_element_loading.js.map +1 -1
  9. package/lib/engine/engine_three_utils.d.ts +1 -0
  10. package/lib/engine/engine_three_utils.js +10 -0
  11. package/lib/engine/engine_three_utils.js.map +1 -1
  12. package/lib/engine/engine_time.js +2 -0
  13. package/lib/engine/engine_time.js.map +1 -1
  14. package/lib/engine/extensions/NEEDLE_techniques_webgl.js +42 -0
  15. package/lib/engine/extensions/NEEDLE_techniques_webgl.js.map +1 -1
  16. package/lib/engine-components/Animation.js +9 -1
  17. package/lib/engine-components/Animation.js.map +1 -1
  18. package/lib/engine-components/AnimatorController.js +4 -1
  19. package/lib/engine-components/AnimatorController.js.map +1 -1
  20. package/lib/engine-components/Camera.d.ts +3 -0
  21. package/lib/engine-components/Camera.js +17 -9
  22. package/lib/engine-components/Camera.js.map +1 -1
  23. package/lib/engine-components/ParticleSystem.d.ts +21 -7
  24. package/lib/engine-components/ParticleSystem.js +519 -246
  25. package/lib/engine-components/ParticleSystem.js.map +1 -1
  26. package/lib/engine-components/ParticleSystemBehaviours.d.ts +0 -0
  27. package/lib/engine-components/ParticleSystemBehaviours.js +2 -0
  28. package/lib/engine-components/ParticleSystemBehaviours.js.map +1 -0
  29. package/lib/engine-components/ParticleSystemModules.d.ts +83 -16
  30. package/lib/engine-components/ParticleSystemModules.js +284 -49
  31. package/lib/engine-components/ParticleSystemModules.js.map +1 -1
  32. package/lib/engine-components/codegen/components.d.ts +4 -0
  33. package/lib/engine-components/codegen/components.js +4 -0
  34. package/lib/engine-components/codegen/components.js.map +1 -1
  35. package/lib/engine-components/js-extensions/RGBAColor.d.ts +1 -0
  36. package/lib/engine-components/js-extensions/RGBAColor.js +7 -0
  37. package/lib/engine-components/js-extensions/RGBAColor.js.map +1 -1
  38. package/package.json +2 -1
  39. package/src/engine/codegen/register_types.js +16 -0
  40. package/src/engine/engine_element_loading.ts +22 -5
  41. package/src/engine/engine_three_utils.ts +11 -2
  42. package/src/engine/engine_time.ts +1 -0
  43. package/src/engine/extensions/NEEDLE_techniques_webgl.ts +43 -1
  44. package/src/engine-components/Animation.ts +7 -1
  45. package/src/engine-components/AnimatorController.ts +5 -1
  46. package/src/engine-components/Camera.ts +17 -10
  47. package/src/engine-components/ParticleSystem.ts +600 -274
  48. package/src/engine-components/ParticleSystemBehaviours.ts +0 -0
  49. package/src/engine-components/ParticleSystemModules.ts +263 -51
  50. package/src/engine-components/codegen/components.ts +4 -0
  51. package/src/engine-components/js-extensions/RGBAColor.ts +7 -0
@@ -6,21 +6,29 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
6
6
  };
7
7
  import { Behaviour } from "./Component";
8
8
  import * as THREE from "three";
9
- import { MainModule, EmissionModule, ShapeModule, ColorOverLifetimeModule, SizeOverLifetimeModule, NoiseModule, ParticleSystemSimulationSpace, ParticleBurst, ParticleSystemRenderMode } from "./ParticleSystemModules";
9
+ import { MainModule, EmissionModule, ShapeModule, ColorOverLifetimeModule, SizeOverLifetimeModule, NoiseModule, ParticleSystemSimulationSpace, ParticleBurst, ParticleSystemRenderMode, VelocityOverLifetimeModule, TextureSheetAnimationModule, RotationOverLifetimeModule } from "./ParticleSystemModules";
10
10
  import { getParam } from "../engine/engine_utils";
11
11
  // https://github.dev/creativelifeform/three-nebula
12
- import System, { Emitter, Position, Life, SpriteRenderer, Particle, Body, MeshRenderer, } from 'three-nebula';
12
+ // import System, { Emitter, Position, Life, SpriteRenderer, Particle, Body, MeshRenderer, } from 'three-nebula';
13
13
  import { serializeable } from "../engine/engine_serialization";
14
- import { Context } from "../engine/engine_setup";
15
- import { AxesHelper, Material, Mesh, Object3D, Vector3 } from "three";
16
- import { getWorldQuaternion, getWorldScale } from "../engine/engine_three_utils";
14
+ import { RGBAColor } from "./js-extensions/RGBAColor";
15
+ import { AxesHelper, Material, Mesh, Object3D, Quaternion, Vector3, Vector4 } from "three";
16
+ import { getWorldPosition, getWorldQuaternion, getWorldScale } from "../engine/engine_three_utils";
17
17
  import { assign } from "../engine/engine_serialization_core";
18
+ import { BatchedParticleRenderer, ConstantColor, ConstantValue, ParticleSystem as _ParticleSystem, RenderMode } from "three.quarks";
19
+ import { createFlatTexture } from "../engine/engine_shaders";
20
+ import { Mathf } from "../engine/engine_math";
18
21
  const debug = getParam("debugparticles");
19
22
  export class ParticleSystemRenderer extends Behaviour {
20
23
  renderMode;
21
24
  particleMaterial;
22
25
  // @serializeable(Mesh)
23
26
  particleMesh;
27
+ get transparent() {
28
+ const res = this.particleMaterial?.transparent ?? false;
29
+ // console.log(res, this.particleMaterial);
30
+ return res;
31
+ }
24
32
  getMesh() {
25
33
  let geo = null;
26
34
  if (this.particleMesh instanceof Mesh) {
@@ -38,6 +46,279 @@ __decorate([
38
46
  __decorate([
39
47
  serializeable(Material)
40
48
  ], ParticleSystemRenderer.prototype, "particleMaterial", void 0);
49
+ class MinMaxCurveFunction {
50
+ _curve;
51
+ constructor(curve) { this._curve = curve; }
52
+ type = "function";
53
+ genValue(t) {
54
+ return this._curve.evaluate(t, Math.random());
55
+ }
56
+ toJSON() {
57
+ throw new Error("Method not implemented.");
58
+ }
59
+ clone() {
60
+ throw new Error("Method not implemented.");
61
+ }
62
+ }
63
+ class MinMaxGradientFunction {
64
+ _curve;
65
+ constructor(curve) { this._curve = curve; }
66
+ type = "function";
67
+ genColor(color, t) {
68
+ const col = this._curve.evaluate(t, Math.random());
69
+ // TODO: incoming color should probably be blended?
70
+ color.set(col.r, col.g, col.b, col.alpha);
71
+ return color;
72
+ }
73
+ toJSON() {
74
+ throw new Error("Method not implemented.");
75
+ }
76
+ clone() {
77
+ throw new Error("Method not implemented.");
78
+ }
79
+ }
80
+ class BaseValueGenerator {
81
+ type = "value";
82
+ toJSON() {
83
+ throw new Error("Method not implemented.");
84
+ }
85
+ clone() {
86
+ throw new Error("Method not implemented.");
87
+ }
88
+ system;
89
+ constructor(system) {
90
+ this.system = system;
91
+ }
92
+ }
93
+ class TextureSheetStartFrameGenerator extends BaseValueGenerator {
94
+ genValue() {
95
+ return this.system.textureSheetAnimation.getStartIndex();
96
+ }
97
+ }
98
+ class ParticleSystemEmissionOverTime extends BaseValueGenerator {
99
+ _lastPosition = new Vector3();
100
+ _lastDistance = 0;
101
+ update() {
102
+ const currentPosition = getWorldPosition(this.system.gameObject);
103
+ this._lastDistance = this._lastPosition.distanceTo(currentPosition);
104
+ this._lastPosition.copy(currentPosition);
105
+ }
106
+ genValue() {
107
+ if (this.system.currentParticles >= this.system.maxParticles)
108
+ return 0;
109
+ // emission over time
110
+ let emission = this.system.emission.rateOverTime.evaluate(this.system.time / this.system.duration, Math.random());
111
+ // if(this.system.currentParticles + emission > this.system.maxParticles)
112
+ // emission = (this.system.maxParticles - this.system.currentParticles);
113
+ // const res = Mathf.clamp(emission, 0, this.system.maxParticles - this.system.currentParticles);
114
+ if (this.system.deltaTime > 0) {
115
+ const distanceEmission = this.system.emission.rateOverDistance.evaluate(this.system.time / this.system.duration, Math.random());
116
+ const meterPerSecond = this._lastDistance / this.system.deltaTime;
117
+ let distanceEmissionValue = meterPerSecond * distanceEmission;
118
+ if (!Number.isFinite(distanceEmissionValue))
119
+ distanceEmissionValue = 0;
120
+ emission += distanceEmissionValue;
121
+ }
122
+ const burst = this.system.emission.getBurst();
123
+ if (burst > 0)
124
+ emission += burst / this.system.deltaTime;
125
+ const maxEmission = (this.system.maxParticles - this.system.currentParticles);
126
+ return Mathf.clamp(emission, 0, maxEmission / this.system.deltaTime);
127
+ }
128
+ }
129
+ class ParticleSystemEmissionOverDistance extends BaseValueGenerator {
130
+ genValue() {
131
+ // this seems not be called yet
132
+ return 0;
133
+ // if (this.system.currentParticles >= this.system.maxParticles) return 0;
134
+ // const emission = this.system.emission.rateOverDistance.evaluate(this.system.time / this.system.duration, Math.random());
135
+ // return emission;
136
+ }
137
+ }
138
+ class ParticleSystemBaseBehaviour {
139
+ system;
140
+ constructor(ps) {
141
+ this.system = ps;
142
+ }
143
+ initialize(_particle) {
144
+ }
145
+ update(_particle, _delta) {
146
+ }
147
+ frameUpdate(_delta) {
148
+ }
149
+ toJSON() { throw new Error("Method not implemented."); }
150
+ clone() { throw new Error("Method not implemented."); }
151
+ }
152
+ class TextureSheetAnimationBehaviour extends ParticleSystemBaseBehaviour {
153
+ type = "NeedleTextureSheet";
154
+ update(particle, _delta) {
155
+ const sheet = this.system.textureSheetAnimation;
156
+ if (sheet.enabled) {
157
+ const t01 = particle.age / particle.life;
158
+ const index = sheet.evaluate(t01);
159
+ ;
160
+ if (index !== undefined)
161
+ particle.uvTile = index;
162
+ }
163
+ }
164
+ }
165
+ class RotationBehaviour extends ParticleSystemBaseBehaviour {
166
+ type = "NeedleRotation";
167
+ update(particle, delta) {
168
+ if (particle.rotation === undefined)
169
+ return;
170
+ if (particle.rotation instanceof Quaternion) {
171
+ }
172
+ else {
173
+ if (this.system.rotationOverLifetime.enabled) {
174
+ particle.rotation += this.system.rotationOverLifetime.evaluate(particle.age / particle.life) * delta;
175
+ }
176
+ // HACK flip y
177
+ else
178
+ particle.rotation = Math.PI;
179
+ }
180
+ }
181
+ }
182
+ class SizeBehaviour extends ParticleSystemBaseBehaviour {
183
+ type = "NeedleSize";
184
+ update(particle, _delta) {
185
+ const t = particle.age / particle.life;
186
+ let size = 1;
187
+ if (this.system.sizeOverLifetime.enabled)
188
+ size *= this.system.sizeOverLifetime.evaluate(t).x;
189
+ const scaleFactor = this.system.scale / this.system.cameraScale;
190
+ particle.size = particle.startSize * size * scaleFactor;
191
+ // console.log(particle.startSize, size, this.system.scale, this.system.cameraScale);
192
+ }
193
+ }
194
+ const $startVelocity = Symbol("startVelocity");
195
+ const $gravityFactor = Symbol("gravityModifier");
196
+ const temp3 = new Vector3();
197
+ const temp4 = new Quaternion();
198
+ class VelocityBehaviour extends ParticleSystemBaseBehaviour {
199
+ type = "NeedleVelocity";
200
+ _gravityDirection = new Vector3();
201
+ initialize(particle) {
202
+ const scale = this.system.scale;
203
+ particle.startSpeed = this.system.main.startSpeed.evaluate(Math.random(), Math.random());
204
+ particle.velocity.copy(this.system.shape.getDirection(particle.position)).multiplyScalar(particle.startSpeed);
205
+ if (!particle[$startVelocity])
206
+ particle[$startVelocity] = particle.velocity.clone();
207
+ else
208
+ particle[$startVelocity].copy(particle.velocity);
209
+ let gravityFactor = Math.PI / scale;
210
+ gravityFactor = this.system.main.gravityModifier.evaluate(Math.random(), Math.random()) * gravityFactor;
211
+ particle[$gravityFactor] = gravityFactor;
212
+ this._gravityDirection.set(0, -1, 0).applyQuaternion(this.system.worldQuaternionInverted);
213
+ }
214
+ update(particle, delta) {
215
+ // const t = particle.age / particle.life;
216
+ // console.log(this.system.deltaTime, delta);
217
+ const baseVelocity = particle[$startVelocity];
218
+ let gravityFactor = particle[$gravityFactor];
219
+ if (gravityFactor !== 0) {
220
+ // gravityFactor *= -1;
221
+ temp3.copy(this._gravityDirection).multiplyScalar(gravityFactor * delta * Math.PI);
222
+ // Gizmos.DrawDirection(particle.position, temp3, 0xff0000, 0, false, 10);
223
+ baseVelocity.add(temp3);
224
+ }
225
+ particle.velocity.copy(baseVelocity);
226
+ const noise = this.system.noise;
227
+ if (noise.enabled) {
228
+ noise.apply(0, particle.position, particle.velocity, delta, particle.age, particle.life);
229
+ }
230
+ const velocity = this.system.velocityOverLifetime;
231
+ if (velocity.enabled) {
232
+ velocity.apply(0, particle.position, particle.velocity, delta, particle.age, particle.life);
233
+ }
234
+ // particle.velocity.multiplyScalar(1-delta);
235
+ // const vel = this.system.velocityOverLifetime.evaluate(particle.age / particle.life);
236
+ }
237
+ }
238
+ class ColorBehaviour extends ParticleSystemBaseBehaviour {
239
+ type = "NeedleColor";
240
+ initialize(particle) {
241
+ const col = this.system.main.startColor.evaluate(particle.age / particle.life, Math.random());
242
+ particle.startColor.set(col.r, col.g, col.b, col.alpha);
243
+ particle.color.copy(particle.startColor);
244
+ }
245
+ update(particle, _delta) {
246
+ if (this.system.colorOverLifetime.enabled) {
247
+ const t = particle.age / particle.life;
248
+ const col = this.system.colorOverLifetime.color.evaluate(t);
249
+ particle.color.set(col.r, col.g, col.b, col.alpha).multiply(particle.startColor);
250
+ }
251
+ }
252
+ }
253
+ class ParticleSystemInterface {
254
+ system;
255
+ emission;
256
+ get anim() {
257
+ return this.system.textureSheetAnimation;
258
+ }
259
+ constructor(system) {
260
+ this.system = system;
261
+ this.emission = new ParticleSystemEmissionOverTime(this.system);
262
+ }
263
+ update() {
264
+ this.emission.update();
265
+ }
266
+ autoDestroy;
267
+ get looping() { return this.system.main.loop; }
268
+ get duration() { return this.system.duration; }
269
+ get shape() { return this.system.shape; }
270
+ get startLife() { return new MinMaxCurveFunction(this.system.main.startLifetime); }
271
+ get startSpeed() { return new MinMaxCurveFunction(this.system.main.startSpeed); }
272
+ get startRotation() { return new MinMaxCurveFunction(this.system.main.startRotation); }
273
+ get startSize() { return new MinMaxCurveFunction(this.system.main.startSize); }
274
+ startLength; /** start length is for trails */
275
+ get startColor() { return new ConstantColor(new Vector4(1, 1, 1, 1)); }
276
+ get emissionOverTime() { return this.emission; }
277
+ /** this is not supported yet */
278
+ get emissionOverDistance() { return new ParticleSystemEmissionOverDistance(this.system); }
279
+ /** not used - burst is controled via emissionOverTime */
280
+ emissionBursts;
281
+ onlyUsedByOther;
282
+ behaviors = [];
283
+ get instancingGeometry() {
284
+ return this.system.renderer.getMesh().geometry;
285
+ }
286
+ get renderMode() {
287
+ if (this.system.trails["enabled"] === true) {
288
+ return RenderMode.Trail;
289
+ }
290
+ switch (this.system.renderer.renderMode) {
291
+ case ParticleSystemRenderMode.Billboard: return RenderMode.BillBoard;
292
+ // case ParticleSystemRenderMode.Stretch: return RenderMode.Stretch;
293
+ // case ParticleSystemRenderMode.HorizontalBillboard: return RenderMode.HorizontalBillboard;
294
+ // case ParticleSystemRenderMode.VerticalBillboard: return RenderMode.VerticalBillboard;
295
+ case ParticleSystemRenderMode.Mesh: return RenderMode.LocalSpace;
296
+ }
297
+ return RenderMode.BillBoard;
298
+ }
299
+ rendererEmitterSettings = {
300
+ startLength: new ConstantValue(220),
301
+ followLocalOrigin: false,
302
+ };
303
+ get speedFactor() { return this.system.main.simulationSpeed; }
304
+ get texture() {
305
+ const mat = this.system.renderer.particleMaterial;
306
+ if (mat && mat["map"]) {
307
+ const tex = mat["map"];
308
+ tex.premultiplyAlpha = false;
309
+ tex.encoding = THREE.LinearEncoding;
310
+ return tex;
311
+ }
312
+ return createFlatTexture(new RGBAColor(1, 1, 1, 1), 1);
313
+ }
314
+ get startTileIndex() { return new TextureSheetStartFrameGenerator(this.system); }
315
+ get uTileCount() { return this.anim.enabled ? this.anim?.numTilesX : undefined; }
316
+ get vTileCount() { return this.anim.enabled ? this.anim?.numTilesY : undefined; }
317
+ get renderOrder() { return 1; }
318
+ get blending() { return this.system.renderer.particleMaterial?.blending ?? THREE.NormalBlending; }
319
+ get transparent() { return this.system.renderer.transparent; }
320
+ get worldSpace() { return this.system.main.simulationSpace === ParticleSystemSimulationSpace.World; }
321
+ }
41
322
  export class ParticleSystem extends Behaviour {
42
323
  colorOverLifetime;
43
324
  main;
@@ -45,11 +326,16 @@ export class ParticleSystem extends Behaviour {
45
326
  sizeOverLifetime;
46
327
  shape;
47
328
  noise;
329
+ // @serializeable(TrailModule)
330
+ trails;
331
+ velocityOverLifetime;
332
+ textureSheetAnimation;
333
+ rotationOverLifetime;
48
334
  get renderer() {
49
335
  return this._renderer;
50
336
  }
51
337
  get currentParticles() {
52
- return this._system?.getCount() ?? 0;
338
+ return this._particleSystem?.particleNum ?? 0;
53
339
  }
54
340
  get maxParticles() {
55
341
  return this.main.maxParticles;
@@ -60,10 +346,34 @@ export class ParticleSystem extends Behaviour {
60
346
  get duration() {
61
347
  return this.main.duration;
62
348
  }
349
+ get deltaTime() {
350
+ return this.context.time.deltaTime * this.main.simulationSpeed;
351
+ }
352
+ get scale() {
353
+ return this.gameObject.scale.x;
354
+ }
355
+ get cameraScale() {
356
+ return this._cameraScale;
357
+ }
358
+ _cameraScale = 1;
359
+ get container() {
360
+ return this._container;
361
+ }
362
+ __worldQuaternion = new Quaternion();
363
+ get worldQuaternion() {
364
+ return this.__worldQuaternion;
365
+ }
366
+ _worldQuaternionInverted = new Quaternion();
367
+ get worldQuaternionInverted() {
368
+ return this._worldQuaternionInverted;
369
+ }
63
370
  _renderer;
64
- _system;
65
- _emitter;
66
- _size;
371
+ _batchSystem;
372
+ _particleSystem;
373
+ _interface;
374
+ // private _system!: System;
375
+ // private _emitter: Emitter;
376
+ // private _size!: SizeBehaviour;
67
377
  _container;
68
378
  _time = 0;
69
379
  /** called from deserialization */
@@ -81,91 +391,58 @@ export class ParticleSystem extends Behaviour {
81
391
  _bursts;
82
392
  awake() {
83
393
  this._renderer = this.gameObject.getComponent(ParticleSystemRenderer);
84
- this._system = new System();
394
+ this._container = new Object3D();
395
+ this._container.matrixAutoUpdate = false;
85
396
  if (this.main.simulationSpace == ParticleSystemSimulationSpace.Local) {
86
- this._container = new Object3D();
87
- this._container.matrixAutoUpdate = false;
88
397
  this.gameObject.add(this._container);
89
398
  }
90
- const container = this.main.simulationSpace == ParticleSystemSimulationSpace.Local ? this._container : this.context.scene;
399
+ else {
400
+ this.context.scene.add(this._container);
401
+ }
402
+ // else this._container = this.context.scene;
403
+ this._batchSystem = new BatchedParticleRenderer();
404
+ this._container.add(this._batchSystem);
405
+ this._interface = new ParticleSystemInterface(this);
406
+ this._particleSystem = new _ParticleSystem(this._batchSystem, this._interface);
407
+ this._particleSystem.addBehavior(new SizeBehaviour(this));
408
+ this._particleSystem.addBehavior(new VelocityBehaviour(this));
409
+ this._particleSystem.addBehavior(new ColorBehaviour(this));
410
+ this._particleSystem.addBehavior(new TextureSheetAnimationBehaviour(this));
411
+ this._particleSystem.addBehavior(new RotationBehaviour(this));
412
+ const emitter = this._particleSystem.emitter;
413
+ this.context.scene.add(emitter);
91
414
  if (debug) {
92
415
  console.log(this);
93
- this.gameObject.add(new AxesHelper(1));
94
- }
95
- const renderMode = this._renderer.renderMode;
96
- let renderer = undefined;
97
- switch (renderMode) {
98
- case ParticleSystemRenderMode.Mesh:
99
- // https://three-nebula-docs.netlify.app/class/src/renderer/meshrenderer.js~meshrenderer
100
- // mesh renderer example: https://github.com/creativelifeform/three-nebula/blob/master/website/components/Examples/MeshRenderer/init.js
101
- renderer = new MeshRenderer(container, THREE);
102
- break;
103
- case ParticleSystemRenderMode.Billboard:
104
- renderer = new SpriteRenderer(container, THREE);
105
- break;
106
- }
107
- if (!renderer) {
108
- console.error("No renderer for particle system");
109
- return;
110
- }
111
- renderer.logRendererType = () => { };
112
- if (renderMode === ParticleSystemRenderMode.Billboard) {
113
- if (this.renderer.particleMaterial) {
114
- const sprite = renderer._body;
115
- sprite.layers.disableAll();
116
- sprite.layers.set(2); // ignore raycasting particles
117
- sprite.renderOrder = 1;
118
- sprite.material.map = this.renderer.particleMaterial.map;
119
- sprite.material.transparent = true;
120
- // sprite.material.sizeAttenuation = false;
121
- sprite.material.blending = this.renderer.particleMaterial.blending;
122
- sprite.material.blendDst = this.renderer.particleMaterial.blendDst;
123
- sprite.material.blendDstAlpha = this.renderer.particleMaterial.blendDstAlpha;
124
- sprite.material.blendEquation = this.renderer.particleMaterial.blendEquation;
125
- sprite.material.blendEquationAlpha = this.renderer.particleMaterial.blendEquationAlpha;
126
- sprite.material.blendSrc = this.renderer.particleMaterial.blendSrc;
127
- sprite.material.blendSrcAlpha = this.renderer.particleMaterial.blendSrcAlpha;
128
- sprite.material.premultipliedAlpha = this.renderer.particleMaterial.premultipliedAlpha;
129
- sprite.material.depthWrite = false;
130
- sprite.material.depthTest = true;
131
- if (debug)
132
- console.log(sprite);
133
- }
134
- }
135
- this._system.addRenderer(renderer);
136
- const initializers = [];
137
- const behaviours = [];
138
- const life = new Life();
139
- life.lifePan = new MinMaxCurveSpan(this.main.startLifetime, 1);
140
- initializers.push(life);
141
- const shape = new Position(this.shape);
142
- initializers.push(shape);
143
- const size = this._size = new SizeBehaviour(this.main.startSize, this.main.startSizeMultiplier);
144
- size.sizeOverLifetimeModule = this.sizeOverLifetime;
145
- behaviours.push(size);
146
- const color = new GradientBehaviour(this.main.startColor, this.colorOverLifetime?.enabled ? this.colorOverLifetime.color : undefined);
147
- behaviours.push(color);
148
- const velocity = new VelocityBehaviour(renderer.container, this.shape, this.noise, this.main.startSpeed, 1);
149
- velocity.gravity = this.main.gravityModifier;
150
- velocity.gravityModifierMultiplier = this.main.gravityModifierMultiplier;
151
- behaviours.push(velocity);
152
- if (renderMode === ParticleSystemRenderMode.Mesh) {
153
- initializers.push(new Body(this._renderer.getMesh()));
416
+ this.container.add(new AxesHelper(1));
154
417
  }
155
- const emitter = this._emitter = new Emitter({});
156
- emitter.setInitializers(initializers);
157
- emitter.setBehaviours(behaviours);
158
- emitter.setRate(this.emission);
159
- emitter.damping = 0;
418
+ // renderer.logRendererType = () => { };
419
+ // if (renderMode === ParticleSystemRenderMode.Billboard) {
420
+ // if (this.renderer.particleMaterial) {
421
+ // const sprite = renderer._body as Sprite;
422
+ // sprite.layers.disableAll();
423
+ // sprite.layers.set(2); // ignore raycasting particles
424
+ // sprite.renderOrder = 1;
425
+ // sprite.material.map = this.renderer.particleMaterial.map;
426
+ // sprite.material.transparent = true;
427
+ // // sprite.material.sizeAttenuation = false;
428
+ // sprite.material.blending = this.renderer.particleMaterial.blending;
429
+ // sprite.material.blendDst = this.renderer.particleMaterial.blendDst;
430
+ // sprite.material.blendDstAlpha = this.renderer.particleMaterial.blendDstAlpha;
431
+ // sprite.material.blendEquation = this.renderer.particleMaterial.blendEquation;
432
+ // sprite.material.blendEquationAlpha = this.renderer.particleMaterial.blendEquationAlpha;
433
+ // sprite.material.blendSrc = this.renderer.particleMaterial.blendSrc;
434
+ // sprite.material.blendSrcAlpha = this.renderer.particleMaterial.blendSrcAlpha;
435
+ // sprite.material.premultipliedAlpha = this.renderer.particleMaterial.premultipliedAlpha;
436
+ // sprite.material.depthWrite = false;
437
+ // sprite.material.depthTest = true;
438
+ // if (debug) console.log(sprite);
439
+ // }
440
+ // }
160
441
  }
161
442
  onEnable() {
162
- this._emitter.emit();
163
- this._system.addEmitter(this._emitter);
164
- this._system.emit({ onStart: () => { }, onUpdate: () => { }, onEnd: () => { }, });
165
443
  this._time = 0;
166
444
  }
167
445
  onDisable() {
168
- this._system.removeEmitter(this._emitter);
169
446
  }
170
447
  onBeforeRender() {
171
448
  if (this._bursts) {
@@ -175,31 +452,29 @@ export class ParticleSystem extends Behaviour {
175
452
  const cam = this.context.mainCamera;
176
453
  if (cam) {
177
454
  const scale = getWorldScale(cam);
178
- this._size.cameraScale = scale.x;
455
+ this._cameraScale = scale.x;
179
456
  }
457
+ let rotationSource = this._container;
458
+ if (this.main.simulationSpace === ParticleSystemSimulationSpace.World)
459
+ rotationSource = this.gameObject;
460
+ getWorldQuaternion(rotationSource, this.__worldQuaternion);
461
+ this._worldQuaternionInverted.copy(this.__worldQuaternion).invert();
180
462
  if (this._container && this.gameObject?.parent) {
181
- // this._container.matrix.copy(this.gameObject.matrixWorld);
182
- // this._container.matrixWorld.copy(this.gameObject.matrixWorld);
183
463
  const scale = getWorldScale(this.gameObject.parent);
184
464
  scale.x = 1 / scale.x;
185
465
  scale.y = 1 / scale.y;
186
466
  scale.z = 1 / scale.z;
187
- // console.log(scale);
188
- // this._container.scale.copy(scale);
189
- // // this._container.updateMatrix();
190
- // // this._container.updateMatrixWorld();
191
- // // // set scale to 1
192
467
  this._container.matrix.identity();
193
468
  this._container.matrix.scale(scale);
194
- // this._container.matrixWorld.copy(this._container.matrix);
195
469
  }
196
- // this._system.time
197
- // this._system.duration = this.main.duration;
198
470
  this.emission.system = this;
471
+ const dt = this.deltaTime;
472
+ this._interface.update();
199
473
  this.shape.update(this.context, this.main.simulationSpace, this.gameObject);
200
474
  this.noise.update(this.context);
201
- this._system.update(this.context.time.deltaTime * this.main.simulationSpeed);
202
- this._time += this.context.time.deltaTime;
475
+ this.velocityOverLifetime.update(this);
476
+ this._batchSystem?.update(dt);
477
+ this._time += dt;
203
478
  if (this._time > this.duration)
204
479
  this._time = 0;
205
480
  }
@@ -222,154 +497,152 @@ __decorate([
222
497
  __decorate([
223
498
  serializeable(NoiseModule)
224
499
  ], ParticleSystem.prototype, "noise", void 0);
225
- class MinMaxCurveSpan {
226
- time;
227
- curve;
228
- multiplier = 1;
229
- constructor(minMaxCurve, multiplier = 1) {
230
- this.time = Context.Current.time;
231
- this.curve = minMaxCurve;
232
- this.multiplier = multiplier;
233
- }
234
- /** called by nebula */
235
- getValue(lerp) {
236
- const res = this.curve.evaluate(this.time.time, lerp) * this.multiplier;
237
- return res;
238
- }
239
- }
240
- class CustomBehaviour extends Behaviour {
241
- }
242
- class MinMaxCurveBehaviour extends CustomBehaviour {
243
- startCurve;
244
- startMultiplier = 1;
245
- constructor(startCurve, startMultiplier = 1) {
246
- super();
247
- this.startCurve = startCurve;
248
- this.startMultiplier = startMultiplier;
249
- }
250
- }
251
- class SizeBehaviour extends MinMaxCurveBehaviour {
252
- sizeOverLifetimeModule;
253
- cameraScale = 1;
254
- applyBehaviour(particle, _deltaTime, _index) {
255
- if (particle.target.type === "Sprite") {
256
- particle.radius = 1 / this.cameraScale;
257
- }
258
- else
259
- particle.radius = 1;
260
- // particle.target.scale.set(1,1,1);
261
- if (this.sizeOverLifetimeModule?.enabled) {
262
- const time = particle.age / particle.life;
263
- const scaleVector = particle.target.scale;
264
- this.sizeOverLifetimeModule.evaluate?.call(this.sizeOverLifetimeModule, time, scaleVector);
265
- // scaleVector.set(.1, .1, .1)
266
- particle.scale = particle.start_scale * scaleVector.x;
267
- // console.log(time, particle.scale);
268
- }
269
- }
270
- initialize(particle) {
271
- particle.scale = this.startCurve.evaluate(0, Math.random()); // * this.startMultiplier;
272
- particle.start_scale = particle.scale;
273
- }
274
- }
275
- class GradientBehaviour extends CustomBehaviour {
276
- startGradient;
277
- gradient;
278
- constructor(startGradient, gradient) {
279
- super();
280
- this.startGradient = startGradient;
281
- this.gradient = gradient;
282
- }
283
- /** called from nebula */
284
- applyBehaviour(target, _deltaTime, _index) {
285
- // console.log(target, time, index)
286
- if (target instanceof Particle) {
287
- if (this.gradient) {
288
- const t = target.age / target.life;
289
- const ev = this.gradient.evaluate(t);
290
- const startColor = target.color_start;
291
- target.color.r = startColor.r * ev.r;
292
- target.color.g = startColor.g * ev.g;
293
- target.color.b = startColor.b * ev.b;
294
- target.alpha = ev.alpha * target.alpha_start;
295
- }
296
- }
297
- }
298
- id = Symbol('gradientId');
299
- _internalId = 0;
300
- /** called from nebula */
301
- initialize(particle) {
302
- // console.log(particle);
303
- // const context = Context.Current;
304
- // const time = context.time.time;
305
- const ev = this.startGradient.evaluate(Math.random());
306
- particle.color.r = ev.r;
307
- particle.color.g = ev.g;
308
- particle.color.b = ev.b;
309
- particle.alpha = ev.alpha;
310
- particle.age = 0;
311
- particle.color_start = { r: ev.r, g: ev.g, b: ev.b };
312
- particle.alpha_start = ev.alpha;
313
- particle.useColor = true;
314
- particle.useAlpha = true;
315
- if (particle[this.id] === undefined)
316
- particle[this.id] = this._internalId++;
317
- }
318
- }
319
- class VelocityBehaviour extends CustomBehaviour {
320
- container;
321
- shape;
322
- noise;
323
- startSpeed;
324
- startMultiplier = 1;
325
- gravity;
326
- gravityModifierMultiplier = 1;
327
- downDirection = new Vector3();
328
- _temp = new Vector3();
329
- constructor(container, shape, noise, startCurve, startMultiplier) {
330
- super();
331
- this.container = container;
332
- this.shape = shape;
333
- this.noise = noise;
334
- this.startSpeed = startCurve;
335
- this.startMultiplier = startMultiplier;
336
- }
337
- applyBehaviour(target, deltaTime, index) {
338
- if (target instanceof Particle) {
339
- // target.velocity.x = target.velocity_start.x;
340
- // target.velocity.y = target.velocity_start.y;
341
- // target.velocity.z = target.velocity_start.z;
342
- if (target.gravity !== 0 && this.gravity) {
343
- // get world down vector
344
- this._temp.copy(this.downDirection);
345
- this._temp.multiplyScalar(target.gravity * deltaTime);
346
- target.velocity.add(this._temp);
347
- }
348
- if (this.noise) {
349
- this.noise.applyNoise(index, target.position, target.velocity, deltaTime, target.age, target.life);
350
- }
351
- }
352
- }
353
- initialize(particle) {
354
- const dir = this.shape.getDirection(particle.position);
355
- // dir.applyQuaternion(quat);
356
- const speed = this.startSpeed.evaluate(0, Math.random()) * this.startMultiplier;
357
- particle.velocity.x = dir.x * speed;
358
- particle.velocity.y = dir.y * speed;
359
- particle.velocity.z = dir.z * speed;
360
- if (!particle.velocity_start)
361
- particle.velocity_start = {};
362
- particle.velocity_start.x = particle.velocity.x;
363
- particle.velocity_start.y = particle.velocity.y;
364
- particle.velocity_start.z = particle.velocity.z;
365
- if (this.gravity) {
366
- particle.gravity = this.gravity.evaluate(Math.random(), Math.random()) * 9.81;
367
- }
368
- else {
369
- particle.gravity = 0;
370
- }
371
- const quat = getWorldQuaternion(this.container);
372
- this.downDirection.set(0, -1, 0).applyQuaternion(quat);
373
- }
374
- }
500
+ __decorate([
501
+ serializeable(VelocityOverLifetimeModule)
502
+ ], ParticleSystem.prototype, "velocityOverLifetime", void 0);
503
+ __decorate([
504
+ serializeable(TextureSheetAnimationModule)
505
+ ], ParticleSystem.prototype, "textureSheetAnimation", void 0);
506
+ __decorate([
507
+ serializeable(RotationOverLifetimeModule)
508
+ ], ParticleSystem.prototype, "rotationOverLifetime", void 0);
509
+ // abstract class CustomBehaviour extends Behaviour {
510
+ // abstract applyBehaviour(target: Particle | Emitter, time: number, index: number);
511
+ // abstract initialize(particle: Particle);
512
+ // }
513
+ // abstract class MinMaxCurveBehaviour extends CustomBehaviour {
514
+ // readonly startCurve: MinMaxCurve;
515
+ // startMultiplier: number = 1;
516
+ // constructor(startCurve: MinMaxCurve, startMultiplier: number = 1) {
517
+ // super();
518
+ // this.startCurve = startCurve;
519
+ // this.startMultiplier = startMultiplier;
520
+ // }
521
+ // }
522
+ // declare type ParticleWithScale = Particle & { target: Object3D, start_scale: number };
523
+ // class SizeBehaviour extends MinMaxCurveBehaviour {
524
+ // sizeOverLifetimeModule!: SizeOverLifetimeModule;
525
+ // cameraScale: number = 1;
526
+ // applyBehaviour(particle: ParticleWithScale, _deltaTime: number, _index: number) {
527
+ // if (particle.target.type === "Sprite") {
528
+ // particle.radius = 1 / this.cameraScale;
529
+ // }
530
+ // else particle.radius = 1;
531
+ // // particle.target.scale.set(1,1,1);
532
+ // if (this.sizeOverLifetimeModule?.enabled) {
533
+ // const time = particle.age / particle.life;
534
+ // const scaleVector = particle.target.scale;
535
+ // this.sizeOverLifetimeModule.evaluate?.call(this.sizeOverLifetimeModule, time, scaleVector);
536
+ // // scaleVector.set(.1, .1, .1)
537
+ // particle.scale = particle.start_scale * scaleVector.x;
538
+ // // console.log(time, particle.scale);
539
+ // }
540
+ // }
541
+ // initialize(particle: ParticleWithScale) {
542
+ // particle.scale = this.startCurve.evaluate(0, Math.random());// * this.startMultiplier;
543
+ // particle.start_scale = particle.scale;
544
+ // }
545
+ // }
546
+ // declare type ParticleWithColor = Particle & { color_start: { r: number, g: number, b: number }, alpha_start: number };
547
+ // class GradientBehaviour extends CustomBehaviour {
548
+ // private startGradient: MinMaxGradient;
549
+ // private gradient?: MinMaxGradient;
550
+ // constructor(startGradient: MinMaxGradient, gradient?: MinMaxGradient) {
551
+ // super();
552
+ // this.startGradient = startGradient;
553
+ // this.gradient = gradient;
554
+ // }
555
+ // /** called from nebula */
556
+ // applyBehaviour(target: ParticleWithColor | Emitter, _deltaTime: number, _index: number) {
557
+ // // console.log(target, time, index)
558
+ // if (target instanceof Particle) {
559
+ // if (this.gradient) {
560
+ // const t = target.age / target.life;
561
+ // const ev = this.gradient.evaluate(t) as RGBAColor;
562
+ // const startColor = target.color_start;
563
+ // target.color.r = startColor.r * ev.r;
564
+ // target.color.g = startColor.g * ev.g;
565
+ // target.color.b = startColor.b * ev.b;
566
+ // target.alpha = ev.alpha * target.alpha_start;
567
+ // }
568
+ // }
569
+ // }
570
+ // private id = Symbol('gradientId');
571
+ // private _internalId: number = 0;
572
+ // /** called from nebula */
573
+ // initialize(particle: ParticleWithColor) {
574
+ // // console.log(particle);
575
+ // // const context = Context.Current;
576
+ // // const time = context.time.time;
577
+ // const ev = this.startGradient.evaluate(Math.random()) as RGBAColor;
578
+ // particle.color.r = ev.r;
579
+ // particle.color.g = ev.g;
580
+ // particle.color.b = ev.b;
581
+ // particle.alpha = ev.alpha
582
+ // particle.age = 0;
583
+ // particle.color_start = { r: ev.r, g: ev.g, b: ev.b };
584
+ // particle.alpha_start = ev.alpha;
585
+ // particle.useColor = true;
586
+ // particle.useAlpha = true;
587
+ // if (particle[this.id] === undefined)
588
+ // particle[this.id] = this._internalId++;
589
+ // }
590
+ // }
591
+ // declare type ParticleWithVelocity = Particle & { velocity_start: { x: number, y: number, u: number }, gravity: number };
592
+ // class VelocityBehaviour extends CustomBehaviour {
593
+ // container: Object3D;
594
+ // shape: ShapeModule;
595
+ // noise: NoiseModule;
596
+ // startSpeed: MinMaxCurve;
597
+ // startMultiplier: number = 1;
598
+ // gravity?: MinMaxCurve;
599
+ // gravityModifierMultiplier: number = 1;
600
+ // downDirection = new Vector3();
601
+ // private _temp: Vector3 = new Vector3();
602
+ // constructor(container: Object3D, shape: ShapeModule, noise: NoiseModule, startCurve: MinMaxCurve, startMultiplier: number) {
603
+ // super();
604
+ // this.container = container;
605
+ // this.shape = shape;
606
+ // this.noise = noise;
607
+ // this.startSpeed = startCurve;
608
+ // this.startMultiplier = startMultiplier;
609
+ // }
610
+ // applyBehaviour(target: ParticleWithVelocity | Emitter, deltaTime: number, index: number) {
611
+ // if (target instanceof Particle) {
612
+ // // target.velocity.x = target.velocity_start.x;
613
+ // // target.velocity.y = target.velocity_start.y;
614
+ // // target.velocity.z = target.velocity_start.z;
615
+ // if (target.gravity !== 0 && this.gravity) {
616
+ // // get world down vector
617
+ // this._temp.copy(this.downDirection);
618
+ // this._temp.multiplyScalar(target.gravity * deltaTime);
619
+ // target.velocity.add(this._temp);
620
+ // }
621
+ // if (this.noise) {
622
+ // this.noise.applyNoise(index, target.position, target.velocity, deltaTime, target.age, target.life);
623
+ // }
624
+ // }
625
+ // }
626
+ // initialize(particle: ParticleWithVelocity) {
627
+ // const dir = this.shape.getDirection(particle.position);
628
+ // // dir.applyQuaternion(quat);
629
+ // const speed = this.startSpeed.evaluate(0, Math.random()) * this.startMultiplier;
630
+ // particle.velocity.x = dir.x * speed;
631
+ // particle.velocity.y = dir.y * speed;
632
+ // particle.velocity.z = dir.z * speed;
633
+ // if (!particle.velocity_start)
634
+ // particle.velocity_start = {};
635
+ // particle.velocity_start.x = particle.velocity.x;
636
+ // particle.velocity_start.y = particle.velocity.y;
637
+ // particle.velocity_start.z = particle.velocity.z;
638
+ // if (this.gravity) {
639
+ // particle.gravity = this.gravity.evaluate(Math.random(), Math.random()) * 9.81;
640
+ // }
641
+ // else {
642
+ // particle.gravity = 0;
643
+ // }
644
+ // const quat = getWorldQuaternion(this.container);
645
+ // this.downDirection.set(0, -1, 0).applyQuaternion(quat);
646
+ // }
647
+ // }
375
648
  //# sourceMappingURL=ParticleSystem.js.map