laymur 0.26.0 → 0.27.0

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 (134) hide show
  1. package/README.md +79 -35
  2. package/dist/core/constraints/UICustomConstraint.d.ts +1 -1
  3. package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +1 -1
  4. package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +1 -1
  5. package/dist/core/constraints/builders/UIConstraint2DBuilder.d.ts +2 -3
  6. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.d.ts +2 -2
  7. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.js +24 -12
  8. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +10 -0
  9. package/dist/core/elements/UIDummy/UIDummy.d.ts +42 -5
  10. package/dist/core/elements/UIDummy/UIDummy.js +119 -25
  11. package/dist/core/elements/UIElement/UIElement.Internal.d.ts +1 -1
  12. package/dist/core/elements/UIElement/UIElement.d.ts +14 -0
  13. package/dist/core/elements/UIElement/UIElement.js +34 -3
  14. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +1 -1
  15. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +1 -1
  16. package/dist/core/elements/UIGraphics/UIGraphics.js +2 -2
  17. package/dist/core/elements/UIImage/UIImage.d.ts +2 -2
  18. package/dist/core/elements/UIImage/UIImage.js +10 -8
  19. package/dist/core/elements/UINineSlice/UINineSlice.d.ts +4 -4
  20. package/dist/core/elements/UINineSlice/UINineSlice.js +11 -9
  21. package/dist/core/elements/UIProgress/UIProgress.d.ts +2 -2
  22. package/dist/core/elements/UIProgress/UIProgress.js +15 -10
  23. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +1 -1
  24. package/dist/core/elements/UIScene/UIScene.d.ts +3 -2
  25. package/dist/core/elements/UIScene/UIScene.js +18 -12
  26. package/dist/core/elements/UIText/UIText.Measuring.d.ts +1 -1
  27. package/dist/core/elements/UIText/UIText.d.ts +3 -3
  28. package/dist/core/elements/UIText/UIText.js +6 -6
  29. package/dist/core/index.d.ts +44 -41
  30. package/dist/core/index.js +12 -9
  31. package/dist/core/layers/UILayer/UILayer.d.ts +40 -4
  32. package/dist/core/layers/UILayer/UILayer.js +70 -9
  33. package/dist/core/layers/UIViewportLayer/UIViewportLayer.Internal.d.ts +27 -0
  34. package/dist/core/layers/{UIFullscreenLayer/UIFullscreenLayer.Internal.js → UIViewportLayer/UIViewportLayer.Internal.js} +1 -1
  35. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +98 -0
  36. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +251 -0
  37. package/dist/core/miscellaneous/asserts.d.ts +14 -0
  38. package/dist/core/miscellaneous/asserts.js +28 -0
  39. package/dist/core/miscellaneous/canvas.js +7 -2
  40. package/dist/core/miscellaneous/color/UIColor.js +4 -2
  41. package/dist/core/miscellaneous/computeTransform.d.ts +12 -0
  42. package/dist/core/miscellaneous/computeTransform.js +20 -0
  43. package/dist/core/miscellaneous/context-monitor/UIContextMonitor.d.ts +1 -0
  44. package/dist/core/miscellaneous/context-monitor/UIContextMonitor.js +60 -0
  45. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.js +1 -1
  46. package/dist/core/miscellaneous/resize-policy/UIResizePolicy.d.ts +7 -4
  47. package/dist/core/miscellaneous/resize-policy/UIResizePolicy.js +13 -6
  48. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCover.js +2 -2
  49. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCross.js +2 -2
  50. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCrossInverted.js +2 -2
  51. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFit.js +2 -2
  52. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedHeight.js +2 -2
  53. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedWidth.js +2 -2
  54. package/dist/core/miscellaneous/text-style/UITextStyle.js +2 -2
  55. package/dist/core/miscellaneous/texture/UITextureView.d.ts +4 -1
  56. package/dist/core/miscellaneous/texture/UITextureView.js +10 -7
  57. package/dist/core/miscellaneous/viewport/UIElementViewport.d.ts +31 -0
  58. package/dist/core/miscellaneous/viewport/UIElementViewport.js +54 -0
  59. package/dist/core/miscellaneous/viewport/UIViewport.d.ts +37 -0
  60. package/dist/core/miscellaneous/viewport/UIViewport.js +31 -0
  61. package/dist/core/miscellaneous/viewport/UIWindowViewport.d.ts +13 -0
  62. package/dist/core/miscellaneous/viewport/UIWindowViewport.js +25 -0
  63. package/dist/core/miscellaneous/webglCapabilities.js +61 -55
  64. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +36 -5
  65. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +23 -2
  66. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +153 -15
  67. package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.Internal.d.ts +1 -1
  68. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +2 -0
  69. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +3 -0
  70. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +36 -16
  71. package/dist/debug/UIAspectDebug.js +4 -0
  72. package/dist/debug/UIHeightDebug.js +4 -0
  73. package/dist/debug/UIHorizontalDistanceDebug.js +5 -0
  74. package/dist/debug/UIHorizontalInterpolationDebug.js +5 -0
  75. package/dist/debug/UILayerDebug.d.ts +7 -0
  76. package/dist/debug/UILayerDebug.js +21 -0
  77. package/dist/debug/UIVerticalDistanceDebug.js +5 -0
  78. package/dist/debug/UIVerticalInterpolationDebug.js +5 -0
  79. package/dist/debug/UIWidthDebug.js +4 -0
  80. package/dist/debug/index.d.ts +1 -1
  81. package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.d.ts +1 -1
  82. package/dist/particles/behaviorModules/UIBehaviorModule.d.ts +5 -1
  83. package/dist/particles/behaviorModules/UIBehaviorPointGravity.d.ts +2 -2
  84. package/dist/particles/behaviorModules/UIBehaviorPointGravity.js +13 -10
  85. package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.d.ts +1 -1
  86. package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.js +6 -4
  87. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.d.ts +1 -1
  88. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.js +4 -3
  89. package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.d.ts +2 -2
  90. package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.js +8 -8
  91. package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.d.ts +4 -3
  92. package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.js +8 -5
  93. package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.d.ts +1 -1
  94. package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.js +8 -8
  95. package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.d.ts +7 -5
  96. package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.js +12 -8
  97. package/dist/particles/elements/UIEmitter.Internal.d.ts +15 -2
  98. package/dist/particles/elements/UIEmitter.Internal.js +6 -0
  99. package/dist/particles/elements/UIEmitter.d.ts +19 -3
  100. package/dist/particles/elements/UIEmitter.js +100 -54
  101. package/dist/particles/index.d.ts +3 -2
  102. package/dist/particles/index.js +1 -0
  103. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.d.ts +8 -2
  104. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.js +16 -11
  105. package/dist/particles/instancedParticle/UIInstancedParticle.d.ts +7 -0
  106. package/dist/particles/instancedParticle/UIInstancedParticle.js +37 -2
  107. package/dist/particles/miscellaneous/miscellaneous.d.ts +34 -0
  108. package/dist/particles/miscellaneous/miscellaneous.js +72 -0
  109. package/dist/particles/renderingModules/UIRenderingColorOverLife.js +2 -2
  110. package/dist/particles/renderingModules/UIRenderingColorOverVelocity.js +2 -3
  111. package/dist/particles/renderingModules/UIRenderingModule.d.ts +3 -2
  112. package/dist/particles/renderingModules/UIRenderingModule.js +2 -1
  113. package/dist/particles/renderingModules/UIRenderingTexture.js +2 -1
  114. package/dist/particles/shaders/UIRenderingColorOverLife.d.ts +1 -2
  115. package/dist/particles/shaders/UIRenderingColorOverLife.js +5 -3
  116. package/dist/particles/shaders/UIRenderingColorOverVelocity.d.ts +1 -9
  117. package/dist/particles/shaders/UIRenderingColorOverVelocity.js +6 -13
  118. package/dist/particles/spawnModules/UISpawnModule.d.ts +5 -2
  119. package/dist/particles/spawnModules/UISpawnRandomLifetime.d.ts +2 -2
  120. package/dist/particles/spawnModules/UISpawnRandomLifetime.js +5 -5
  121. package/dist/particles/spawnModules/UISpawnRandomRotation.d.ts +1 -1
  122. package/dist/particles/spawnModules/UISpawnRandomRotation.js +5 -3
  123. package/dist/particles/spawnModules/UISpawnRandomScale.d.ts +1 -1
  124. package/dist/particles/spawnModules/UISpawnRandomScale.js +4 -3
  125. package/dist/particles/spawnModules/UISpawnRandomTorque.d.ts +1 -1
  126. package/dist/particles/spawnModules/UISpawnRandomTorque.js +5 -3
  127. package/dist/particles/spawnModules/UISpawnRandomVelocity.d.ts +1 -1
  128. package/dist/particles/spawnModules/UISpawnRandomVelocity.js +6 -4
  129. package/dist/particles/spawnModules/UISpawnRectangle.d.ts +1 -1
  130. package/dist/particles/spawnModules/UISpawnRectangle.js +6 -4
  131. package/package.json +13 -4
  132. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.Internal.d.ts +0 -21
  133. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.d.ts +0 -63
  134. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.js +0 -144
