@drawcall/lod 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/README.md +5 -8
  2. package/dist/bake.d.ts +16 -2
  3. package/dist/bake.js +92 -4
  4. package/dist/bake.js.map +1 -1
  5. package/dist/baked/index.d.ts +2 -2
  6. package/dist/baked/index.js +2 -2
  7. package/dist/baked/interior/faces.js +4 -2
  8. package/dist/baked/interior/faces.js.map +1 -1
  9. package/dist/baked/interior/material.js +1 -1
  10. package/dist/baked/interior/material.js.map +1 -1
  11. package/dist/baked/interior/rooms.d.ts +1 -1
  12. package/dist/baked/interior/rooms.js +1 -1
  13. package/dist/dispose.d.ts +3 -3
  14. package/dist/dispose.js +6 -6
  15. package/dist/dispose.js.map +1 -1
  16. package/dist/foliage.d.ts +32 -0
  17. package/dist/foliage.js +137 -0
  18. package/dist/foliage.js.map +1 -0
  19. package/dist/impostor/bake/index.d.ts +3 -3
  20. package/dist/impostor/bake/index.js +12 -3
  21. package/dist/impostor/bake/index.js.map +1 -1
  22. package/dist/impostor/bake/source.d.ts +2 -1
  23. package/dist/impostor/bake/source.js +2 -2
  24. package/dist/impostor/bake/source.js.map +1 -1
  25. package/dist/impostor/impostor.d.ts +1 -1
  26. package/dist/impostor/impostor.js +1 -1
  27. package/dist/impostor/ktx2/encode.js +5 -2
  28. package/dist/impostor/ktx2/encode.js.map +1 -1
  29. package/dist/impostor/ktx2/uastc.js +3 -2
  30. package/dist/impostor/ktx2/uastc.js.map +1 -1
  31. package/dist/impostor/ktx2/worker.js +0 -2
  32. package/dist/impostor/ktx2/worker.js.map +1 -1
  33. package/dist/impostor/mesh/card.d.ts +1 -1
  34. package/dist/impostor/mesh/index.d.ts +2 -2
  35. package/dist/impostor/mesh/index.js +3 -3
  36. package/dist/impostor/mesh/index.js.map +1 -1
  37. package/dist/index.d.ts +3 -6
  38. package/dist/index.js +3 -6
  39. package/dist/index.js.map +1 -1
  40. package/dist/instanced/camera.d.ts +13 -25
  41. package/dist/instanced/camera.js +17 -42
  42. package/dist/instanced/camera.js.map +1 -1
  43. package/dist/instanced/clusters.d.ts +68 -0
  44. package/dist/instanced/clusters.js +172 -0
  45. package/dist/instanced/clusters.js.map +1 -0
  46. package/dist/instanced/cull.d.ts +3 -3
  47. package/dist/instanced/cull.js +2 -2
  48. package/dist/instanced/cull.js.map +1 -1
  49. package/dist/instanced/instances.d.ts +39 -0
  50. package/dist/instanced/instances.js +104 -0
  51. package/dist/instanced/instances.js.map +1 -0
  52. package/dist/instanced/key.d.ts +1 -1
  53. package/dist/instanced/key.js +1 -1
  54. package/dist/instanced/key.js.map +1 -1
  55. package/dist/instanced/levels.d.ts +9 -9
  56. package/dist/instanced/levels.js +7 -6
  57. package/dist/instanced/levels.js.map +1 -1
  58. package/dist/instanced/lists.d.ts +29 -39
  59. package/dist/instanced/lists.js +45 -62
  60. package/dist/instanced/lists.js.map +1 -1
  61. package/dist/instanced/measure.d.ts +2 -2
  62. package/dist/instanced/measure.js +2 -2
  63. package/dist/instanced/meshes.d.ts +11 -9
  64. package/dist/instanced/meshes.js +26 -16
  65. package/dist/instanced/meshes.js.map +1 -1
  66. package/dist/instanced/mirror.d.ts +16 -12
  67. package/dist/instanced/mirror.js +51 -12
  68. package/dist/instanced/mirror.js.map +1 -1
  69. package/dist/instanced/raycast.d.ts +6 -9
  70. package/dist/instanced/raycast.js +15 -18
  71. package/dist/instanced/raycast.js.map +1 -1
  72. package/dist/instanced/rows.d.ts +22 -73
  73. package/dist/instanced/rows.js +24 -189
  74. package/dist/instanced/rows.js.map +1 -1
  75. package/dist/instanced/shape.d.ts +3 -1
  76. package/dist/instanced/shape.js +6 -3
  77. package/dist/instanced/shape.js.map +1 -1
  78. package/dist/instanced/sort.d.ts +36 -34
  79. package/dist/instanced/sort.js +168 -132
  80. package/dist/instanced/sort.js.map +1 -1
  81. package/dist/instanced/update.d.ts +17 -12
  82. package/dist/instanced/update.js +64 -85
  83. package/dist/instanced/update.js.map +1 -1
  84. package/dist/levels/compile/index.d.ts +4 -2
  85. package/dist/levels/compile/index.js +10 -2
  86. package/dist/levels/compile/index.js.map +1 -1
  87. package/dist/levels/compile/pipelines.d.ts +1 -1
  88. package/dist/levels/compile/pipelines.js +1 -6
  89. package/dist/levels/compile/pipelines.js.map +1 -1
  90. package/dist/levels/draw.d.ts +1 -1
  91. package/dist/levels/draw.js +3 -3
  92. package/dist/levels/fade.d.ts +13 -22
  93. package/dist/levels/fade.js +17 -38
  94. package/dist/levels/fade.js.map +1 -1
  95. package/dist/levels/index.d.ts +7 -7
  96. package/dist/levels/index.js +1 -1
  97. package/dist/levels/item.d.ts +10 -10
  98. package/dist/levels/item.js +15 -23
  99. package/dist/levels/item.js.map +1 -1
  100. package/dist/levels/load.d.ts +1 -1
  101. package/dist/levels/load.js +5 -3
  102. package/dist/levels/load.js.map +1 -1
  103. package/dist/levels/manager.d.ts +30 -31
  104. package/dist/levels/manager.js +75 -62
  105. package/dist/levels/manager.js.map +1 -1
  106. package/dist/levels/material.d.ts +2 -2
  107. package/dist/levels/material.js +2 -2
  108. package/dist/levels/renderer.d.ts +8 -5
  109. package/dist/levels/renderer.js +20 -4
  110. package/dist/levels/renderer.js.map +1 -1
  111. package/dist/levels/requests.d.ts +4 -1
  112. package/dist/levels/requests.js +3 -1
  113. package/dist/levels/requests.js.map +1 -1
  114. package/dist/loader/context.d.ts +9 -11
  115. package/dist/loader/context.js +12 -7
  116. package/dist/loader/context.js.map +1 -1
  117. package/dist/loader/entry.d.ts +4 -0
  118. package/dist/loader/entry.js +43 -0
  119. package/dist/loader/entry.js.map +1 -0
  120. package/dist/loader/index.d.ts +49 -35
  121. package/dist/loader/index.js +123 -135
  122. package/dist/loader/index.js.map +1 -1
  123. package/dist/loader/level.js +5 -5
  124. package/dist/loader/level.js.map +1 -1
  125. package/dist/loader/node.d.ts +26 -0
  126. package/dist/loader/node.js +199 -0
  127. package/dist/loader/node.js.map +1 -0
  128. package/dist/loader/request.d.ts +11 -44
  129. package/dist/loader/request.js +7 -47
  130. package/dist/loader/request.js.map +1 -1
  131. package/dist/loader/url.d.ts +10 -9
  132. package/dist/loader/url.js +21 -17
  133. package/dist/loader/url.js.map +1 -1
  134. package/dist/lod.d.ts +122 -62
  135. package/dist/lod.js +332 -187
  136. package/dist/lod.js.map +1 -1
  137. package/dist/meshopt.d.ts +1 -1
  138. package/dist/meshopt.js +3 -2
  139. package/dist/meshopt.js.map +1 -1
  140. package/dist/node-process.d.ts +5 -0
  141. package/dist/node-process.js +6 -0
  142. package/dist/node-process.js.map +1 -0
  143. package/dist/node.d.ts +2 -0
  144. package/dist/node.js +5 -0
  145. package/dist/node.js.map +1 -0
  146. package/dist/simplify/index.d.ts +2 -2
  147. package/dist/simplify/index.js +7 -7
  148. package/dist/simplify/index.js.map +1 -1
  149. package/dist/source/detail.d.ts +7 -2
  150. package/dist/source/detail.js +26 -22
  151. package/dist/source/detail.js.map +1 -1
  152. package/dist/source/sampler.js +5 -2
  153. package/dist/source/sampler.js.map +1 -1
  154. package/dist/three.d.ts +8 -7
  155. package/dist/three.js +5 -4
  156. package/dist/three.js.map +1 -1
  157. package/dist/wind/bounds.js +1 -1
  158. package/dist/wind/bounds.js.map +1 -1
  159. package/dist/wind/hooks.d.ts +1 -1
  160. package/dist/wind/hooks.js +1 -1
  161. package/dist/wind/index.d.ts +3 -3
  162. package/dist/wind/index.js +11 -9
  163. package/dist/wind/index.js.map +1 -1
  164. package/package.json +10 -6
  165. package/skills/lod/SKILL.md +65 -54
  166. package/dist/gpu.d.ts +0 -39
  167. package/dist/gpu.js +0 -41
  168. package/dist/gpu.js.map +0 -1
  169. package/dist/instanced/chunk.d.ts +0 -97
  170. package/dist/instanced/chunk.js +0 -156
  171. package/dist/instanced/chunk.js.map +0 -1
  172. package/dist/instanced/chunks.d.ts +0 -60
  173. package/dist/instanced/chunks.js +0 -208
  174. package/dist/instanced/chunks.js.map +0 -1
  175. package/dist/instanced/index.d.ts +0 -120
  176. package/dist/instanced/index.js +0 -290
  177. package/dist/instanced/index.js.map +0 -1
  178. package/dist/instanced/pages/buffers.d.ts +0 -37
  179. package/dist/instanced/pages/buffers.js +0 -15
  180. package/dist/instanced/pages/buffers.js.map +0 -1
  181. package/dist/instanced/pages/chunk.d.ts +0 -51
  182. package/dist/instanced/pages/chunk.js +0 -81
  183. package/dist/instanced/pages/chunk.js.map +0 -1
  184. package/dist/instanced/pages/index.d.ts +0 -74
  185. package/dist/instanced/pages/index.js +0 -269
  186. package/dist/instanced/pages/index.js.map +0 -1
  187. package/dist/instanced/pages/passes.d.ts +0 -21
  188. package/dist/instanced/pages/passes.js +0 -52
  189. package/dist/instanced/pages/passes.js.map +0 -1
  190. package/dist/instanced/pages/slots.d.ts +0 -11
  191. package/dist/instanced/pages/slots.js +0 -90
  192. package/dist/instanced/pages/slots.js.map +0 -1
  193. package/dist/instanced/pages/workgroup.d.ts +0 -14
  194. package/dist/instanced/pages/workgroup.js +0 -38
  195. package/dist/instanced/pages/workgroup.js.map +0 -1
  196. package/dist/levels/wait.d.ts +0 -27
  197. package/dist/levels/wait.js +0 -61
  198. package/dist/levels/wait.js.map +0 -1
  199. package/dist/loader/cache.d.ts +0 -12
  200. package/dist/loader/cache.js +0 -68
  201. package/dist/loader/cache.js.map +0 -1
  202. package/dist/loader/dependencies.d.ts +0 -32
  203. package/dist/loader/dependencies.js +0 -113
  204. package/dist/loader/dependencies.js.map +0 -1
