laymur 0.27.1 → 0.29.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 (138) hide show
  1. package/README.md +99 -5
  2. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.d.ts +14 -3
  3. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.js +40 -6
  4. package/dist/core/constraints/UICustomConstraint.js +2 -2
  5. package/dist/core/constraints/UIHeightConstraint/UIHeightConstraint.js +3 -2
  6. package/dist/core/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.js +3 -2
  7. package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +3 -2
  8. package/dist/core/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.js +3 -2
  9. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.d.ts +6 -0
  10. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.js +8 -0
  11. package/dist/core/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.js +3 -2
  12. package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +3 -2
  13. package/dist/core/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.js +3 -2
  14. package/dist/core/constraints/UIWidthConstraint/UIWidthConstraint.js +3 -2
  15. package/dist/core/constraints/constraintAsserts.d.ts +41 -0
  16. package/dist/core/constraints/constraintAsserts.js +171 -0
  17. package/dist/core/elements/UICanvasElement/UICanvasElement.d.ts +54 -0
  18. package/dist/core/elements/UICanvasElement/UICanvasElement.js +70 -0
  19. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +2 -0
  20. package/dist/core/elements/UIDummy/UIDummy.d.ts +8 -5
  21. package/dist/core/elements/UIDummy/UIDummy.js +24 -11
  22. package/dist/core/elements/UIElement/UIElement.Internal.d.ts +6 -5
  23. package/dist/core/elements/UIElement/UIElement.Internal.js +0 -4
  24. package/dist/core/elements/UIElement/UIElement.d.ts +12 -6
  25. package/dist/core/elements/UIElement/UIElement.js +14 -7
  26. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +42 -2
  27. package/dist/core/elements/UIGraphics/UIGraphics.Internal.js +2 -2
  28. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +48 -34
  29. package/dist/core/elements/UIGraphics/UIGraphics.js +196 -93
  30. package/dist/core/elements/UIImage/UIImage.d.ts +11 -1
  31. package/dist/core/elements/UIImage/UIImage.js +14 -0
  32. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +10 -1
  33. package/dist/core/elements/UIScene/UIScene.Internal.js +1 -0
  34. package/dist/core/elements/UIScene/UIScene.d.ts +11 -1
  35. package/dist/core/elements/UIScene/UIScene.js +29 -5
  36. package/dist/core/elements/UIText/UIText.Interfaces.d.ts +2 -2
  37. package/dist/core/elements/UIText/UIText.Internal.d.ts +14 -1
  38. package/dist/core/elements/UIText/UIText.Internal.js +12 -0
  39. package/dist/core/elements/UIText/UIText.Measuring.d.ts +9 -4
  40. package/dist/core/elements/UIText/UIText.Measuring.js +17 -6
  41. package/dist/core/elements/UIText/UIText.Rendering.d.ts +7 -16
  42. package/dist/core/elements/UIText/UIText.Rendering.js +212 -46
  43. package/dist/core/elements/UIText/UIText.d.ts +64 -29
  44. package/dist/core/elements/UIText/UIText.js +134 -149
  45. package/dist/core/index.d.ts +10 -4
  46. package/dist/core/index.js +5 -1
  47. package/dist/core/layers/UILayer/UILayer.d.ts +17 -3
  48. package/dist/core/layers/UILayer/UILayer.js +35 -4
  49. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +2 -0
  50. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +4 -1
  51. package/dist/core/miscellaneous/UIBlendMode.d.ts +5 -0
  52. package/dist/core/miscellaneous/UIBlendMode.js +7 -0
  53. package/dist/core/miscellaneous/asserts.d.ts +0 -41
  54. package/dist/core/miscellaneous/asserts.js +0 -170
  55. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.d.ts +46 -0
  56. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.js +86 -0
  57. package/dist/core/miscellaneous/color/UIColor.d.ts +17 -0
  58. package/dist/core/miscellaneous/color/UIColor.js +22 -0
  59. package/dist/core/miscellaneous/color/canvasColor.d.ts +9 -0
  60. package/dist/core/miscellaneous/color/canvasColor.js +15 -0
  61. package/dist/core/miscellaneous/insets/UIInsets.d.ts +3 -0
  62. package/dist/core/miscellaneous/insets/UIInsets.js +15 -0
  63. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.d.ts +10 -7
  64. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.js +11 -8
  65. package/dist/core/miscellaneous/paint/UIGradient.Internal.d.ts +37 -0
  66. package/dist/core/miscellaneous/paint/UIGradient.Internal.js +14 -0
  67. package/dist/core/miscellaneous/paint/UIGradient.d.ts +18 -0
  68. package/dist/core/miscellaneous/paint/UIGradient.js +35 -0
  69. package/dist/core/miscellaneous/paint/UILinearGradient.d.ts +14 -0
  70. package/dist/core/miscellaneous/paint/UILinearGradient.js +22 -0
  71. package/dist/core/miscellaneous/paint/UIPaint.Internal.d.ts +21 -0
  72. package/dist/core/miscellaneous/paint/UIPaint.Internal.js +1 -0
  73. package/dist/core/miscellaneous/paint/UIPaint.d.ts +17 -0
  74. package/dist/core/miscellaneous/paint/UIPaint.js +21 -0
  75. package/dist/core/miscellaneous/paint/UIRadialGradient.d.ts +18 -0
  76. package/dist/core/miscellaneous/paint/UIRadialGradient.js +33 -0
  77. package/dist/core/miscellaneous/shared.d.ts +14 -0
  78. package/dist/core/miscellaneous/shared.js +11 -0
  79. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.d.ts +91 -30
  80. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.js +12 -8
  81. package/dist/core/miscellaneous/text-style/UITextStyle.d.ts +41 -63
  82. package/dist/core/miscellaneous/text-style/UITextStyle.js +165 -291
  83. package/dist/core/miscellaneous/texture/UITextureView.Internal.d.ts +2 -1
  84. package/dist/core/miscellaneous/texture/UITextureView.Internal.js +12 -3
  85. package/dist/core/shaders/UIProgressMaskFunctionCircular.js +1 -2
  86. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +7 -3
  87. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +2 -1
  88. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +19 -10
  89. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +6 -2
  90. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +41 -9
  91. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +97 -35
  92. package/dist/particles/behaviorModules/UIBehaviorAlignToVelocity.d.ts +44 -0
  93. package/dist/particles/behaviorModules/UIBehaviorAlignToVelocity.js +75 -0
  94. package/dist/particles/behaviorModules/UIBehaviorBounce.d.ts +51 -0
  95. package/dist/particles/behaviorModules/UIBehaviorBounce.js +107 -0
  96. package/dist/particles/behaviorModules/UIBehaviorOrbit.d.ts +39 -0
  97. package/dist/particles/behaviorModules/UIBehaviorOrbit.js +73 -0
  98. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.d.ts +7 -2
  99. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.js +9 -5
  100. package/dist/particles/behaviorModules/UIBehaviorSeek.d.ts +38 -0
  101. package/dist/particles/behaviorModules/UIBehaviorSeek.js +75 -0
  102. package/dist/particles/elements/UIEmitter.Internal.d.ts +48 -1
  103. package/dist/particles/elements/UIEmitter.Internal.js +26 -1
  104. package/dist/particles/elements/UIEmitter.Schedule.d.ts +31 -0
  105. package/dist/particles/elements/UIEmitter.Schedule.js +81 -0
  106. package/dist/particles/elements/UIEmitter.d.ts +79 -9
  107. package/dist/particles/elements/UIEmitter.js +241 -83
  108. package/dist/particles/index.d.ts +13 -1
  109. package/dist/particles/index.js +11 -1
  110. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.d.ts +6 -0
  111. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.js +27 -0
  112. package/dist/particles/instancedParticle/UIInstancedParticle.d.ts +7 -1
  113. package/dist/particles/instancedParticle/UIInstancedParticle.js +12 -1
  114. package/dist/particles/miscellaneous/miscellaneous.d.ts +22 -0
  115. package/dist/particles/miscellaneous/miscellaneous.js +38 -0
  116. package/dist/particles/renderingModules/UIRenderingFlipbook.d.ts +41 -0
  117. package/dist/particles/renderingModules/UIRenderingFlipbook.js +70 -0
  118. package/dist/particles/renderingModules/UIRenderingModule.d.ts +2 -1
  119. package/dist/particles/renderingModules/UIRenderingModule.js +2 -1
  120. package/dist/particles/renderingModules/UIRenderingRandomColor.d.ts +19 -0
  121. package/dist/particles/renderingModules/UIRenderingRandomColor.js +28 -0
  122. package/dist/particles/renderingModules/UIRenderingShape.d.ts +31 -0
  123. package/dist/particles/renderingModules/UIRenderingShape.js +54 -0
  124. package/dist/particles/renderingModules/UIRenderingTexture.d.ts +1 -1
  125. package/dist/particles/renderingModules/UIRenderingTexture.js +4 -28
  126. package/dist/particles/shaders/UIRenderingFlipbook.d.ts +5 -0
  127. package/dist/particles/shaders/UIRenderingFlipbook.js +19 -0
  128. package/dist/particles/shaders/UIRenderingRandomColor.d.ts +2 -0
  129. package/dist/particles/shaders/UIRenderingRandomColor.js +10 -0
  130. package/dist/particles/shaders/UIRenderingShape.d.ts +6 -0
  131. package/dist/particles/shaders/UIRenderingShape.js +15 -0
  132. package/dist/particles/shaders/UIRenderingTexture.d.ts +4 -2
  133. package/dist/particles/shaders/UIRenderingTexture.js +27 -2
  134. package/dist/particles/spawnModules/UISpawnCircle.d.ts +44 -0
  135. package/dist/particles/spawnModules/UISpawnCircle.js +81 -0
  136. package/dist/particles/spawnModules/UISpawnRadialVelocity.d.ts +37 -0
  137. package/dist/particles/spawnModules/UISpawnRadialVelocity.js +75 -0
  138. package/package.json +1 -1