@@ -1,12 +1,23 @@
1
1
  import { Vector2 } from "three";
2
2
  import { UIAnchor } from "../../core/elements/UIAnchor/UIAnchor.js";
3
- import { assertValidNumber } from "../../core/miscellaneous/asserts.js";
3
+ import { assertValidNumber, assertValidPositiveNumber } from "../../core/miscellaneous/asserts.js";
4
4
  import { resolveGLSLTypeInfo } from "../../core/miscellaneous/generic-plane/shared.js";
5
5
  import { isUIModeVisible } from "../../core/miscellaneous/UIMode.js";
6
+ import { collectProperties, collectRenderingUniforms, } from "../instancedParticle/UIInstancedParticle.Internal.js";
6
7
  import { UIInstancedParticle } from "../instancedParticle/UIInstancedParticle.js";
7
- import { collectProperties, collectUniforms, } from "../instancedParticle/UIInstancedParticle.Internal.js";
8
- import { BUILTIN_OFFSET_AGE, BUILTIN_OFFSET_POSITION_X, BUILTIN_OFFSET_POSITION_Y, BUILTIN_OFFSET_RANDOM_A, BUILTIN_OFFSET_RANDOM_B, BUILTIN_OFFSET_RANDOM_C, BUILTIN_OFFSET_RANDOM_D, BUILTIN_OFFSET_RANDOM_E, BUILTIN_OFFSET_RANDOM_F, BUILTIN_OFFSET_ROTATION, BUILTIN_OFFSET_TORQUE, BUILTIN_OFFSET_VELOCITY_X, BUILTIN_OFFSET_VELOCITY_Y, } from "../miscellaneous/miscellaneous.js";
9
- import { EMITTER_DEFAULT_AUTOMATICALLY_DESTROY_MODULES, EMITTER_DEFAULT_CAPACITY_STEP, EMITTER_DEFAULT_EXPECTED_CAPACITY, EMITTER_DEFAULT_MODE, ignoreInput, } from "./UIEmitter.Internal.js";
8
+ import { assertValidNonNegativeInteger, BUILTIN_OFFSET_AGE, BUILTIN_OFFSET_LIFETIME, BUILTIN_OFFSET_POSITION_X, BUILTIN_OFFSET_POSITION_Y, BUILTIN_OFFSET_RANDOM_A, BUILTIN_OFFSET_RANDOM_B, BUILTIN_OFFSET_RANDOM_C, BUILTIN_OFFSET_RANDOM_D, BUILTIN_OFFSET_RANDOM_E, BUILTIN_OFFSET_ROTATION, BUILTIN_OFFSET_SCALE_X, BUILTIN_OFFSET_SCALE_Y, BUILTIN_OFFSET_TORQUE, BUILTIN_OFFSET_VELOCITY_X, BUILTIN_OFFSET_VELOCITY_Y, computeParticleSeed, particleRandom, particleRandomStream, writeParticleSeed, } from "../miscellaneous/miscellaneous.js";
9
+ import { EMITTER_BEHAVIOR_SALT_BASE, EMITTER_DEFAULT_AUTOMATICALLY_DESTROY_MODULES, EMITTER_DEFAULT_CAPACITY_STEP, EMITTER_DEFAULT_EXPECTED_CAPACITY, EMITTER_DEFAULT_LIFETIME, EMITTER_DEFAULT_MAX_DELTA_TIME, EMITTER_DEFAULT_MODE, EMITTER_SHADER_SALT, EMITTER_SPAWN_SALT_BASE, ignoreInput, } from "./UIEmitter.Internal.js";
10
+ /** Builtin slots of the random values the shaders read, each with its stream */
11
+ const SHADER_RANDOM_SLOTS = [
12
+ BUILTIN_OFFSET_RANDOM_A,
13
+ BUILTIN_OFFSET_RANDOM_B,
14
+ BUILTIN_OFFSET_RANDOM_C,
15
+ BUILTIN_OFFSET_RANDOM_D,
16
+ BUILTIN_OFFSET_RANDOM_E,
17
+ ].map((offset, channel) => ({
18
+ offset,
19
+ stream: particleRandomStream(EMITTER_SHADER_SALT, channel),
20
+ }));
10
21
  /**
11
22
  * Particle emitter element.
12
23
  *
@@ -29,30 +40,31 @@ export class UIEmitter extends UIAnchor {
29
40
  this.emissionAccumulator = 0;
30
41
  this.emissionDuration = Infinity;
31
42
  this.emissionElapsed = 0;
32
- this.onRendering = (renderer, deltaTime) => {
43
+ /** Particles spawned since the seed was set; picks the seed of the next particle */
44
+ this.spawnedCount = 0;
45
+ this.onRendering = (renderer, rawDeltaTime) => {
33
46
  this.layer.sceneWrapper.setOrderedObjectDepth(this.mesh, this.layer.inputWrapper.getDepth(this.catcherHandler));
34
- {
35
- const { builtin } = this.mesh.propertyBuffers;
36
- const { array, itemSize } = builtin;
47
+ const deltaTime = Math.min(rawDeltaTime, this.maxDeltaTimeInternal);
48
+ if (this.mesh.instanceCount > 0) {
49
+ const { array, itemSize } = this.mesh.propertyBuffers.builtin;
37
50
  for (let i = 0; i < this.mesh.instanceCount; i++) {
38
51
  array[i * itemSize + BUILTIN_OFFSET_AGE] += deltaTime;
39
52
  }
40
- builtin.needsUpdate = true;
41
53
  this.mesh.removeDeadParticles();
42
54
  }
43
55
  if (this.emissionRate > 0) {
56
+ const emittedBefore = this.emissionElapsed;
44
57
  this.emissionElapsed += deltaTime;
58
+ this.emissionAccumulator +=
59
+ this.emissionRate * (Math.min(this.emissionElapsed, this.emissionDuration) - emittedBefore);
60
+ const particlesToSpawn = Math.floor(this.emissionAccumulator);
61
+ if (particlesToSpawn > 0) {
62
+ this.burst(particlesToSpawn);
63
+ this.emissionAccumulator -= particlesToSpawn;
64
+ }
45
65
  if (this.emissionElapsed >= this.emissionDuration) {
46
66
  this.stop();
47
67
  }
48
- else {
49
- this.emissionAccumulator += this.emissionRate * deltaTime;
50
- const particlesToSpawn = Math.floor(this.emissionAccumulator);
51
- if (particlesToSpawn > 0) {
52
- this.burst(particlesToSpawn);
53
- this.emissionAccumulator -= particlesToSpawn;
54
- }
55
- }
56
68
  }
57
69
  if (this.mesh.instanceCount === 0) {
58
70
  return;
@@ -60,30 +72,23 @@ export class UIEmitter extends UIAnchor {
60
72
  this.mesh.setOrigin(this.x, this.y);
61
73
  {
62
74
  const { propertyBuffers, instanceCount } = this.mesh;
63
- for (const module of this.behaviorSequence) {
64
- module.update(propertyBuffers, instanceCount, deltaTime);
75
+ for (let index = 0; index < this.behaviorSequence.length; index++) {
76
+ this.behaviorSequence[index].update(propertyBuffers, instanceCount, deltaTime, EMITTER_BEHAVIOR_SALT_BASE + index);
65
77
  }
66
78
  }
67
79
  const { builtin } = this.mesh.propertyBuffers;
68
80
  const { array, itemSize } = builtin;
69
- {
70
- for (let i = 0; i < this.mesh.instanceCount; i++) {
71
- const itemOffset = i * itemSize;
72
- array[itemOffset + BUILTIN_OFFSET_POSITION_X] +=
73
- array[itemOffset + BUILTIN_OFFSET_VELOCITY_X] * deltaTime;
74
- array[itemOffset + BUILTIN_OFFSET_POSITION_Y] +=
75
- array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y] * deltaTime;
76
- }
77
- builtin.needsUpdate = true;
78
- }
79
- {
80
- for (let i = 0; i < this.mesh.instanceCount; i++) {
81
- const itemOffset = i * itemSize;
82
- array[itemOffset + BUILTIN_OFFSET_ROTATION] +=
83
- array[itemOffset + BUILTIN_OFFSET_TORQUE] * deltaTime;
84
- }
85
- builtin.needsUpdate = true;
81
+ for (let i = 0; i < this.mesh.instanceCount; i++) {
82
+ const itemOffset = i * itemSize;
83
+ array[itemOffset + BUILTIN_OFFSET_POSITION_X] +=
84
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_X] * deltaTime;
85
+ array[itemOffset + BUILTIN_OFFSET_POSITION_Y] +=
86
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y] * deltaTime;
87
+ array[itemOffset + BUILTIN_OFFSET_ROTATION] +=
88
+ array[itemOffset + BUILTIN_OFFSET_TORQUE] * deltaTime;
86
89
  }
90
+ builtin.needsUpdate = true;
91
+ this.mesh.limitUploadToLiveInstances();
87
92
  };
88
93
  const collectedProperties = {
89
94
  builtin: resolveGLSLTypeInfo("Matrix4"),
@@ -92,8 +97,8 @@ export class UIEmitter extends UIAnchor {
92
97
  // 4, 5 scaleX, scaleY
93
98
  // 6, 7 rotation, torque
94
99
  // 8, 9 lifetime, age
95
- // 10, 11, 12 randomX, randomY, randomZ
96
- // 13, 14, 16 reservedX, reservedY, reservedZ
100
+ // 10-14 random values A-E for shaders
101
+ // 15 seed, read by shaders as random value F
97
102
  };
98
103
  collectProperties(collectedProperties, spawnSequence, "UIEmitter.constructor.spawnSequence");
99
104
  collectProperties(collectedProperties, behaviorSequence, "UIEmitter.constructor.behaviorSequence");
@@ -101,15 +106,33 @@ export class UIEmitter extends UIAnchor {
101
106
  const collectedUniforms = {
102
107
  origin: { value: new Vector2(), glslTypeInfo: resolveGLSLTypeInfo("Vector2") },
103
108
  };
104
- collectUniforms(collectedUniforms, renderingSequence, "UIEmitter.constructor.renderingSequence");
109
+ const renderingSources = collectRenderingUniforms(collectedUniforms, renderingSequence, "UIEmitter.constructor.renderingSequence");
105
110
  const zIndex = 0;
106
- this.catcherHandler = this.layer.inputWrapper.createInputCatcher(ignoreInput, ignoreInput, ignoreInput, zIndex);
107
- this.mesh = new UIInstancedParticle(this.renderingSequence.map((module) => module.source), collectedUniforms, collectedProperties, options.expectedCapacity ?? EMITTER_DEFAULT_EXPECTED_CAPACITY, options.capacityStep ?? EMITTER_DEFAULT_CAPACITY_STEP);
111
+ this.catcherHandler = this.layer.inputWrapper.createInputCatcher({
112
+ catchPointerDown: ignoreInput,
113
+ catchPointerMove: ignoreInput,
114
+ catchPointerUp: ignoreInput,
115
+ catchPointerCancel: ignoreInput,
116
+ }, zIndex);
117
+ this.mesh = new UIInstancedParticle(renderingSources, collectedUniforms, collectedProperties, options.expectedCapacity ?? EMITTER_DEFAULT_EXPECTED_CAPACITY, options.capacityStep ?? EMITTER_DEFAULT_CAPACITY_STEP);
108
118
  this.layer.signalRendering.on(this.onRendering);
109
119
  this.layer.sceneWrapper.insertOrderedObject(this.mesh, this.layer.inputWrapper.getDepth(this.catcherHandler));
110
120
  this.automaticallyDestroyModules =
111
121
  options.automaticallyDestroyModules ?? EMITTER_DEFAULT_AUTOMATICALLY_DESTROY_MODULES;
112
122
  this.modeInternal = options.mode ?? EMITTER_DEFAULT_MODE;
123
+ this.mesh.visible = isUIModeVisible(this.modeInternal);
124
+ this.maxDeltaTimeInternal = options.maxDeltaTime ?? EMITTER_DEFAULT_MAX_DELTA_TIME;
125
+ assertValidPositiveNumber(this.maxDeltaTimeInternal, "UIEmitter.constructor.maxDeltaTime");
126
+ this.seedInternal = options.seed ?? Math.floor(Math.random() * 0x100000000);
127
+ assertValidNonNegativeInteger(this.seedInternal, "UIEmitter.constructor.seed");
128
+ }
129
+ /** Seed of the random values */
130
+ get seed() {
131
+ return this.seedInternal;
132
+ }
133
+ /** Longest frame step in seconds; longer frames are clamped */
134
+ get maxDeltaTime() {
135
+ return this.maxDeltaTimeInternal;
113
136
  }
114
137
  /** Visibility mode */
115
138
  get mode() {
@@ -130,6 +153,22 @@ export class UIEmitter extends UIAnchor {
130
153
  this.mesh.visible = isUIModeVisible(value);
131
154
  }
132
155
  }
156
+ /** Longest frame step in seconds; longer frames are clamped */
157
+ set maxDeltaTime(value) {
158
+ this.assertNotDestroyed("UIEmitter.maxDeltaTime");
159
+ assertValidPositiveNumber(value, "UIEmitter.maxDeltaTime");
160
+ this.maxDeltaTimeInternal = value;
161
+ }
162
+ /**
163
+ * Sets the seed and restarts its sequence: the next particles repeat those spawned after the
164
+ * same seed was set before
165
+ */
166
+ set seed(value) {
167
+ this.assertNotDestroyed("UIEmitter.seed");
168
+ assertValidNonNegativeInteger(value, "UIEmitter.seed");
169
+ this.seedInternal = value;
170
+ this.spawnedCount = 0;
171
+ }
133
172
  /**
134
173
  * Rendering order among the elements of the layer. Higher values on top; elements with equal
135
174
  * values keep the order of creation, a later one on top.
@@ -140,32 +179,39 @@ export class UIEmitter extends UIAnchor {
140
179
  this.layer.inputWrapper.setZIndex(this.catcherHandler, value);
141
180
  }
142
181
  /**
143
- * Spawns a specific number of particles immediately.
182
+ * Spawns a specific number of particles immediately. Before the spawn modules run, every
183
+ * property of a new particle is 0, except scale 1 and lifetime 1 second.
144
184
  *
145
- * @param count - Number of particles to create
185
+ * @param count - Number of particles to create, a non-negative integer
146
186
  */
147
187
  burst(count) {
148
188
  this.assertNotDestroyed("UIEmitter.burst");
189
+ assertValidNonNegativeInteger(count, "UIEmitter.burst.count");
190
+ if (count === 0) {
191
+ return;
192
+ }
149
193
  const instanceBegin = this.mesh.instanceCount;
150
194
  this.mesh.createInstances(count);
151
195
  const instanceEnd = this.mesh.instanceCount;
196
+ // A slot may hold a dead particle's values.
197
+ this.mesh.resetInstances(instanceBegin, instanceEnd);
152
198
  {
153
- const { builtin } = this.mesh.propertyBuffers;
154
- const { array, itemSize } = builtin;
199
+ const { array, itemSize } = this.mesh.propertyBuffers.builtin;
155
200
  for (let i = instanceBegin; i < instanceEnd; i++) {
156
201
  const itemOffset = i * itemSize;
157
- array[itemOffset + BUILTIN_OFFSET_RANDOM_A] = Math.random();
158
- array[itemOffset + BUILTIN_OFFSET_RANDOM_B] = Math.random();
159
- array[itemOffset + BUILTIN_OFFSET_RANDOM_C] = Math.random();
160
- array[itemOffset + BUILTIN_OFFSET_RANDOM_D] = Math.random();
161
- array[itemOffset + BUILTIN_OFFSET_RANDOM_E] = Math.random();
162
- array[itemOffset + BUILTIN_OFFSET_RANDOM_F] = Math.random();
202
+ array[itemOffset + BUILTIN_OFFSET_SCALE_X] = 1;
203
+ array[itemOffset + BUILTIN_OFFSET_SCALE_Y] = 1;
204
+ array[itemOffset + BUILTIN_OFFSET_LIFETIME] = EMITTER_DEFAULT_LIFETIME;
205
+ writeParticleSeed(array, itemOffset, computeParticleSeed(this.seedInternal, this.spawnedCount++));
206
+ for (const { offset, stream } of SHADER_RANDOM_SLOTS) {
207
+ array[itemOffset + offset] = particleRandom(array, itemOffset, stream);
208
+ }
163
209
  }
164
- builtin.needsUpdate = true;
165
210
  }
166
- for (const spawnModule of this.spawnSequence) {
167
- spawnModule.spawn(this.mesh.propertyBuffers, instanceBegin, instanceEnd);
211
+ for (let index = 0; index < this.spawnSequence.length; index++) {
212
+ this.spawnSequence[index].spawn(this.mesh.propertyBuffers, instanceBegin, instanceEnd, EMITTER_SPAWN_SALT_BASE + index);
168
213
  }
214
+ this.mesh.limitUploadToLiveInstances();
169
215
  }
170
216
  /** Removes all active particles */
171
217
  clear() {
@@ -1,16 +1,17 @@
1
1
  export { UIBehaviorDirectionalGravity } from "./behaviorModules/UIBehaviorDirectionalGravity.js";
2
2
  export { UIBehaviorModule } from "./behaviorModules/UIBehaviorModule.js";
3
3
  export { UIBehaviorPointGravity } from "./behaviorModules/UIBehaviorPointGravity.js";
4
- export { UIBehaviorScaleOscillation } from "./behaviorModules/UIBehaviorScaleOscillation.js";
5
4
  export type { UIScaleOscillationAxis } from "./behaviorModules/UIBehaviorScaleOscillation.js";
5
+ export { UIBehaviorScaleOscillation } from "./behaviorModules/UIBehaviorScaleOscillation.js";
6
6
  export { UIBehaviorScaleOverLife } from "./behaviorModules/UIBehaviorScaleOverLife.js";
7
7
  export { UIBehaviorTorqueDamping } from "./behaviorModules/UIBehaviorTorqueDamping.js";
8
8
  export { UIBehaviorTorqueNoise } from "./behaviorModules/UIBehaviorTorqueNoise.js";
9
9
  export { UIBehaviorVelocityDamping } from "./behaviorModules/UIBehaviorVelocityDamping.js";
10
10
  export { UIBehaviorVelocityNoise } from "./behaviorModules/UIBehaviorVelocityNoise.js";
11
- export { UIEmitter } from "./elements/UIEmitter.js";
12
11
  export type { UIEmitterMode, UIEmitterOptions, UIEmitterPlayOptions, } from "./elements/UIEmitter.Internal.js";
12
+ export { UIEmitter } from "./elements/UIEmitter.js";
13
13
  export type { UIAspectConfig, UIRange, UIRangeConfig, UIVector2Config, } from "./miscellaneous/miscellaneous.js";
14
+ export { particleRandom, particleRandomStream } from "./miscellaneous/miscellaneous.js";
14
15
  export { UIRenderingColorOverLife } from "./renderingModules/UIRenderingColorOverLife.js";
15
16
  export { UIRenderingColorOverVelocity } from "./renderingModules/UIRenderingColorOverVelocity.js";
16
17
  export { UIRenderingModule } from "./renderingModules/UIRenderingModule.js";
@@ -8,6 +8,7 @@ export { UIBehaviorTorqueNoise } from "./behaviorModules/UIBehaviorTorqueNoise.j
8
8
  export { UIBehaviorVelocityDamping } from "./behaviorModules/UIBehaviorVelocityDamping.js";
9
9
  export { UIBehaviorVelocityNoise } from "./behaviorModules/UIBehaviorVelocityNoise.js";
10
10
  export { UIEmitter } from "./elements/UIEmitter.js";
11
+ export { particleRandom, particleRandomStream } from "./miscellaneous/miscellaneous.js";
11
12
  export { UIRenderingColorOverLife } from "./renderingModules/UIRenderingColorOverLife.js";
12
13
  export { UIRenderingColorOverVelocity } from "./renderingModules/UIRenderingColorOverVelocity.js";
13
14
  export { UIRenderingModule } from "./renderingModules/UIRenderingModule.js";
@@ -4,9 +4,15 @@ export declare const INSTANCED_PARTICLE_GEOMETRY: InstancedBufferGeometry;
4
4
  export declare function collectProperties(keeper: Record<string, GLTypeInfo>, modules: readonly {
5
5
  requiredProperties?: Record<string, UIPropertyName>;
6
6
  }[], debugContext: string): void;
7
- export declare function collectUniforms(keeper: Record<string, GLProperty>, modules: readonly {
7
+ /**
8
+ * Collects the uniforms of rendering modules under per-module names, `p_<name>` of module i
9
+ * becoming `p_m<i>_<name>`, so modules of one type keep their own values.
10
+ * @returns Sources with their uniform references renamed accordingly
11
+ */
12
+ export declare function collectRenderingUniforms(keeper: Record<string, GLProperty>, modules: readonly {
8
13
  requiredUniforms: Record<string, UIProperty>;
9
- }[], debugContext: string): void;
14
+ source: string;
15
+ }[], debugContext: string): string[];
10
16
  export declare function buildParticleVertexShader(uniformDeclarations: string[], attributeDeclarations: string[], varyingDeclarations: string[], vertexAssignments: string[]): string;
11
17
  /**
12
18
  * Builds the fragment shader of a particle mesh. Sources read particle values through the
@@ -54,20 +54,25 @@ export function collectProperties(keeper, modules, debugContext) {
54
54
  }
55
55
  }
56
56
  }
57
- export function collectUniforms(keeper, modules, debugContext) {
58
- for (const module of modules) {
57
+ /**
58
+ * Collects the uniforms of rendering modules under per-module names, `p_<name>` of module i
59
+ * becoming `p_m<i>_<name>`, so modules of one type keep their own values.
60
+ * @returns Sources with their uniform references renamed accordingly
61
+ */
62
+ export function collectRenderingUniforms(keeper, modules, debugContext) {
63
+ return modules.map((module, index) => {
64
+ let { source } = module;
59
65
  for (const key in module.requiredUniforms) {
60
- const value = module.requiredUniforms[key];
61
- const existingGLProperty = keeper[key];
62
- const newPropertyTypeInfo = resolveGLSLTypeInfo(value);
63
- if (existingGLProperty === undefined) {
64
- keeper[key] = { value, glslTypeInfo: newPropertyTypeInfo };
65
- }
66
- else if (existingGLProperty.glslTypeInfo.glslTypeName !== newPropertyTypeInfo.glslTypeName) {
67
- throw new Error(`${debugContext}: uniform conflict for "${key}" (${existingGLProperty.glslTypeInfo.glslTypeName} and ${newPropertyTypeInfo.glslTypeName})`);
66
+ if (!/^\w+$/.test(key)) {
67
+ throw new Error(`${debugContext}[${index}]: uniform name "${key}" is not a GLSL identifier`);
68
68
  }
69
+ const value = module.requiredUniforms[key];
70
+ const name = `m${index}_${key}`;
71
+ keeper[name] = { value, glslTypeInfo: resolveGLSLTypeInfo(value) };
72
+ source = source.replace(new RegExp(`\\bp_${key}\\b`, "g"), `p_${name}`);
69
73
  }
70
- }
74
+ return source;
75
+ });
71
76
  }
72
77
  function builtinComponent(base, index) {
73
78
  return `${base}[${Math.floor(index / 4)}].${"xyzw"[index % 4]}`;
@@ -12,6 +12,13 @@ export declare class UIInstancedParticle extends Mesh {
12
12
  constructor(sources: string[], uniforms: Record<string, GLProperty>, varyings: Record<string, GLTypeInfo>, expectedCapacity: number, capacityStep: number);
13
13
  get instanceCount(): number;
14
14
  createInstances(count: number): void;
15
+ /** Zeroes every property of the instances in [instanceBegin, instanceEnd) */
16
+ resetInstances(instanceBegin: number, instanceEnd: number): void;
17
+ /**
18
+ * Limits the next upload of each marked buffer to the live instances. Call only with live
19
+ * instances: an empty range uploads the whole buffer.
20
+ */
21
+ limitUploadToLiveInstances(): void;
15
22
  setOrigin(x: number, y: number): void;
16
23
  removeDeadParticles(): void;
17
24
  drop(): void;
@@ -2,7 +2,19 @@ import { InstancedBufferAttribute, Mesh, StreamDrawUsage } from "three";
2
2
  import { assertVaryingBudget, assertVertexAttributeBudget, } from "../../core/miscellaneous/asserts.js";
3
3
  import { resolvePropertyUniform } from "../../core/miscellaneous/generic-plane/shared.js";
4
4
  import { BUILTIN_OFFSET_AGE, BUILTIN_OFFSET_LIFETIME } from "../miscellaneous/miscellaneous.js";
5
- import { buildParticleMaterial, INSTANCED_PARTICLE_GEOMETRY } from "./UIInstancedParticle.Internal.js";
5
+ import { buildParticleMaterial, INSTANCED_PARTICLE_GEOMETRY, } from "./UIInstancedParticle.Internal.js";
6
+ function setUpdateRange(attribute, count) {
7
+ const ranged = attribute;
8
+ // Newer three.js replaced updateRange with addUpdateRange.
9
+ if (ranged.addUpdateRange !== undefined && ranged.clearUpdateRanges !== undefined) {
10
+ ranged.clearUpdateRanges();
11
+ ranged.addUpdateRange(0, count);
12
+ }
13
+ else if (ranged.updateRange !== undefined) {
14
+ ranged.updateRange.offset = 0;
15
+ ranged.updateRange.count = count;
16
+ }
17
+ }
6
18
  export class UIInstancedParticle extends Mesh {
7
19
  constructor(sources, uniforms, varyings, expectedCapacity, capacityStep) {
8
20
  const instancedGeometry = INSTANCED_PARTICLE_GEOMETRY.clone();
@@ -45,6 +57,26 @@ export class UIInstancedParticle extends Mesh {
45
57
  this.ensureCapacity(currentInstanceCount + count);
46
58
  this.instancedGeometry.instanceCount += count;
47
59
  }
60
+ /** Zeroes every property of the instances in [instanceBegin, instanceEnd) */
61
+ resetInstances(instanceBegin, instanceEnd) {
62
+ for (const name in this.propertyBuffers) {
63
+ const attribute = this.propertyBuffers[name];
64
+ const { array, itemSize } = attribute;
65
+ array.fill(0, instanceBegin * itemSize, instanceEnd * itemSize);
66
+ attribute.needsUpdate = true;
67
+ }
68
+ }
69
+ /**
70
+ * Limits the next upload of each marked buffer to the live instances. Call only with live
71
+ * instances: an empty range uploads the whole buffer.
72
+ */
73
+ limitUploadToLiveInstances() {
74
+ const { instanceCount } = this.instancedGeometry;
75
+ for (const name in this.propertyBuffers) {
76
+ const attribute = this.propertyBuffers[name];
77
+ setUpdateRange(attribute, instanceCount * attribute.itemSize);
78
+ }
79
+ }
48
80
  setOrigin(x, y) {
49
81
  const uniform = resolvePropertyUniform("origin", this.shaderMaterial);
50
82
  uniform.value.set(x, y);
@@ -86,7 +118,10 @@ export class UIInstancedParticle extends Mesh {
86
118
  if (requiredCapacity <= this.capacity) {
87
119
  return;
88
120
  }
89
- const newCapacity = Math.ceil(requiredCapacity / this.capacityStep) * this.capacityStep;
121
+ const newCapacity = Math.ceil(Math.max(this.capacity * 2, requiredCapacity) / this.capacityStep) *
122
+ this.capacityStep;
123
+ // Frees the GPU buffers of the replaced attributes; the renderer uploads the new ones.
124
+ this.instancedGeometry.dispose();
90
125
  for (const name in this.propertyBuffers) {
91
126
  const { itemSize, array, usage } = this.propertyBuffers[name];
92
127
  const newArray = new Float32Array(newCapacity * itemSize);
@@ -1,6 +1,7 @@
1
1
  import type { Vector2Tuple } from "three";
2
2
  import { DataTexture } from "three";
3
3
  import type { UIColor } from "../../core/index.js";
4
+ export { assertValidNonNegativeInteger, assertValidUnitNumber, } from "../../core/miscellaneous/asserts.js";
4
5
  import type { Vector2Like } from "../../core/miscellaneous/math.js";
5
6
  import type { UITextureConfig } from "../../core/miscellaneous/texture/UITextureView.Internal.js";
6
7
  export interface UIRange {
@@ -10,6 +11,10 @@ export interface UIRange {
10
11
  export type UIRangeConfig = UIRange | [number, number] | number;
11
12
  export type UIVector2Config = Vector2Like | Vector2Tuple | number;
12
13
  export type UIAspectConfig = number | UITextureConfig;
14
+ /** Formats a number as a GLSL float literal */
15
+ export declare function toGLSLFloat(value: number): string;
16
+ /** GLSL expression of the u that samples a gradient texture of `size` texels at t in [0, 1] */
17
+ export declare function buildGradientCoordinate(t: string, size: number): string;
13
18
  export declare function resolveUIRangeConfig(config: UIRangeConfig): UIRange;
14
19
  export declare function resolveUIVector2Config(config: UIVector2Config): Vector2Like;
15
20
  export declare function resolveAspect(config: number | UITextureConfig): number;
@@ -31,3 +36,32 @@ export declare const BUILTIN_OFFSET_RANDOM_C = 12;
31
36
  export declare const BUILTIN_OFFSET_RANDOM_D = 13;
32
37
  export declare const BUILTIN_OFFSET_RANDOM_E = 14;
33
38
  export declare const BUILTIN_OFFSET_RANDOM_F = 15;
39
+ /**
40
+ * Seed of the particle i of an emitter: a 24-bit integer
41
+ * @param emitterSeed Seed of the emitter
42
+ * @param index Number of particles the emitter spawned before this one
43
+ */
44
+ export declare function computeParticleSeed(emitterSeed: number, index: number): number;
45
+ /**
46
+ * Stores a particle seed in the builtin buffer. The slot doubles as `PARTICLE_RANDOM_F`: the
47
+ * stored value seed / 2^24 is uniform in [0, 1).
48
+ */
49
+ export declare function writeParticleSeed(array: {
50
+ [index: number]: number;
51
+ }, itemOffset: number, seed: number): void;
52
+ /**
53
+ * Stream of random values of one module: one value per particle, independent of the other
54
+ * streams. Constant for a module, so compute it once per spawn or update call.
55
+ * @param salt Salt the emitter gives the module
56
+ * @param channel Index of the value within the module, below 64
57
+ * @returns Stream key for `particleRandom`
58
+ */
59
+ export declare function particleRandomStream(salt: number, channel: number): number;
60
+ /**
61
+ * Random value of a particle in a stream, constant over the particle's life
62
+ * @param array Builtin buffer
63
+ * @param itemOffset Offset of the particle in the builtin buffer
64
+ * @param stream Key from `particleRandomStream`
65
+ * @returns Uniform value in [0, 1)
66
+ */
67
+ export declare function particleRandom(array: ArrayLike<number>, itemOffset: number, stream: number): number;
@@ -1,6 +1,17 @@
1
1
  import { makeNoise2D } from "fast-simplex-noise";
2
2
  import { ClampToEdgeWrapping, DataTexture, LinearFilter, RGBAFormat, SRGBColorSpace, Texture, UnsignedByteType, UVMapping, } from "three";
3
+ export { assertValidNonNegativeInteger, assertValidUnitNumber, } from "../../core/miscellaneous/asserts.js";
3
4
  import { getTextureImageSize } from "../../core/miscellaneous/texture/UITextureView.Internal.js";
5
+ /** Formats a number as a GLSL float literal */
6
+ export function toGLSLFloat(value) {
7
+ const literal = String(value);
8
+ return /[.e]/.test(literal) ? literal : `${literal}.0`;
9
+ }
10
+ /** GLSL expression of the u that samples a gradient texture of `size` texels at t in [0, 1] */
11
+ export function buildGradientCoordinate(t, size) {
12
+ // Texel centers: the first stop at t = 0, the last at t = 1.
13
+ return `${t} * ${toGLSLFloat((size - 1) / size)} + ${toGLSLFloat(0.5 / size)}`;
14
+ }
4
15
  export function resolveUIRangeConfig(config) {
5
16
  if (typeof config === "number") {
6
17
  return { min: config, max: config };
@@ -62,3 +73,64 @@ export const BUILTIN_OFFSET_RANDOM_C = 12;
62
73
  export const BUILTIN_OFFSET_RANDOM_D = 13;
63
74
  export const BUILTIN_OFFSET_RANDOM_E = 14;
64
75
  export const BUILTIN_OFFSET_RANDOM_F = 15;
76
+ /** 2^24: integers below it are exact in float32, so the builtin buffer can carry a seed */
77
+ const PARTICLE_SEED_RANGE = 16777216;
78
+ /**
79
+ * 32-bit integer hash (lowbias32 by C. Wellons). A bijection in which every input bit affects
80
+ * every output bit. CPU only: GLSL ES 1.00 has no integer bit operations.
81
+ * @param value Integer; taken modulo 2^32
82
+ * @returns Unsigned 32-bit hash
83
+ */
84
+ function hashUint32(value) {
85
+ let x = value >>> 0;
86
+ x ^= x >>> 16;
87
+ x = Math.imul(x, 0x7feb352d);
88
+ x ^= x >>> 15;
89
+ x = Math.imul(x, 0x846ca68b);
90
+ x ^= x >>> 16;
91
+ return x >>> 0;
92
+ }
93
+ /**
94
+ * Maps a hash to [0, 1) with 24 bits: exact in float32, so a stored value stays below 1
95
+ * @param hash Unsigned 32-bit hash
96
+ * @returns Uniform value in [0, 1)
97
+ */
98
+ function hashToUnit(hash) {
99
+ return (hash >>> 8) / PARTICLE_SEED_RANGE;
100
+ }
101
+ /**
102
+ * Seed of the particle i of an emitter: a 24-bit integer
103
+ * @param emitterSeed Seed of the emitter
104
+ * @param index Number of particles the emitter spawned before this one
105
+ */
106
+ export function computeParticleSeed(emitterSeed, index) {
107
+ return hashUint32(emitterSeed ^ hashUint32(index)) >>> 8;
108
+ }
109
+ /**
110
+ * Stores a particle seed in the builtin buffer. The slot doubles as `PARTICLE_RANDOM_F`: the
111
+ * stored value seed / 2^24 is uniform in [0, 1).
112
+ */
113
+ export function writeParticleSeed(array, itemOffset, seed) {
114
+ array[itemOffset + BUILTIN_OFFSET_RANDOM_F] = seed / PARTICLE_SEED_RANGE;
115
+ }
116
+ /**
117
+ * Stream of random values of one module: one value per particle, independent of the other
118
+ * streams. Constant for a module, so compute it once per spawn or update call.
119
+ * @param salt Salt the emitter gives the module
120
+ * @param channel Index of the value within the module, below 64
121
+ * @returns Stream key for `particleRandom`
122
+ */
123
+ export function particleRandomStream(salt, channel) {
124
+ return hashUint32(Math.imul(salt, 64) + channel);
125
+ }
126
+ /**
127
+ * Random value of a particle in a stream, constant over the particle's life
128
+ * @param array Builtin buffer
129
+ * @param itemOffset Offset of the particle in the builtin buffer
130
+ * @param stream Key from `particleRandomStream`
131
+ * @returns Uniform value in [0, 1)
132
+ */
133
+ export function particleRandom(array, itemOffset, stream) {
134
+ const seed = Math.round(array[itemOffset + BUILTIN_OFFSET_RANDOM_F] * PARTICLE_SEED_RANGE);
135
+ return hashToUnit(hashUint32(seed ^ stream));
136
+ }
@@ -1,5 +1,5 @@
1
1
  import { buildGradientTexture } from "../miscellaneous/miscellaneous.js";
2
- import source from "../shaders/UIRenderingColorOverLife.js";
2
+ import { buildColorOverLifeSource } from "../shaders/UIRenderingColorOverLife.js";
3
3
  import { UIRenderingModule } from "./UIRenderingModule.js";
4
4
  /**
5
5
  * Colors particles based on lifetime progression.
@@ -15,11 +15,11 @@ export class UIRenderingColorOverLife extends UIRenderingModule {
15
15
  this.requiredProperties = {
16
16
  builtin: "Matrix4",
17
17
  };
18
- this.source = source;
19
18
  if (colors.length === 0) {
20
19
  throw new Error("UIRenderingColorOverLife.constructor.colors: colors array cannot be empty");
21
20
  }
22
21
  this.requiredUniforms = { colorOverLifeTexture: buildGradientTexture(colors) };
22
+ this.source = buildColorOverLifeSource(colors.length);
23
23
  }
24
24
  destroy() {
25
25
  this.requiredUniforms.colorOverLifeTexture.dispose();
@@ -1,5 +1,5 @@
1
1
  import { buildGradientTexture } from "../miscellaneous/miscellaneous.js";
2
- import source from "../shaders/UIRenderingColorOverVelocity.js";
2
+ import { buildColorOverVelocitySource } from "../shaders/UIRenderingColorOverVelocity.js";
3
3
  import { UIRenderingModule } from "./UIRenderingModule.js";
4
4
  /**
5
5
  * Colors particles based on velocity magnitude.
@@ -16,15 +16,14 @@ export class UIRenderingColorOverVelocity extends UIRenderingModule {
16
16
  this.requiredProperties = {
17
17
  builtin: "Matrix4",
18
18
  };
19
- this.source = source;
20
19
  if (colors.length === 0) {
21
20
  throw new Error("UIRenderingColorOverVelocity.constructor.colors: colors array cannot be empty");
22
21
  }
23
22
  if (maxVelocity <= 0) {
24
23
  throw new Error(`UIRenderingColorOverVelocity.constructor.maxVelocity: value must be greater than 0, got ${maxVelocity}`);
25
24
  }
26
- this.source = source.replace("COLOR_OVER_VELOCITY_MAX", `COLOR_OVER_VELOCITY_MAX ${maxVelocity.toFixed(2)}`);
27
25
  this.requiredUniforms = { colorOverVelocityTexture: buildGradientTexture(colors) };
26
+ this.source = buildColorOverVelocitySource(colors.length, maxVelocity);
28
27
  }
29
28
  destroy() {
30
29
  this.requiredUniforms.colorOverVelocityTexture.dispose();
@@ -4,12 +4,13 @@ import type { UIProperty, UIPropertyName } from "../../core/miscellaneous/generi
4
4
  *
5
5
  * The source defines `vec4 draw()`; the colors of all rendering modules are multiplied. It reads
6
6
  * `p_uv`, uniforms and properties as `p_<name>`, and builtin values through the `PARTICLE_*`
7
- * macros, which are cheaper than reading `p_builtin` as a whole.
7
+ * macros, which are cheaper than reading `p_builtin` as a whole. Uniforms belong to the module:
8
+ * the emitter renames them per module, so modules of one type keep their own values.
8
9
  */
9
10
  export declare abstract class UIRenderingModule {
10
11
  /** Per-particle properties the source reads, by name and type */
11
12
  readonly requiredProperties?: Record<string, UIPropertyName>;
12
- /** Uniforms the source reads, as `p_<name>` */
13
+ /** Uniforms the source reads, as `p_<name>`; names must be GLSL identifiers */
13
14
  abstract readonly requiredUniforms: Record<string, UIProperty>;
14
15
  /** GLSL source defining `vec4 draw()` */
15
16
  abstract readonly source: string;
@@ -3,7 +3,8 @@
3
3
  *
4
4
  * The source defines `vec4 draw()`; the colors of all rendering modules are multiplied. It reads
5
5
  * `p_uv`, uniforms and properties as `p_<name>`, and builtin values through the `PARTICLE_*`
6
- * macros, which are cheaper than reading `p_builtin` as a whole.
6
+ * macros, which are cheaper than reading `p_builtin` as a whole. Uniforms belong to the module:
7
+ * the emitter renames them per module, so modules of one type keep their own values.
7
8
  */
8
9
  export class UIRenderingModule {
9
10
  }
@@ -16,7 +16,8 @@ function buildMultiTextureShader(count) {
16
16
  }
17
17
  return [
18
18
  "vec4 draw() {",
19
- ` int idx = int(PARTICLE_RANDOM_A * ${count}.0);`,
19
+ // Clamped: at mediump precision a value just below 1 can round up to 1
20
+ ` int idx = int(min(PARTICLE_RANDOM_A * ${count}.0, ${count}.0 - 0.5));`,
20
21
  " mat3 t;",
21
22
  ...branches,
22
23
  " vec2 transformedUV = (t * vec3(p_uv, 1.0)).xy;",
@@ -1,2 +1 @@
1
- declare const source: string;
2
- export default source;
1
+ export declare function buildColorOverLifeSource(gradientSize: number): string;
@@ -1,7 +1,9 @@
1
- const source = `
1
+ import { buildGradientCoordinate } from "../miscellaneous/miscellaneous.js";
2
+ export function buildColorOverLifeSource(gradientSize) {
3
+ return `
2
4
  vec4 draw() {
3
5
  float t = clamp(PARTICLE_AGE / PARTICLE_LIFETIME, 0.0, 1.0);
4
- return srgbTexture2D(p_colorOverLifeTexture, vec2(t, 0.5));
6
+ return srgbTexture2D(p_colorOverLifeTexture, vec2(${buildGradientCoordinate("t", gradientSize)}, 0.5));
5
7
  }
6
8
  `;
7
- export default source;
9
+ }