laymur 0.25.2 → 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 (186) hide show
  1. package/README.md +114 -34
  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/UIAnchor/UIAnchor.d.ts +2 -2
  7. package/dist/core/elements/UIAnchor/UIAnchor.js +3 -2
  8. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.d.ts +2 -2
  9. package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.js +24 -12
  10. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +13 -0
  11. package/dist/core/elements/UIDummy/UIDummy.d.ts +74 -5
  12. package/dist/core/elements/UIDummy/UIDummy.js +222 -29
  13. package/dist/core/elements/UIElement/UIElement.Internal.d.ts +1 -1
  14. package/dist/core/elements/UIElement/UIElement.d.ts +37 -1
  15. package/dist/core/elements/UIElement/UIElement.js +84 -6
  16. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +1 -1
  17. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +1 -1
  18. package/dist/core/elements/UIGraphics/UIGraphics.js +4 -4
  19. package/dist/core/elements/UIImage/UIImage.d.ts +2 -2
  20. package/dist/core/elements/UIImage/UIImage.js +10 -8
  21. package/dist/core/elements/UINineSlice/UINineSlice.d.ts +4 -4
  22. package/dist/core/elements/UINineSlice/UINineSlice.js +11 -9
  23. package/dist/core/elements/UIProgress/UIProgress.d.ts +2 -2
  24. package/dist/core/elements/UIProgress/UIProgress.js +17 -15
  25. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +1 -1
  26. package/dist/core/elements/UIScene/UIScene.d.ts +3 -2
  27. package/dist/core/elements/UIScene/UIScene.js +21 -14
  28. package/dist/core/elements/UIText/UIText.Measuring.d.ts +1 -1
  29. package/dist/core/elements/UIText/UIText.d.ts +3 -3
  30. package/dist/core/elements/UIText/UIText.js +8 -8
  31. package/dist/core/index.d.ts +60 -11
  32. package/dist/core/index.js +22 -7
  33. package/dist/core/layers/UILayer/UILayer.d.ts +45 -5
  34. package/dist/core/layers/UILayer/UILayer.js +103 -10
  35. package/dist/core/layers/UIViewportLayer/UIViewportLayer.Internal.d.ts +27 -0
  36. package/dist/core/layers/{UIFullscreenLayer/UIFullscreenLayer.Internal.js → UIViewportLayer/UIViewportLayer.Internal.js} +1 -1
  37. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +98 -0
  38. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +251 -0
  39. package/dist/core/miscellaneous/UIAffineMatrix3.d.ts +13 -0
  40. package/dist/core/miscellaneous/UIAffineMatrix3.js +13 -0
  41. package/dist/core/miscellaneous/area/UIArea.d.ts +10 -1
  42. package/dist/core/miscellaneous/area/UIArea.js +4 -1
  43. package/dist/core/miscellaneous/area/UIAreaCircle.d.ts +1 -0
  44. package/dist/core/miscellaneous/area/UIAreaCircle.js +0 -1
  45. package/dist/core/miscellaneous/area/UIAreaPolygon.d.ts +1 -0
  46. package/dist/core/miscellaneous/area/UIAreaPolygon.js +0 -1
  47. package/dist/core/miscellaneous/area/UIAreaRectangle.d.ts +1 -0
  48. package/dist/core/miscellaneous/area/UIAreaRectangle.js +0 -1
  49. package/dist/core/miscellaneous/area/UIAreaRoundedRectangle.d.ts +1 -0
  50. package/dist/core/miscellaneous/area/UIAreaRoundedRectangle.js +0 -1
  51. package/dist/core/miscellaneous/asserts.d.ts +23 -0
  52. package/dist/core/miscellaneous/asserts.js +44 -0
  53. package/dist/core/miscellaneous/canvas.js +7 -2
  54. package/dist/core/miscellaneous/color/UIColor.js +5 -2
  55. package/dist/core/miscellaneous/computeTransform.d.ts +12 -0
  56. package/dist/core/miscellaneous/computeTransform.js +20 -0
  57. package/dist/core/miscellaneous/context-monitor/UIContextMonitor.d.ts +1 -0
  58. package/dist/core/miscellaneous/context-monitor/UIContextMonitor.js +60 -0
  59. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.d.ts +17 -3
  60. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.js +88 -26
  61. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Layout.d.ts +9 -0
  62. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Layout.js +29 -6
  63. package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.js +51 -11
  64. package/dist/core/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.Internal.d.ts +2 -0
  65. package/dist/core/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.js +47 -6
  66. package/dist/core/miscellaneous/generic-plane/shared.d.ts +15 -3
  67. package/dist/core/miscellaneous/generic-plane/shared.js +38 -7
  68. package/dist/core/miscellaneous/generic-plane/varyingPacking.d.ts +46 -0
  69. package/dist/core/miscellaneous/generic-plane/varyingPacking.js +170 -0
  70. package/dist/core/miscellaneous/mask/UIMask.Internal.d.ts +61 -0
  71. package/dist/core/miscellaneous/mask/UIMask.Internal.js +56 -0
  72. package/dist/core/miscellaneous/mask/UIMask.d.ts +84 -0
  73. package/dist/core/miscellaneous/mask/UIMask.js +265 -0
  74. package/dist/core/miscellaneous/mask/UIMaskMaterialPatch.d.ts +50 -0
  75. package/dist/core/miscellaneous/mask/UIMaskMaterialPatch.js +233 -0
  76. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunction.d.ts +10 -1
  77. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunction.js +7 -1
  78. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.d.ts +2 -0
  79. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.js +0 -1
  80. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.d.ts +2 -0
  81. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.js +0 -1
  82. package/dist/core/miscellaneous/micro/UIMicro.Internal.d.ts +3 -0
  83. package/dist/core/miscellaneous/micro/UIMicro.Internal.js +1 -0
  84. package/dist/core/miscellaneous/micro/UIMicro.d.ts +18 -1
  85. package/dist/core/miscellaneous/micro/UIMicro.js +23 -2
  86. package/dist/core/miscellaneous/resize-policy/UIResizePolicy.d.ts +19 -1
  87. package/dist/core/miscellaneous/resize-policy/UIResizePolicy.js +16 -3
  88. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCover.js +2 -2
  89. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCross.js +2 -2
  90. package/dist/core/miscellaneous/resize-policy/UIResizePolicyCrossInverted.js +2 -2
  91. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFit.js +2 -2
  92. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedHeight.js +2 -2
  93. package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedWidth.js +2 -2
  94. package/dist/core/miscellaneous/text-style/UITextStyle.js +2 -2
  95. package/dist/core/miscellaneous/texture/UITextureView.d.ts +6 -2
  96. package/dist/core/miscellaneous/texture/UITextureView.js +13 -9
  97. package/dist/core/miscellaneous/viewport/UIElementViewport.d.ts +31 -0
  98. package/dist/core/miscellaneous/viewport/UIElementViewport.js +54 -0
  99. package/dist/core/miscellaneous/viewport/UIViewport.d.ts +37 -0
  100. package/dist/core/miscellaneous/viewport/UIViewport.js +31 -0
  101. package/dist/core/miscellaneous/viewport/UIWindowViewport.d.ts +13 -0
  102. package/dist/core/miscellaneous/viewport/UIWindowViewport.js +25 -0
  103. package/dist/core/miscellaneous/webglCapabilities.js +61 -55
  104. package/dist/core/shaders/UIMask.d.ts +15 -0
  105. package/dist/core/shaders/UIMask.js +45 -0
  106. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +36 -5
  107. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +23 -2
  108. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +153 -15
  109. package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.Internal.d.ts +3 -1
  110. package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.d.ts +1 -0
  111. package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.js +3 -0
  112. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +2 -0
  113. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +3 -0
  114. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +36 -16
  115. package/dist/debug/UIAspectDebug.js +4 -0
  116. package/dist/debug/UIHeightDebug.js +4 -0
  117. package/dist/debug/UIHorizontalDistanceDebug.js +5 -0
  118. package/dist/debug/UIHorizontalInterpolationDebug.js +5 -0
  119. package/dist/debug/UILayerDebug.d.ts +7 -0
  120. package/dist/debug/UILayerDebug.js +21 -0
  121. package/dist/debug/UIVerticalDistanceDebug.js +5 -0
  122. package/dist/debug/UIVerticalInterpolationDebug.js +5 -0
  123. package/dist/debug/UIWidthDebug.js +4 -0
  124. package/dist/debug/index.d.ts +2 -0
  125. package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.d.ts +8 -1
  126. package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.js +0 -2
  127. package/dist/particles/behaviorModules/UIBehaviorModule.d.ts +12 -2
  128. package/dist/particles/behaviorModules/UIBehaviorModule.js +2 -1
  129. package/dist/particles/behaviorModules/UIBehaviorPointGravity.d.ts +8 -1
  130. package/dist/particles/behaviorModules/UIBehaviorPointGravity.js +13 -12
  131. package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.d.ts +7 -0
  132. package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.js +6 -6
  133. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.d.ts +7 -0
  134. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.js +4 -5
  135. package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.d.ts +8 -1
  136. package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.js +8 -10
  137. package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.d.ts +10 -2
  138. package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.js +8 -7
  139. package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.d.ts +7 -0
  140. package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.js +8 -10
  141. package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.d.ts +13 -4
  142. package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.js +12 -10
  143. package/dist/particles/elements/UIEmitter.Internal.d.ts +21 -1
  144. package/dist/particles/elements/UIEmitter.Internal.js +6 -0
  145. package/dist/particles/elements/UIEmitter.d.ts +20 -11
  146. package/dist/particles/elements/UIEmitter.js +100 -54
  147. package/dist/particles/index.d.ts +7 -1
  148. package/dist/particles/index.js +4 -0
  149. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.d.ts +19 -4
  150. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.js +117 -83
  151. package/dist/particles/instancedParticle/UIInstancedParticle.d.ts +8 -0
  152. package/dist/particles/instancedParticle/UIInstancedParticle.js +41 -4
  153. package/dist/particles/miscellaneous/miscellaneous.d.ts +34 -0
  154. package/dist/particles/miscellaneous/miscellaneous.js +72 -0
  155. package/dist/particles/renderingModules/UIRenderingColorOverLife.d.ts +4 -0
  156. package/dist/particles/renderingModules/UIRenderingColorOverLife.js +2 -4
  157. package/dist/particles/renderingModules/UIRenderingColorOverVelocity.d.ts +4 -0
  158. package/dist/particles/renderingModules/UIRenderingColorOverVelocity.js +2 -5
  159. package/dist/particles/renderingModules/UIRenderingModule.d.ts +11 -1
  160. package/dist/particles/renderingModules/UIRenderingModule.js +4 -1
  161. package/dist/particles/renderingModules/UIRenderingTexture.d.ts +4 -0
  162. package/dist/particles/renderingModules/UIRenderingTexture.js +2 -1
  163. package/dist/particles/shaders/UIRenderingColorOverLife.d.ts +1 -2
  164. package/dist/particles/shaders/UIRenderingColorOverLife.js +5 -3
  165. package/dist/particles/shaders/UIRenderingColorOverVelocity.d.ts +1 -9
  166. package/dist/particles/shaders/UIRenderingColorOverVelocity.js +6 -13
  167. package/dist/particles/spawnModules/UISpawnModule.d.ts +16 -1
  168. package/dist/particles/spawnModules/UISpawnModule.js +3 -1
  169. package/dist/particles/spawnModules/UISpawnOffset.d.ts +7 -0
  170. package/dist/particles/spawnModules/UISpawnOffset.js +0 -2
  171. package/dist/particles/spawnModules/UISpawnRandomLifetime.d.ts +8 -1
  172. package/dist/particles/spawnModules/UISpawnRandomLifetime.js +5 -7
  173. package/dist/particles/spawnModules/UISpawnRandomRotation.d.ts +7 -0
  174. package/dist/particles/spawnModules/UISpawnRandomRotation.js +5 -5
  175. package/dist/particles/spawnModules/UISpawnRandomScale.d.ts +7 -0
  176. package/dist/particles/spawnModules/UISpawnRandomScale.js +4 -5
  177. package/dist/particles/spawnModules/UISpawnRandomTorque.d.ts +7 -0
  178. package/dist/particles/spawnModules/UISpawnRandomTorque.js +5 -5
  179. package/dist/particles/spawnModules/UISpawnRandomVelocity.d.ts +7 -0
  180. package/dist/particles/spawnModules/UISpawnRandomVelocity.js +6 -6
  181. package/dist/particles/spawnModules/UISpawnRectangle.d.ts +7 -0
  182. package/dist/particles/spawnModules/UISpawnRectangle.js +6 -6
  183. package/package.json +13 -4
  184. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.Internal.d.ts +0 -21
  185. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.d.ts +0 -63
  186. package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.js +0 -144