@@ -0,0 +1,107 @@
1
+ import { assertValidNumber, assertValidUnitNumber } from "../../core/miscellaneous/asserts.js";
2
+ import { BUILTIN_OFFSET_POSITION_X, BUILTIN_OFFSET_POSITION_Y, BUILTIN_OFFSET_VELOCITY_X, BUILTIN_OFFSET_VELOCITY_Y, resolveUIVector2Config, } from "../miscellaneous/miscellaneous.js";
3
+ import { UIBehaviorModule } from "./UIBehaviorModule.js";
4
+ /**
5
+ * Keeps particles on one side of a line and bounces them off it, as a floor or a wall.
6
+ *
7
+ * The line passes through a point, and its normal points to the free side. A particle behind it
8
+ * is put back on the line, the velocity into it is reflected and scaled by the restitution, and
9
+ * the velocity along it decays by the friction while in contact, as `UIBehaviorVelocityDamping`
10
+ * does. The point and the normal are in the particle space of the emitter.
11
+ */
12
+ export class UIBehaviorBounce extends UIBehaviorModule {
13
+ /**
14
+ * @param point - A point of the line
15
+ * @param normal - Direction to the free side, normalized by the module
16
+ * @param restitution - Share of the speed into the line kept after a bounce, from 0 to 1
17
+ * @param friction - Share of the speed along the line lost per second of contact, from 0 to 1
18
+ */
19
+ constructor(point, normal = { x: 0, y: 1 }, restitution = 0.5, friction = 0.9) {
20
+ super();
21
+ this.requiredProperties = { builtin: "Matrix4" };
22
+ this.normalX = 0;
23
+ this.normalY = 1;
24
+ this.pointInternal = resolveUIVector2Config(point);
25
+ assertValidNumber(this.pointInternal.x, "UIBehaviorBounce.constructor.point.x");
26
+ assertValidNumber(this.pointInternal.y, "UIBehaviorBounce.constructor.point.y");
27
+ this.setNormal(normal, "UIBehaviorBounce.constructor.normal");
28
+ assertValidUnitNumber(restitution, "UIBehaviorBounce.constructor.restitution");
29
+ assertValidUnitNumber(friction, "UIBehaviorBounce.constructor.friction");
30
+ this.restitutionInternal = restitution;
31
+ this.frictionInternal = friction;
32
+ }
33
+ /** A point of the line */
34
+ get point() {
35
+ return this.pointInternal;
36
+ }
37
+ /** Unit direction to the free side */
38
+ get normal() {
39
+ return { x: this.normalX, y: this.normalY };
40
+ }
41
+ /** Share of the speed into the line kept after a bounce */
42
+ get restitution() {
43
+ return this.restitutionInternal;
44
+ }
45
+ /** Share of the speed along the line lost per second of contact */
46
+ get friction() {
47
+ return this.frictionInternal;
48
+ }
49
+ /** A point of the line */
50
+ set point(value) {
51
+ this.pointInternal = resolveUIVector2Config(value);
52
+ assertValidNumber(this.pointInternal.x, "UIBehaviorBounce.point.x");
53
+ assertValidNumber(this.pointInternal.y, "UIBehaviorBounce.point.y");
54
+ }
55
+ /** Direction to the free side, normalized by the module */
56
+ set normal(value) {
57
+ this.setNormal(value, "UIBehaviorBounce.normal");
58
+ }
59
+ /** Share of the speed into the line kept after a bounce */
60
+ set restitution(value) {
61
+ assertValidUnitNumber(value, "UIBehaviorBounce.restitution");
62
+ this.restitutionInternal = value;
63
+ }
64
+ /** Share of the speed along the line lost per second of contact */
65
+ set friction(value) {
66
+ assertValidUnitNumber(value, "UIBehaviorBounce.friction");
67
+ this.frictionInternal = value;
68
+ }
69
+ update(properties, instanceCount, deltaTime) {
70
+ const { builtin } = properties;
71
+ const { array, itemSize } = builtin;
72
+ const { x: pointX, y: pointY } = this.pointInternal;
73
+ const { normalX, normalY } = this;
74
+ const keptAlong = Math.pow(1 - this.frictionInternal, deltaTime);
75
+ for (let i = 0; i < instanceCount; i++) {
76
+ const itemOffset = i * itemSize;
77
+ const depth = (array[itemOffset + BUILTIN_OFFSET_POSITION_X] - pointX) * normalX +
78
+ (array[itemOffset + BUILTIN_OFFSET_POSITION_Y] - pointY) * normalY;
79
+ if (depth >= 0) {
80
+ continue;
81
+ }
82
+ array[itemOffset + BUILTIN_OFFSET_POSITION_X] -= normalX * depth;
83
+ array[itemOffset + BUILTIN_OFFSET_POSITION_Y] -= normalY * depth;
84
+ const velocityX = array[itemOffset + BUILTIN_OFFSET_VELOCITY_X];
85
+ const velocityY = array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y];
86
+ // Split into the parts across the line (normal * across) and along it
87
+ const across = velocityX * normalX + velocityY * normalY;
88
+ const alongX = velocityX - normalX * across;
89
+ const alongY = velocityY - normalY * across;
90
+ const acrossAfter = across < 0 ? -across * this.restitutionInternal : across;
91
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_X] = alongX * keptAlong + normalX * acrossAfter;
92
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y] = alongY * keptAlong + normalY * acrossAfter;
93
+ }
94
+ builtin.needsUpdate = true;
95
+ }
96
+ setNormal(value, subject) {
97
+ const { x, y } = resolveUIVector2Config(value);
98
+ assertValidNumber(x, `${subject}.x`);
99
+ assertValidNumber(y, `${subject}.y`);
100
+ const length = Math.hypot(x, y);
101
+ if (length === 0) {
102
+ throw new Error(`${subject}: normal must not be zero`);
103
+ }
104
+ this.normalX = x / length;
105
+ this.normalY = y / length;
106
+ }
107
+ }
@@ -0,0 +1,39 @@
1
+ import type { InstancedBufferAttribute } from "three";
2
+ import type { Vector2Like } from "../../core/miscellaneous/math.js";
3
+ import type { UIRange, UIRangeConfig, UIVector2Config } from "../miscellaneous/miscellaneous.js";
4
+ import { UIBehaviorModule } from "./UIBehaviorModule.js";
5
+ /**
6
+ * Turns the motion of particles around a center: position and velocity rotate together at an
7
+ * angular speed, so a particle at rest circles the center at its distance and one moving outward
8
+ * spirals.
9
+ *
10
+ * The rotation is kinematic, not a force: the distance to the center stays unless the velocity
11
+ * changes it. The center is in the particle space of the emitter.
12
+ */
13
+ export declare class UIBehaviorOrbit extends UIBehaviorModule<{
14
+ builtin: "Matrix4";
15
+ }> {
16
+ readonly requiredProperties: {
17
+ readonly builtin: "Matrix4";
18
+ };
19
+ private centerInternal;
20
+ private angularSpeedInternal;
21
+ /**
22
+ * @param angularSpeed - Radians per second, counter-clockwise for positive values. Accepts
23
+ * number, tuple, or range object
24
+ * @param center - Center of the rotation
25
+ */
26
+ constructor(angularSpeed: UIRangeConfig, center?: UIVector2Config);
27
+ /** Radians per second, counter-clockwise for positive values */
28
+ get angularSpeed(): UIRange;
29
+ /** Center of the rotation */
30
+ get center(): Vector2Like;
31
+ /** Radians per second, counter-clockwise for positive values */
32
+ set angularSpeed(value: UIRangeConfig);
33
+ /** Center of the rotation */
34
+ set center(value: UIVector2Config);
35
+ update(properties: {
36
+ builtin: InstancedBufferAttribute;
37
+ }, instanceCount: number, deltaTime: number, randomSalt: number): void;
38
+ private assertValid;
39
+ }
@@ -0,0 +1,73 @@
1
+ import { MathUtils } from "three";
2
+ import { assertValidNumber } from "../../core/miscellaneous/asserts.js";
3
+ import { BUILTIN_OFFSET_POSITION_X, BUILTIN_OFFSET_POSITION_Y, BUILTIN_OFFSET_VELOCITY_X, BUILTIN_OFFSET_VELOCITY_Y, particleRandom, particleRandomStream, resolveUIRangeConfig, resolveUIVector2Config, } from "../miscellaneous/miscellaneous.js";
4
+ import { UIBehaviorModule } from "./UIBehaviorModule.js";
5
+ /**
6
+ * Turns the motion of particles around a center: position and velocity rotate together at an
7
+ * angular speed, so a particle at rest circles the center at its distance and one moving outward
8
+ * spirals.
9
+ *
10
+ * The rotation is kinematic, not a force: the distance to the center stays unless the velocity
11
+ * changes it. The center is in the particle space of the emitter.
12
+ */
13
+ export class UIBehaviorOrbit extends UIBehaviorModule {
14
+ /**
15
+ * @param angularSpeed - Radians per second, counter-clockwise for positive values. Accepts
16
+ * number, tuple, or range object
17
+ * @param center - Center of the rotation
18
+ */
19
+ constructor(angularSpeed, center = 0) {
20
+ super();
21
+ this.requiredProperties = { builtin: "Matrix4" };
22
+ this.angularSpeedInternal = resolveUIRangeConfig(angularSpeed);
23
+ this.centerInternal = resolveUIVector2Config(center);
24
+ this.assertValid("UIBehaviorOrbit.constructor");
25
+ }
26
+ /** Radians per second, counter-clockwise for positive values */
27
+ get angularSpeed() {
28
+ return this.angularSpeedInternal;
29
+ }
30
+ /** Center of the rotation */
31
+ get center() {
32
+ return this.centerInternal;
33
+ }
34
+ /** Radians per second, counter-clockwise for positive values */
35
+ set angularSpeed(value) {
36
+ this.angularSpeedInternal = resolveUIRangeConfig(value);
37
+ this.assertValid("UIBehaviorOrbit");
38
+ }
39
+ /** Center of the rotation */
40
+ set center(value) {
41
+ this.centerInternal = resolveUIVector2Config(value);
42
+ this.assertValid("UIBehaviorOrbit");
43
+ }
44
+ update(properties, instanceCount, deltaTime, randomSalt) {
45
+ const { builtin } = properties;
46
+ const { array, itemSize } = builtin;
47
+ const { x: centerX, y: centerY } = this.centerInternal;
48
+ const { min: speedMin, max: speedMax } = this.angularSpeedInternal;
49
+ const speedStream = particleRandomStream(randomSalt, 0);
50
+ for (let i = 0; i < instanceCount; i++) {
51
+ const itemOffset = i * itemSize;
52
+ const angle = MathUtils.lerp(speedMin, speedMax, particleRandom(array, itemOffset, speedStream)) *
53
+ deltaTime;
54
+ const cos = Math.cos(angle);
55
+ const sin = Math.sin(angle);
56
+ const offsetX = array[itemOffset + BUILTIN_OFFSET_POSITION_X] - centerX;
57
+ const offsetY = array[itemOffset + BUILTIN_OFFSET_POSITION_Y] - centerY;
58
+ array[itemOffset + BUILTIN_OFFSET_POSITION_X] = centerX + offsetX * cos - offsetY * sin;
59
+ array[itemOffset + BUILTIN_OFFSET_POSITION_Y] = centerY + offsetX * sin + offsetY * cos;
60
+ const velocityX = array[itemOffset + BUILTIN_OFFSET_VELOCITY_X];
61
+ const velocityY = array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y];
62
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_X] = velocityX * cos - velocityY * sin;
63
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y] = velocityX * sin + velocityY * cos;
64
+ }
65
+ builtin.needsUpdate = true;
66
+ }
67
+ assertValid(subject) {
68
+ assertValidNumber(this.angularSpeedInternal.min, `${subject}.angularSpeed.min`);
69
+ assertValidNumber(this.angularSpeedInternal.max, `${subject}.angularSpeed.max`);
70
+ assertValidNumber(this.centerInternal.x, `${subject}.center.x`);
71
+ assertValidNumber(this.centerInternal.y, `${subject}.center.y`);
72
+ }
73
+ }
@@ -1,10 +1,12 @@
1
1
  import type { InstancedBufferAttribute } from "three";