@@ -1,97 +0,0 @@
1
- import { Box3, type Camera, type Color, type InstancedBufferAttribute, Matrix4, type StorageBufferAttribute, type StorageInstancedBufferAttribute } from "three/webgpu";
2
- /** The instances of a chunk to add (see `InstancedLod.addChunk`), as an InstancedMesh holds its own. */
3
- export interface InstancedLodChunkParameters {
4
- /**
5
- * 16 floats per instance, as InstancedMesh.instanceMatrix holds them. From the CPU, an InstancedBufferAttribute that
6
- * stays live as an InstancedMesh's does: edit it (see `setMatrixAt`) and set `needsUpdate` (after `addUpdateRange`
7
- * to upload part of it), and the next render takes the change in. The InstancedLod takes and clears its update
8
- * ranges, so an InstancedMesh also drawing it uploads it whole. From the GPU, a StorageInstancedBufferAttribute an
9
- * app's pass writes (such as `instancedArray(n, "mat4")`), read once, in the first render that updates the
10
- * InstancedLod (see `InstancedLod.update`): the app may reuse it after that render. A matrix of scale 0 is no
11
- * instance.
12
- */
13
- instanceMatrix: InstancedBufferAttribute | StorageInstancedBufferAttribute;
14
- /**
15
- * RGB floats per instance, as InstancedMesh.instanceColor holds them, of the same kind as `instanceMatrix`, live or
16
- * read once as it is, its update ranges taken alike; white without.
17
- */
18
- instanceColor?: InstancedBufferAttribute | StorageInstancedBufferAttribute;
19
- /** The first instance of the attributes to read, and how many: the whole attributes by default, as a drawRange. */
20
- start?: number;
21
- count?: number;
22
- /**
23
- * From the GPU only: a u32 storage attribute (such as `attributeArray(n, "uint")`) whose item `instanceCountIndex`
24
- * the app's pass wrote, at most `count`, as drawIndirect's instanceCount: only that many are read. Without it all
25
- * `count` are.
26
- */
27
- instanceCount?: StorageBufferAttribute | StorageInstancedBufferAttribute;
28
- instanceCountIndex?: number;
29
- /**
30
- * From the GPU, required, as the CPU cannot measure the GPU's instances; from the CPU, measured, so not given: a box
31
- * holding every instance's origin (its matrix's translation) in the InstancedLod's space, and bounds on how much
32
- * the matrices scale: none shorter than `minScale` along its longest axis, none stretching any direction more than
33
- * `maxScale`. Which levels load is judged by them.
34
- */
35
- originBox?: Box3;
36
- minScale?: number;
37
- maxScale?: number;
38
- }
39
- /**
40
- * What an InstancedLod keeps of one of its chunks, which the chunk shows: its bounds, re-measured as its CPU attributes
41
- * change, and how it finds the level an instance draws, while the InstancedLod holds it.
42
- */
43
- export interface ChunkState {
44
- /** Whether its attributes are the GPU's, told once: binding a CPU attribute through storage() flags it as storage. */
45
- readonly gpu: boolean;
46
- readonly originBox: Box3;
47
- minScale: number;
48
- maxScale: number;
49
- readonly boundingBox: Box3;
50
- levelAt: ((index: number, camera: Camera) => number | null) | null;
51
- }
52
- /**
53
- * Instances added to an InstancedLod in one call and removed whole (see `InstancedLod.addChunk`). An instance's index
54
- * is its index in the chunk's range of its attributes, stable while other chunks come and go. The methods reading or
55
- * writing instances work for a chunk from the CPU only: a GPU chunk's instances live on the GPU, and its methods throw.
56
- */
57
- export declare class InstancedLodChunk {
58
- private readonly state;
59
- readonly instanceMatrix: InstancedBufferAttribute | StorageInstancedBufferAttribute;
60
- readonly instanceColor: InstancedBufferAttribute | StorageInstancedBufferAttribute | null;
61
- readonly start: number;
62
- readonly count: number;
63
- readonly instanceCount: StorageBufferAttribute | StorageInstancedBufferAttribute | null;
64
- readonly instanceCountIndex: number;
65
- /** Made by `InstancedLod.addChunk`. */
66
- constructor(parameters: InstancedLodChunkParameters, state: ChunkState);
67
- /** Holds every instance's origin, in the InstancedLod's space: given for a GPU chunk, measured for a CPU one. */
68
- get originBox(): Box3;
69
- /** No matrix scales its longest axis less: given for a GPU chunk, measured for a CPU one. */
70
- get minScale(): number;
71
- /** No matrix stretches any direction more: given for a GPU chunk, measured for a CPU one. */
72
- get maxScale(): number;
73
- /**
74
- * Bounds every instance's every level, swaying included, in the InstancedLod's space: the box of its origins grown
75
- * by the largest level.
76
- */
77
- get boundingBox(): Box3;
78
- /** Write instance `index`'s matrix into `instanceMatrix`; set its `needsUpdate` to draw it. */
79
- setMatrixAt(index: number, value: Matrix4): void;
80
- getMatrixAt(index: number, target: Matrix4): Matrix4;
81
- /** Write instance `index`'s colour into `instanceColor`; set its `needsUpdate` to draw it. */
82
- setColorAt(index: number, color: Color): void;
83
- /** Instance `index`'s colour: white without `instanceColor`. */
84
- getColorAt(index: number, color: Color): Color;
85
- /**
86
- * The level that draws most of instance `index`'s pixels for `camera` from where it is now, with the levels loaded
87
- * now and no arrival under way, as Lod.getLevel tells; null at scale 0, thinned out by `cullDistance`, or while no
88
- * level it could draw is loaded. From the attributes as they are, edits not yet drawn included. It throws for every
89
- * instance while `cullDistance` or the thresholds are invalid.
90
- */
91
- getLevelAt(index: number, camera: Camera): number | null;
92
- /** This chunk, whose instances the CPU holds, after checking `index` is one of them. */
93
- private cpu;
94
- private colors;
95
- }
96
- /** Measure into `state` the origins and scales of `chunk`'s instances at a scale above 0, from its CPU attributes. */
97
- export declare function measureChunk(chunk: InstancedLodChunk, state: ChunkState): void;
@@ -1,156 +0,0 @@
1
- import { Box3, Matrix4, Vector3, } from "three/webgpu";
2
- import { isStorageInstanced, stretch } from "../three.js";
3
- const matrix = new Matrix4();
4
- const origin = new Vector3();
5
- /**
6
- * Instances added to an InstancedLod in one call and removed whole (see `InstancedLod.addChunk`). An instance's index
7
- * is its index in the chunk's range of its attributes, stable while other chunks come and go. The methods reading or
8
- * writing instances work for a chunk from the CPU only: a GPU chunk's instances live on the GPU, and its methods throw.
9
- */
10
- export class InstancedLodChunk {
11
- state;
12
- instanceMatrix;
13
- instanceColor;
14
- start;
15
- count;
16
- instanceCount;
17
- instanceCountIndex;
18
- /** Made by `InstancedLod.addChunk`. */
19
- constructor(parameters, state) {
20
- this.state = state;
21
- const { instanceMatrix, instanceColor = null, instanceCount = null, instanceCountIndex = 0 } = parameters;
22
- const { gpu } = state;
23
- if (isStorageInstanced(instanceMatrix) !== gpu)
24
- throw new Error("InstancedLod: a chunk's state is of another kind than its instanceMatrix");
25
- const { start = 0, count = instanceMatrix.count - start } = parameters;
26
- if (instanceMatrix.itemSize !== 16)
27
- throw new TypeError(`InstancedLod: a chunk's instanceMatrix holds 16 floats per instance, not ${instanceMatrix.itemSize}`);
28
- if (!(instanceMatrix.array instanceof Float32Array) || instanceMatrix.normalized)
29
- throw new TypeError("InstancedLod: a chunk's instanceMatrix holds floats, not normalized");
30
- if (!Number.isInteger(start) || !Number.isInteger(count) || start < 0 || count < 0 || start + count > instanceMatrix.count)
31
- throw new RangeError(`InstancedLod: a chunk's range of ${count} from ${start} passes the ${instanceMatrix.count} instances of its instanceMatrix`);
32
- if (instanceColor) {
33
- if (isStorageInstanced(instanceColor) !== gpu)
34
- throw new TypeError("InstancedLod: a chunk's instanceColor is of another kind than its instanceMatrix");
35
- // A storage attribute's vec3 is padded to four floats once on the GPU.
36
- if (instanceColor.itemSize !== 3 && !(gpu && instanceColor.itemSize === 4))
37
- throw new TypeError(`InstancedLod: a chunk's instanceColor holds RGB per instance, not ${instanceColor.itemSize} floats`);
38
- if (!(instanceColor.array instanceof Float32Array) || instanceColor.normalized)
39
- throw new TypeError("InstancedLod: a chunk's instanceColor holds floats, not normalized");
40
- if (instanceColor.count < start + count)
41
- throw new RangeError("InstancedLod: a chunk's instanceColor holds fewer instances than its range");
42
- }
43
- if (instanceCount) {
44
- if (!gpu)
45
- throw new TypeError("InstancedLod: only a GPU chunk takes an instanceCount");
46
- if (!(instanceCount.array instanceof Uint32Array) || instanceCount.itemSize !== 1)
47
- throw new TypeError("InstancedLod: a chunk's instanceCount holds u32 items");
48
- if (!Number.isInteger(instanceCountIndex) || instanceCountIndex < 0 || instanceCountIndex >= instanceCount.count)
49
- throw new RangeError(`InstancedLod: a chunk's instanceCountIndex ${instanceCountIndex} passes the end of its instanceCount`);
50
- }
51
- const { originBox, minScale, maxScale } = parameters;
52
- const given = originBox !== undefined || minScale !== undefined || maxScale !== undefined;
53
- if (gpu && (!originBox || minScale === undefined || maxScale === undefined))
54
- throw new TypeError("InstancedLod: a GPU chunk needs its originBox, minScale and maxScale: the CPU cannot measure the GPU's instances");
55
- if (!gpu && given)
56
- throw new TypeError("InstancedLod: a CPU chunk's originBox, minScale and maxScale are measured from its instances, not given");
57
- if (originBox && minScale !== undefined && maxScale !== undefined) {
58
- if (!(minScale > 0 && minScale <= maxScale && Number.isFinite(maxScale)))
59
- throw new RangeError(`InstancedLod: a chunk's scales must be 0 < minScale <= maxScale, got ${minScale}, ${maxScale}`);
60
- if (originBox.isEmpty() || ![...originBox.min.toArray(), ...originBox.max.toArray()].every(Number.isFinite))
61
- throw new RangeError("InstancedLod: a chunk's originBox must be finite and not empty");
62
- state.originBox.copy(originBox);
63
- state.minScale = minScale;
64
- state.maxScale = maxScale;
65
- }
66
- this.instanceMatrix = instanceMatrix;
67
- this.instanceColor = instanceColor;
68
- this.start = start;
69
- this.count = count;
70
- this.instanceCount = instanceCount;
71
- this.instanceCountIndex = instanceCountIndex;
72
- if (!gpu)
73
- measureChunk(this, state);
74
- }
75
- /** Holds every instance's origin, in the InstancedLod's space: given for a GPU chunk, measured for a CPU one. */
76
- get originBox() {
77
- return this.state.originBox;
78
- }
79
- /** No matrix scales its longest axis less: given for a GPU chunk, measured for a CPU one. */
80
- get minScale() {
81
- return this.state.minScale;
82
- }
83
- /** No matrix stretches any direction more: given for a GPU chunk, measured for a CPU one. */
84
- get maxScale() {
85
- return this.state.maxScale;
86
- }
87
- /**
88
- * Bounds every instance's every level, swaying included, in the InstancedLod's space: the box of its origins grown
89
- * by the largest level.
90
- */
91
- get boundingBox() {
92
- return this.state.boundingBox;
93
- }
94
- /** Write instance `index`'s matrix into `instanceMatrix`; set its `needsUpdate` to draw it. */
95
- setMatrixAt(index, value) {
96
- value.toArray(this.cpu(index).instanceMatrix.array, (this.start + index) * 16);
97
- }
98
- getMatrixAt(index, target) {
99
- return target.fromArray(this.cpu(index).instanceMatrix.array, (this.start + index) * 16);
100
- }
101
- /** Write instance `index`'s colour into `instanceColor`; set its `needsUpdate` to draw it. */
102
- setColorAt(index, color) {
103
- color.toArray(this.colors(index).array, (this.start + index) * 3);
104
- }
105
- /** Instance `index`'s colour: white without `instanceColor`. */
106
- getColorAt(index, color) {
107
- const { instanceColor } = this.cpu(index);
108
- return instanceColor ? color.fromArray(instanceColor.array, (this.start + index) * 3) : color.setRGB(1, 1, 1);
109
- }
110
- /**
111
- * The level that draws most of instance `index`'s pixels for `camera` from where it is now, with the levels loaded
112
- * now and no arrival under way, as Lod.getLevel tells; null at scale 0, thinned out by `cullDistance`, or while no
113
- * level it could draw is loaded. From the attributes as they are, edits not yet drawn included. It throws for every
114
- * instance while `cullDistance` or the thresholds are invalid.
115
- */
116
- getLevelAt(index, camera) {
117
- const { levelAt } = this.state;
118
- if (!levelAt)
119
- throw new Error("InstancedLod: the chunk was removed from its InstancedLod");
120
- this.cpu(index);
121
- return levelAt(index, camera);
122
- }
123
- /** This chunk, whose instances the CPU holds, after checking `index` is one of them. */
124
- cpu(index) {
125
- if (this.state.gpu)
126
- throw new Error("InstancedLod: a GPU chunk's instances live on the GPU: the CPU cannot read or write them");
127
- if (!Number.isInteger(index) || index < 0 || index >= this.count)
128
- throw new RangeError(`InstancedLod: ${index} is not an instance of the chunk's ${this.count}`);
129
- return this;
130
- }
131
- colors(index) {
132
- const { instanceColor } = this.cpu(index);
133
- if (!instanceColor)
134
- throw new Error("InstancedLod: give the chunk an instanceColor to colour its instances");
135
- return instanceColor;
136
- }
137
- }
138
- /** Measure into `state` the origins and scales of `chunk`'s instances at a scale above 0, from its CPU attributes. */
139
- export function measureChunk(chunk, state) {
140
- const { array } = chunk.instanceMatrix;
141
- state.originBox.makeEmpty();
142
- state.minScale = Infinity;
143
- state.maxScale = 0;
144
- for (let i = chunk.start; i < chunk.start + chunk.count; i++) {
145
- matrix.fromArray(array, i * 16);
146
- const scale = matrix.getMaxScaleOnAxis();
147
- if (scale === 0)
148
- continue;
149
- state.originBox.expandByPoint(origin.setFromMatrixPosition(matrix));
150
- state.minScale = Math.min(state.minScale, scale);
151
- state.maxScale = Math.max(state.maxScale, stretch(matrix));
152
- }
153
- if (state.originBox.isEmpty())
154
- state.minScale = state.maxScale = 0;
155
- }
156
- //# sourceMappingURL=chunk.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"chunk.js","sourceRoot":"","sources":["../../src/instanced/chunk.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EAIJ,OAAO,EAGP,OAAO,GACR,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAwD1D,MAAM,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;AAC7B,MAAM,MAAM,GAAG,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IAC5B,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC1B,KAAK,CAAC,QAAQ,GAAG,CAAC,CAAC;IACnB,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7D,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;QAChC,MAAM,KAAK,GAAG,MAAM,CAAC,iBAAiB,EAAE,CAAC;QACzC,IAAI,KAAK,KAAK,CAAC;YAAE,SAAS;QAC1B,KAAK,CAAC,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC,CAAC;QACpE,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QACjD,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7D,CAAC;IACD,IAAI,KAAK,CAAC,SAAS,CAAC,OAAO,EAAE;QAAE,KAAK,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,GAAG,CAAC,CAAC;AACrE,CAAC"}
@@ -1,60 +0,0 @@
1
- import { Box3, type Camera, type Sphere, type Vector3 } from "three/webgpu";
2
- import { InstancedLodChunk, type InstancedLodChunkParameters } from "./chunk.js";
3
- import type { Cull } from "./cull.js";
4
- import type { InstancePagePool } from "./pages/index.js";
5
- /**
6
- * The chunks of one InstancedLod: their pool slots, the mirrors the pool reads CPU attributes through, and their
7
- * bounds, which load levels and cull the level meshes.
8
- */
9
- export declare class ChunkSet {
10
- private readonly pool;
11
- private readonly row;
12
- private readonly bounds;
13
- private readonly reach;
14
- private readonly levelAt;
15
- private readonly held;
16
- private readonly mirrors;
17
- /** Bumped whenever a chunk was added, removed or re-measured: what `heights` reads changed. */
18
- version: number;
19
- /**
20
- * Chunks over `pool`, for the InstancedLod sorting as row `row`, whose instances `bounds` bounds in the InstancedLod's space; `reach` tells how far from an
21
- * instance's origin, in its space, every level and its sway reach, and `levelAt` is what a chunk's getLevelAt asks.
22
- */
23
- constructor(pool: InstancePagePool, row: number, bounds: {
24
- readonly sphere: Sphere;
25
- readonly box: Box3;
26
- }, reach: () => number, levelAt: (chunk: InstancedLodChunk, index: number, camera: Camera) => number | null);
27
- get size(): number;
28
- /** The chunks, in the order they were added. */
29
- keys(): IterableIterator<InstancedLodChunk>;
30
- /** The pool slots of the chunks. */
31
- slots(): number[];
32
- /** The chunks whose instances the CPU holds. */
33
- cpu(): InstancedLodChunk[];
34
- /**
35
- * What makes chunks of their own with every chunk's instances, their attributes copied (see `InstancedLod.copy`). A
36
- * GPU chunk cannot be copied: its instances live on the GPU.
37
- */
38
- copies(): InstancedLodChunkParameters[];
39
- /** Hold `parameters`' instances as a chunk, copied into the pool at its next run; `bound` bounds it. */
40
- add(parameters: InstancedLodChunkParameters): InstancedLodChunk;
41
- /** Let go of `chunk`; its pages return to the pool at its next run. */
42
- remove(chunk: InstancedLodChunk): void;
43
- /**
44
- * Take in the CPU attributes' edits, those whose versions moved, for every chunk reading them: re-measured, and
45
- * copied into the pool again at its next run. Whether any changed; `bound` bounds them.
46
- */
47
- refresh(): boolean;
48
- /** Bound every chunk, its box (see `InstancedLodChunk.boundingBox`), and all of them, survivors grown by up to `grow`. */
49
- bound(grow: number): void;
50
- /**
51
- * The heights on a screen of `scale` from `eye`, both in the InstancedLod's space, that each chunk's instances can
52
- * have, `source` an instance's source sphere: its shortest instance at its farthest, its tallest at its nearest.
53
- */
54
- heights(source: Sphere, eye: Vector3, scale: number, ortho: boolean, cull: Cull | null): Generator<[number, number]>;
55
- /** Free the chunks' slots and release the mirrors; the chunks no longer find their levels. */
56
- dispose(): void;
57
- /** The mirror of `attribute`, which `chunk` reads too. */
58
- private mirror;
59
- private heldOf;
60
- }
@@ -1,208 +0,0 @@
1
- import { Box3, InstancedBufferAttribute } from "three/webgpu";
2
- import { isStorageInstanced } from "../three.js";
3
- import { InstancedLodChunk, measureChunk } from "./chunk.js";
4
- import { Mirror } from "./mirror.js";
5
- import { MAX_SURVIVOR_SCALE } from "./measure.js";
6
- /** `attribute`, a GPU chunk's, as the storage attribute InstancedLodChunk checked it is. */
7
- function onGpu(attribute) {
8
- if (!isStorageInstanced(attribute))
9
- throw new TypeError("InstancedLod: a GPU chunk's attribute is not a storage attribute");
10
- return attribute;
11
- }
12
- const grown = new Box3();
13
- /**
14
- * The chunks of one InstancedLod: their pool slots, the mirrors the pool reads CPU attributes through, and their
15
- * bounds, which load levels and cull the level meshes.
16
- */
17
- export class ChunkSet {
18
- pool;
19
- row;
20
- bounds;
21
- reach;
22
- levelAt;
23
- held = new Map();
24
- mirrors = new Map();
25
- /** Bumped whenever a chunk was added, removed or re-measured: what `heights` reads changed. */
26
- version = 0;
27
- /**
28
- * Chunks over `pool`, for the InstancedLod sorting as row `row`, whose instances `bounds` bounds in the InstancedLod's space; `reach` tells how far from an
29
- * instance's origin, in its space, every level and its sway reach, and `levelAt` is what a chunk's getLevelAt asks.
30
- */
31
- constructor(pool, row, bounds, reach, levelAt) {
32
- this.pool = pool;
33
- this.row = row;
34
- this.bounds = bounds;
35
- this.reach = reach;
36
- this.levelAt = levelAt;
37
- }
38
- get size() {
39
- return this.held.size;
40
- }
41
- /** The chunks, in the order they were added. */
42
- keys() {
43
- return this.held.keys();
44
- }
45
- /** The pool slots of the chunks. */
46
- slots() {
47
- return [...this.held.values()].map(({ slot }) => slot);
48
- }
49
- /** The chunks whose instances the CPU holds. */
50
- cpu() {
51
- return [...this.held].flatMap(([chunk, { state }]) => (state.gpu ? [] : [chunk]));
52
- }
53
- /**
54
- * What makes chunks of their own with every chunk's instances, their attributes copied (see `InstancedLod.copy`). A
55
- * GPU chunk cannot be copied: its instances live on the GPU.
56
- */
57
- copies() {
58
- const cpu = this.cpu();
59
- if (cpu.length !== this.size)
60
- throw new Error("InstancedLod.copy: a GPU chunk's instances live on the GPU and cannot be copied");
61
- return cpu.map(({ instanceMatrix, instanceColor, start, count }) => ({
62
- instanceMatrix: copyOf(instanceMatrix),
63
- instanceColor: instanceColor ? copyOf(instanceColor) : undefined,
64
- start,
65
- count,
66
- }));
67
- }
68
- /** Hold `parameters`' instances as a chunk, copied into the pool at its next run; `bound` bounds it. */
69
- add(parameters) {
70
- const gpu = isStorageInstanced(parameters.instanceMatrix);
71
- const state = { gpu, originBox: new Box3(), minScale: 0, maxScale: 0, boundingBox: new Box3(), levelAt: null };
72
- const chunk = new InstancedLodChunk(parameters, state);
73
- const { instanceMatrix, instanceColor, instanceCount } = chunk;
74
- const attributes = gpu
75
- ? // A storage attribute's RGB is padded to four floats.
76
- { matrices: onGpu(instanceMatrix), colors: instanceColor && { attribute: onGpu(instanceColor), stride: 4 }, counts: instanceCount }
77
- : {
78
- matrices: this.mirror(instanceMatrix, chunk).attribute,
79
- colors: instanceColor && { attribute: this.mirror(instanceColor, chunk).attribute, stride: 3 },
80
- counts: null,
81
- };
82
- const slot = this.pool.add({ attributes, start: chunk.start, count: chunk.count, countIndex: chunk.instanceCountIndex, row: this.row });
83
- state.levelAt = (index, camera) => this.levelAt(chunk, index, camera);
84
- this.held.set(chunk, { slot, state });
85
- this.version++;
86
- return chunk;
87
- }
88
- /** Let go of `chunk`; its pages return to the pool at its next run. */
89
- remove(chunk) {
90
- const held = this.held.get(chunk);
91
- if (!held)
92
- throw new Error("InstancedLod: the chunk is not one of its chunks");
93
- this.pool.remove(held.slot);
94
- held.state.levelAt = null;
95
- this.held.delete(chunk);
96
- this.version++;
97
- for (const attribute of [chunk.instanceMatrix, chunk.instanceColor]) {
98
- const shared = attribute && this.mirrors.get(attribute);
99
- if (!shared)
100
- continue;
101
- shared.users.delete(chunk);
102
- if (shared.users.size > 0)
103
- continue;
104
- shared.mirror.dispose();
105
- this.mirrors.delete(shared.mirror.source);
106
- }
107
- }
108
- /**
109
- * Take in the CPU attributes' edits, those whose versions moved, for every chunk reading them: re-measured, and
110
- * copied into the pool again at its next run. Whether any changed; `bound` bounds them.
111
- */
112
- refresh() {
113
- const changed = new Set();
114
- for (const { mirror, users } of this.mirrors.values()) {
115
- if (!mirror.refresh())
116
- continue;
117
- for (const chunk of users) {
118
- if (chunk.instanceMatrix === mirror.source)
119
- measureChunk(chunk, this.heldOf(chunk).state);
120
- changed.add(chunk);
121
- }
122
- }
123
- for (const chunk of changed)
124
- this.pool.change(this.heldOf(chunk).slot);
125
- if (changed.size > 0)
126
- this.version++;
127
- return changed.size > 0;
128
- }
129
- /** Bound every chunk, its box (see `InstancedLodChunk.boundingBox`), and all of them, survivors grown by up to `grow`. */
130
- bound(grow) {
131
- const { sphere, box } = this.bounds;
132
- const reach = this.reach();
133
- box.makeEmpty();
134
- for (const { state } of this.held.values()) {
135
- if (state.originBox.isEmpty()) {
136
- state.boundingBox.makeEmpty();
137
- continue;
138
- }
139
- state.boundingBox.copy(state.originBox).expandByScalar(reach * state.maxScale);
140
- box.union(grown.copy(state.originBox).expandByScalar(reach * state.maxScale * grow));
141
- }
142
- if (box.isEmpty())
143
- sphere.makeEmpty();
144
- else
145
- box.getBoundingSphere(sphere);
146
- }
147
- /**
148
- * The heights on a screen of `scale` from `eye`, both in the InstancedLod's space, that each chunk's instances can
149
- * have, `source` an instance's source sphere: its shortest instance at its farthest, its tallest at its nearest.
150
- */
151
- *heights(source, eye, scale, ortho, cull) {
152
- const grow = cull?.survivors ? MAX_SURVIVOR_SCALE : 1;
153
- const diameter = 2 * source.radius * scale;
154
- for (const { state } of this.held.values()) {
155
- const { originBox, minScale, maxScale } = state;
156
- if (originBox.isEmpty() || maxScale === 0)
157
- continue;
158
- if (cull && originBox.distanceToPoint(cull.eye) >= cull.far)
159
- continue;
160
- const shortest = diameter * minScale;
161
- const tallest = diameter * maxScale * grow;
162
- if (ortho) {
163
- yield [shortest, tallest];
164
- continue;
165
- }
166
- // An instance's centre lies this far from its origin.
167
- const offset = source.center.length() * maxScale * grow;
168
- const near = Math.max(originBox.distanceToPoint(eye) - offset, 1e-6);
169
- const far = farthest(originBox, eye) + offset;
170
- yield [shortest / far, tallest / near];
171
- }
172
- }
173
- /** Free the chunks' slots and release the mirrors; the chunks no longer find their levels. */
174
- dispose() {
175
- for (const { slot, state } of this.held.values()) {
176
- this.pool.remove(slot);
177
- state.levelAt = null;
178
- }
179
- this.held.clear();
180
- this.version++;
181
- for (const { mirror } of this.mirrors.values())
182
- mirror.dispose();
183
- this.mirrors.clear();
184
- }
185
- /** The mirror of `attribute`, which `chunk` reads too. */
186
- mirror(attribute, chunk) {
187
- const shared = this.mirrors.get(attribute) ?? { mirror: new Mirror(attribute), users: new Set() };
188
- this.mirrors.set(attribute, shared);
189
- shared.users.add(chunk);
190
- return shared.mirror;
191
- }
192
- heldOf(chunk) {
193
- const held = this.held.get(chunk);
194
- if (!held)
195
- throw new Error("InstancedLod: a mirror's chunk is not one of its chunks");
196
- return held;
197
- }
198
- }
199
- /** The distance from `point` to the farthest point of `box`. */
200
- function farthest(box, point) {
201
- const { min, max } = box;
202
- return Math.hypot(Math.max(Math.abs(point.x - min.x), Math.abs(point.x - max.x)), Math.max(Math.abs(point.y - min.y), Math.abs(point.y - max.y)), Math.max(Math.abs(point.z - min.z), Math.abs(point.z - max.z)));
203
- }
204
- /** A CPU attribute of its own with `attribute`'s instances. */
205
- function copyOf(attribute) {
206
- return new InstancedBufferAttribute(attribute.array.slice(), attribute.itemSize);
207
- }
208
- //# sourceMappingURL=chunks.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"chunks.js","sourceRoot":"","sources":["../../src/instanced/chunks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,wBAAwB,EAAgF,MAAM,cAAc,CAAC;AAC5I,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAA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