laymur 0.26.0 → 0.27.1
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.
- package/README.md +80 -36
- package/dist/core/constraints/UICustomConstraint.d.ts +1 -1
- package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +1 -1
- package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +1 -1
- package/dist/core/constraints/builders/UIConstraint2DBuilder.d.ts +2 -3
- package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.d.ts +2 -2
- package/dist/core/elements/UIAnimatedImage/UIAnimatedImage.js +24 -12
- package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +10 -0
- package/dist/core/elements/UIDummy/UIDummy.d.ts +42 -5
- package/dist/core/elements/UIDummy/UIDummy.js +119 -25
- package/dist/core/elements/UIElement/UIElement.Internal.d.ts +1 -1
- package/dist/core/elements/UIElement/UIElement.d.ts +14 -0
- package/dist/core/elements/UIElement/UIElement.js +34 -3
- package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +1 -1
- package/dist/core/elements/UIGraphics/UIGraphics.d.ts +1 -1
- package/dist/core/elements/UIGraphics/UIGraphics.js +2 -2
- package/dist/core/elements/UIImage/UIImage.d.ts +2 -2
- package/dist/core/elements/UIImage/UIImage.js +10 -8
- package/dist/core/elements/UINineSlice/UINineSlice.d.ts +4 -4
- package/dist/core/elements/UINineSlice/UINineSlice.js +11 -9
- package/dist/core/elements/UIProgress/UIProgress.d.ts +2 -2
- package/dist/core/elements/UIProgress/UIProgress.js +15 -10
- package/dist/core/elements/UIScene/UIScene.Internal.d.ts +1 -1
- package/dist/core/elements/UIScene/UIScene.d.ts +3 -2
- package/dist/core/elements/UIScene/UIScene.js +18 -12
- package/dist/core/elements/UIText/UIText.Measuring.d.ts +1 -1
- package/dist/core/elements/UIText/UIText.d.ts +3 -3
- package/dist/core/elements/UIText/UIText.js +6 -6
- package/dist/core/index.d.ts +44 -41
- package/dist/core/index.js +12 -9
- package/dist/core/layers/UILayer/UILayer.d.ts +40 -4
- package/dist/core/layers/UILayer/UILayer.js +70 -9
- package/dist/core/layers/UIViewportLayer/UIViewportLayer.Internal.d.ts +27 -0
- package/dist/core/layers/{UIFullscreenLayer/UIFullscreenLayer.Internal.js → UIViewportLayer/UIViewportLayer.Internal.js} +1 -1
- package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +98 -0
- package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +251 -0
- package/dist/core/miscellaneous/asserts.d.ts +14 -0
- package/dist/core/miscellaneous/asserts.js +28 -0
- package/dist/core/miscellaneous/canvas.js +10 -2
- package/dist/core/miscellaneous/color/UIColor.js +4 -2
- package/dist/core/miscellaneous/computeTransform.d.ts +12 -0
- package/dist/core/miscellaneous/computeTransform.js +20 -0
- package/dist/core/miscellaneous/context-monitor/UIContextMonitor.d.ts +1 -0
- package/dist/core/miscellaneous/context-monitor/UIContextMonitor.js +60 -0
- package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.js +2 -1
- package/dist/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.js +1 -1
- package/dist/core/miscellaneous/mask/UIMaskMaterialPatch.js +2 -1
- package/dist/core/miscellaneous/resize-policy/UIResizePolicy.d.ts +7 -4
- package/dist/core/miscellaneous/resize-policy/UIResizePolicy.js +13 -6
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyCover.d.ts +1 -1
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyCover.js +3 -3
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyCross.js +2 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyCrossInverted.js +2 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyFit.d.ts +1 -1
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyFit.js +3 -3
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedHeight.js +2 -2
- package/dist/core/miscellaneous/resize-policy/UIResizePolicyFixedWidth.js +2 -2
- package/dist/core/miscellaneous/text-style/UITextStyle.js +2 -2
- package/dist/core/miscellaneous/texture/UITextureView.d.ts +4 -1
- package/dist/core/miscellaneous/texture/UITextureView.js +10 -7
- package/dist/core/miscellaneous/viewport/UIElementViewport.d.ts +31 -0
- package/dist/core/miscellaneous/viewport/UIElementViewport.js +54 -0
- package/dist/core/miscellaneous/viewport/UIViewport.d.ts +37 -0
- package/dist/core/miscellaneous/viewport/UIViewport.js +31 -0
- package/dist/core/miscellaneous/viewport/UIWindowViewport.d.ts +13 -0
- package/dist/core/miscellaneous/viewport/UIWindowViewport.js +25 -0
- package/dist/core/miscellaneous/webglCapabilities.js +61 -55
- package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +36 -5
- package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +23 -2
- package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +153 -15
- package/dist/core/wrappers/UISceneWrapper/UISceneWrapper.Internal.d.ts +1 -1
- package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +2 -0
- package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +3 -0
- package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +36 -16
- package/dist/debug/UIAspectDebug.js +8 -7
- package/dist/debug/UIHeightDebug.js +8 -4
- package/dist/debug/UIHorizontalDistanceDebug.js +9 -7
- package/dist/debug/UIHorizontalInterpolationDebug.js +16 -10
- package/dist/debug/UILayerDebug.d.ts +7 -0
- package/dist/debug/UILayerDebug.js +21 -0
- package/dist/debug/UIVerticalDistanceDebug.js +9 -7
- package/dist/debug/UIVerticalInterpolationDebug.js +16 -10
- package/dist/debug/UIWidthDebug.js +8 -4
- package/dist/debug/index.d.ts +1 -1
- package/dist/debug/miscellaneous.d.ts +7 -0
- package/dist/debug/miscellaneous.js +11 -1
- package/dist/particles/behaviorModules/UIBehaviorDirectionalGravity.d.ts +1 -1
- package/dist/particles/behaviorModules/UIBehaviorModule.d.ts +5 -1
- package/dist/particles/behaviorModules/UIBehaviorPointGravity.d.ts +2 -2
- package/dist/particles/behaviorModules/UIBehaviorPointGravity.js +13 -10
- package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.d.ts +1 -1
- package/dist/particles/behaviorModules/UIBehaviorScaleOscillation.js +6 -4
- package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.d.ts +1 -1
- package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.js +4 -3
- package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.d.ts +2 -2
- package/dist/particles/behaviorModules/UIBehaviorTorqueDamping.js +8 -8
- package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.d.ts +4 -3
- package/dist/particles/behaviorModules/UIBehaviorTorqueNoise.js +8 -5
- package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.d.ts +1 -1
- package/dist/particles/behaviorModules/UIBehaviorVelocityDamping.js +8 -8
- package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.d.ts +7 -5
- package/dist/particles/behaviorModules/UIBehaviorVelocityNoise.js +12 -8
- package/dist/particles/elements/UIEmitter.Internal.d.ts +15 -2
- package/dist/particles/elements/UIEmitter.Internal.js +6 -0
- package/dist/particles/elements/UIEmitter.d.ts +19 -3
- package/dist/particles/elements/UIEmitter.js +100 -54
- package/dist/particles/index.d.ts +3 -2
- package/dist/particles/index.js +1 -0
- package/dist/particles/instancedParticle/UIInstancedParticle.Internal.d.ts +8 -2
- package/dist/particles/instancedParticle/UIInstancedParticle.Internal.js +16 -11
- package/dist/particles/instancedParticle/UIInstancedParticle.d.ts +7 -0
- package/dist/particles/instancedParticle/UIInstancedParticle.js +37 -2
- package/dist/particles/miscellaneous/miscellaneous.d.ts +34 -0
- package/dist/particles/miscellaneous/miscellaneous.js +72 -0
- package/dist/particles/renderingModules/UIRenderingColorOverLife.js +2 -2
- package/dist/particles/renderingModules/UIRenderingColorOverVelocity.js +2 -3
- package/dist/particles/renderingModules/UIRenderingModule.d.ts +3 -2
- package/dist/particles/renderingModules/UIRenderingModule.js +2 -1
- package/dist/particles/renderingModules/UIRenderingTexture.js +2 -1
- package/dist/particles/shaders/UIRenderingColorOverLife.d.ts +1 -2
- package/dist/particles/shaders/UIRenderingColorOverLife.js +5 -3
- package/dist/particles/shaders/UIRenderingColorOverVelocity.d.ts +1 -9
- package/dist/particles/shaders/UIRenderingColorOverVelocity.js +6 -13
- package/dist/particles/spawnModules/UISpawnModule.d.ts +5 -2
- package/dist/particles/spawnModules/UISpawnRandomLifetime.d.ts +2 -2
- package/dist/particles/spawnModules/UISpawnRandomLifetime.js +5 -5
- package/dist/particles/spawnModules/UISpawnRandomRotation.d.ts +1 -1
- package/dist/particles/spawnModules/UISpawnRandomRotation.js +5 -3
- package/dist/particles/spawnModules/UISpawnRandomScale.d.ts +1 -1
- package/dist/particles/spawnModules/UISpawnRandomScale.js +4 -3
- package/dist/particles/spawnModules/UISpawnRandomTorque.d.ts +1 -1
- package/dist/particles/spawnModules/UISpawnRandomTorque.js +5 -3
- package/dist/particles/spawnModules/UISpawnRandomVelocity.d.ts +1 -1
- package/dist/particles/spawnModules/UISpawnRandomVelocity.js +6 -4
- package/dist/particles/spawnModules/UISpawnRectangle.d.ts +1 -1
- package/dist/particles/spawnModules/UISpawnRectangle.js +6 -4
- package/package.json +34 -22
- package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.Internal.d.ts +0 -21
- package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.d.ts +0 -63
- package/dist/core/layers/UIFullscreenLayer/UIFullscreenLayer.js +0 -144
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { MathUtils } from "three";
|
|
2
2
|
import { assertValidNonNegativeNumber, assertValidPositiveNumber, } from "../../core/miscellaneous/asserts.js";
|
|
3
|
-
import { BUILTIN_OFFSET_POSITION_X, BUILTIN_OFFSET_POSITION_Y,
|
|
3
|
+
import { BUILTIN_OFFSET_POSITION_X, BUILTIN_OFFSET_POSITION_Y, BUILTIN_OFFSET_VELOCITY_X, BUILTIN_OFFSET_VELOCITY_Y, generateNoise2D, particleRandom, particleRandomStream, resolveUIRangeConfig, } from "../miscellaneous/miscellaneous.js";
|
|
4
4
|
import { UIBehaviorModule } from "./UIBehaviorModule.js";
|
|
5
5
|
/**
|
|
6
|
-
* Applies noise-based acceleration to particles.
|
|
6
|
+
* Applies noise-based acceleration to particles: a still flow field that particles drift through.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* The noise is sampled at the particle position times scale, so 1 / scale is the size of a noise
|
|
9
|
+
* detail in layer units. A detail much smaller than the distance a particle moves in a frame
|
|
10
|
+
* turns the drift into jitter.
|
|
9
11
|
*/
|
|
10
12
|
export class UIBehaviorVelocityNoise extends UIBehaviorModule {
|
|
11
13
|
/**
|
|
12
|
-
* @param scale - Noise frequency range. Accepts number, tuple, or range object
|
|
14
|
+
* @param scale - Noise frequency range in 1 / layer units. Accepts number, tuple, or range object
|
|
13
15
|
* @param strength - Force multiplier range. Accepts number, tuple, or range object
|
|
14
16
|
*/
|
|
15
|
-
constructor(scale =
|
|
17
|
+
constructor(scale = 0.005, strength = 500) {
|
|
16
18
|
super();
|
|
17
19
|
this.requiredProperties = { builtin: "Matrix4" };
|
|
18
20
|
this.scaleInternal = resolveUIRangeConfig(scale);
|
|
@@ -42,18 +44,20 @@ export class UIBehaviorVelocityNoise extends UIBehaviorModule {
|
|
|
42
44
|
assertValidNonNegativeNumber(this.strengthInternal.min, `UIBehaviorVelocityNoise.strength.min`);
|
|
43
45
|
assertValidNonNegativeNumber(this.strengthInternal.max, `UIBehaviorVelocityNoise.strength.max`);
|
|
44
46
|
}
|
|
45
|
-
update(properties, instanceCount, deltaTime) {
|
|
47
|
+
update(properties, instanceCount, deltaTime, randomSalt) {
|
|
46
48
|
const { builtin } = properties;
|
|
47
49
|
const { array, itemSize } = builtin;
|
|
50
|
+
const scaleStream = particleRandomStream(randomSalt, 0);
|
|
51
|
+
const strengthStream = particleRandomStream(randomSalt, 1);
|
|
48
52
|
for (let i = 0; i < instanceCount; i++) {
|
|
49
53
|
const itemOffset = i * itemSize;
|
|
50
54
|
const x = array[itemOffset + BUILTIN_OFFSET_POSITION_X];
|
|
51
55
|
const y = array[itemOffset + BUILTIN_OFFSET_POSITION_Y];
|
|
52
|
-
const scaleT = array
|
|
56
|
+
const scaleT = particleRandom(array, itemOffset, scaleStream);
|
|
53
57
|
const scale = MathUtils.lerp(this.scaleInternal.min, this.scaleInternal.max, scaleT);
|
|
54
58
|
const noiseX = generateNoise2D(x * scale, y * scale);
|
|
55
59
|
const noiseY = generateNoise2D((x + 100) * scale, (y + 100) * scale);
|
|
56
|
-
const strengthT = array
|
|
60
|
+
const strengthT = particleRandom(array, itemOffset, strengthStream);
|
|
57
61
|
const strength = MathUtils.lerp(this.strengthInternal.min, this.strengthInternal.max, strengthT);
|
|
58
62
|
array[itemOffset + BUILTIN_OFFSET_VELOCITY_X] += noiseX * strength * deltaTime;
|
|
59
63
|
array[itemOffset + BUILTIN_OFFSET_VELOCITY_Y] += noiseY * strength * deltaTime;
|
|
@@ -1,13 +1,20 @@
|
|
|
1
|
-
import { UIMode } from "../../core/index.js";
|
|
2
1
|
import type { UIAnchorOptions } from "../../core/elements/UIAnchor/UIAnchor.Internal.js";
|
|
2
|
+
import { UIMode } from "../../core/index.js";
|
|
3
3
|
/** Configuration options for UIEmitter */
|
|
4
4
|
export interface UIEmitterOptions extends UIAnchorOptions {
|
|
5
5
|
/** Whether the particles are drawn */
|
|
6
6
|
mode: UIEmitterMode;
|
|
7
7
|
/** Number of particles to allocate for up front; a hint, not a limit */
|
|
8
8
|
expectedCapacity: number;
|
|
9
|
-
/**
|
|
9
|
+
/** Capacity is kept a multiple of this; the buffers double when they grow */
|
|
10
10
|
capacityStep: number;
|
|
11
|
+
/** Longest frame step in seconds; longer frames, such as after a hidden tab, are clamped */
|
|
12
|
+
maxDeltaTime: number;
|
|
13
|
+
/**
|
|
14
|
+
* Seed of the random values, a non-negative integer. Emitters with one seed and one sequence
|
|
15
|
+
* of spawns produce the same particles. Random by default.
|
|
16
|
+
*/
|
|
17
|
+
seed: number;
|
|
11
18
|
/** Whether to call destroy() on all modules when the emitter is destroyed */
|
|
12
19
|
automaticallyDestroyModules: boolean;
|
|
13
20
|
}
|
|
@@ -19,4 +26,10 @@ export type UIEmitterMode = UIMode.HIDDEN | UIMode.VISIBLE;
|
|
|
19
26
|
export declare const EMITTER_DEFAULT_MODE: UIEmitterMode;
|
|
20
27
|
export declare const EMITTER_DEFAULT_EXPECTED_CAPACITY = 32;
|
|
21
28
|
export declare const EMITTER_DEFAULT_CAPACITY_STEP = 32;
|
|
29
|
+
export declare const EMITTER_DEFAULT_MAX_DELTA_TIME = 0.1;
|
|
30
|
+
export declare const EMITTER_DEFAULT_LIFETIME = 1;
|
|
22
31
|
export declare const EMITTER_DEFAULT_AUTOMATICALLY_DESTROY_MODULES = true;
|
|
32
|
+
/** Salts the emitter hands out: one for the builtin values the shaders read, one per module */
|
|
33
|
+
export declare const EMITTER_SHADER_SALT = 0;
|
|
34
|
+
export declare const EMITTER_SPAWN_SALT_BASE = 256;
|
|
35
|
+
export declare const EMITTER_BEHAVIOR_SALT_BASE = 512;
|
|
@@ -5,4 +5,10 @@ export function ignoreInput() {
|
|
|
5
5
|
export const EMITTER_DEFAULT_MODE = UIMode.VISIBLE;
|
|
6
6
|
export const EMITTER_DEFAULT_EXPECTED_CAPACITY = 32;
|
|
7
7
|
export const EMITTER_DEFAULT_CAPACITY_STEP = 32;
|
|
8
|
+
export const EMITTER_DEFAULT_MAX_DELTA_TIME = 0.1;
|
|
9
|
+
export const EMITTER_DEFAULT_LIFETIME = 1;
|
|
8
10
|
export const EMITTER_DEFAULT_AUTOMATICALLY_DESTROY_MODULES = true;
|
|
11
|
+
/** Salts the emitter hands out: one for the builtin values the shaders read, one per module */
|
|
12
|
+
export const EMITTER_SHADER_SALT = 0;
|
|
13
|
+
export const EMITTER_SPAWN_SALT_BASE = 0x100;
|
|
14
|
+
export const EMITTER_BEHAVIOR_SALT_BASE = 0x200;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { UILayer } from "../../core/index.js";
|
|
2
1
|
import { UIAnchor } from "../../core/elements/UIAnchor/UIAnchor.js";
|
|
2
|
+
import type { UILayer } from "../../core/index.js";
|
|
3
3
|
import type { UIBehaviorModule } from "../behaviorModules/UIBehaviorModule.js";
|
|
4
4
|
import type { UIRenderingModule } from "../renderingModules/UIRenderingModule.js";
|
|
5
5
|
import type { UISpawnModule } from "../spawnModules/UISpawnModule.js";
|
|
@@ -22,6 +22,10 @@ export declare class UIEmitter extends UIAnchor {
|
|
|
22
22
|
private emissionDuration;
|
|
23
23
|
private emissionElapsed;
|
|
24
24
|
private modeInternal;
|
|
25
|
+
private maxDeltaTimeInternal;
|
|
26
|
+
private seedInternal;
|
|
27
|
+
/** Particles spawned since the seed was set; picks the seed of the next particle */
|
|
28
|
+
private spawnedCount;
|
|
25
29
|
/**
|
|
26
30
|
* @param layer - Layer containing this emitter
|
|
27
31
|
* @param spawnSequence - Modules that initialize new particles
|
|
@@ -30,6 +34,10 @@ export declare class UIEmitter extends UIAnchor {
|
|
|
30
34
|
* @param options - Configuration options
|
|
31
35
|
*/
|
|
32
36
|
constructor(layer: UILayer, spawnSequence: UISpawnModule[], behaviorSequence: readonly UIBehaviorModule[], renderingSequence: readonly UIRenderingModule[], options?: Partial<UIEmitterOptions>);
|
|
37
|
+
/** Seed of the random values */
|
|
38
|
+
get seed(): number;
|
|
39
|
+
/** Longest frame step in seconds; longer frames are clamped */
|
|
40
|
+
get maxDeltaTime(): number;
|
|
33
41
|
/** Visibility mode */
|
|
34
42
|
get mode(): UIEmitterMode;
|
|
35
43
|
/**
|
|
@@ -39,15 +47,23 @@ export declare class UIEmitter extends UIAnchor {
|
|
|
39
47
|
get zIndex(): number;
|
|
40
48
|
/** Visibility mode */
|
|
41
49
|
set mode(value: UIEmitterMode);
|
|
50
|
+
/** Longest frame step in seconds; longer frames are clamped */
|
|
51
|
+
set maxDeltaTime(value: number);
|
|
52
|
+
/**
|
|
53
|
+
* Sets the seed and restarts its sequence: the next particles repeat those spawned after the
|
|
54
|
+
* same seed was set before
|
|
55
|
+
*/
|
|
56
|
+
set seed(value: number);
|
|
42
57
|
/**
|
|
43
58
|
* Rendering order among the elements of the layer. Higher values on top; elements with equal
|
|
44
59
|
* values keep the order of creation, a later one on top.
|
|
45
60
|
*/
|
|
46
61
|
set zIndex(value: number);
|
|
47
62
|
/**
|
|
48
|
-
* Spawns a specific number of particles immediately.
|
|
63
|
+
* Spawns a specific number of particles immediately. Before the spawn modules run, every
|
|
64
|
+
* property of a new particle is 0, except scale 1 and lifetime 1 second.
|
|
49
65
|
*
|
|
50
|
-
* @param count - Number of particles to create
|
|
66
|
+
* @param count - Number of particles to create, a non-negative integer
|
|
51
67
|
*/
|
|
52
68
|
burst(count: number): void;
|
|
53
69
|
/** Removes all active particles */
|
|
@@ -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 {
|
|
8
|
-
import {
|
|
9
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
64
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
array[itemOffset +
|
|
73
|
-
|
|
74
|
-
array[itemOffset +
|
|
75
|
-
|
|
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
|
|
96
|
-
//
|
|
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
|
-
|
|
109
|
+
const renderingSources = collectRenderingUniforms(collectedUniforms, renderingSequence, "UIEmitter.constructor.renderingSequence");
|
|
105
110
|
const zIndex = 0;
|
|
106
|
-
this.catcherHandler = this.layer.inputWrapper.createInputCatcher(
|
|
107
|
-
|
|
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 {
|
|
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 +
|
|
158
|
-
array[itemOffset +
|
|
159
|
-
array[itemOffset +
|
|
160
|
-
array
|
|
161
|
-
|
|
162
|
-
|
|
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 (
|
|
167
|
-
|
|
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";
|
package/dist/particles/index.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
58
|
-
|
|
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
|
-
|
|
61
|
-
|
|
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) *
|
|
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;
|