2
2
  import type { UIAspectConfig, UIRangeConfig } from "../miscellaneous/miscellaneous.js";
3
+ import { UIEasing } from "../miscellaneous/miscellaneous.js";
3
4
  import { UIBehaviorModule } from "./UIBehaviorModule.js";
4
5
  /**
5
6
  * Animates particle scale over lifetime.
6
7
  *
7
- * Interpolates between scale anchors based on particle age. Requires at least 2 anchors.
8
+ * Interpolates between scale anchors based on particle age, with the easing within each span
9
+ * between two anchors. Requires at least 2 anchors.
8
10
  */
9
11
  export declare class UIBehaviorScaleOverLife extends UIBehaviorModule<{
10
12
  builtin: "Matrix4";
@@ -12,13 +14,16 @@ export declare class UIBehaviorScaleOverLife extends UIBehaviorModule<{
12
14
  readonly requiredProperties: {
13
15
  readonly builtin: "Matrix4";
14
16
  };
17
+ /** Easing within each span between two anchors */
18
+ easing: UIEasing;
15
19
  private readonly scales;
16
20
  private aspectInternal;
17
21
  /**
18
22
  * @param scales - Scale anchor points. Each accepts number, tuple, or range object
19
23
  * @param aspect - Width/height ratio. Accepts number or texture
24
+ * @param easing - Easing within each span between two anchors
20
25
  */
21
- constructor(scales: UIRangeConfig[], aspect?: UIAspectConfig);
26
+ constructor(scales: UIRangeConfig[], aspect?: UIAspectConfig, easing?: UIEasing);
22
27
  /** Width/height aspect ratio */
23
28
  get aspect(): number;
24
29
  /** Width/height aspect ratio */
@@ -1,21 +1,24 @@
1
1
  import { MathUtils } from "three";
2
2
  import { assertValidNonNegativeNumber, assertValidPositiveNumber, } from "../../core/miscellaneous/asserts.js";
3
- import { BUILTIN_OFFSET_AGE, BUILTIN_OFFSET_LIFETIME, BUILTIN_OFFSET_SCALE_X, BUILTIN_OFFSET_SCALE_Y, particleRandom, particleRandomStream, resolveAspect, resolveUIRangeConfig, } from "../miscellaneous/miscellaneous.js";
3
+ import { applyEasing, BUILTIN_OFFSET_AGE, BUILTIN_OFFSET_LIFETIME, BUILTIN_OFFSET_SCALE_X, BUILTIN_OFFSET_SCALE_Y, particleRandom, particleRandomStream, resolveAspect, resolveUIRangeConfig, UIEasing, } from "../miscellaneous/miscellaneous.js";
4
4
  import { UIBehaviorModule } from "./UIBehaviorModule.js";
5
5
  /**
6
6
  * Animates particle scale over lifetime.
7
7
  *
8
- * Interpolates between scale anchors based on particle age. Requires at least 2 anchors.
8
+ * Interpolates between scale anchors based on particle age, with the easing within each span
9
+ * between two anchors. Requires at least 2 anchors.
9
10
  */
10
11
  export class UIBehaviorScaleOverLife extends UIBehaviorModule {
11
12
  /**
12
13
  * @param scales - Scale anchor points. Each accepts number, tuple, or range object
13
14
  * @param aspect - Width/height ratio. Accepts number or texture
15
+ * @param easing - Easing within each span between two anchors
14
16
  */
15
- constructor(scales, aspect = 1) {
17
+ constructor(scales, aspect = 1, easing = UIEasing.LINEAR) {
16
18
  super();
17
19
  this.requiredProperties = { builtin: "Matrix4" };
18
20
  this.scales = [];
21
+ this.easing = easing;
19
22
  if (scales.length < 2) {
20
23
  throw new Error(`UIBehaviorScaleOverLife.constructor.scales: at least two scale anchors are required, got ${scales.length}`);
21
24
  }
@@ -45,9 +48,10 @@ export class UIBehaviorScaleOverLife extends UIBehaviorModule {
45
48
  const itemOffset = i * itemSize;
46
49
  const lifeT = Math.min(array[itemOffset + BUILTIN_OFFSET_AGE] / array[itemOffset + BUILTIN_OFFSET_LIFETIME], 1);
47
50
  const segment = (this.scales.length - 1) * lifeT;
48
- const index = Math.floor(segment);
51
+ // The last span also takes lifeT = 1, of a particle that outlives its lifetime this frame
52
+ const index = Math.min(Math.floor(segment), this.scales.length - 2);
49
53
  const scaleT = particleRandom(array, itemOffset, scaleStream);
50
- const localT = segment - index;
54
+ const localT = applyEasing(this.easing, segment - index);
51
55
  const scaleRange0 = this.scales[index];
52
56
  const scaleRange1 = this.scales[index + 1];
53
57
  const scale0 = MathUtils.lerp(scaleRange0.min, scaleRange0.max, scaleT);
@@ -0,0 +1,38 @@
1
+ import type { InstancedBufferAttribute } from "three";
2
+ import type { Vector2Like } from "../../core/miscellaneous/math.js";
3
+ import type { UIVector2Config } from "../miscellaneous/miscellaneous.js";
4
+ import { UIBehaviorModule } from "./UIBehaviorModule.js";
5
+ /**
6
+ * Steers particles into a target point at the end of their life, such as coins flying into a
7
+ * counter. With `UIEmitter.signalParticleDied` the death of a particle marks its arrival.
8
+ *
9
+ * Each frame the particle gets the constant acceleration that would bring it from its position and
10
+ * velocity to the target in the life left, so it keeps its initial motion and curves into the
11
+ * target. Put it last in the behavior sequence: modules after it move the particle off course.
12
+ * The target is in the particle space of the emitter.
13
+ */
14
+ export declare class UIBehaviorSeek extends UIBehaviorModule<{
15
+ builtin: "Matrix4";
16
+ }> {
17
+ readonly requiredProperties: {
18
+ readonly builtin: "Matrix4";
19
+ };
20
+ private targetInternal;
21
+ private delayInternal;
22
+ /**
23
+ * @param target - Point the particles arrive at
24
+ * @param delay - Part of the life before steering starts, from 0 to 1
25
+ */
26
+ constructor(target: UIVector2Config, delay?: number);
27
+ /** Point the particles arrive at */
28
+ get target(): Vector2Like;
29
+ /** Part of the life before steering starts */
30
+ get delay(): number;
31
+ /** Point the particles arrive at */
32
+ set target(value: UIVector2Config);
33
+ /** Part of the life before steering starts */
34
+ set delay(value: number);
35
+ update(properties: {
36
+ builtin: InstancedBufferAttribute;
37
+ }, instanceCount: number, deltaTime: number): void;
38
+ }
@@ -0,0 +1,75 @@
1
+ import { assertValidNumber } from "../../core/miscellaneous/asserts.js";
2
+ import { assertValidUnitNumber, BUILTIN_OFFSET_AGE, BUILTIN_OFFSET_LIFETIME, BUILTIN_OFFSET_POSITION_X, BUILTIN_OFFSET_POSITION_Y, BUILTIN_OFFSET_VELOCITY_X, BUILTIN_OFFSET_VELOCITY_Y, resolveUIVector2Config, } from "../miscellaneous/miscellaneous.js";
3
+ import { UIBehaviorModule } from "./UIBehaviorModule.js";
4
+ /**
5
+ * Steers particles into a target point at the end of their life, such as coins flying into a
6
+ * counter. With `UIEmitter.signalParticleDied` the death of a particle marks its arrival.
7
+ *
8
+ * Each frame the particle gets the constant acceleration that would bring it from its position and
9
+ * velocity to the target in the life left, so it keeps its initial motion and curves into the
10
+ * target. Put it last in the behavior sequence: modules after it move the particle off course.
11
+ * The target is in the particle space of the emitter.
12
+ */
13
+ export class UIBehaviorSeek extends UIBehaviorModule {
14
+ /**
15
+ * @param target - Point the particles arrive at
16
+ * @param delay - Part of the life before steering starts, from 0 to 1
17
+ */
18
+ constructor(target, delay = 0) {
19
+ super();
20
+ this.requiredProperties = { builtin: "Matrix4" };
21
+ this.targetInternal = resolveUIVector2Config(target);
22
+ assertValidNumber(this.targetInternal.x, "UIBehaviorSeek.constructor.target.x");
23
+ assertValidNumber(this.targetInternal.y, "UIBehaviorSeek.constructor.target.y");
24
+ assertValidUnitNumber(delay, "UIBehaviorSeek.constructor.delay");
25
+ this.delayInternal = delay;
26
+ }
27
+ /** Point the particles arrive at */
28
+ get target() {
29
+ return this.targetInternal;
30
+ }
31
+ /** Part of the life before steering starts */
32
+ get delay() {
33
+ return this.delayInternal;
34
+ }
35
+ /** Point the particles arrive at */
36
+ set target(value) {
37
+ this.targetInternal = resolveUIVector2Config(value);
38
+ assertValidNumber(this.targetInternal.x, "UIBehaviorSeek.target.x");
39
+ assertValidNumber(this.targetInternal.y, "UIBehaviorSeek.target.y");
40
+ }
41
+ /** Part of the life before steering starts */
42
+ set delay(value) {
43
+ assertValidUnitNumber(value, "UIBehaviorSeek.delay");
44
+ this.delayInternal = value;
45
+ }
46
+ update(properties, instanceCount, deltaTime) {
47
+ const { builtin } = properties;
48
+ const { array, itemSize } = builtin;
49
+ const { x: targetX, y: targetY } = this.targetInternal;
50
+ for (let i = 0; i < instanceCount; i++) {
51
+ const itemOffset = i * itemSize;
52
+ const lifetime = array[itemOffset + BUILTIN_OFFSET_LIFETIME];
53
+ const age = array[itemOffset + BUILTIN_OFFSET_AGE];
54
+ if (age < lifetime * this.delayInternal) {
55
+ continue;
56
+ }
57
+ const offsetX = targetX - array[itemOffset + BUILTIN_OFFSET_POSITION_X];
58
+ const offsetY = targetY - array[itemOffset + BUILTIN_OFFSET_POSITION_Y];
59
+ const timeLeft = lifetime - age;
60
+ if (timeLeft <= deltaTime) {
61
+ // The last frame of the life: the particle lands on the target at its end.
62
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_X] = offsetX / deltaTime;
63
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y] = offsetY / deltaTime;
64
+ continue;
65
+ }
66
+ // p + v * t + a * t^2 / 2 = target at t = timeLeft
67
+ const velocityX = array[itemOffset + BUILTIN_OFFSET_VELOCITY_X];
68
+ const velocityY = array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y];
69
+ const scale = (2 * deltaTime) / (timeLeft * timeLeft);
70
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_X] += (offsetX - velocityX * timeLeft) * scale;
71
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y] += (offsetY - velocityY * timeLeft) * scale;
72
+ }
73
+ builtin.needsUpdate = true;
74
+ }
75
+ }
@@ -1,9 +1,25 @@
1
1
  import type { UIAnchorOptions } from "../../core/elements/UIAnchor/UIAnchor.Internal.js";
2
- import { UIMode } from "../../core/index.js";
2
+ import { UIBlendMode, UIMode } from "../../core/index.js";
3
+ /** Coordinate space of particle positions */
4
+ export declare enum UIEmitterSpace {
5
+ /** Relative to the emitter: live particles move with it */
6
+ LOCAL = 0,
7
+ /** Layer coordinates: a particle stays where it was spawned when the emitter moves */
8
+ WORLD = 1
9
+ }
3
10
  /** Configuration options for UIEmitter */
4
11
  export interface UIEmitterOptions extends UIAnchorOptions {
5
12
  /** Whether the particles are drawn */
6
13
  mode: UIEmitterMode;
14
+ /** Color blending of the particles with what is drawn under them */
15
+ blendMode: UIBlendMode;
16
+ /** Rendering order among the elements of the layer, see UIEmitter.zIndex */
17
+ zIndex: number;
18
+ /**
19
+ * Coordinate space of particle positions, and of the points behavior modules take, such as a
20
+ * gravity center. Spawn modules always write positions relative to the emitter.
21
+ */
22
+ space: UIEmitterSpace;
7
23
  /** Number of particles to allocate for up front; a hint, not a limit */
8
24
  expectedCapacity: number;
9
25
  /** Capacity is kept a multiple of this; the buffers double when they grow */
@@ -18,18 +34,49 @@ export interface UIEmitterOptions extends UIAnchorOptions {
18
34
  /** Whether to call destroy() on all modules when the emitter is destroyed */
19
35
  automaticallyDestroyModules: boolean;
20
36
  }
37
+ /** Options of `UIEmitter.play` */
21
38
  export interface UIEmitterPlayOptions {
39
+ /** Seconds of emission, endless by default */
22
40
  duration: number;
41
+ /** Particles per unit of distance the emitter moves, on top of the particles per second */
42
+ ratePerDistance: number;
43
+ /** Particles per burst, the first burst at the start. No bursts by default. */
44
+ burstCount: number;
45
+ /** Seconds between bursts, a single burst by default */
46
+ burstInterval: number;
47
+ }
48
+ /**
49
+ * Path of the emitter over a frame of `time` seconds, in layer coordinates. Particles spawned at a
50
+ * moment of the frame start at the point of that moment, interpolated linearly.
51
+ */
52
+ export interface UISpawnPath {
53
+ time: number;
54
+ fromX: number;
55
+ fromY: number;
56
+ toX: number;
57
+ toY: number;
23
58
  }
24
59
  export declare function ignoreInput(): boolean;
25
60
  export type UIEmitterMode = UIMode.HIDDEN | UIMode.VISIBLE;
26
61
  export declare const EMITTER_DEFAULT_MODE: UIEmitterMode;
62
+ export declare const EMITTER_DEFAULT_BLEND_MODE = UIBlendMode.NORMAL;
63
+ export declare const EMITTER_DEFAULT_Z_INDEX = 0;
64
+ export declare const EMITTER_DEFAULT_SPACE = UIEmitterSpace.LOCAL;
27
65
  export declare const EMITTER_DEFAULT_EXPECTED_CAPACITY = 32;
28
66
  export declare const EMITTER_DEFAULT_CAPACITY_STEP = 32;
29
67
  export declare const EMITTER_DEFAULT_MAX_DELTA_TIME = 0.1;
30
68
  export declare const EMITTER_DEFAULT_LIFETIME = 1;
69
+ export declare const EMITTER_DEFAULT_PREWARM_STEP: number;
31
70
  export declare const EMITTER_DEFAULT_AUTOMATICALLY_DESTROY_MODULES = true;
32
71
  /** Salts the emitter hands out: one for the builtin values the shaders read, one per module */
33
72
  export declare const EMITTER_SHADER_SALT = 0;
34
73
  export declare const EMITTER_SPAWN_SALT_BASE = 256;
35
74
  export declare const EMITTER_BEHAVIOR_SALT_BASE = 512;
75
+ /**
76
+ * Moves a particle along its velocity and torque for `time` seconds, negative to move it back
77
+ * @param array Builtin buffer
78
+ * @param itemOffset Offset of the particle in the builtin buffer
79
+ */
80
+ export declare function advanceParticle(array: {
81
+ [index: number]: number;
82
+ }, itemOffset: number, time: number): void;
@@ -1,14 +1,39 @@
1
- import { UIMode } from "../../core/index.js";
1
+ import { UIBlendMode, UIMode } from "../../core/index.js";
2
+ import { BUILTIN_OFFSET_POSITION_X, BUILTIN_OFFSET_POSITION_Y, BUILTIN_OFFSET_ROTATION, BUILTIN_OFFSET_TORQUE, BUILTIN_OFFSET_VELOCITY_X, BUILTIN_OFFSET_VELOCITY_Y, } from "../miscellaneous/miscellaneous.js";
3
+ /** Coordinate space of particle positions */
4
+ export var UIEmitterSpace;
5
+ (function (UIEmitterSpace) {
6
+ /** Relative to the emitter: live particles move with it */
7
+ UIEmitterSpace[UIEmitterSpace["LOCAL"] = 0] = "LOCAL";
8
+ /** Layer coordinates: a particle stays where it was spawned when the emitter moves */
9
+ UIEmitterSpace[UIEmitterSpace["WORLD"] = 1] = "WORLD";
10
+ })(UIEmitterSpace || (UIEmitterSpace = {}));
2
11
  export function ignoreInput() {
3
12
  return false;
4
13
  }
5
14
  export const EMITTER_DEFAULT_MODE = UIMode.VISIBLE;
15
+ export const EMITTER_DEFAULT_BLEND_MODE = UIBlendMode.NORMAL;
16
+ export const EMITTER_DEFAULT_Z_INDEX = 0;
17
+ export const EMITTER_DEFAULT_SPACE = UIEmitterSpace.LOCAL;
6
18
  export const EMITTER_DEFAULT_EXPECTED_CAPACITY = 32;
7
19
  export const EMITTER_DEFAULT_CAPACITY_STEP = 32;
8
20
  export const EMITTER_DEFAULT_MAX_DELTA_TIME = 0.1;
9
21
  export const EMITTER_DEFAULT_LIFETIME = 1;
22
+ export const EMITTER_DEFAULT_PREWARM_STEP = 1 / 30;
10
23
  export const EMITTER_DEFAULT_AUTOMATICALLY_DESTROY_MODULES = true;
11
24
  /** Salts the emitter hands out: one for the builtin values the shaders read, one per module */
12
25
  export const EMITTER_SHADER_SALT = 0;
13
26
  export const EMITTER_SPAWN_SALT_BASE = 0x100;
14
27
  export const EMITTER_BEHAVIOR_SALT_BASE = 0x200;
28
+ /**
29
+ * Moves a particle along its velocity and torque for `time` seconds, negative to move it back
30
+ * @param array Builtin buffer
31
+ * @param itemOffset Offset of the particle in the builtin buffer
32
+ */
33
+ export function advanceParticle(array, itemOffset, time) {
34
+ array[itemOffset + BUILTIN_OFFSET_POSITION_X] +=
35
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_X] * time;
36
+ array[itemOffset + BUILTIN_OFFSET_POSITION_Y] +=
37
+ array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y] * time;
38
+ array[itemOffset + BUILTIN_OFFSET_ROTATION] += array[itemOffset + BUILTIN_OFFSET_TORQUE] * time;
39
+ }
@@ -0,0 +1,31 @@
1
+ import type { UIEmitterPlayOptions } from "./UIEmitter.Internal.js";
2
+ /**
3
+ * Receives the particles due within a frame: `count` of them, at `firstMoment + k * interval`
4
+ * seconds from the frame start
5
+ */
6
+ export type UIEmissionSpawn = (count: number, firstMoment: number, interval: number) => void;
7
+ /**
8
+ * Timing of an emission: a stream by time and by distance, and bursts at an interval. Knows when
9
+ * particles are due, not where or how they are spawned.
10
+ */
11
+ export declare class UIEmissionSchedule {
12
+ private playing;
13
+ private rate;
14
+ private ratePerDistance;
15
+ private duration;
16
+ private burstInterval;
17
+ private burstCount;
18
+ private elapsed;
19
+ /** Particles of the stream due but not spawned yet, below 1 after each frame */
20
+ private accumulator;
21
+ private nextBurst;
22
+ /** Whether the emission has not ended yet */
23
+ get isPlaying(): boolean;
24
+ start(rate: number, options: Partial<UIEmitterPlayOptions>): void;
25
+ stop(): void;
26
+ /**
27
+ * Advances the emission by a frame and hands the particles due within it to `spawn`
28
+ * @param distance Distance the emitter moved during the frame
29
+ */
30
+ advance(deltaTime: number, distance: number, spawn: UIEmissionSpawn): void;
31
+ }
@@ -0,0 +1,81 @@
1
+ import { assertValidNonNegativeNumber, assertValidPositiveNumber, } from "../../core/miscellaneous/asserts.js";
2
+ import { assertValidNonNegativeInteger } from "../miscellaneous/miscellaneous.js";
3
+ /**
4
+ * Timing of an emission: a stream by time and by distance, and bursts at an interval. Knows when
5
+ * particles are due, not where or how they are spawned.
6
+ */
7
+ export class UIEmissionSchedule {
8
+ constructor() {
9
+ this.playing = false;
10
+ this.rate = 0;
11
+ this.ratePerDistance = 0;
12
+ this.duration = Infinity;
13
+ this.burstInterval = Infinity;
14
+ this.burstCount = 0;
15
+ this.elapsed = 0;
16
+ /** Particles of the stream due but not spawned yet, below 1 after each frame */
17
+ this.accumulator = 0;
18
+ this.nextBurst = 0;
19
+ }
20
+ /** Whether the emission has not ended yet */
21
+ get isPlaying() {
22
+ return this.playing;
23
+ }
24
+ start(rate, options) {
25
+ assertValidNonNegativeNumber(rate, "UIEmitter.play.rate");
26
+ this.ratePerDistance = options.ratePerDistance ?? 0;
27
+ assertValidNonNegativeNumber(this.ratePerDistance, "UIEmitter.play.options.ratePerDistance");
28
+ // Infinite unless given: endless emission, a single burst
29
+ this.duration = options.duration ?? Infinity;
30
+ if (options.duration !== undefined) {
31
+ assertValidNonNegativeNumber(options.duration, "UIEmitter.play.options.duration");
32
+ }
33
+ this.burstInterval = options.burstInterval ?? Infinity;
34
+ if (options.burstInterval !== undefined) {
35
+ assertValidPositiveNumber(options.burstInterval, "UIEmitter.play.options.burstInterval");
36
+ }
37
+ this.burstCount = options.burstCount ?? 0;
38
+ assertValidNonNegativeInteger(this.burstCount, "UIEmitter.play.options.burstCount");
39
+ this.playing = true;
40
+ this.rate = rate;
41
+ this.elapsed = 0;
42
+ this.accumulator = 0;
43
+ this.nextBurst = 0;
44
+ }
45
+ stop() {
46
+ this.playing = false;
47
+ }
48
+ /**
49
+ * Advances the emission by a frame and hands the particles due within it to `spawn`
50
+ * @param distance Distance the emitter moved during the frame
51
+ */
52
+ advance(deltaTime, distance, spawn) {
53
+ if (!this.playing) {
54
+ return;
55
+ }
56
+ const before = this.elapsed;
57
+ this.elapsed += deltaTime;
58
+ const end = Math.min(this.elapsed, this.duration);
59
+ const emittingTime = end - before;
60
+ while (this.burstCount > 0 && this.nextBurst <= end) {
61
+ spawn(this.burstCount, this.nextBurst - before, 0);
62
+ this.nextBurst += this.burstInterval;
63
+ }
64
+ if (emittingTime > 0) {
65
+ // The emitter is taken to move at a constant speed within the frame.
66
+ const added = this.rate * emittingTime + (this.ratePerDistance * distance * emittingTime) / deltaTime;
67
+ const accumulatorBefore = this.accumulator;
68
+ this.accumulator += added;
69
+ const count = Math.floor(this.accumulator);
70
+ if (count > 0) {
71
+ // The k-th particle of the frame, from 0, is due once the accumulator reaches k + 1.
72
+ const interval = emittingTime / added;
73
+ spawn(count, (1 - accumulatorBefore) * interval, interval);
74
+ this.accumulator -= count;
75
+ }
76
+ }
77
+ if (this.elapsed >= this.duration) {
78
+ this.playing = false;
79
+ }
80
+ }
81
+ }