@@ -1,7 +1,9 @@
1
1
  import { BufferAttribute, DoubleSide, Float32BufferAttribute, InstancedBufferGeometry, ShaderMaterial, } from "three";
2
2
  import { UIColor } from "../../core/index.js";
3
3
  import { resolveGLSLTypeInfo } from "../../core/miscellaneous/generic-plane/shared.js";
4
+ import { isIdentifierReferenced, packVaryings, } from "../../core/miscellaneous/generic-plane/varyingPacking.js";
4
5
  import { checkSRGBSupport } from "../../core/miscellaneous/webglCapabilities.js";
6
+ import { 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_RANDOM_F, BUILTIN_OFFSET_ROTATION, BUILTIN_OFFSET_SCALE_X, BUILTIN_OFFSET_SCALE_Y, BUILTIN_OFFSET_TORQUE, BUILTIN_OFFSET_VELOCITY_X, BUILTIN_OFFSET_VELOCITY_Y, } from "../miscellaneous/miscellaneous.js";
5
7
  export const INSTANCED_PARTICLE_GEOMETRY = (() => {
6
8
  const geometry = new InstancedBufferGeometry();
7
9
  const indices = new Uint16Array([0, 2, 1, 2, 3, 1]);
@@ -12,6 +14,26 @@ export const INSTANCED_PARTICLE_GEOMETRY = (() => {
12
14
  geometry.setAttribute("uv", new Float32BufferAttribute(uvs, 2));
13
15
  return geometry;
14
16
  })();
17
+ const BUILTIN_PROPERTY = "builtin";
18
+ /** Macros of the builtin particle values and their offsets in the builtin mat4 */
19
+ const PARTICLE_BUILTIN_MACROS = [
20
+ ["PARTICLE_POSITION_X", BUILTIN_OFFSET_POSITION_X],
21
+ ["PARTICLE_POSITION_Y", BUILTIN_OFFSET_POSITION_Y],
22
+ ["PARTICLE_VELOCITY_X", BUILTIN_OFFSET_VELOCITY_X],
23
+ ["PARTICLE_VELOCITY_Y", BUILTIN_OFFSET_VELOCITY_Y],
24
+ ["PARTICLE_SCALE_X", BUILTIN_OFFSET_SCALE_X],
25
+ ["PARTICLE_SCALE_Y", BUILTIN_OFFSET_SCALE_Y],
26
+ ["PARTICLE_ROTATION", BUILTIN_OFFSET_ROTATION],
27
+ ["PARTICLE_TORQUE", BUILTIN_OFFSET_TORQUE],
28
+ ["PARTICLE_LIFETIME", BUILTIN_OFFSET_LIFETIME],
29
+ ["PARTICLE_AGE", BUILTIN_OFFSET_AGE],
30
+ ["PARTICLE_RANDOM_A", BUILTIN_OFFSET_RANDOM_A],
31
+ ["PARTICLE_RANDOM_B", BUILTIN_OFFSET_RANDOM_B],
32
+ ["PARTICLE_RANDOM_C", BUILTIN_OFFSET_RANDOM_C],
33
+ ["PARTICLE_RANDOM_D", BUILTIN_OFFSET_RANDOM_D],
34
+ ["PARTICLE_RANDOM_E", BUILTIN_OFFSET_RANDOM_E],
35
+ ["PARTICLE_RANDOM_F", BUILTIN_OFFSET_RANDOM_F],
36
+ ];
15
37
  export function collectProperties(keeper, modules, debugContext) {
16
38
  for (const module of modules) {
17
39
  if (module.requiredProperties === undefined) {
@@ -20,6 +42,9 @@ export function collectProperties(keeper, modules, debugContext) {
20
42
  for (const key in module.requiredProperties) {
21
43
  const existingTypeInfo = keeper[key];
22
44
  const newTypeInfo = resolveGLSLTypeInfo(module.requiredProperties[key]);
45
+ if (newTypeInfo.affine === true) {
46
+ throw new Error(`${debugContext}: "${key}" cannot be UIAffineMatrix3 per particle, use Matrix3`);
47
+ }
23
48
  if (existingTypeInfo === undefined) {
24
49
  keeper[key] = newTypeInfo;
25
50
  }
@@ -29,45 +54,35 @@ export function collectProperties(keeper, modules, debugContext) {
29
54
  }
30
55
  }
31
56
  }
32
- export function collectUniforms(keeper, modules, debugContext) {
33
- 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;
34
65
  for (const key in module.requiredUniforms) {
35
- const value = module.requiredUniforms[key];
36
- const existingGLProperty = keeper[key];
37
- const newPropertyTypeInfo = resolveGLSLTypeInfo(value);
38
- if (existingGLProperty === undefined) {
39
- keeper[key] = { value, glslTypeInfo: newPropertyTypeInfo };
40
- }
41
- else if (existingGLProperty.glslTypeInfo.glslTypeName !== newPropertyTypeInfo.glslTypeName) {
42
- 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`);
43
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}`);
44
73
  }
45
- }
74
+ return source;
75
+ });
76
+ }
77
+ function builtinComponent(base, index) {
78
+ return `${base}[${Math.floor(index / 4)}].${"xyzw"[index % 4]}`;
79
+ }
80
+ function buildBuiltinMacros(base) {
81
+ return PARTICLE_BUILTIN_MACROS.map(([name, index]) => `#define ${name} ${builtinComponent(base, index)}`).join("\n");
46
82
  }
47
83
  export function buildParticleVertexShader(uniformDeclarations, attributeDeclarations, varyingDeclarations, vertexAssignments) {
48
84
  return `
49
-
50
- #define PARTICLE_POSITION_X a_builtin[0][0]
51
- #define PARTICLE_POSITION_Y a_builtin[0][1]
52
-
53
- #define PARTICLE_VELOCITY_X a_builtin[0][2]
54
- #define PARTICLE_VELOCITY_Y a_builtin[0][3]
55
-
56
- #define PARTICLE_SCALE_X a_builtin[1][0]
57
- #define PARTICLE_SCALE_Y a_builtin[1][1]
58
-
59
- #define PARTICLE_ROTATION a_builtin[1][2]
60
- #define PARTICLE_TORQUE a_builtin[1][3]
61
-
62
- #define PARTICLE_LIFETIME a_builtin[2][0]
63
- #define PARTICLE_AGE a_builtin[2][1]
64
-
65
- #define PARTICLE_RANDOM_A a_builtin[2][2]
66
- #define PARTICLE_RANDOM_B a_builtin[2][3]
67
- #define PARTICLE_RANDOM_C a_builtin[3][0]
68
- #define PARTICLE_RANDOM_D a_builtin[3][1]
69
- #define PARTICLE_RANDOM_E a_builtin[3][2]
70
- #define PARTICLE_RANDOM_F a_builtin[3][3]
85
+ ${buildBuiltinMacros("a_builtin")}
71
86
 
72
87
  // User attributes
73
88
  ${attributeDeclarations.join("\n")}
@@ -75,17 +90,12 @@ export function buildParticleVertexShader(uniformDeclarations, attributeDeclarat
75
90
  // Uniforms
76
91
  ${uniformDeclarations.join("\n")}
77
92
 
78
- // Builtin varyings
79
- varying vec2 p_uv;
80
-
81
- // User varyings
93
+ // Packed varyings
82
94
  ${varyingDeclarations.join("\n")}
83
95
 
84
96
  void main() {
85
97
  ${vertexAssignments.join("\n")}
86
98
 
87
- p_uv = uv;
88
-
89
99
  vec2 transformedPosition = position.xy;
90
100
  transformedPosition.x *= PARTICLE_SCALE_X;
91
101
  transformedPosition.y *= PARTICLE_SCALE_Y;
@@ -106,7 +116,13 @@ export function buildParticleVertexShader(uniformDeclarations, attributeDeclarat
106
116
  }
107
117
  `;
108
118
  }
109
- export function buildParticleFragmentShader(uniformDeclarations, varyingDeclarations, sources) {
119
+ /**
120
+ * Builds the fragment shader of a particle mesh. Sources read particle values through the
121
+ * PARTICLE_* macros and properties as p_* globals, all filled from packed varyings.
122
+ * @param fieldDeclarations Packed varyings, p_* globals and PARTICLE_* macros
123
+ * @param fieldAssignments Statements filling the p_* globals at the start of main
124
+ */
125
+ export function buildParticleFragmentShader(uniformDeclarations, fieldDeclarations, fieldAssignments, sources) {
110
126
  const renamedSources = sources.map((source, index) => {
111
127
  return source.replace(/vec4\s+draw\s*\(\s*\)/g, `vec4 draw${index}()`);
112
128
  });
@@ -120,38 +136,11 @@ export function buildParticleFragmentShader(uniformDeclarations, varyingDeclarat
120
136
  #define PI 3.14159265359
121
137
  #define SRGB_SUPPORTED ${checkSRGBSupport() ? "1" : "0"}
122
138
 
123
- #define PARTICLE_POSITION_X p_builtin[0][0]
124
- #define PARTICLE_POSITION_Y p_builtin[0][1]
125
-
126
- #define PARTICLE_VELOCITY_X p_builtin[0][2]
127
- #define PARTICLE_VELOCITY_Y p_builtin[0][3]
128
-
129
- #define PARTICLE_SCALE_X p_builtin[1][0]
130
- #define PARTICLE_SCALE_Y p_builtin[1][1]
131
-
132
- #define PARTICLE_ROTATION p_builtin[1][2]
133
- #define PARTICLE_TORQUE p_builtin[1][3]
134
-
135
- #define PARTICLE_LIFETIME p_builtin[2][0]
136
- #define PARTICLE_AGE p_builtin[2][1]
137
-
138
- #define PARTICLE_RANDOM_A p_builtin[2][2]
139
- #define PARTICLE_RANDOM_B p_builtin[2][3]
140
- #define PARTICLE_RANDOM_C p_builtin[3][0]
141
- #define PARTICLE_RANDOM_D p_builtin[3][1]
142
- #define PARTICLE_RANDOM_E p_builtin[3][2]
143
- #define PARTICLE_RANDOM_F p_builtin[3][3]
144
-
145
139
  // Uniforms
146
140
  ${uniformDeclarations.join("\n")}
147
141
 
148
- // Builtin varyings
149
- varying vec2 p_uv;
150
-
151
- // User varyings
152
- ${varyingDeclarations.join("\n")}
153
-
154
- #include <alphahash_pars_fragment>
142
+ // Packed varyings and the fields read from them
143
+ ${fieldDeclarations.join("\n")}
155
144
 
156
145
  // sRGB decode helper
157
146
  vec4 srgbTexture2D(sampler2D textureSampler, vec2 uv) {
@@ -167,15 +156,12 @@ export function buildParticleFragmentShader(uniformDeclarations, varyingDeclarat
167
156
  ${renamedSources.join("\n")}
168
157
 
169
158
  void main() {
159
+ ${fieldAssignments.join("\n")}
160
+
170
161
  vec4 diffuseColor = ${drawCalls};
171
162
  if (diffuseColor.a < 0.0035) {
172
163
  discard;
173
164
  }
174
- #ifdef USE_ALPHAHASH
175
- if (diffuseColor.a < getAlphaHashThreshold(p_position)) {
176
- discard;
177
- }
178
- #endif
179
165
  gl_FragColor = linearToOutputTexel(diffuseColor);
180
166
  }
181
167
  `;
@@ -184,27 +170,75 @@ export function buildParticleMaterial(sources, uniformProperties, varyingPropert
184
170
  const uniforms = {};
185
171
  const uniformDeclarations = [];
186
172
  const attributeDeclarations = [];
187
- const varyingDeclarations = [];
188
- const vertexAssignments = [];
189
173
  for (const name in uniformProperties) {
190
174
  const { value, glslTypeInfo } = uniformProperties[name];
191
175
  uniforms[`p_${name}`] = { value: value instanceof UIColor ? value.toGLSLColor() : value };
192
176
  uniformDeclarations.push(`uniform ${glslTypeInfo.glslTypeName} p_${name};`);
193
177
  }
194
178
  for (const name in varyingProperties) {
195
- const { glslTypeName } = varyingProperties[name];
196
- attributeDeclarations.push(`attribute ${glslTypeName} a_${name};`);
197
- varyingDeclarations.push(`varying ${glslTypeName} p_${name};`);
198
- vertexAssignments.push(`p_${name} = a_${name};`);
179
+ attributeDeclarations.push(`attribute ${varyingProperties[name].glslTypeName} a_${name};`);
180
+ }
181
+ // Only what the fragment shader reads takes varyings. Most sources read a couple of builtin
182
+ // values through the PARTICLE_* macros, so those are passed one by one rather than as the
183
+ // whole builtin mat4; a source naming p_builtin itself gets the whole matrix.
184
+ const unpackedFragment = buildParticleFragmentShader(uniformDeclarations, [], [], sources);
185
+ const fields = [];
186
+ const macroFields = [];
187
+ const wholeBuiltin = isIdentifierReferenced(unpackedFragment, `p_${BUILTIN_PROPERTY}`);
188
+ if (isIdentifierReferenced(unpackedFragment, "p_uv")) {
189
+ fields.push({ name: "p_uv", glslTypeName: "vec2", components: ["uv.x", "uv.y"] });
190
+ }
191
+ for (const name in varyingProperties) {
192
+ const { glslTypeName, bufferSize, columnCount } = varyingProperties[name];
193
+ if (name === BUILTIN_PROPERTY && !wholeBuiltin) {
194
+ for (const [macro, index] of PARTICLE_BUILTIN_MACROS) {
195
+ if (isIdentifierReferenced(unpackedFragment, macro)) {
196
+ fields.push({
197
+ name: macro,
198
+ glslTypeName: "float",
199
+ components: [builtinComponent(`a_${name}`, index)],
200
+ });
201
+ macroFields.push(macro);
202
+ }
203
+ }
204
+ continue;
205
+ }
206
+ if (!isIdentifierReferenced(unpackedFragment, `p_${name}`)) {
207
+ continue;
208
+ }
209
+ const columnSize = bufferSize / columnCount;
210
+ const components = [];
211
+ for (let i = 0; i < bufferSize; i++) {
212
+ const column = columnCount > 1 ? `a_${name}[${Math.floor(i / columnSize)}]` : `a_${name}`;
213
+ components.push(bufferSize > 1 ? `${column}.${"xyzw"[i % columnSize]}` : column);
214
+ }
215
+ fields.push({ name: `p_${name}`, glslTypeName, components });
216
+ }
217
+ const packed = packVaryings(fields);
218
+ const fieldDeclarations = [...packed.declarations];
219
+ const fieldAssignments = [];
220
+ for (const { name, glslTypeName } of fields) {
221
+ const expression = packed.fragmentExpressions.get(name) ?? "";
222
+ if (macroFields.includes(name)) {
223
+ fieldDeclarations.push(`#define ${name} ${expression}`);
224
+ }
225
+ else {
226
+ fieldDeclarations.push(`${glslTypeName} ${name};`);
227
+ fieldAssignments.push(`${name} = ${expression};`);
228
+ }
229
+ }
230
+ if (wholeBuiltin) {
231
+ fieldDeclarations.push(buildBuiltinMacros(`p_${BUILTIN_PROPERTY}`));
199
232
  }
200
- return new ShaderMaterial({
233
+ const material = new ShaderMaterial({
201
234
  uniforms,
202
- vertexShader: buildParticleVertexShader(uniformDeclarations, attributeDeclarations, varyingDeclarations, vertexAssignments),
203
- fragmentShader: buildParticleFragmentShader(uniformDeclarations, varyingDeclarations, sources),
235
+ vertexShader: buildParticleVertexShader(uniformDeclarations, attributeDeclarations, packed.declarations, packed.vertexAssignments),
236
+ fragmentShader: buildParticleFragmentShader(uniformDeclarations, fieldDeclarations, fieldAssignments, sources),
204
237
  transparent: true,
205
238
  side: DoubleSide,
206
239
  forceSinglePass: true,
207
240
  depthWrite: false,
208
241
  depthTest: true,
209
242
  });
243
+ return { material, varyingRowCount: packed.rowCount };
210
244
  }
@@ -7,10 +7,18 @@ export declare class UIInstancedParticle extends Mesh {
7
7
  private readonly shaderMaterial;
8
8
  private readonly vertexAttributeCount;
9
9
  private readonly vertexAttributeSubject;
10
+ private readonly varyingRowCount;
10
11
  private capacity;
11
12
  constructor(sources: string[], uniforms: Record<string, GLProperty>, varyings: Record<string, GLTypeInfo>, expectedCapacity: number, capacityStep: number);
12
13
  get instanceCount(): number;
13
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;
14
22
  setOrigin(x: number, y: number): void;
15
23
  removeDeadParticles(): void;
16
24
  drop(): void;
@@ -1,17 +1,30 @@
1
1
  import { InstancedBufferAttribute, Mesh, StreamDrawUsage } from "three";
2
- import { assertVertexAttributeBudget } from "../../core/miscellaneous/asserts.js";
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();
9
- const shaderMaterial = buildParticleMaterial(sources, uniforms, varyings);
21
+ const { material: shaderMaterial, varyingRowCount } = buildParticleMaterial(sources, uniforms, varyings);
10
22
  super(instancedGeometry, shaderMaterial);
11
23
  this.capacityStep = capacityStep;
12
24
  this.propertyBuffers = {};
13
25
  this.onBeforeRenderInternal = (renderer) => {
14
26
  assertVertexAttributeBudget(renderer, this.vertexAttributeCount, this.vertexAttributeSubject);
27
+ assertVaryingBudget(renderer, this.varyingRowCount, this.vertexAttributeSubject);
15
28
  };
16
29
  this.frustumCulled = false;
17
30
  this.matrixAutoUpdate = false;
@@ -26,6 +39,7 @@ export class UIInstancedParticle extends Mesh {
26
39
  }
27
40
  this.vertexAttributeCount = vertexAttributeCount;
28
41
  this.vertexAttributeSubject = `UIInstancedParticle.onBeforeRender.properties (${Object.keys(varyings).join(", ")})`;
42
+ this.varyingRowCount = varyingRowCount;
29
43
  this.onBeforeRender = this.onBeforeRenderInternal;
30
44
  for (const name in varyings) {
31
45
  const { bufferSize } = varyings[name];
@@ -43,6 +57,26 @@ export class UIInstancedParticle extends Mesh {
43
57
  this.ensureCapacity(currentInstanceCount + count);
44
58
  this.instancedGeometry.instanceCount += count;
45
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
+ }
46
80
  setOrigin(x, y) {
47
81
  const uniform = resolvePropertyUniform("origin", this.shaderMaterial);
48
82
  uniform.value.set(x, y);
@@ -84,7 +118,10 @@ export class UIInstancedParticle extends Mesh {
84
118
  if (requiredCapacity <= this.capacity) {
85
119
  return;
86
120
  }
87
- 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();
88
125
  for (const name in this.propertyBuffers) {
89
126
  const { itemSize, array, usage } = this.propertyBuffers[name];
90
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,4 +1,5 @@
1
1
  import type { UIColor } from "../../core/index.js";
2
+ import type { UIProperty, UIPropertyName } from "../../core/miscellaneous/generic-plane/shared.js";
2
3
  import { UIRenderingModule } from "./UIRenderingModule.js";
3
4
  /**
4
5
  * Colors particles based on lifetime progression.
@@ -6,6 +7,9 @@ import { UIRenderingModule } from "./UIRenderingModule.js";
6
7
  * Interpolates through color gradient as particles age from birth to death.
7
8
  */
8
9
  export declare class UIRenderingColorOverLife extends UIRenderingModule {
10
+ readonly requiredProperties: Record<string, UIPropertyName>;
11
+ readonly requiredUniforms: Record<string, UIProperty>;
12
+ readonly source: string;
9
13
  /**
10
14
  * @param colors - Color gradient stops from birth to death
11
15
  */
@@ -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.
@@ -12,16 +12,14 @@ export class UIRenderingColorOverLife extends UIRenderingModule {
12
12
  */
13
13
  constructor(colors) {
14
14
  super();
15
- /** @internal */
16
15
  this.requiredProperties = {
17
16
  builtin: "Matrix4",
18
17
  };
19
- /** @internal */
20
- this.source = source;
21
18
  if (colors.length === 0) {
22
19
  throw new Error("UIRenderingColorOverLife.constructor.colors: colors array cannot be empty");
23
20
  }
24
21
  this.requiredUniforms = { colorOverLifeTexture: buildGradientTexture(colors) };
22
+ this.source = buildColorOverLifeSource(colors.length);
25
23
  }
26
24
  destroy() {
27
25
  this.requiredUniforms.colorOverLifeTexture.dispose();
@@ -1,4 +1,5 @@
1
1
  import type { UIColor } from "../../core/index.js";
2
+ import type { UIProperty, UIPropertyName } from "../../core/miscellaneous/generic-plane/shared.js";
2
3
  import { UIRenderingModule } from "./UIRenderingModule.js";
3
4
  /**
4
5
  * Colors particles based on velocity magnitude.
@@ -6,6 +7,9 @@ import { UIRenderingModule } from "./UIRenderingModule.js";
6
7
  * Maps velocity from 0 to maxVelocity across the color gradient.
7
8
  */
8
9
  export declare class UIRenderingColorOverVelocity extends UIRenderingModule {
10
+ readonly requiredProperties: Record<string, UIPropertyName>;
11
+ readonly requiredUniforms: Record<string, UIProperty>;
12
+ readonly source: string;
9
13
  /**
10
14
  * @param colors - Color gradient from slow to fast
11
15
  * @param maxVelocity - Velocity mapped to final color
@@ -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.
@@ -13,20 +13,17 @@ export class UIRenderingColorOverVelocity extends UIRenderingModule {
13
13
  */
14
14
  constructor(colors, maxVelocity) {
15
15
  super();
16
- /** @internal */
17
16
  this.requiredProperties = {
18
17
  builtin: "Matrix4",
19
18
  };
20
- /** @internal */
21
- this.source = source;
22
19
  if (colors.length === 0) {
23
20
  throw new Error("UIRenderingColorOverVelocity.constructor.colors: colors array cannot be empty");
24
21
  }
25
22
  if (maxVelocity <= 0) {
26
23
  throw new Error(`UIRenderingColorOverVelocity.constructor.maxVelocity: value must be greater than 0, got ${maxVelocity}`);
27
24
  }
28
- this.source = source.replace("COLOR_OVER_VELOCITY_MAX", `COLOR_OVER_VELOCITY_MAX ${maxVelocity.toFixed(2)}`);
29
25
  this.requiredUniforms = { colorOverVelocityTexture: buildGradientTexture(colors) };
26
+ this.source = buildColorOverVelocitySource(colors.length, maxVelocity);
30
27
  }
31
28
  destroy() {
32
29
  this.requiredUniforms.colorOverVelocityTexture.dispose();
@@ -1,8 +1,18 @@
1
+ import type { UIProperty, UIPropertyName } from "../../core/miscellaneous/generic-plane/shared.js";
1
2
  /**
2
3
  * Base class for particle rendering modules.
3
4
  *
4
- * Rendering modules provide fragment shader code and uniforms for particle appearance.
5
+ * The source defines `vec4 draw()`; the colors of all rendering modules are multiplied. It reads
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. Uniforms belong to the module:
8
+ * the emitter renames them per module, so modules of one type keep their own values.
5
9
  */
6
10
  export declare abstract class UIRenderingModule {
11
+ /** Per-particle properties the source reads, by name and type */
12
+ readonly requiredProperties?: Record<string, UIPropertyName>;
13
+ /** Uniforms the source reads, as `p_<name>`; names must be GLSL identifiers */
14
+ abstract readonly requiredUniforms: Record<string, UIProperty>;
15
+ /** GLSL source defining `vec4 draw()` */
16
+ abstract readonly source: string;
7
17
  destroy?(): void;
8
18
  }
@@ -1,7 +1,10 @@
1
1
  /**
2
2
  * Base class for particle rendering modules.
3
3
  *
4
- * Rendering modules provide fragment shader code and uniforms for particle appearance.
4
+ * The source defines `vec4 draw()`; the colors of all rendering modules are multiplied. It reads
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. Uniforms belong to the module:
7
+ * the emitter renames them per module, so modules of one type keep their own values.
5
8
  */
6
9
  export class UIRenderingModule {
7
10
  }
@@ -1,3 +1,4 @@
1
+ import type { UIProperty, UIPropertyName } from "../../core/miscellaneous/generic-plane/shared.js";
1
2
  import type { UITextureConfig } from "../../core/miscellaneous/texture/UITextureView.Internal.js";
2
3
  import { UIRenderingModule } from "./UIRenderingModule.js";
3
4
  /**
@@ -8,6 +9,9 @@ import { UIRenderingModule } from "./UIRenderingModule.js";
8
9
  * All configs in the array must share the same underlying Three.js Texture object.
9
10
  */
10
11
  export declare class UIRenderingTexture extends UIRenderingModule {
12
+ readonly requiredProperties?: Record<string, UIPropertyName>;
13
+ readonly requiredUniforms: Record<string, UIProperty>;
14
+ readonly source: string;
11
15
  /**
12
16
  * @param config - Single texture config or array of configs (same atlas required for arrays)
13
17
  */