@expofp/renderer 0.0.0-experimental.cf063fc
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 +145 -0
- package/dist/Renderer.d.ts +102 -0
- package/dist/Renderer.js +96 -0
- package/dist/controls/CameraControls.d.ts +108 -0
- package/dist/controls/CameraControls.js +203 -0
- package/dist/controls/Controls.d.ts +53 -0
- package/dist/controls/Controls.js +54 -0
- package/dist/controls/camera-api.d.ts +226 -0
- package/dist/controls/camera-api.js +179 -0
- package/dist/controls/gestures.d.ts +71 -0
- package/dist/controls/gestures.js +98 -0
- package/dist/controls/handlers/DoubleTapZoom.d.ts +23 -0
- package/dist/controls/handlers/DoubleTapZoom.js +82 -0
- package/dist/controls/handlers/Inertia.d.ts +60 -0
- package/dist/controls/handlers/Inertia.js +222 -0
- package/dist/controls/handlers/Pan.d.ts +23 -0
- package/dist/controls/handlers/Pan.js +212 -0
- package/dist/controls/handlers/Pitch.d.ts +19 -0
- package/dist/controls/handlers/Pitch.js +200 -0
- package/dist/controls/handlers/Roll.d.ts +30 -0
- package/dist/controls/handlers/Roll.js +196 -0
- package/dist/controls/handlers/Zoom.d.ts +28 -0
- package/dist/controls/handlers/Zoom.js +34 -0
- package/dist/controls/hooks.d.ts +136 -0
- package/dist/controls/hooks.js +200 -0
- package/dist/controls/index.d.ts +9 -0
- package/dist/controls/index.js +7 -0
- package/dist/controls/zoom-to.d.ts +89 -0
- package/dist/controls/zoom-to.js +91 -0
- package/dist/core/InstanceUniforms.d.ts +108 -0
- package/dist/core/InstanceUniforms.js +298 -0
- package/dist/core/SquareDataTexture.d.ts +277 -0
- package/dist/core/SquareDataTexture.js +476 -0
- package/dist/core/batch/BatchedMesh.d.ts +74 -0
- package/dist/core/batch/BatchedMesh.js +155 -0
- package/dist/core/instanced/FrustumCulling.d.ts +141 -0
- package/dist/core/instanced/FrustumCulling.js +246 -0
- package/dist/core/instanced/InstancedMesh.d.ts +302 -0
- package/dist/core/instanced/InstancedMesh.js +497 -0
- package/dist/core/instanced/Instances.d.ts +119 -0
- package/dist/core/instanced/Instances.js +230 -0
- package/dist/core/instanced/Raycasting.d.ts +118 -0
- package/dist/core/instanced/Raycasting.js +134 -0
- package/dist/core/instanced/instanced-material.d.ts +77 -0
- package/dist/core/instanced/instanced-material.js +228 -0
- package/dist/core/lines/BatchedLines.d.ts +137 -0
- package/dist/core/lines/BatchedLines.js +215 -0
- package/dist/core/lines/FrustumCullingLines.d.ts +44 -0
- package/dist/core/lines/FrustumCullingLines.js +85 -0
- package/dist/core/lines/RaycastingLines.d.ts +45 -0
- package/dist/core/lines/RaycastingLines.js +91 -0
- package/dist/core/lines/lines-material.d.ts +47 -0
- package/dist/core/lines/lines-material.js +124 -0
- package/dist/core/shader-source.d.ts +70 -0
- package/dist/core/shader-source.js +110 -0
- package/dist/core/sprites/BatchedSprites.d.ts +115 -0
- package/dist/core/sprites/BatchedSprites.js +173 -0
- package/dist/core/sprites/RaycastingSprites.d.ts +41 -0
- package/dist/core/sprites/RaycastingSprites.js +71 -0
- package/dist/core/sprites/sprite-material.d.ts +59 -0
- package/dist/core/sprites/sprite-material.js +192 -0
- package/dist/core/text/BatchedText.d.ts +233 -0
- package/dist/core/text/BatchedText.js +473 -0
- package/dist/core/text/MemberTexture.d.ts +90 -0
- package/dist/core/text/MemberTexture.js +289 -0
- package/dist/core/text/Text.d.ts +88 -0
- package/dist/core/text/Text.js +230 -0
- package/dist/core/text/batched-text-material.d.ts +56 -0
- package/dist/core/text/batched-text-material.js +123 -0
- package/dist/core/text/glyph-packing.d.ts +17 -0
- package/dist/core/text/glyph-packing.js +61 -0
- package/dist/core/typed-array.d.ts +16 -0
- package/dist/core/typed-array.js +20 -0
- package/dist/debug/components/DebugPanelProvider.d.ts +20 -0
- package/dist/debug/components/DebugPanelProvider.js +50 -0
- package/dist/debug/components/DebugTools.d.ts +22 -0
- package/dist/debug/components/DebugTools.js +28 -0
- package/dist/debug/components/Gui.d.ts +22 -0
- package/dist/debug/components/Gui.js +42 -0
- package/dist/debug/components/MemoryInfo.d.ts +22 -0
- package/dist/debug/components/MemoryInfo.js +42 -0
- package/dist/debug/components/Panel.d.ts +31 -0
- package/dist/debug/components/Panel.js +104 -0
- package/dist/debug/components/Stats.d.ts +9 -0
- package/dist/debug/components/Stats.js +24 -0
- package/dist/debug/debug-chunk.d.ts +26 -0
- package/dist/debug/debug-chunk.js +32 -0
- package/dist/debug/debug-context.d.ts +41 -0
- package/dist/debug/debug-context.js +59 -0
- package/dist/debug/index.d.ts +41 -0
- package/dist/debug/index.js +43 -0
- package/dist/events/LayoutDriver.d.ts +36 -0
- package/dist/events/LayoutDriver.js +192 -0
- package/dist/events/PointerMissed.d.ts +23 -0
- package/dist/events/PointerMissed.js +58 -0
- package/dist/events/def-culling.d.ts +46 -0
- package/dist/events/def-culling.js +69 -0
- package/dist/events/layout-targets.d.ts +151 -0
- package/dist/events/layout-targets.js +397 -0
- package/dist/events/placement/box.d.ts +59 -0
- package/dist/events/placement/box.js +96 -0
- package/dist/events/placement/grid.d.ts +161 -0
- package/dist/events/placement/grid.js +235 -0
- package/dist/events/placement/index.d.ts +2 -0
- package/dist/events/placement/index.js +1 -0
- package/dist/events/placement/pass.d.ts +70 -0
- package/dist/events/placement/pass.js +617 -0
- package/dist/events/pointer.d.ts +194 -0
- package/dist/events/pointer.js +356 -0
- package/dist/external/ExternalCanvas.d.ts +56 -0
- package/dist/external/ExternalCanvas.js +177 -0
- package/dist/external/external-camera.d.ts +18 -0
- package/dist/external/external-camera.js +103 -0
- package/dist/external/external-events.d.ts +14 -0
- package/dist/external/external-events.js +33 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/objects/Background.d.ts +31 -0
- package/dist/objects/Background.js +37 -0
- package/dist/objects/containers/SlotContainer.d.ts +312 -0
- package/dist/objects/containers/SlotContainer.js +311 -0
- package/dist/objects/containers/hooks.d.ts +61 -0
- package/dist/objects/containers/hooks.js +196 -0
- package/dist/objects/containers/index.d.ts +6 -0
- package/dist/objects/containers/index.js +4 -0
- package/dist/objects/containers/keys.d.ts +10 -0
- package/dist/objects/containers/keys.js +18 -0
- package/dist/objects/containers/mesh-writes.d.ts +106 -0
- package/dist/objects/containers/mesh-writes.js +97 -0
- package/dist/objects/containers/props.d.ts +33 -0
- package/dist/objects/containers/props.js +1 -0
- package/dist/objects/images/ImageInstance.d.ts +29 -0
- package/dist/objects/images/ImageInstance.js +152 -0
- package/dist/objects/images/Images.d.ts +49 -0
- package/dist/objects/images/Images.js +394 -0
- package/dist/objects/images/atlas-packer.d.ts +129 -0
- package/dist/objects/images/atlas-packer.js +287 -0
- package/dist/objects/images/image-container.d.ts +74 -0
- package/dist/objects/images/image-container.js +100 -0
- package/dist/objects/images/index.d.ts +2 -0
- package/dist/objects/images/index.js +1 -0
- package/dist/objects/images/pack-sources.d.ts +190 -0
- package/dist/objects/images/pack-sources.js +272 -0
- package/dist/objects/images/texture-budget.d.ts +120 -0
- package/dist/objects/images/texture-budget.js +215 -0
- package/dist/objects/images/worker/atlas-worker.d.ts +56 -0
- package/dist/objects/images/worker/atlas-worker.js +123 -0
- package/dist/objects/images/worker/client.d.ts +62 -0
- package/dist/objects/images/worker/client.js +249 -0
- package/dist/objects/images/worker/index.d.ts +10 -0
- package/dist/objects/images/worker/index.js +1 -0
- package/dist/objects/layer/Layer.d.ts +28 -0
- package/dist/objects/layer/Layer.js +134 -0
- package/dist/objects/layer/index.d.ts +2 -0
- package/dist/objects/layer/index.js +1 -0
- package/dist/objects/layer/layer-context.d.ts +74 -0
- package/dist/objects/layer/layer-context.js +82 -0
- package/dist/objects/layer/mount-scheduler.d.ts +131 -0
- package/dist/objects/layer/mount-scheduler.js +289 -0
- package/dist/objects/layer/visibility-gui.d.ts +19 -0
- package/dist/objects/layer/visibility-gui.js +93 -0
- package/dist/objects/lighting/Lighting.d.ts +92 -0
- package/dist/objects/lighting/Lighting.js +384 -0
- package/dist/objects/lighting/OcclusionPasses.d.ts +23 -0
- package/dist/objects/lighting/OcclusionPasses.js +271 -0
- package/dist/objects/lighting/index.d.ts +11 -0
- package/dist/objects/lighting/index.js +10 -0
- package/dist/objects/lighting/lighting-gui.d.ts +24 -0
- package/dist/objects/lighting/lighting-gui.js +114 -0
- package/dist/objects/lighting/occluder.d.ts +34 -0
- package/dist/objects/lighting/occluder.js +62 -0
- package/dist/objects/lighting/occlusion-targets.d.ts +143 -0
- package/dist/objects/lighting/occlusion-targets.js +276 -0
- package/dist/objects/lines/LineInstance.d.ts +21 -0
- package/dist/objects/lines/LineInstance.js +73 -0
- package/dist/objects/lines/Lines.d.ts +17 -0
- package/dist/objects/lines/Lines.js +31 -0
- package/dist/objects/lines/index.d.ts +2 -0
- package/dist/objects/lines/index.js +1 -0
- package/dist/objects/lines/line-container.d.ts +35 -0
- package/dist/objects/lines/line-container.js +40 -0
- package/dist/objects/materials/ColorMaterial.d.ts +92 -0
- package/dist/objects/materials/ColorMaterial.js +124 -0
- package/dist/objects/materials/LineMaterial.d.ts +44 -0
- package/dist/objects/materials/LineMaterial.js +74 -0
- package/dist/objects/materials/ModelMaterial.d.ts +53 -0
- package/dist/objects/materials/ModelMaterial.js +111 -0
- package/dist/objects/materials/TextureMaterial.d.ts +33 -0
- package/dist/objects/materials/TextureMaterial.js +48 -0
- package/dist/objects/materials/effects/ao/ambient-occlusion-pass.d.ts +77 -0
- package/dist/objects/materials/effects/ao/ambient-occlusion-pass.js +296 -0
- package/dist/objects/materials/effects/ao/ambient-occlusion-patch.d.ts +29 -0
- package/dist/objects/materials/effects/ao/ambient-occlusion-patch.js +115 -0
- package/dist/objects/materials/effects/ao/blur-pass.d.ts +61 -0
- package/dist/objects/materials/effects/ao/blur-pass.js +101 -0
- package/dist/objects/materials/effects/ao/index.d.ts +129 -0
- package/dist/objects/materials/effects/ao/index.js +72 -0
- package/dist/objects/materials/effects/color-dim.d.ts +115 -0
- package/dist/objects/materials/effects/color-dim.js +206 -0
- package/dist/objects/materials/effects/contact-shadow.d.ts +100 -0
- package/dist/objects/materials/effects/contact-shadow.js +167 -0
- package/dist/objects/materials/effects/effects-context.d.ts +53 -0
- package/dist/objects/materials/effects/effects-context.js +24 -0
- package/dist/objects/materials/shared.d.ts +24 -0
- package/dist/objects/materials/shared.js +72 -0
- package/dist/objects/models/ModelInstance.d.ts +22 -0
- package/dist/objects/models/ModelInstance.js +33 -0
- package/dist/objects/models/Models.d.ts +22 -0
- package/dist/objects/models/Models.js +186 -0
- package/dist/objects/models/gltf-cache.d.ts +17 -0
- package/dist/objects/models/gltf-cache.js +50 -0
- package/dist/objects/models/index.d.ts +2 -0
- package/dist/objects/models/index.js +1 -0
- package/dist/objects/models/model-asset.d.ts +48 -0
- package/dist/objects/models/model-asset.js +77 -0
- package/dist/objects/models/model-container.d.ts +123 -0
- package/dist/objects/models/model-container.js +207 -0
- package/dist/objects/shapes/ShapeInstance.d.ts +26 -0
- package/dist/objects/shapes/ShapeInstance.js +39 -0
- package/dist/objects/shapes/Shapes.d.ts +21 -0
- package/dist/objects/shapes/Shapes.js +131 -0
- package/dist/objects/shapes/index.d.ts +2 -0
- package/dist/objects/shapes/index.js +1 -0
- package/dist/objects/shapes/shape-container.d.ts +141 -0
- package/dist/objects/shapes/shape-container.js +242 -0
- package/dist/objects/shapes/shape-geometry.d.ts +27 -0
- package/dist/objects/shapes/shape-geometry.js +94 -0
- package/dist/objects/text/TextInstance.d.ts +22 -0
- package/dist/objects/text/TextInstance.js +95 -0
- package/dist/objects/text/Texts.d.ts +30 -0
- package/dist/objects/text/Texts.js +43 -0
- package/dist/objects/text/index.d.ts +2 -0
- package/dist/objects/text/index.js +1 -0
- package/dist/objects/text/text-container.d.ts +121 -0
- package/dist/objects/text/text-container.js +229 -0
- package/dist/objects/text/text-layout.d.ts +92 -0
- package/dist/objects/text/text-layout.js +180 -0
- package/dist/space/Camera.d.ts +103 -0
- package/dist/space/Camera.js +321 -0
- package/dist/space/Scene.d.ts +47 -0
- package/dist/space/Scene.js +127 -0
- package/dist/space/Space.d.ts +325 -0
- package/dist/space/Space.js +359 -0
- package/dist/space/VisibleRect.d.ts +45 -0
- package/dist/space/VisibleRect.js +129 -0
- package/dist/space/camera-handle.d.ts +67 -0
- package/dist/space/camera-handle.js +63 -0
- package/dist/space/camera-kind.d.ts +26 -0
- package/dist/space/camera-kind.js +30 -0
- package/dist/space/canvas-size.d.ts +24 -0
- package/dist/space/canvas-size.js +20 -0
- package/dist/space/clipping.d.ts +69 -0
- package/dist/space/clipping.js +130 -0
- package/dist/space/layout-measure.d.ts +67 -0
- package/dist/space/layout-measure.js +175 -0
- package/dist/space/viewport.d.ts +112 -0
- package/dist/space/viewport.js +67 -0
- package/dist/types/defs.d.ts +234 -0
- package/dist/types/defs.js +1 -0
- package/dist/types/events.d.ts +198 -0
- package/dist/types/events.js +1 -0
- package/dist/types/guards.d.ts +21 -0
- package/dist/types/guards.js +52 -0
- package/dist/update/index.d.ts +3 -0
- package/dist/update/index.js +2 -0
- package/dist/update/slot-writers.d.ts +30 -0
- package/dist/update/slot-writers.js +76 -0
- package/dist/update/store.d.ts +41 -0
- package/dist/update/store.js +104 -0
- package/dist/utils/arrays.d.ts +13 -0
- package/dist/utils/arrays.js +20 -0
- package/dist/utils/colors.d.ts +19 -0
- package/dist/utils/colors.js +27 -0
- package/dist/utils/hot-state.d.ts +41 -0
- package/dist/utils/hot-state.js +37 -0
- package/dist/utils/logging.d.ts +29 -0
- package/dist/utils/logging.js +55 -0
- package/dist/utils/math.d.ts +52 -0
- package/dist/utils/math.js +76 -0
- package/package.json +79 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { type WebGLProgramParametersWithUniforms } from 'three';
|
|
2
|
+
import { SquareDataTexture, type UniformType, type UniformValue, type UniformValueObj } from './SquareDataTexture';
|
|
3
|
+
/** The GLSL types a per-instance uniform may take, keyed by the name it gets in the shader. */
|
|
4
|
+
export type UniformSchema = Record<string, UniformType>;
|
|
5
|
+
/** Per-instance uniforms to declare, split by the shader stage that reads them. */
|
|
6
|
+
export interface UniformSchemaShader {
|
|
7
|
+
/** Uniforms read in the vertex shader. */
|
|
8
|
+
vertex?: UniformSchema;
|
|
9
|
+
/** Uniforms read in the fragment shader. */
|
|
10
|
+
fragment?: UniformSchema;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The per-instance uniform store shared by all core classes: a {@link SquareDataTexture} plus the schema
|
|
14
|
+
* describing how uniforms pack into it.
|
|
15
|
+
*
|
|
16
|
+
* Ported from the identical uniform code both three.ez packages carry (`Uniforms.ts` in each), with
|
|
17
|
+
* one change: uniforms are **added**, not declared once. {@link add} merges into whatever is already
|
|
18
|
+
* there and rebuilds the texture, carrying existing values across — so a material and its container
|
|
19
|
+
* can each declare what they need without coordinating, and a declaration after instances are
|
|
20
|
+
* written keeps them. Removal is deliberately unsupported; rebuild the mesh if it is ever needed.
|
|
21
|
+
*
|
|
22
|
+
* The shader wiring lives here as well ({@link patchShader}), because every mesh does the same two
|
|
23
|
+
* things with it: bind the texture, and open `main` with the prologue that fetches from it. What
|
|
24
|
+
* differs between them — where the per-instance index comes from, and what each adds around the
|
|
25
|
+
* prologue — is what they pass in.
|
|
26
|
+
*/
|
|
27
|
+
export declare class InstanceUniforms {
|
|
28
|
+
/** The backing texture, or `null` until {@link add} declares the first uniform. */
|
|
29
|
+
texture: SquareDataTexture | null;
|
|
30
|
+
/** Everything declared so far, merged across calls, so a rebuild recomputes the whole layout. */
|
|
31
|
+
private declared;
|
|
32
|
+
/** Backs {@link shaderKey}, rebuilt whenever {@link declared} changes. */
|
|
33
|
+
private key;
|
|
34
|
+
/**
|
|
35
|
+
* What the declared uniforms generate, as one string.
|
|
36
|
+
*
|
|
37
|
+
* A material that fetches per-instance uniforms compiles different GLSL for every schema, and
|
|
38
|
+
* three caches a compiled program under `customProgramCacheKey` alone, with no way to compare
|
|
39
|
+
* source. So every patcher that injects this store's prologue folds this into its own key.
|
|
40
|
+
* Without it, two meshes whose schemas differ share one program and the second silently draws
|
|
41
|
+
* against the first's layout.
|
|
42
|
+
*
|
|
43
|
+
* Empty while nothing is declared, which is itself a distinct key: that program has no prologue.
|
|
44
|
+
* @returns The key, stable for as long as the schema is.
|
|
45
|
+
*/
|
|
46
|
+
get shaderKey(): string;
|
|
47
|
+
/**
|
|
48
|
+
* Declares uniforms, merging with any already declared and rebuilding the texture.
|
|
49
|
+
*
|
|
50
|
+
* The layout packs every uniform into as few texels as it can, so adding one generally moves the
|
|
51
|
+
* others — hence the rebuild-and-copy rather than an in-place append. Redeclaring a name with a
|
|
52
|
+
* different type replaces it.
|
|
53
|
+
* @param schema - The uniforms to add.
|
|
54
|
+
* @param capacity - Instances the texture must hold.
|
|
55
|
+
*/
|
|
56
|
+
add(schema: UniformSchemaShader, capacity: number): void;
|
|
57
|
+
/**
|
|
58
|
+
* Reads one instance's uniform.
|
|
59
|
+
* @param id - The instance to read.
|
|
60
|
+
* @param name - The uniform's name.
|
|
61
|
+
* @param target - Receives the result; unused for a `float`, which is returned directly.
|
|
62
|
+
* @returns The value.
|
|
63
|
+
*/
|
|
64
|
+
get(id: number, name: string, target?: UniformValueObj): UniformValue;
|
|
65
|
+
/**
|
|
66
|
+
* Writes one instance's uniform and marks its row for upload.
|
|
67
|
+
* @param id - The instance to write.
|
|
68
|
+
* @param name - The uniform's name.
|
|
69
|
+
* @param value - The value to write.
|
|
70
|
+
*/
|
|
71
|
+
set(id: number, name: string, value: UniformValue): void;
|
|
72
|
+
/**
|
|
73
|
+
* Grows the texture to a new instance capacity, keeping what is written.
|
|
74
|
+
* @param capacity - The new capacity.
|
|
75
|
+
*/
|
|
76
|
+
resize(capacity: number): void;
|
|
77
|
+
/**
|
|
78
|
+
* Points a compiling shader at these uniforms: binds the texture, and opens `main` in both stages
|
|
79
|
+
* with the prologue that fetches this instance's values and brings them into scope under the
|
|
80
|
+
* names they were declared with.
|
|
81
|
+
*
|
|
82
|
+
* Does nothing and answers `false` while no uniform is declared — there is no texture to bind and
|
|
83
|
+
* nothing to name, so a caller whose own GLSL reads those names has to skip it too.
|
|
84
|
+
* @param shader - The program parameters to edit.
|
|
85
|
+
* @param indexName - The per-instance index the fetch addresses by, in shader scope where the
|
|
86
|
+
* prologue lands (a float; see {@link SquareDataTexture}). A caller computing it inside `main`
|
|
87
|
+
* supplies it with a later {@link openMain}, which lands ahead of this fetch.
|
|
88
|
+
* @param body - GLSL to run straight after the fetch, in both stages. Where code reading the
|
|
89
|
+
* uniforms goes, since nothing ahead of the prologue can see them.
|
|
90
|
+
* @returns Whether the shader was patched.
|
|
91
|
+
* @throws {Error} If either stage has no `main` to inject into.
|
|
92
|
+
*/
|
|
93
|
+
patchShader(shader: WebGLProgramParametersWithUniforms, indexName: string, body?: string): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Adopts another store's schema and an independent copy of its values, for cloning a mesh.
|
|
96
|
+
* @param source - The store to copy.
|
|
97
|
+
*/
|
|
98
|
+
copyFrom(source: InstanceUniforms): void;
|
|
99
|
+
/** Frees the texture. */
|
|
100
|
+
dispose(): void;
|
|
101
|
+
/**
|
|
102
|
+
* The texture, or a readable failure when nothing declared a uniform.
|
|
103
|
+
* @returns The texture.
|
|
104
|
+
* @throws {Error} If {@link add} has not been called.
|
|
105
|
+
*/
|
|
106
|
+
private require;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=InstanceUniforms.d.ts.map
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { Matrix3, Matrix4, Vector2, Vector3, Vector4, } from 'three';
|
|
2
|
+
import { declareBeforeMain, openMain } from './shader-source';
|
|
3
|
+
import { SquareDataTexture, } from './SquareDataTexture';
|
|
4
|
+
/**
|
|
5
|
+
* The per-instance uniform store shared by all core classes: a {@link SquareDataTexture} plus the schema
|
|
6
|
+
* describing how uniforms pack into it.
|
|
7
|
+
*
|
|
8
|
+
* Ported from the identical uniform code both three.ez packages carry (`Uniforms.ts` in each), with
|
|
9
|
+
* one change: uniforms are **added**, not declared once. {@link add} merges into whatever is already
|
|
10
|
+
* there and rebuilds the texture, carrying existing values across — so a material and its container
|
|
11
|
+
* can each declare what they need without coordinating, and a declaration after instances are
|
|
12
|
+
* written keeps them. Removal is deliberately unsupported; rebuild the mesh if it is ever needed.
|
|
13
|
+
*
|
|
14
|
+
* The shader wiring lives here as well ({@link patchShader}), because every mesh does the same two
|
|
15
|
+
* things with it: bind the texture, and open `main` with the prologue that fetches from it. What
|
|
16
|
+
* differs between them — where the per-instance index comes from, and what each adds around the
|
|
17
|
+
* prologue — is what they pass in.
|
|
18
|
+
*/
|
|
19
|
+
export class InstanceUniforms {
|
|
20
|
+
/** The backing texture, or `null` until {@link add} declares the first uniform. */
|
|
21
|
+
texture = null;
|
|
22
|
+
/** Everything declared so far, merged across calls, so a rebuild recomputes the whole layout. */
|
|
23
|
+
declared = {};
|
|
24
|
+
/** Backs {@link shaderKey}, rebuilt whenever {@link declared} changes. */
|
|
25
|
+
key = '';
|
|
26
|
+
/**
|
|
27
|
+
* What the declared uniforms generate, as one string.
|
|
28
|
+
*
|
|
29
|
+
* A material that fetches per-instance uniforms compiles different GLSL for every schema, and
|
|
30
|
+
* three caches a compiled program under `customProgramCacheKey` alone, with no way to compare
|
|
31
|
+
* source. So every patcher that injects this store's prologue folds this into its own key.
|
|
32
|
+
* Without it, two meshes whose schemas differ share one program and the second silently draws
|
|
33
|
+
* against the first's layout.
|
|
34
|
+
*
|
|
35
|
+
* Empty while nothing is declared, which is itself a distinct key: that program has no prologue.
|
|
36
|
+
* @returns The key, stable for as long as the schema is.
|
|
37
|
+
*/
|
|
38
|
+
get shaderKey() {
|
|
39
|
+
return this.key;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Declares uniforms, merging with any already declared and rebuilding the texture.
|
|
43
|
+
*
|
|
44
|
+
* The layout packs every uniform into as few texels as it can, so adding one generally moves the
|
|
45
|
+
* others — hence the rebuild-and-copy rather than an in-place append. Redeclaring a name with a
|
|
46
|
+
* different type replaces it.
|
|
47
|
+
* @param schema - The uniforms to add.
|
|
48
|
+
* @param capacity - Instances the texture must hold.
|
|
49
|
+
*/
|
|
50
|
+
add(schema, capacity) {
|
|
51
|
+
const vertex = { ...this.declared.vertex, ...schema.vertex };
|
|
52
|
+
const fragment = { ...this.declared.fragment, ...schema.fragment };
|
|
53
|
+
const layout = buildUniformLayout({ vertex, fragment });
|
|
54
|
+
const texture = new SquareDataTexture(Float32Array, layout.channels, layout.pixelsPerInstance, capacity, layout.uniformMap, layout.fetchInFragmentShader);
|
|
55
|
+
const previous = this.texture;
|
|
56
|
+
if (previous) {
|
|
57
|
+
copyUniforms(previous, texture, declaredTypes(this.declared), capacity);
|
|
58
|
+
previous.dispose();
|
|
59
|
+
}
|
|
60
|
+
this.texture = texture;
|
|
61
|
+
this.declared = { vertex, fragment };
|
|
62
|
+
this.key = schemaKey(this.declared);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Reads one instance's uniform.
|
|
66
|
+
* @param id - The instance to read.
|
|
67
|
+
* @param name - The uniform's name.
|
|
68
|
+
* @param target - Receives the result; unused for a `float`, which is returned directly.
|
|
69
|
+
* @returns The value.
|
|
70
|
+
*/
|
|
71
|
+
get(id, name, target) {
|
|
72
|
+
return this.require().getUniformAt(id, name, target);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Writes one instance's uniform and marks its row for upload.
|
|
76
|
+
* @param id - The instance to write.
|
|
77
|
+
* @param name - The uniform's name.
|
|
78
|
+
* @param value - The value to write.
|
|
79
|
+
*/
|
|
80
|
+
set(id, name, value) {
|
|
81
|
+
const texture = this.require();
|
|
82
|
+
texture.setUniformAt(id, name, value);
|
|
83
|
+
texture.enqueueUpdate(id);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Grows the texture to a new instance capacity, keeping what is written.
|
|
87
|
+
* @param capacity - The new capacity.
|
|
88
|
+
*/
|
|
89
|
+
resize(capacity) {
|
|
90
|
+
this.texture?.resize(capacity);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Points a compiling shader at these uniforms: binds the texture, and opens `main` in both stages
|
|
94
|
+
* with the prologue that fetches this instance's values and brings them into scope under the
|
|
95
|
+
* names they were declared with.
|
|
96
|
+
*
|
|
97
|
+
* Does nothing and answers `false` while no uniform is declared — there is no texture to bind and
|
|
98
|
+
* nothing to name, so a caller whose own GLSL reads those names has to skip it too.
|
|
99
|
+
* @param shader - The program parameters to edit.
|
|
100
|
+
* @param indexName - The per-instance index the fetch addresses by, in shader scope where the
|
|
101
|
+
* prologue lands (a float; see {@link SquareDataTexture}). A caller computing it inside `main`
|
|
102
|
+
* supplies it with a later {@link openMain}, which lands ahead of this fetch.
|
|
103
|
+
* @param body - GLSL to run straight after the fetch, in both stages. Where code reading the
|
|
104
|
+
* uniforms goes, since nothing ahead of the prologue can see them.
|
|
105
|
+
* @returns Whether the shader was patched.
|
|
106
|
+
* @throws {Error} If either stage has no `main` to inject into.
|
|
107
|
+
*/
|
|
108
|
+
patchShader(shader, indexName, body = '') {
|
|
109
|
+
const texture = this.texture;
|
|
110
|
+
if (!texture)
|
|
111
|
+
return false;
|
|
112
|
+
const glsl = texture.getUniformsGLSL(UNIFORMS_TEXTURE, indexName);
|
|
113
|
+
shader.uniforms[UNIFORMS_TEXTURE] = { value: texture };
|
|
114
|
+
shader.vertexShader = openMain(declareBeforeMain(shader.vertexShader, glsl.vertex.declarations), `${glsl.vertex.body}\n${body}`);
|
|
115
|
+
shader.fragmentShader = openMain(declareBeforeMain(shader.fragmentShader, glsl.fragment.declarations), `${glsl.fragment.body}\n${body}`);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Adopts another store's schema and an independent copy of its values, for cloning a mesh.
|
|
120
|
+
* @param source - The store to copy.
|
|
121
|
+
*/
|
|
122
|
+
copyFrom(source) {
|
|
123
|
+
this.declared = {
|
|
124
|
+
vertex: { ...source.declared.vertex },
|
|
125
|
+
fragment: { ...source.declared.fragment },
|
|
126
|
+
};
|
|
127
|
+
this.key = source.key;
|
|
128
|
+
this.texture = source.texture ? source.texture.clone() : null;
|
|
129
|
+
}
|
|
130
|
+
/** Frees the texture. */
|
|
131
|
+
dispose() {
|
|
132
|
+
this.texture?.dispose();
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The texture, or a readable failure when nothing declared a uniform.
|
|
136
|
+
* @returns The texture.
|
|
137
|
+
* @throws {Error} If {@link add} has not been called.
|
|
138
|
+
*/
|
|
139
|
+
require() {
|
|
140
|
+
if (!this.texture) {
|
|
141
|
+
throw new Error('InstanceUniforms: declare uniforms with "addUniforms" before reading or writing them.');
|
|
142
|
+
}
|
|
143
|
+
return this.texture;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Packs a schema into texels, largest uniform first so small ones fill the gaps.
|
|
148
|
+
* @param schema - Every uniform to lay out.
|
|
149
|
+
* @returns The layout.
|
|
150
|
+
*/
|
|
151
|
+
function buildUniformLayout(schema) {
|
|
152
|
+
const vertexSchema = schema.vertex ?? {};
|
|
153
|
+
const fragmentSchema = schema.fragment ?? {};
|
|
154
|
+
const uniforms = [];
|
|
155
|
+
let totalSize = 0;
|
|
156
|
+
// Only when nothing is read in the vertex shader can the fetch move to the fragment shader.
|
|
157
|
+
let fetchInFragmentShader = true;
|
|
158
|
+
for (const name in vertexSchema) {
|
|
159
|
+
const type = vertexSchema[name];
|
|
160
|
+
const size = uniformSize(type);
|
|
161
|
+
totalSize += size;
|
|
162
|
+
uniforms.push({ name, type, size });
|
|
163
|
+
fetchInFragmentShader = false;
|
|
164
|
+
}
|
|
165
|
+
for (const name in fragmentSchema) {
|
|
166
|
+
if (vertexSchema[name])
|
|
167
|
+
continue;
|
|
168
|
+
const type = fragmentSchema[name];
|
|
169
|
+
const size = uniformSize(type);
|
|
170
|
+
totalSize += size;
|
|
171
|
+
uniforms.push({ name, type, size });
|
|
172
|
+
}
|
|
173
|
+
uniforms.sort((a, b) => b.size - a.size);
|
|
174
|
+
const uniformMap = new Map();
|
|
175
|
+
const filled = [];
|
|
176
|
+
for (const { name, size, type } of uniforms) {
|
|
177
|
+
uniformMap.set(name, { offset: uniformOffset(size, filled), size, type });
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
channels: Math.min(totalSize, 4),
|
|
181
|
+
pixelsPerInstance: Math.ceil(totalSize / 4),
|
|
182
|
+
uniformMap,
|
|
183
|
+
fetchInFragmentShader,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Places one uniform, reusing a partly filled texel when it fits.
|
|
188
|
+
* @param size - Elements the uniform spans.
|
|
189
|
+
* @param filled - Elements used so far in each texel; extended as texels are opened.
|
|
190
|
+
* @returns The uniform's element offset within an instance's record.
|
|
191
|
+
*/
|
|
192
|
+
function uniformOffset(size, filled) {
|
|
193
|
+
if (size < 4) {
|
|
194
|
+
for (let i = 0; i < filled.length; i++) {
|
|
195
|
+
if (filled[i] + size <= 4) {
|
|
196
|
+
const offset = i * 4 + filled[i];
|
|
197
|
+
filled[i] += size;
|
|
198
|
+
return offset;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const offset = filled.length * 4;
|
|
203
|
+
for (let remaining = size; remaining > 0; remaining -= 4) {
|
|
204
|
+
filled.push(remaining);
|
|
205
|
+
}
|
|
206
|
+
return offset;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Elements a uniform type occupies.
|
|
210
|
+
* @param type - The GLSL type.
|
|
211
|
+
* @returns Its element count.
|
|
212
|
+
* @throws {Error} If the type is not one this texture can store.
|
|
213
|
+
*/
|
|
214
|
+
function uniformSize(type) {
|
|
215
|
+
switch (type) {
|
|
216
|
+
case 'float':
|
|
217
|
+
return 1;
|
|
218
|
+
case 'vec2':
|
|
219
|
+
return 2;
|
|
220
|
+
case 'vec3':
|
|
221
|
+
return 3;
|
|
222
|
+
case 'vec4':
|
|
223
|
+
return 4;
|
|
224
|
+
case 'mat3':
|
|
225
|
+
return 9;
|
|
226
|
+
case 'mat4':
|
|
227
|
+
return 16;
|
|
228
|
+
default:
|
|
229
|
+
throw new Error(`InstanceUniforms: invalid uniform type "${String(type)}".`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Builds the key that stands for the GLSL a schema generates. See {@link InstanceUniforms.shaderKey}.
|
|
234
|
+
*
|
|
235
|
+
* Names, types and the stage each is read in, because all three reach the generated source. Left in
|
|
236
|
+
* declaration order rather than sorted, since that is the order the layout packs them in, so two
|
|
237
|
+
* schemas that differ only in order still pack differently and have to compile apart.
|
|
238
|
+
* @param schema - The declared uniforms.
|
|
239
|
+
* @returns The key.
|
|
240
|
+
*/
|
|
241
|
+
function schemaKey(schema) {
|
|
242
|
+
const stage = (prefix, uniforms = {}) => Object.entries(uniforms).map(([name, type]) => `${prefix}${name}:${type}`);
|
|
243
|
+
return [...stage('v', schema.vertex), ...stage('f', schema.fragment)].join(',');
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Flattens a schema into the name-to-type pairs a rebuild has to carry across.
|
|
247
|
+
* @param schema - The declared uniforms.
|
|
248
|
+
* @returns Every uniform, whichever stage reads it.
|
|
249
|
+
*/
|
|
250
|
+
function declaredTypes(schema) {
|
|
251
|
+
return { ...schema.vertex, ...schema.fragment };
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Carries per-instance values from one layout to the next, so declaring a uniform does not discard
|
|
255
|
+
* what is already written. Goes through the public accessors, which is what makes it indifferent to
|
|
256
|
+
* the offsets moving underneath.
|
|
257
|
+
* @param previous - The texture being replaced.
|
|
258
|
+
* @param next - The texture replacing it.
|
|
259
|
+
* @param types - The uniforms to carry, by name.
|
|
260
|
+
* @param count - Instances to copy.
|
|
261
|
+
*/
|
|
262
|
+
function copyUniforms(previous, next, types, count) {
|
|
263
|
+
for (const name in types) {
|
|
264
|
+
// Allocated once per uniform, not per instance, so a rebuild costs a handful of objects however
|
|
265
|
+
// many instances it carries across.
|
|
266
|
+
const target = uniformScratch(types[name]);
|
|
267
|
+
for (let id = 0; id < count; id++) {
|
|
268
|
+
next.setUniformAt(id, name, previous.getUniformAt(id, name, target));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* A target for reading a uniform of the given type.
|
|
274
|
+
* @param type - The GLSL type.
|
|
275
|
+
* @returns The scratch object, or `undefined` for a `float`, which is read by value.
|
|
276
|
+
*/
|
|
277
|
+
function uniformScratch(type) {
|
|
278
|
+
switch (type) {
|
|
279
|
+
case 'vec2':
|
|
280
|
+
return new Vector2();
|
|
281
|
+
case 'vec3':
|
|
282
|
+
return new Vector3();
|
|
283
|
+
case 'vec4':
|
|
284
|
+
return new Vector4();
|
|
285
|
+
case 'mat3':
|
|
286
|
+
return new Matrix3();
|
|
287
|
+
case 'mat4':
|
|
288
|
+
return new Matrix4();
|
|
289
|
+
default:
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* The sampler name the uniforms texture binds to. Private because both sides of the agreement it
|
|
295
|
+
* enforces — the generated GLSL and the `shader.uniforms[…]` entry — are set by
|
|
296
|
+
* {@link InstanceUniforms.patchShader}.
|
|
297
|
+
*/
|
|
298
|
+
const UNIFORMS_TEXTURE = 'uniformsTexture';
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { type Color, DataTexture, type Matrix3, type Matrix4, type PixelFormat, type TextureDataType, type TypedArray, type TypedArrayConstructor, type Vector2, type Vector3, type Vector4, type WebGLRenderer, WebGLUtils } from 'three';
|
|
2
|
+
/** Represents the number of elements per pixel. */
|
|
3
|
+
export type ChannelSize = 1 | 2 | 3 | 4;
|
|
4
|
+
/** Represents the texture information including its data, size, format, and data type. */
|
|
5
|
+
export interface TextureInfo {
|
|
6
|
+
/** The allocated typed array. */
|
|
7
|
+
array: TypedArray;
|
|
8
|
+
/** The square texture's edge in pixels. */
|
|
9
|
+
size: number;
|
|
10
|
+
/** The pixel format. */
|
|
11
|
+
format: PixelFormat;
|
|
12
|
+
/** The data type. */
|
|
13
|
+
type: TextureDataType;
|
|
14
|
+
}
|
|
15
|
+
/** Represents information for updating rows in the texture. */
|
|
16
|
+
export interface UpdateRowInfo {
|
|
17
|
+
/** The first row of the range. */
|
|
18
|
+
row: number;
|
|
19
|
+
/** How many rows the range spans. */
|
|
20
|
+
count: number;
|
|
21
|
+
}
|
|
22
|
+
/** Defines the possible types of uniforms that can be used in shaders. */
|
|
23
|
+
export type UniformType = 'float' | 'vec2' | 'vec3' | 'vec4' | 'mat3' | 'mat4';
|
|
24
|
+
/** Represents a value that can be used as a uniform. */
|
|
25
|
+
export type UniformValueObj = Vector2 | Vector3 | Vector4 | Matrix3 | Matrix4 | Color;
|
|
26
|
+
/** Defines a uniform value as either a number or a compatible three object. */
|
|
27
|
+
export type UniformValue = number | UniformValueObj;
|
|
28
|
+
/** Represents the schema for a uniform, defining its offset, size, and type. */
|
|
29
|
+
export interface UniformMapType {
|
|
30
|
+
/** The uniform's element offset within an instance's record. */
|
|
31
|
+
offset: number;
|
|
32
|
+
/** How many elements it spans. */
|
|
33
|
+
size: number;
|
|
34
|
+
/** Its GLSL type. */
|
|
35
|
+
type: UniformType;
|
|
36
|
+
}
|
|
37
|
+
/** Represents a map of uniform names to their schema definitions. */
|
|
38
|
+
export type UniformMap = Map<string, UniformMapType>;
|
|
39
|
+
/**
|
|
40
|
+
* The GLSL one shader stage needs in order to read a texture's uniforms, split by where it goes.
|
|
41
|
+
*
|
|
42
|
+
* Split rather than handed over as one string that opens `main` itself, which is what this used to
|
|
43
|
+
* emit: a prologue carrying its own `void main() {` made every patch that came after it aim at *that*
|
|
44
|
+
* one instead of the shader's, and the ordering that produced had to be re-explained at each call
|
|
45
|
+
* site. Both halves now go in through `shader-source`, which owns the anchor.
|
|
46
|
+
*/
|
|
47
|
+
export interface UniformsStageGLSL {
|
|
48
|
+
/** Samplers and varyings, which belong above `main`. */
|
|
49
|
+
declarations: string;
|
|
50
|
+
/** The fetch and the varying traffic, which belong as `main`'s first statements. */
|
|
51
|
+
body: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Calculates the square texture size based on the capacity and pixels per instance, so the texture
|
|
55
|
+
* is large enough to store every instance in a square layout.
|
|
56
|
+
* @param capacity - The maximum number of instances allowed in the texture.
|
|
57
|
+
* @param pixelsPerInstance - The number of pixels required for each instance.
|
|
58
|
+
* @returns The size of the square texture needed to store all the instances.
|
|
59
|
+
*/
|
|
60
|
+
export declare function getSquareTextureSize(capacity: number, pixelsPerInstance: number): number;
|
|
61
|
+
/**
|
|
62
|
+
* Generates texture information (size, format, type) for a square texture.
|
|
63
|
+
* @param arrayType - The constructor for the TypedArray.
|
|
64
|
+
* @param channels - The number of channels in the texture.
|
|
65
|
+
* @param pixelsPerInstance - The number of pixels required for each instance.
|
|
66
|
+
* @param capacity - The maximum number of instances allowed in the texture.
|
|
67
|
+
* @returns An object containing the texture's array, size, format, and data type.
|
|
68
|
+
*/
|
|
69
|
+
export declare function getSquareTextureInfo(arrayType: TypedArrayConstructor, channels: ChannelSize, pixelsPerInstance: number, capacity: number): TextureInfo;
|
|
70
|
+
/**
|
|
71
|
+
* A `DataTexture` laid out as a square grid of per-instance records, with partial row uploads and
|
|
72
|
+
* per-instance uniforms addressed by instance id.
|
|
73
|
+
*
|
|
74
|
+
* Vendored from three.ez, which ships two near-identical copies of this file — one per package —
|
|
75
|
+
* and we would otherwise bundle both:
|
|
76
|
+
* - {@link https://github.com/agargaro/batched-mesh-extensions/blob/master/src/core/SquareDataTexture.ts | batched-mesh-extensions}
|
|
77
|
+
* - {@link https://github.com/agargaro/instanced-mesh/blob/master/src/core/utils/SquareDataTexture.ts | instanced-mesh}
|
|
78
|
+
*
|
|
79
|
+
* This is the batched-mesh-extensions version, the later of the two: its `update` hands whole-texture
|
|
80
|
+
* uploads back to three instead of hand-binding a texture unit, which drops `bindToProgram`,
|
|
81
|
+
* `getSlot`, `updateFull` and `updatePartial` along with the `?? maxTextures - 1` slot guess the
|
|
82
|
+
* instanced-mesh copy still carries.
|
|
83
|
+
*
|
|
84
|
+
* Three deliberate departures from both originals:
|
|
85
|
+
*
|
|
86
|
+
* 1. **No `flat` on the varyings.** With flat shading a fragment takes its value from the
|
|
87
|
+
* primitive's *provoking* vertex, and WebGL's default last-vertex convention is the expensive one
|
|
88
|
+
* for drivers — which is why upstream wants `WEBGL_provoking_vertex` enabled. Every value here is
|
|
89
|
+
* per-instance and therefore identical across a primitive's vertices, so interpolating instead of
|
|
90
|
+
* flat-shading yields the same result and needs no extension.
|
|
91
|
+
* 2. **The index varying is always `float`**, so `getUniformsGLSL` no longer takes an index type.
|
|
92
|
+
* It follows from the first: GLSL ES 3.00 requires integer varyings to be `flat`, so an index
|
|
93
|
+
* that is not float could not have the qualifier removed. **Callers must therefore hand it a float
|
|
94
|
+
* index** — `BatchedMeshImpl` does, from three's own `getIndirectIndex( gl_DrawID )`, and
|
|
95
|
+
* `InstancedMeshImpl` does, from the float index texture that replaced upstream's `uint` attribute.
|
|
96
|
+
* 3. **`clone` and `copy` produce an independent texture.** See each method.
|
|
97
|
+
*/
|
|
98
|
+
export declare class SquareDataTexture extends DataTexture {
|
|
99
|
+
/**
|
|
100
|
+
* Whether to enable partial texture updates by row. If `false`, the entire texture is updated.
|
|
101
|
+
* @default true
|
|
102
|
+
*/
|
|
103
|
+
partialUpdate: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* The maximum number of update calls per frame. Past it, the whole texture is re-uploaded instead.
|
|
106
|
+
* @default Infinity
|
|
107
|
+
*/
|
|
108
|
+
maxUpdateCalls: number;
|
|
109
|
+
/** Format/type conversion helper, built on first upload. */
|
|
110
|
+
protected _utils: WebGLUtils | null;
|
|
111
|
+
/** Whether any row has been marked since the last upload. */
|
|
112
|
+
protected _needsUpdate: boolean;
|
|
113
|
+
/** Width at the last upload, used to notice a resize; `null` until the first one. */
|
|
114
|
+
protected _lastWidth: number | null;
|
|
115
|
+
/** The typed array backing the texture. */
|
|
116
|
+
protected _data: TypedArray;
|
|
117
|
+
/** Elements per pixel. */
|
|
118
|
+
protected _channels: ChannelSize;
|
|
119
|
+
/** Pixels each instance occupies. */
|
|
120
|
+
protected _pixelsPerInstance: number;
|
|
121
|
+
/** Elements each instance occupies. */
|
|
122
|
+
protected _stride: number;
|
|
123
|
+
/** Rows marked dirty for the next partial upload. */
|
|
124
|
+
protected _rowToUpdate: boolean[];
|
|
125
|
+
/** Reused pool of dirty-row ranges, refilled each upload so a frame allocates nothing. */
|
|
126
|
+
protected readonly _rowRanges: UpdateRowInfo[];
|
|
127
|
+
/** Uniform layout, shared with whoever built the schema. */
|
|
128
|
+
protected _uniformMap: UniformMap;
|
|
129
|
+
/** Whether uniforms are fetched in the fragment shader rather than passed as varyings. */
|
|
130
|
+
protected _fetchUniformsInFragmentShader: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* The backing typed array. An instanced mesh reads and writes its matrix and color data through
|
|
133
|
+
* it directly, the way three's own `InstancedMesh` writes its instance attributes — a per-instance
|
|
134
|
+
* accessor would be pure overhead on that hot path. Pair a write with {@link enqueueUpdate} so the
|
|
135
|
+
* row is re-uploaded.
|
|
136
|
+
* @returns The array.
|
|
137
|
+
*/
|
|
138
|
+
get data(): TypedArray;
|
|
139
|
+
/**
|
|
140
|
+
* @param arrayType - The constructor for the TypedArray.
|
|
141
|
+
* @param channels - The number of channels in the texture.
|
|
142
|
+
* @param pixelsPerInstance - The number of pixels required for each instance.
|
|
143
|
+
* @param capacity - The total number of instances.
|
|
144
|
+
* @param uniformMap - Optional map for handling uniform values.
|
|
145
|
+
* @param fetchInFragmentShader - Optional flag that determines if uniform values should be fetched
|
|
146
|
+
* in the fragment shader instead of the vertex shader.
|
|
147
|
+
*/
|
|
148
|
+
constructor(arrayType: TypedArrayConstructor, channels: ChannelSize, pixelsPerInstance: number, capacity: number, uniformMap?: UniformMap, fetchInFragmentShader?: boolean);
|
|
149
|
+
/**
|
|
150
|
+
* Resizes the texture to accommodate a new number of instances.
|
|
151
|
+
* @param count - The new total number of instances.
|
|
152
|
+
*/
|
|
153
|
+
resize(count: number): void;
|
|
154
|
+
/**
|
|
155
|
+
* Marks a row of the texture for update during the next render cycle, so only rows that changed
|
|
156
|
+
* are re-uploaded.
|
|
157
|
+
* @param index - The index of the instance to update.
|
|
158
|
+
*/
|
|
159
|
+
enqueueUpdate(index: number): void;
|
|
160
|
+
/**
|
|
161
|
+
* Uploads whatever changed since the last frame, a row range at a time.
|
|
162
|
+
*
|
|
163
|
+
* Defers to three's own full upload whenever it is going to happen anyway — before the texture
|
|
164
|
+
* exists, after a resize, or when the version moved — and whenever the dirty rows are so scattered
|
|
165
|
+
* that `maxUpdateCalls` would be exceeded.
|
|
166
|
+
* @param renderer - The renderer to upload through.
|
|
167
|
+
*/
|
|
168
|
+
update(renderer: WebGLRenderer): void;
|
|
169
|
+
/**
|
|
170
|
+
* Sets a uniform value at the specified instance ID in the texture.
|
|
171
|
+
* @param id - The instance ID to set the uniform for.
|
|
172
|
+
* @param name - The name of the uniform.
|
|
173
|
+
* @param value - The value to set for the uniform.
|
|
174
|
+
*/
|
|
175
|
+
setUniformAt(id: number, name: string, value: UniformValue): void;
|
|
176
|
+
/**
|
|
177
|
+
* Retrieves a uniform value at the specified instance ID from the texture.
|
|
178
|
+
* @param id - The instance ID to retrieve the uniform from.
|
|
179
|
+
* @param name - The name of the uniform.
|
|
180
|
+
* @param target - Optional target object to store the uniform value.
|
|
181
|
+
* @returns The uniform value for the specified instance.
|
|
182
|
+
*/
|
|
183
|
+
getUniformAt(id: number, name: string, target?: UniformValueObj): UniformValue;
|
|
184
|
+
/**
|
|
185
|
+
* Generates the GLSL for reading this texture's uniforms.
|
|
186
|
+
*
|
|
187
|
+
* The index varying is `float` and has no index-type parameter, because these varyings carry no
|
|
188
|
+
* `flat` qualifier — see the class docs. `indexName` must name a float in the shader.
|
|
189
|
+
* @param textureName - The name of the texture in the GLSL shader.
|
|
190
|
+
* @param indexName - The name of the index in the GLSL shader.
|
|
191
|
+
* @returns What each stage declares and what it runs first; see {@link UniformsStageGLSL}.
|
|
192
|
+
*/
|
|
193
|
+
getUniformsGLSL(textureName: string, indexName: string): {
|
|
194
|
+
vertex: UniformsStageGLSL;
|
|
195
|
+
fragment: UniformsStageGLSL;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Copies another texture's pixels and layout, leaving nothing shared between the two.
|
|
199
|
+
*
|
|
200
|
+
* Both originals assign `_rowToUpdate` by reference and inherit `Texture.copy`'s by-reference
|
|
201
|
+
* `source`, so a copy and its original wrote through into each other's pixels and marked each
|
|
202
|
+
* other's rows dirty. Only `_uniformMap` stays shared, which is safe: it is the layout descriptor
|
|
203
|
+
* built once from the schema and never mutated.
|
|
204
|
+
* @param source - The texture to copy.
|
|
205
|
+
* @returns `this`.
|
|
206
|
+
*/
|
|
207
|
+
copy(source: SquareDataTexture): this;
|
|
208
|
+
/**
|
|
209
|
+
* Duplicates the texture.
|
|
210
|
+
*
|
|
211
|
+
* Neither original overrides this, so three's `Texture.clone` ran `new this.constructor()` with no
|
|
212
|
+
* arguments — and this constructor derives its size, format and typed array from those arguments,
|
|
213
|
+
* so the inherited clone always threw, taking every `copy` of an owning mesh down with it.
|
|
214
|
+
* @returns The independent copy.
|
|
215
|
+
*/
|
|
216
|
+
clone(): this;
|
|
217
|
+
/**
|
|
218
|
+
* Groups the dirty rows into contiguous ranges, so neighbors upload in one call. Fills the reused
|
|
219
|
+
* {@link _rowRanges} pool — growing it only when a frame has more ranges than ever before — rather
|
|
220
|
+
* than allocating a fresh array and range objects every frame.
|
|
221
|
+
* @returns How many ranges were written to the pool.
|
|
222
|
+
*/
|
|
223
|
+
protected collectDirtyRanges(): number;
|
|
224
|
+
/**
|
|
225
|
+
* Uploads the given row ranges with `texSubImage2D`.
|
|
226
|
+
* @param webglTexture - The uploaded texture to write into.
|
|
227
|
+
* @param renderer - The renderer to upload through.
|
|
228
|
+
* @param rangeCount - How many of the pooled {@link _rowRanges} to upload.
|
|
229
|
+
*/
|
|
230
|
+
protected updateRows(webglTexture: WebGLTexture, renderer: WebGLRenderer, rangeCount: number): void;
|
|
231
|
+
/**
|
|
232
|
+
* The vertex stage's share: either the whole fetch plus the varyings it feeds, or — when uniforms
|
|
233
|
+
* are read in the fragment shader — just the index handed across.
|
|
234
|
+
* @param textureName - The sampler name.
|
|
235
|
+
* @param indexName - The per-instance index available in the vertex shader; must be a float.
|
|
236
|
+
* @returns What the stage declares and what it runs first.
|
|
237
|
+
*/
|
|
238
|
+
protected getUniformsVertexGLSL(textureName: string, indexName: string): UniformsStageGLSL;
|
|
239
|
+
/**
|
|
240
|
+
* The fragment stage's share: either the varyings the vertex shader filled, or the fetch itself.
|
|
241
|
+
* @param textureName - The sampler name.
|
|
242
|
+
* @param indexName - The per-instance index carried across as a varying.
|
|
243
|
+
* @returns What the stage declares and what it runs first.
|
|
244
|
+
*/
|
|
245
|
+
protected getUniformsFragmentGLSL(textureName: string, indexName: string): UniformsStageGLSL;
|
|
246
|
+
/**
|
|
247
|
+
* GLSL fetching every texel one instance occupies.
|
|
248
|
+
* @param textureName - The sampler name.
|
|
249
|
+
* @param indexName - The instance index expression.
|
|
250
|
+
* @returns The GLSL statements.
|
|
251
|
+
*/
|
|
252
|
+
protected texelsFetchGLSL(textureName: string, indexName: string): string;
|
|
253
|
+
/**
|
|
254
|
+
* GLSL unpacking each uniform out of the fetched texels.
|
|
255
|
+
* @returns The GLSL declarations.
|
|
256
|
+
*/
|
|
257
|
+
protected getFromTexelsGLSL(): string;
|
|
258
|
+
/**
|
|
259
|
+
* The varying declarations carrying each uniform from vertex to fragment stage.
|
|
260
|
+
*
|
|
261
|
+
* No `flat` qualifier, unlike both originals — see the class docs.
|
|
262
|
+
* @returns The declaration, assignment and read snippets.
|
|
263
|
+
*/
|
|
264
|
+
protected getVarying(): {
|
|
265
|
+
declareVarying: string;
|
|
266
|
+
assignVarying: string;
|
|
267
|
+
getVarying: string;
|
|
268
|
+
};
|
|
269
|
+
/**
|
|
270
|
+
* The swizzle selecting a uniform's components out of its texel.
|
|
271
|
+
* @param offset - The uniform's element offset within an instance.
|
|
272
|
+
* @param size - How many elements it spans.
|
|
273
|
+
* @returns The component swizzle, such as `rgb`.
|
|
274
|
+
*/
|
|
275
|
+
protected getUniformComponents(offset: number, size: number): string;
|
|
276
|
+
}
|
|
277
|
+
//# sourceMappingURL=SquareDataTexture.d.ts.map
|