@needle-tools/three-mesh-effects 0.3.7 → 0.4.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 (55) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +14 -1
  3. package/build/box-impostor-runtime.js +321 -195
  4. package/build/box-impostor-runtime.js.map +4 -4
  5. package/build/impostor-runtime.js +944 -202
  6. package/build/impostor-runtime.js.map +4 -4
  7. package/build/index.js +7 -7
  8. package/build/index.js.map +2 -2
  9. package/build/internal/box-impostor/box-impostor-display.js +319 -193
  10. package/build/internal/box-impostor/box-impostor-display.js.map +4 -4
  11. package/build/internal/box-impostor/box-impostor-faces.js +8 -6
  12. package/build/internal/box-impostor/box-impostor-faces.js.map +2 -2
  13. package/build/internal/box-impostor/box-impostor-material.js +319 -193
  14. package/build/internal/box-impostor/box-impostor-material.js.map +4 -4
  15. package/build/internal/box-impostor/box-impostor-projection.js +8 -5
  16. package/build/internal/box-impostor/box-impostor-projection.js.map +2 -2
  17. package/build/internal/box-impostor/box-impostor-runtime.js +321 -195
  18. package/build/internal/box-impostor/box-impostor-runtime.js.map +4 -4
  19. package/build/internal/impostor/adaptive-impostor-renderer.js +434 -0
  20. package/build/internal/impostor/adaptive-impostor-renderer.js.map +7 -0
  21. package/build/internal/impostor/impostor-node-material.js +944 -202
  22. package/build/internal/impostor/impostor-node-material.js.map +4 -4
  23. package/build/internal/impostor/impostor-performance.js +136 -0
  24. package/build/internal/impostor/impostor-performance.js.map +7 -0
  25. package/build/internal/pixel-impostor/pixel-impostor-runtime.js +6 -1
  26. package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +2 -2
  27. package/build/internal/rendering/instance-space.js +110 -0
  28. package/build/internal/rendering/instance-space.js.map +7 -0
  29. package/build/internal/rendering/renderer-support.js +6 -1
  30. package/build/internal/rendering/renderer-support.js.map +2 -2
  31. package/build/pixel-impostor-runtime.js +6 -1
  32. package/build/pixel-impostor-runtime.js.map +2 -2
  33. package/build/types/box-impostor/box-impostor-faces.d.ts +3 -1
  34. package/build/types/box-impostor/box-impostor-projection.d.ts +3 -1
  35. package/build/types/impostor/adaptive-impostor-renderer.d.ts +74 -0
  36. package/build/types/impostor/adaptive-impostor-renderer.js +1 -0
  37. package/build/types/impostor/impostor-node-material.d.ts +5 -1
  38. package/build/types/impostor/impostor-performance.d.ts +90 -0
  39. package/build/types/impostor/impostor-performance.js +1 -0
  40. package/build/types/public/impostor-runtime.d.ts +2 -0
  41. package/build/types/public/index.d.ts +3 -0
  42. package/build/types/rendering/instance-space.d.ts +26 -0
  43. package/build/types/rendering/instance-space.js +1 -0
  44. package/build/types/rendering/renderer-support.d.ts +13 -0
  45. package/package.json +2 -2
  46. package/src/box-impostor/box-impostor-faces.js +7 -5
  47. package/src/box-impostor/box-impostor-material.js +34 -19
  48. package/src/box-impostor/box-impostor-projection.js +7 -4
  49. package/src/impostor/adaptive-impostor-renderer.js +423 -0
  50. package/src/impostor/impostor-node-material.js +347 -157
  51. package/src/impostor/impostor-performance.js +152 -0
  52. package/src/public/impostor-runtime.js +2 -0
  53. package/src/public/index.js +12 -9
  54. package/src/rendering/instance-space.js +121 -0
  55. package/src/rendering/renderer-support.js +9 -0
@@ -32,9 +32,9 @@ function capturedSurfaceDepth(position, shadowBias = 0) {
32
32
 
33
33
  // src/box-impostor/box-impostor-material.js
34
34
  import {
35
- DataTexture as DataTexture3,
36
- FloatType as FloatType3,
37
- RGBAFormat as RGBAFormat3,
35
+ DataTexture as DataTexture4,
36
+ FloatType as FloatType4,
37
+ RGBAFormat as RGBAFormat4,
38
38
  RedIntegerFormat,
39
39
  UnsignedIntType,
40
40
  NearestFilter as NearestFilter3,
@@ -49,27 +49,27 @@ import {
49
49
  Loop as Loop2,
50
50
  Break,
51
51
  attribute as attribute3,
52
- varying,
53
- positionLocal as positionLocal2,
52
+ varying as varying2,
53
+ positionGeometry as positionGeometry2,
54
54
  positionViewDirection,
55
55
  modelWorldMatrixInverse,
56
56
  cameraPosition,
57
57
  cameraWorldMatrix,
58
58
  cameraProjectionMatrix as cameraProjectionMatrix2,
59
59
  vec2 as vec24,
60
- vec3 as vec34,
61
- vec4 as vec45,
60
+ vec3 as vec35,
61
+ vec4 as vec46,
62
62
  float as float4,
63
- int as int4,
63
+ int as int5,
64
64
  uint as uint2,
65
- dot as dot3,
65
+ dot as dot4,
66
66
  abs as abs3,
67
67
  max as max4,
68
68
  min as min2,
69
69
  sign as sign3,
70
70
  texture as texture3,
71
- textureLoad as textureLoad4,
72
- ivec2 as ivec24,
71
+ textureLoad as textureLoad5,
72
+ ivec2 as ivec25,
73
73
  normalize as normalize2,
74
74
  floor as floor2,
75
75
  fract,
@@ -80,9 +80,114 @@ import {
80
80
  uniform
81
81
  } from "three/tsl";
82
82
 
83
+ // src/rendering/instance-space.js
84
+ import { DataTexture, DynamicDrawUsage, FloatType, InstancedInterleavedBuffer, InterpolationSamplingType, NodeUpdateType, RGBAFormat } from "three/webgpu";
85
+ import { cross, dot, instanceIndex, instancedBufferAttribute, instancedDynamicBufferAttribute, int, ivec2, textureLoad, varying, vec3, vec4 as vec42 } from "three/tsl";
86
+ var spaces = /* @__PURE__ */ new WeakMap();
87
+ var webglSpaces = /* @__PURE__ */ new WeakMap();
88
+ function instanceColumns(object, matrix, useTexture) {
89
+ const columns = [0, 1, 2, 3];
90
+ if (useTexture) {
91
+ const rowSize = Math.ceil(Math.sqrt(matrix.count) / 2);
92
+ const rows = Math.ceil(matrix.count / rowSize);
93
+ const data = new Float32Array(rowSize * rows * 16);
94
+ const texture4 = new DataTexture(data, rowSize * 4, rows, RGBAFormat, FloatType);
95
+ const upload = () => {
96
+ data.set(matrix.array);
97
+ texture4.needsUpdate = true;
98
+ };
99
+ upload();
100
+ const release = () => {
101
+ object.removeEventListener("dispose", release);
102
+ texture4.dispose();
103
+ webglSpaces.delete(matrix);
104
+ };
105
+ object.addEventListener("dispose", release);
106
+ const x = instanceIndex.mod(rowSize).mul(4);
107
+ const y = instanceIndex.div(rowSize);
108
+ return { nodes: columns.map((column) => textureLoad(texture4, ivec2(int(x.add(column)), int(y)))), upload };
109
+ }
110
+ const interleaved = new InstancedInterleavedBuffer(matrix.array, 16, 1);
111
+ const read = matrix.usage === DynamicDrawUsage ? instancedDynamicBufferAttribute : instancedBufferAttribute;
112
+ return {
113
+ nodes: columns.map((column) => read(
114
+ interleaved,
115
+ /** @type {const} */
116
+ "vec4",
117
+ 16,
118
+ column * 4
119
+ )),
120
+ upload: () => {
121
+ interleaved.needsUpdate = true;
122
+ }
123
+ };
124
+ }
125
+ function instanceSpace(builder) {
126
+ const object = (
127
+ /** @type {InstancedMesh} */
128
+ builder.object
129
+ );
130
+ const matrix = object?.isInstancedMesh ? object.instanceMatrix : null;
131
+ if (!matrix) return null;
132
+ const backend = (
133
+ /** @type {{isWebGPUBackend?: boolean} | undefined} */
134
+ builder.renderer.backend
135
+ );
136
+ const useTexture = !backend?.isWebGPUBackend && matrix.count * 64 > (builder.getUniformBufferLimit?.() ?? 16384);
137
+ const cache = useTexture ? webglSpaces : spaces;
138
+ const cached = cache.get(matrix);
139
+ if (cached) return cached;
140
+ const { nodes, upload } = instanceColumns(object, matrix, useTexture);
141
+ let version = matrix.version;
142
+ nodes[0].updateBeforeType = NodeUpdateType.OBJECT;
143
+ nodes[0].updateBefore = () => {
144
+ if (matrix.version !== version) {
145
+ version = matrix.version;
146
+ upload();
147
+ }
148
+ return void 0;
149
+ };
150
+ const [a, b, c, d] = nodes.map((node) => node.xyz);
151
+ const determinant = dot(a, cross(b, c));
152
+ const rows = [cross(b, c), cross(c, a), cross(a, b)].map((row) => row.div(determinant));
153
+ const toLocalDirection = (v) => vec3(dot(rows[0], v), dot(rows[1], v), dot(rows[2], v));
154
+ const toMeshDirection = (v) => a.mul(v.x).add(b.mul(v.y)).add(c.mul(v.z));
155
+ const translation = [d.x, d.y, d.z];
156
+ const packed = [a, b, c].map(
157
+ (column, k) => varying(vec42(column, translation[k]), `instanceSpace${k}`).setInterpolation(InterpolationSamplingType.FLAT)
158
+ );
159
+ const [fa, fb, fc] = packed.map((column) => column.xyz);
160
+ const cofactors = () => [cross(fb, fc), cross(fc, fa), cross(fa, fb)];
161
+ const space = {
162
+ toLocalPoint: (p) => toLocalDirection(p.sub(d)),
163
+ toLocalDirection,
164
+ toMeshPoint: (p) => toMeshDirection(p).add(d),
165
+ toMeshDirection,
166
+ fragment: {
167
+ toMeshPoint: (p) => fa.mul(p.x).add(fb.mul(p.y)).add(fc.mul(p.z)).add(vec3(packed[0].w, packed[1].w, packed[2].w)),
168
+ toMeshDirection: (v) => fa.mul(v.x).add(fb.mul(v.y)).add(fc.mul(v.z)),
169
+ toMeshNormal: (n) => {
170
+ const [x, y, z] = cofactors();
171
+ return x.mul(n.x).add(y.mul(n.y)).add(z.mul(n.z)).mul(dot(fa, x).sign()).normalize();
172
+ },
173
+ toLocalDirection: (v) => {
174
+ const [x, y, z] = cofactors();
175
+ return vec3(dot(x, v), dot(y, v), dot(z, v)).div(dot(fa, x));
176
+ },
177
+ toLocalPoint: (p) => {
178
+ const [x, y, z] = cofactors();
179
+ const v = p.sub(vec3(packed[0].w, packed[1].w, packed[2].w));
180
+ return vec3(dot(x, v), dot(y, v), dot(z, v)).div(dot(fa, x));
181
+ }
182
+ }
183
+ };
184
+ cache.set(matrix, space);
185
+ return space;
186
+ }
187
+
83
188
  // src/rendering/captured-surface-material.js
84
189
  import { MeshStandardNodeMaterial } from "three/webgpu";
85
- import { context, modelViewMatrix as modelViewMatrix2, modelWorldMatrix, vec4 as vec42 } from "three/tsl";
190
+ import { context, modelViewMatrix as modelViewMatrix2, modelWorldMatrix, vec4 as vec43 } from "three/tsl";
86
191
  var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
87
192
  /** @type {import('three/webgpu').Node<'vec3'> | null} */
88
193
  capturedPositionNode = null;
@@ -93,8 +198,8 @@ var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
93
198
  setupLighting(builder) {
94
199
  const lighting = super.setupLighting(builder);
95
200
  if (!this.capturedPositionNode) return lighting;
96
- const world = modelWorldMatrix.mul(vec42(this.capturedPositionNode, 1)).xyz;
97
- const view = modelViewMatrix2.mul(vec42(this.capturedPositionNode, 1)).xyz;
201
+ const world = modelWorldMatrix.mul(vec43(this.capturedPositionNode, 1)).xyz;
202
+ const view = modelViewMatrix2.mul(vec43(this.capturedPositionNode, 1)).xyz;
98
203
  return context(lighting, {
99
204
  positionView: view,
100
205
  positionWorld: world,
@@ -104,7 +209,7 @@ var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
104
209
  };
105
210
 
106
211
  // src/box-impostor/box-impostor-ray-bounds.js
107
- import { Matrix4, NodeUpdateType, StorageBufferAttribute, Vector3 } from "three/webgpu";
212
+ import { Matrix4, NodeUpdateType as NodeUpdateType2, StorageBufferAttribute, Vector3 } from "three/webgpu";
108
213
  import {
109
214
  Fn as Fn2,
110
215
  If,
@@ -114,17 +219,17 @@ import {
114
219
  clamp,
115
220
  float,
116
221
  floor,
117
- instanceIndex,
118
- int,
119
- ivec2,
222
+ instanceIndex as instanceIndex2,
223
+ int as int2,
224
+ ivec2 as ivec22,
120
225
  max,
121
226
  min,
122
227
  storage,
123
- textureLoad,
228
+ textureLoad as textureLoad2,
124
229
  uint,
125
230
  vec2,
126
- vec3,
127
- vec4 as vec43
231
+ vec3 as vec32,
232
+ vec4 as vec44
128
233
  } from "three/tsl";
129
234
 
130
235
  // src/box-impostor/box-impostor-face-axes.js
@@ -179,18 +284,18 @@ function buildPatches(descriptor, maps, rects, tiles, size, output) {
179
284
  const descriptions = storage(tiles, "vec4", tiles.count).toReadOnly();
180
285
  const patches = storage(output, "vec4", output.count);
181
286
  return Fn2(() => {
182
- const meta = descriptions.element(instanceIndex).toVar();
183
- const key = int(meta.x), face = key.mod(6), axis = face.div(2);
184
- const rect = textureLoad(rects, ivec2(face, key.div(6))).mul(descriptor.atlasResolution).round().toVar();
185
- const extent = ivec2(rect.zw.add(1).sub(meta.yz)).min(ivec2(size));
186
- const origin = ivec2(rect.xy.add(meta.yz));
287
+ const meta = descriptions.element(instanceIndex2).toVar();
288
+ const key = int2(meta.x), face = key.mod(6), axis = face.div(2);
289
+ const rect = textureLoad2(rects, ivec22(face, key.div(6))).mul(descriptor.atlasResolution).round().toVar();
290
+ const extent = ivec22(rect.zw.add(1).sub(meta.yz)).min(ivec22(size));
291
+ const origin = ivec22(rect.xy.add(meta.yz));
187
292
  const near = float(1).toVar(), far = float(0).toVar();
188
293
  function visit(process) {
189
- Loop({ start: int(0), end: extent.y, type: "int", name: "row" }, ({ row }) => {
190
- Loop({ start: int(0), end: extent.x, type: "int", name: "column" }, ({ column }) => {
191
- const pixel = origin.add(ivec2(column, row));
192
- If(textureLoad(maps.orm, pixel).a.greaterThan(0.5), () => {
193
- const packed = textureLoad(maps.normalDepth, pixel);
294
+ Loop({ start: int2(0), end: extent.y, type: "int", name: "row" }, ({ row }) => {
295
+ Loop({ start: int2(0), end: extent.x, type: "int", name: "column" }, ({ column }) => {
296
+ const pixel = origin.add(ivec22(column, row));
297
+ If(textureLoad2(maps.orm, pixel).a.greaterThan(0.5), () => {
298
+ const packed = textureLoad2(maps.normalDepth, pixel);
194
299
  const depth = packed.b.mul(65280).add(packed.a.mul(255)).div(DEPTH_MAX).toVar();
195
300
  process(column, row, depth);
196
301
  });
@@ -202,11 +307,11 @@ function buildPatches(descriptor, maps, rects, tiles, size, output) {
202
307
  far.assign(max(far, depth));
203
308
  });
204
309
  const middle = near.add(far).mul(0.5).toVar();
205
- const low = [vec3(2).toVar(), vec3(2).toVar()], high = [vec3(-2).toVar(), vec3(-2).toVar()];
310
+ const low = [vec32(2).toVar(), vec32(2).toVar()], high = [vec32(-2).toVar(), vec32(-2).toVar()];
206
311
  visit((x, y, depth) => {
207
312
  const uv2 = meta.yz.add(vec2(x, y)).div(rect.zw).mul(2).sub(1);
208
- const lo = vec3(uv2.clamp(-1, 1), float(1).sub(depth.mul(2)).sub(meta.w).max(-1)).toVar();
209
- const hi = vec3(
313
+ const lo = vec32(uv2.clamp(-1, 1), float(1).sub(depth.mul(2)).sub(meta.w).max(-1)).toVar();
314
+ const hi = vec32(
210
315
  uv2.add(vec2(2).div(rect.zw)).clamp(-1, 1),
211
316
  float(1).sub(depth.mul(2)).add(meta.w).min(1)
212
317
  ).toVar();
@@ -223,38 +328,38 @@ function buildPatches(descriptor, maps, rects, tiles, size, output) {
223
328
  });
224
329
  });
225
330
  const facing = face.mod(2).equal(0).select(1, -1);
226
- const right = axis.equal(0).select(vec3(0, 0, facing.negate()), axis.equal(1).select(vec3(1, 0, 0), vec3(facing, 0, 0)));
227
- const up = axis.equal(1).select(vec3(0, 0, facing.negate()), vec3(0, 1, 0));
331
+ const right = axis.equal(0).select(vec32(0, 0, facing.negate()), axis.equal(1).select(vec32(1, 0, 0), vec32(facing, 0, 0)));
332
+ const up = axis.equal(1).select(vec32(0, 0, facing.negate()), vec32(0, 1, 0));
228
333
  const normal = right.cross(up);
229
334
  const rotate = (p) => right.mul(p.x).add(up.mul(p.y)).add(normal.mul(p.z));
230
335
  for (let group = 0; group < 2; group++) {
231
- const a = rotate(low[group]), b = rotate(high[group]), slot = instanceIndex.mul(4).add(group * 2);
336
+ const a = rotate(low[group]), b = rotate(high[group]), slot = instanceIndex2.mul(4).add(group * 2);
232
337
  const valid = low[group].x.lessThanEqual(high[group].x);
233
- patches.element(slot).assign(vec43(min(a, b), valid.select(meta.x, -1)));
234
- patches.element(slot.add(1)).assign(vec43(max(a, b), 0));
338
+ patches.element(slot).assign(vec44(min(a, b), valid.select(meta.x, -1)));
339
+ patches.element(slot.add(1)).assign(vec44(max(a, b), 0));
235
340
  }
236
341
  })().compute(tiles.count);
237
342
  }
238
343
  function makeViewPasses(patchBuffer, views, ranges, count, resolution) {
239
344
  const table = storage(ranges, "uint", count).toAtomic();
240
345
  const reset = Fn2(() => {
241
- atomicStore(table.element(instanceIndex), uint(DEPTH_MAX));
346
+ atomicStore(table.element(instanceIndex2), uint(DEPTH_MAX));
242
347
  })().compute(count);
243
348
  const patches = storage(patchBuffer, "vec4", patchBuffer.count).toReadOnly();
244
349
  const scatter = Fn2(() => {
245
- const a = patches.element(instanceIndex.mul(2)).toVar(), b = patches.element(instanceIndex.mul(2).add(1)).toVar();
350
+ const a = patches.element(instanceIndex2.mul(2)).toVar(), b = patches.element(instanceIndex2.mul(2).add(1)).toVar();
246
351
  If(a.w.greaterThanEqual(0), () => {
247
- const key = int(a.w), box = key.div(6);
352
+ const key = int2(a.w), box = key.div(6);
248
353
  const mapping = views.element(box.mul(2)), reference = views.element(box.mul(2).add(1));
249
354
  If(reference.w.greaterThanEqual(0), () => {
250
- const axis = int(reference.w).mod(3), x = axis.add(1).mod(3), y = axis.add(2).mod(3);
355
+ const axis = int2(reference.w).mod(3), x = axis.add(1).mod(3), y = axis.add(2).mod(3);
251
356
  const e = vec2(reference.element(x), reference.element(y)), ez = reference.element(axis);
252
357
  const za = a.element(axis), zb = b.element(axis);
253
358
  const weight = reference.w.lessThan(3).select(1, 0).toVar();
254
359
  const eyeSlope = e.div(ez).toVar(), inverseDistance = weight.div(ez).toVar();
255
360
  const project = (v, z) => v.sub(eyeSlope.mul(z)).div(float(1).sub(inverseDistance.mul(z)));
256
361
  const l = vec2(a.element(x), a.element(y)), h = vec2(b.element(x), b.element(y));
257
- const cell = (v, bias) => ivec2(floor(v.sub(mapping.xy).mul(mapping.zw).add(bias)).clamp(0, resolution - 1));
362
+ const cell = (v, bias) => ivec22(floor(v.sub(mapping.xy).mul(mapping.zw).add(bias)).clamp(0, resolution - 1));
258
363
  const lo = cell(min(project(l, za), project(l, zb)), -5e-3);
259
364
  const hi = cell(max(project(h, za), project(h, zb)), 5e-3);
260
365
  const padding = vec2(0.506).div(mapping.zw).mul(inverseDistance.abs().add(1)).add(2e-6);
@@ -406,7 +511,7 @@ function createBoxRayBounds(descriptor, maps, rects, material) {
406
511
  slot.ortho = ortho;
407
512
  slot.ready = true;
408
513
  }
409
- views.updateBeforeType = NodeUpdateType.OBJECT;
514
+ views.updateBeforeType = NodeUpdateType2.OBJECT;
410
515
  views.updateBefore = prepare;
411
516
  material.addEventListener("dispose", () => {
412
517
  for (const state of states.values()) {
@@ -428,7 +533,7 @@ function createBoxRayBounds(descriptor, maps, rects, material) {
428
533
  if (!builder.renderer.backend?.isWebGPUBackend) return null;
429
534
  const mapping = views.element(id.mul(2)).toVar();
430
535
  const ref = views.element(id.mul(2).add(1)).toVar();
431
- const axis = int(max(ref.w, 0)).mod(3), x = axis.add(1).mod(3), y = axis.add(2).mod(3);
536
+ const axis = int2(max(ref.w, 0)).mod(3), x = axis.add(1).mod(3), y = axis.add(2).mod(3);
432
537
  const rz = r.element(axis).toVar(), oz = o.element(axis).toVar();
433
538
  const ez = ref.element(axis), e = vec2(ref.element(x), ref.element(y));
434
539
  const eyeSlope = e.div(ez), inverseDistance = ref.w.lessThan(3).select(1, 0).div(ez);
@@ -438,9 +543,9 @@ function createBoxRayBounds(descriptor, maps, rects, material) {
438
543
  return p.sub(mapping.xy).mul(mapping.zw);
439
544
  };
440
545
  const a = project(enter).toVar(), b = project(exit).toVar();
441
- const cellLo = ivec2(floor(min(a, b).sub(1e-3)).clamp(0, resolution - 1)).toVar();
442
- const cellHi = ivec2(floor(max(a, b).add(1e-3)).clamp(0, resolution - 1)).toVar();
443
- const valid = ref.w.greaterThanEqual(0).and(rz.abs().greaterThan(1e-12)).and(cellHi.sub(cellLo).lessThanEqual(ivec2(2)).all()).toVar();
546
+ const cellLo = ivec22(floor(min(a, b).sub(1e-3)).clamp(0, resolution - 1)).toVar();
547
+ const cellHi = ivec22(floor(max(a, b).add(1e-3)).clamp(0, resolution - 1)).toVar();
548
+ const valid = ref.w.greaterThanEqual(0).and(rz.abs().greaterThan(1e-12)).and(cellHi.sub(cellLo).lessThanEqual(ivec22(2)).all()).toVar();
444
549
  return (face, enter2, exit2) => {
445
550
  const start = enter2.toVar(), end = exit2.toVar();
446
551
  If(valid, () => {
@@ -484,21 +589,21 @@ function refineSurfaceCrossing({ float: float5, If: If5, Loop: Loop3 }, { lo, hi
484
589
  }
485
590
 
486
591
  // src/box-impostor/box-impostor-faces.js
487
- import { DataTexture, FloatType, RGBAFormat, NearestFilter } from "three";
592
+ import { DataTexture as DataTexture2, FloatType as FloatType2, RGBAFormat as RGBAFormat2, NearestFilter } from "three";
488
593
  import {
489
594
  Fn as Fn3,
490
595
  If as If2,
491
596
  attribute,
492
- positionLocal,
597
+ positionGeometry,
493
598
  uv,
494
599
  texture,
495
- textureLoad as textureLoad2,
496
- ivec2 as ivec22,
600
+ textureLoad as textureLoad3,
601
+ ivec2 as ivec23,
497
602
  vec2 as vec22,
498
- vec3 as vec32,
603
+ vec3 as vec33,
499
604
  float as float2,
500
- int as int2,
501
- dot,
605
+ int as int3,
606
+ dot as dot2,
502
607
  abs,
503
608
  max as max2,
504
609
  sign,
@@ -560,45 +665,47 @@ function applyBoxFaces({
560
665
  transparency,
561
666
  half,
562
667
  toBox,
563
- fromBox
668
+ fromBox,
669
+ meshVertex,
670
+ meshNormal
564
671
  }) {
565
672
  const values = new Float32Array(descriptor.boxes.length * 6 * 4);
566
673
  descriptor.boxes.forEach((box, i) => {
567
674
  for (let face2 = 0; face2 < 6; face2++)
568
675
  values[(i * 6 + face2) * 4] = box.planes?.[face2] ?? 0.5;
569
676
  });
570
- const planes = new DataTexture(
677
+ const planes = new DataTexture2(
571
678
  values,
572
679
  6,
573
680
  descriptor.boxes.length,
574
- RGBAFormat,
575
- FloatType
681
+ RGBAFormat2,
682
+ FloatType2
576
683
  );
577
684
  planes.minFilter = planes.magFilter = NearestFilter;
578
685
  planes.needsUpdate = true;
579
686
  material.addEventListener("dispose", () => planes.dispose());
580
- const face = int2(attribute("boxFace", "float"));
687
+ const face = int3(attribute("boxFace", "float"));
581
688
  const n = attribute("normal", "vec3");
582
- const depth = textureLoad2(
689
+ const depth = textureLoad3(
583
690
  planes,
584
- ivec22(face, int2(attribute("boxId", "float")))
691
+ ivec23(face, int3(attribute("boxId", "float")))
585
692
  ).r;
586
- const proxy = positionLocal.sub(
693
+ const proxy = positionGeometry.sub(
587
694
  n.mul(
588
- dot(abs(toBox(n)), half).mul(depth).mul(2)
695
+ dot2(abs(toBox(n)), half).mul(depth).mul(2)
589
696
  )
590
697
  );
591
- material.positionNode = proxy;
592
- material.castShadowPositionNode = proxy;
698
+ material.positionNode = meshVertex(proxy);
699
+ material.castShadowPositionNode = material.positionNode;
593
700
  const base = texture(maps.baseColor, uv()).level(0).toVar("boxFaceColor");
594
701
  const orm = texture(maps.orm, uv()).level(0);
595
702
  const coverage = max2(orm.a, 1e-10);
596
703
  const normal = Fn3(() => {
597
- const packed = textureLoad2(
704
+ const packed = textureLoad3(
598
705
  maps.normalDepth,
599
- ivec22(uv().mul(descriptor.atlasResolution))
706
+ ivec23(uv().mul(descriptor.atlasResolution))
600
707
  ).rg.mul(2).sub(1);
601
- const decoded = vec32(
708
+ const decoded = vec33(
602
709
  packed,
603
710
  float2(1).sub(abs(packed.x)).sub(abs(packed.y))
604
711
  ).toVar();
@@ -607,13 +714,13 @@ function applyBoxFaces({
607
714
  vec22(1).sub(abs(decoded.yx)).mul(sign(decoded.xy.add(1e-20)))
608
715
  );
609
716
  });
610
- const result = vec32(0).toVar();
717
+ const result = vec33(0).toVar();
611
718
  for (let f = 0; f < 6; f++) {
612
719
  const a = BOX_FACE_AXES[f];
613
720
  If2(face.equal(f), () => {
614
721
  result.assign(
615
722
  fromBox(
616
- vec32(...a.right).mul(decoded.x).add(vec32(...a.up).mul(decoded.y)).add(vec32(...a.normal).mul(decoded.z))
723
+ vec33(...a.right).mul(decoded.x).add(vec33(...a.up).mul(decoded.y)).add(vec33(...a.normal).mul(decoded.z))
617
724
  ).normalize()
618
725
  );
619
726
  });
@@ -621,7 +728,7 @@ function applyBoxFaces({
621
728
  return result;
622
729
  })();
623
730
  material.colorNode = base.rgb.div(coverage);
624
- material.normalNode = transformNormalToView(normal);
731
+ material.normalNode = transformNormalToView(meshNormal(normal));
625
732
  material.roughnessNode = orm.g.div(coverage);
626
733
  material.metalnessNode = orm.b.div(coverage);
627
734
  material.aoNode = orm.r.div(coverage);
@@ -636,23 +743,23 @@ function applyBoxFaces({
636
743
  }
637
744
 
638
745
  // src/box-impostor/box-impostor-projection.js
639
- import { DataTexture as DataTexture2, FloatType as FloatType2, RGBAFormat as RGBAFormat2, NearestFilter as NearestFilter2 } from "three";
746
+ import { DataTexture as DataTexture3, FloatType as FloatType3, RGBAFormat as RGBAFormat3, NearestFilter as NearestFilter2 } from "three";
640
747
  import {
641
748
  Fn as Fn4,
642
749
  If as If3,
643
750
  vec2 as vec23,
644
- vec3 as vec33,
645
- vec4 as vec44,
751
+ vec3 as vec34,
752
+ vec4 as vec45,
646
753
  float as float3,
647
- int as int3,
648
- ivec2 as ivec23,
649
- dot as dot2,
754
+ int as int4,
755
+ ivec2 as ivec24,
756
+ dot as dot3,
650
757
  abs as abs2,
651
758
  max as max3,
652
759
  sign as sign2,
653
760
  normalize,
654
761
  texture as texture2,
655
- textureLoad as textureLoad3,
762
+ textureLoad as textureLoad4,
656
763
  attribute as attribute2,
657
764
  struct,
658
765
  modelWorldMatrix as modelWorldMatrix2,
@@ -673,19 +780,21 @@ function applyBoxProjection({
673
780
  eye,
674
781
  viewAxis,
675
782
  perspective,
676
- raster
783
+ raster,
784
+ meshPoint,
785
+ meshNormal
677
786
  }) {
678
787
  const data = new Float32Array(descriptor.boxes.length * 24);
679
788
  descriptor.boxes.forEach((box, i) => {
680
789
  for (let face = 0; face < 6; face++)
681
790
  data[(i * 6 + face) * 4] = box.planes?.[face] ?? 0.5;
682
791
  });
683
- const planes = new DataTexture2(
792
+ const planes = new DataTexture3(
684
793
  data,
685
794
  6,
686
795
  descriptor.boxes.length,
687
- RGBAFormat2,
688
- FloatType2
796
+ RGBAFormat3,
797
+ FloatType3
689
798
  );
690
799
  planes.minFilter = planes.magFilter = NearestFilter2;
691
800
  planes.needsUpdate = true;
@@ -702,11 +811,11 @@ function applyBoxProjection({
702
811
  "BoxProjectedSurface"
703
812
  );
704
813
  const readNormal = (uv2, right, up, n) => {
705
- const packed = textureLoad3(
814
+ const packed = textureLoad4(
706
815
  maps.normalDepth,
707
- ivec23(uv2.mul(descriptor.atlasResolution))
816
+ ivec24(uv2.mul(descriptor.atlasResolution))
708
817
  ).rg.mul(2).sub(1);
709
- const decoded = vec33(
818
+ const decoded = vec34(
710
819
  packed,
711
820
  float3(1).sub(abs2(packed.x)).sub(abs2(packed.y))
712
821
  ).toVar();
@@ -730,20 +839,20 @@ function applyBoxProjection({
730
839
  );
731
840
  const o = toBox(origin.sub(center)).div(half), r = toBox(ray).div(half);
732
841
  const look = normalize(toBox(ray).negate()), angles = abs2(look);
733
- const positions = vec33(0).toVar();
734
- const selectedUv = vec23(0).toVar(), selectedRight = vec33(1, 0, 0).toVar(), selectedUp = vec33(0, 1, 0).toVar(), selectedNormal = vec33(0, 0, 1).toVar();
842
+ const positions = vec34(0).toVar();
843
+ const selectedUv = vec23(0).toVar(), selectedRight = vec34(1, 0, 0).toVar(), selectedUp = vec34(0, 1, 0).toVar(), selectedNormal = vec34(0, 0, 1).toVar();
735
844
  const nearest = float3(1e20).toVar("boxPlaneDistance");
736
845
  const opacity = float3(0).toVar();
737
846
  const candidates = [];
738
847
  for (let axis = 0; axis < 3; axis++) {
739
848
  const angle = angles.element(axis);
740
849
  const positive = look.element(axis).greaterThanEqual(0), a = BOX_FACE_AXES[axis * 2], b = BOX_FACE_AXES[axis * 2 + 1];
741
- const n = positive.select(vec33(...a.normal), vec33(...b.normal)), right = positive.select(vec33(...a.right), vec33(...b.right)), up = positive.select(vec33(...a.up), vec33(...b.up));
850
+ const n = positive.select(vec34(...a.normal), vec34(...b.normal)), right = positive.select(vec34(...a.right), vec34(...b.right)), up = positive.select(vec34(...a.up), vec34(...b.up));
742
851
  const weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
743
852
  const candidate = settings.interpolation === "none" ? null : {
744
853
  uv: vec23(0).toVar(),
745
- base: vec44(0).toVar(),
746
- orm: vec44(0).toVar(),
854
+ base: vec45(0).toVar(),
855
+ orm: vec45(0).toVar(),
747
856
  t: float3(1e20).toVar(),
748
857
  tolerance: float3(0).toVar(),
749
858
  right,
@@ -753,17 +862,17 @@ function applyBoxProjection({
753
862
  };
754
863
  if (candidate) candidates.push(candidate);
755
864
  If3(angle.greaterThan(1e-4), () => {
756
- const face = positive.select(int3(axis * 2), int3(axis * 2 + 1)), id = int3(attribute2("boxId", "float"));
865
+ const face = positive.select(int4(axis * 2), int4(axis * 2 + 1)), id = int4(attribute2("boxId", "float"));
757
866
  const plane = float3(1).sub(
758
- textureLoad3(planes, ivec23(face, id)).r.mul(2)
867
+ textureLoad4(planes, ivec24(face, id)).r.mul(2)
759
868
  );
760
- const divisor2 = dot2(r, n), safe = sign2(divisor2.add(1e-20)).mul(max3(abs2(divisor2), 1e-10));
761
- const t = plane.sub(dot2(o, n)).div(safe), point = o.add(r.mul(t));
762
- const xy = vec23(dot2(point, right), dot2(point, up)).mul(0.5).add(0.5);
869
+ const divisor2 = dot3(r, n), safe = sign2(divisor2.add(1e-20)).mul(max3(abs2(divisor2), 1e-10));
870
+ const t = plane.sub(dot3(o, n)).div(safe), point = o.add(r.mul(t));
871
+ const xy = vec23(dot3(point, right), dot3(point, up)).mul(0.5).add(0.5);
763
872
  let supported = t.greaterThanEqual(0).and(xy.x.greaterThanEqual(0)).and(xy.y.greaterThanEqual(0)).and(xy.x.lessThanEqual(1)).and(xy.y.lessThanEqual(1));
764
873
  if (!candidate) supported = supported.and(t.lessThan(nearest));
765
874
  If3(supported, () => {
766
- const rect = textureLoad3(rects, ivec23(face, id)), uv2 = rect.xy.add(xy.mul(rect.zw));
875
+ const rect = textureLoad4(rects, ivec24(face, id)), uv2 = rect.xy.add(xy.mul(rect.zw));
767
876
  const base = texture2(maps.baseColor, uv2).level(0), orm = texture2(maps.orm, uv2).level(0);
768
877
  const alpha2 = base.a.clamp(0, 1);
769
878
  If3(
@@ -787,10 +896,10 @@ function applyBoxProjection({
787
896
  candidate.t.assign(t);
788
897
  candidate.tolerance.assign(
789
898
  max3(
790
- dot2(abs2(right), half).div(
899
+ dot3(abs2(right), half).div(
791
900
  rect.z.mul(descriptor.atlasResolution)
792
901
  ),
793
- dot2(abs2(up), half).div(
902
+ dot3(abs2(up), half).div(
794
903
  rect.w.mul(descriptor.atlasResolution)
795
904
  )
796
905
  ).mul(4).div(max3(angle, 0.1))
@@ -813,7 +922,7 @@ function applyBoxProjection({
813
922
  positions
814
923
  );
815
924
  }
816
- const colors = vec33(0).toVar(), orms = vec33(0).toVar(), emissions = vec33(0).toVar(), normals = vec33(0).toVar();
925
+ const colors = vec34(0).toVar(), orms = vec34(0).toVar(), emissions = vec34(0).toVar(), normals = vec34(0).toVar();
817
926
  const weights = float3(0).toVar(), support = float3(0).toVar(), alpha = float3(0).toVar();
818
927
  for (const c of candidates) {
819
928
  If3(
@@ -842,9 +951,10 @@ function applyBoxProjection({
842
951
  positions
843
952
  );
844
953
  })().toVar("boxProjectedSurface");
845
- material.capturedPositionNode = surface.get("position");
954
+ const position = meshPoint(surface.get("position")).toVar("boxProjectedMeshPosition");
955
+ material.capturedPositionNode = position;
846
956
  material.colorNode = surface.get("color");
847
- material.normalNode = transformNormalToView2(surface.get("normal"));
957
+ material.normalNode = transformNormalToView2(meshNormal(surface.get("normal")));
848
958
  material.roughnessNode = surface.get("orm").g;
849
959
  material.metalnessNode = surface.get("orm").b;
850
960
  material.aoNode = surface.get("orm").r;
@@ -852,9 +962,9 @@ function applyBoxProjection({
852
962
  material.maskNode = surface.get("alpha").greaterThanEqual(transparency === "cutout" ? alphaCutoff : float3(1 / 255));
853
963
  material.maskShadowNode = material.maskNode;
854
964
  if (transparency !== "cutout") material.opacityNode = surface.get("alpha");
855
- material.depthNode = capturedSurfaceDepth(surface.get("position"));
965
+ material.depthNode = capturedSurfaceDepth(position);
856
966
  material.receivedShadowPositionNode = modelWorldMatrix2.mul(
857
- vec44(surface.get("position"), 1)
967
+ vec45(position, 1)
858
968
  ).xyz;
859
969
  material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
860
970
  return material;
@@ -872,7 +982,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
872
982
  const surfaceCoverage = (ormAlpha, baseAlpha) => filtering && descriptor.alphaMode !== "blend" ? min2(ormAlpha, baseAlpha) : ormAlpha;
873
983
  const unpackNormal = (encoded) => {
874
984
  const packed = encoded.mul(2).sub(1);
875
- const normal2 = vec34(packed, float4(1).sub(abs3(packed.x)).sub(abs3(packed.y))).toVar();
985
+ const normal2 = vec35(packed, float4(1).sub(abs3(packed.x)).sub(abs3(packed.y))).toVar();
876
986
  If4(normal2.z.lessThan(0), () => {
877
987
  normal2.xy.assign(vec24(1).sub(abs3(normal2.yx)).mul(sign3(normal2.xy.add(1e-20))));
878
988
  });
@@ -885,14 +995,14 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
885
995
  const nearestNormal = decodeNormal(nearest.rg), result = decodeNormal(sampled.rg.div(coverage2)).toVar();
886
996
  const sampledDepth = sampled.b.mul(65280).add(sampled.a.mul(255)).div(65535);
887
997
  const nearestDepth = nearest.b.mul(65280).add(nearest.a.mul(255)).div(65535);
888
- If4(nearestNormal.z.lessThan(0).or(dot3(result, nearestNormal).lessThan(0.75)).or(coverage2.lessThan(0.9999)).or(abs3(sampledDepth.sub(nearestDepth)).greaterThan(tolerance)), () => {
998
+ If4(nearestNormal.z.lessThan(0).or(dot4(result, nearestNormal).lessThan(0.75)).or(coverage2.lessThan(0.9999)).or(abs3(sampledDepth.sub(nearestDepth)).greaterThan(tolerance)), () => {
889
999
  const p = uv2.mul(descriptor.atlasResolution).sub(0.5), lo = floor2(p), f = fract(p);
890
- const normal2 = vec34(0).toVar(), weights = float4(0).toVar();
1000
+ const normal2 = vec35(0).toVar(), weights = float4(0).toVar();
891
1001
  for (const [x, y] of [[0, 0], [1, 0], [0, 1], [1, 1]]) {
892
- const texel = ivec24(lo.add(vec24(x, y)).clamp(0, descriptor.atlasResolution - 1));
893
- const packed = textureLoad4(maps.normalDepth, texel).toVar();
894
- const alpha2 = textureLoad4(maps.baseColor, texel).a;
895
- const coverage3 = descriptor.alphaMode === "blend" ? textureLoad4(maps.orm, texel).a : alpha2;
1002
+ const texel = ivec25(lo.add(vec24(x, y)).clamp(0, descriptor.atlasResolution - 1));
1003
+ const packed = textureLoad5(maps.normalDepth, texel).toVar();
1004
+ const alpha2 = textureLoad5(maps.baseColor, texel).a;
1005
+ const coverage3 = descriptor.alphaMode === "blend" ? textureLoad5(maps.orm, texel).a : alpha2;
896
1006
  const capturedDepth = packed.b.mul(65280).add(packed.a.mul(255)).div(65535);
897
1007
  const supported = coverage3.greaterThan(1e-4).and(alpha2.greaterThanEqual(minimumAlpha.mul(coverage3))).and(abs3(capturedDepth.sub(depth)).lessThanEqual(tolerance));
898
1008
  If4(supported, () => {
@@ -934,46 +1044,57 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
934
1044
  )
935
1045
  )
936
1046
  );
937
- const rects = new DataTexture3(
1047
+ const rects = new DataTexture4(
938
1048
  rectData,
939
1049
  6,
940
1050
  descriptor.boxes.length,
941
- RGBAFormat3,
942
- FloatType3
1051
+ RGBAFormat4,
1052
+ FloatType4
943
1053
  );
944
1054
  rects.needsUpdate = true;
945
1055
  rects.magFilter = rects.minFilter = NearestFilter3;
946
1056
  material.addEventListener("dispose", () => rects.dispose());
947
1057
  const center = attribute3("boxCenter", "vec3"), half = attribute3("boxHalfSize", "vec3");
948
1058
  const ax = attribute3("boxAxisX", "vec3"), ay = attribute3("boxAxisY", "vec3"), az = attribute3("boxAxisZ", "vec3");
949
- const toBox = (p) => vec34(dot3(p, ax), dot3(p, ay), dot3(p, az));
1059
+ const toBox = (p) => vec35(dot4(p, ax), dot4(p, ay), dot4(p, az));
950
1060
  const fromBox = (p) => ax.mul(p.x).add(ay.mul(p.y)).add(az.mul(p.z));
951
- const eye = modelWorldMatrixInverse.mul(vec45(cameraPosition, 1)).xyz;
952
- const viewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec45(0, 0, 1, 0))).xyz.normalize();
1061
+ const meshVertex = (p) => Fn5((_, builder) => instanceSpace(builder)?.toMeshPoint(p) ?? p)();
1062
+ const meshPoint = (p) => Fn5((_, builder) => instanceSpace(builder)?.fragment.toMeshPoint(p) ?? p)();
1063
+ const meshNormal = (n) => Fn5((_, builder) => instanceSpace(builder)?.fragment.toMeshNormal(n) ?? n)();
1064
+ const meshEye = modelWorldMatrixInverse.mul(vec46(cameraPosition, 1)).xyz;
1065
+ const meshViewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec46(0, 0, 1, 0))).xyz;
1066
+ const eye = Fn5((_, builder) => instanceSpace(builder)?.fragment.toLocalPoint(meshEye) ?? meshEye)();
1067
+ const viewAxis = Fn5((_, builder) => instanceSpace(builder)?.fragment.toLocalDirection(meshViewAxis) ?? meshViewAxis)().normalize();
1068
+ const vertexEye = Fn5((_, builder) => instanceSpace(builder)?.toLocalPoint(meshEye) ?? meshEye)();
1069
+ const vertexViewAxis = Fn5((_, builder) => instanceSpace(builder)?.toLocalDirection(meshViewAxis) ?? meshViewAxis)().normalize();
1070
+ const cameraUp = Fn5((_, builder) => {
1071
+ const up = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec46(0, 1, 0, 0))).xyz;
1072
+ return instanceSpace(builder)?.toLocalDirection(up) ?? up;
1073
+ })();
953
1074
  const perspective = cameraProjectionMatrix2.element(3).w.equal(0);
954
- let proxy = positionLocal2;
1075
+ let proxy = positionGeometry2;
955
1076
  if (descriptor.mode !== "fitted") {
956
1077
  proxy = Fn5((_, builder) => {
957
- const delta = eye.sub(center);
958
- const forward = perspective.and(delta.length().greaterThan(1e-6)).select(normalize2(delta), viewAxis);
959
- const right = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec45(0, 1, 0, 0))).xyz.cross(forward).normalize();
1078
+ const delta = vertexEye.sub(center);
1079
+ const forward = perspective.and(delta.length().greaterThan(1e-6)).select(normalize2(delta), vertexViewAxis);
1080
+ const right = cameraUp.cross(forward).normalize();
960
1081
  const up = forward.cross(right).normalize();
961
1082
  const radius = half.length();
962
- const distance = dot3(eye.sub(center), forward);
1083
+ const distance = dot4(vertexEye.sub(center), forward);
963
1084
  const planeOffset = perspective.select(min2(radius, distance.sub(cameraNear2.mul(2))), radius);
964
1085
  const plane = center.add(forward.mul(planeOffset));
965
1086
  const lo = vec24(1e20).toVar(), hi = vec24(-1e20).toVar();
966
1087
  for (const x of [-1, 1])
967
1088
  for (const y of [-1, 1])
968
1089
  for (const z of [-1, 1]) {
969
- const corner2 = fromBox(half.mul(vec34(x, y, z)));
1090
+ const corner2 = fromBox(half.mul(vec35(x, y, z)));
970
1091
  const scale = perspective.select(
971
1092
  max4(distance.sub(planeOffset), 1e-5).div(
972
- max4(distance.sub(dot3(corner2, forward)), 1e-5)
1093
+ max4(distance.sub(dot4(corner2, forward)), 1e-5)
973
1094
  ),
974
1095
  float4(1)
975
1096
  );
976
- const p = vec24(dot3(corner2, right), dot3(corner2, up)).mul(scale);
1097
+ const p = vec24(dot4(corner2, right), dot4(corner2, up)).mul(scale);
977
1098
  lo.assign(min2(lo, p));
978
1099
  hi.assign(max4(hi, p));
979
1100
  }
@@ -981,10 +1102,10 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
981
1102
  const xy = lo.add(hi.sub(lo).mul(corner));
982
1103
  return plane.add(right.mul(xy.x)).add(up.mul(xy.y));
983
1104
  })();
984
- material.positionNode = proxy;
985
- material.castShadowPositionNode = proxy;
1105
+ material.positionNode = meshVertex(proxy);
1106
+ material.castShadowPositionNode = material.positionNode;
986
1107
  }
987
- const raster = varying(proxy, "boxRasterPosition");
1108
+ const raster = varying2(proxy, "boxRasterPosition");
988
1109
  if (boxDepthMode(descriptor, settings) === "projection" && descriptor.mode === "fitted" && settings.interpolation === "none") {
989
1110
  material.side = FrontSide;
990
1111
  return applyBoxFaces({
@@ -995,7 +1116,9 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
995
1116
  transparency,
996
1117
  half,
997
1118
  toBox,
998
- fromBox
1119
+ fromBox,
1120
+ meshVertex,
1121
+ meshNormal
999
1122
  });
1000
1123
  }
1001
1124
  if (boxDepthMode(descriptor, settings) === "projection")
@@ -1014,7 +1137,9 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1014
1137
  eye,
1015
1138
  viewAxis,
1016
1139
  perspective,
1017
- raster
1140
+ raster,
1141
+ meshPoint,
1142
+ meshNormal
1018
1143
  });
1019
1144
  const accelerated = descriptor.boxes.every((b) => b.depthBounds);
1020
1145
  const gridSize = BOX_OCCUPANCY_RESOLUTION;
@@ -1027,7 +1152,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1027
1152
  descriptor.boxes.forEach(
1028
1153
  (box, index) => words.set(box.occupancy, index * wordsPerBox)
1029
1154
  );
1030
- occupancy = new DataTexture3(
1155
+ occupancy = new DataTexture4(
1031
1156
  words,
1032
1157
  wordsPerBox,
1033
1158
  descriptor.boxes.length,
@@ -1044,16 +1169,16 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1044
1169
  "BoxImpostorHit"
1045
1170
  );
1046
1171
  const faceBasis = (axis, facing) => {
1047
- const n = vec34(
1172
+ const n = vec35(
1048
1173
  axis.equal(0).select(facing, 0),
1049
1174
  axis.equal(1).select(facing, 0),
1050
1175
  axis.equal(2).select(facing, 0)
1051
1176
  ).toVar();
1052
1177
  const right = axis.equal(0).select(
1053
- vec34(0, 0, facing.negate()),
1054
- axis.equal(1).select(vec34(1, 0, 0), vec34(facing, 0, 0))
1178
+ vec35(0, 0, facing.negate()),
1179
+ axis.equal(1).select(vec35(1, 0, 0), vec35(facing, 0, 0))
1055
1180
  ).toVar();
1056
- const up = axis.equal(1).select(vec34(0, 0, facing.negate()), vec34(0, 1, 0)).toVar();
1181
+ const up = axis.equal(1).select(vec35(0, 0, facing.negate()), vec35(0, 1, 0)).toVar();
1057
1182
  return { n, right, up };
1058
1183
  };
1059
1184
  const ray = perspective.select(normalize2(raster.sub(eye)), viewAxis.negate());
@@ -1064,17 +1189,17 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1064
1189
  );
1065
1190
  const o = toBox(origin.sub(center)).div(half), r = toBox(ray).div(half);
1066
1191
  const safe = sign3(r.add(1e-20)).mul(max4(abs3(r), 1e-10));
1067
- const a = vec34(-1).sub(o).div(safe), b = vec34(1).sub(o).div(safe);
1192
+ const a = vec35(-1).sub(o).div(safe), b = vec35(1).sub(o).div(safe);
1068
1193
  const enter = max4(max4(min2(a, b).x, min2(a, b).y), min2(a, b).z).max(0);
1069
1194
  const exit = min2(min2(max4(a, b).x, max4(a, b).y), max4(a, b).z);
1070
- const rayBounds = viewBounds?.(builder, int4(attribute3("boxId", "float")), o, r, enter, exit);
1195
+ const rayBounds = viewBounds?.(builder, int5(attribute3("boxId", "float")), o, r, enter, exit);
1071
1196
  const occupiedStart = enter.toVar();
1072
1197
  if (occupancy) {
1073
1198
  const gridOrigin = o.add(1).mul(gridSize / 2);
1074
1199
  const gridRay = r.mul(gridSize / 2);
1075
1200
  const step = sign3(gridRay.add(1e-20));
1076
1201
  const cell = floor2(gridOrigin.add(gridRay.mul(enter))).clamp(0, gridSize - 1).toVar();
1077
- const inverse = vec34(1).div(safe.mul(gridSize / 2));
1202
+ const inverse = vec35(1).div(safe.mul(gridSize / 2));
1078
1203
  const delta = abs3(inverse);
1079
1204
  const next = cell.add(step.add(1).mul(0.5)).sub(gridOrigin).mul(inverse).toVar();
1080
1205
  const t = enter.toVar();
@@ -1086,9 +1211,9 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1086
1211
  const cellIndex = uint2(
1087
1212
  cell.x.add(cell.y.mul(gridSize)).add(cell.z.mul(gridSize ** 2))
1088
1213
  ).toVar();
1089
- const word = textureLoad4(
1214
+ const word = textureLoad5(
1090
1215
  occupancy,
1091
- ivec24(int4(cellIndex.shiftRight(5)), int4(attribute3("boxId", "float")))
1216
+ ivec25(int5(cellIndex.shiftRight(5)), int5(attribute3("boxId", "float")))
1092
1217
  ).r;
1093
1218
  const support = word.shiftRight(cellIndex.bitAnd(31)).bitAnd(1);
1094
1219
  If4(support.notEqual(uint2(0)), () => {
@@ -1096,7 +1221,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1096
1221
  Break();
1097
1222
  });
1098
1223
  const edge = min2(min2(next.x, next.y), next.z).toVar();
1099
- const advance = vec34(
1224
+ const advance = vec35(
1100
1225
  next.x.lessThanEqual(edge).select(1, 0),
1101
1226
  next.y.lessThanEqual(edge).select(1, 0),
1102
1227
  next.z.lessThanEqual(edge).select(1, 0)
@@ -1113,15 +1238,15 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1113
1238
  });
1114
1239
  }
1115
1240
  const look = toBox(ray).negate();
1116
- const bestT = exit.add(1).toVar(), bestUv = vec24(0).toVar(), bestFace = int4(0).toVar(), appearanceNear = preferFacingCapture ? float4(0).toVar() : null, valid = float4(0).toVar(), found = float4(0).toVar();
1241
+ const bestT = exit.add(1).toVar(), bestUv = vec24(0).toVar(), bestFace = int5(0).toVar(), appearanceNear = preferFacingCapture ? float4(0).toVar() : null, valid = float4(0).toVar(), found = float4(0).toVar();
1117
1242
  const angles = abs3(look).toVar();
1118
1243
  const major = angles.x.greaterThanEqual(angles.y).and(angles.x.greaterThanEqual(angles.z)).select(
1119
- int4(0),
1120
- angles.y.greaterThanEqual(angles.z).select(int4(1), int4(2))
1244
+ int5(0),
1245
+ angles.y.greaterThanEqual(angles.z).select(int5(1), int5(2))
1121
1246
  ).toVar();
1122
1247
  const nextAxis = major.add(1).mod(3), lastAxis = major.add(2).mod(3);
1123
1248
  const second = angles.element(nextAxis).greaterThanEqual(angles.element(lastAxis)).select(nextAxis, lastAxis).toVar();
1124
- const third = int4(3).sub(major).sub(second).toVar();
1249
+ const third = int5(3).sub(major).sub(second).toVar();
1125
1250
  Loop2(
1126
1251
  { start: 0, end: 6, type: "int", name: "viewIndex" },
1127
1252
  ({ viewIndex: i }) => {
@@ -1135,24 +1260,24 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1135
1260
  look.element(axis).greaterThanEqual(0),
1136
1261
  look.element(axis).lessThan(0)
1137
1262
  );
1138
- const face = axis.mul(2).add(positive.select(int4(0), int4(1)));
1263
+ const face = axis.mul(2).add(positive.select(int5(0), int5(1)));
1139
1264
  const bounds = rayBounds?.(face, enter, exit);
1140
1265
  const facing = positive.select(1, -1);
1141
1266
  const { n, right, up } = faceBasis(axis, facing);
1142
1267
  const direction = i.lessThan(3).select(1, -1);
1143
- const rect = textureLoad4(
1268
+ const rect = textureLoad5(
1144
1269
  rects,
1145
- ivec24(face, int4(attribute3("boxId", "float")))
1270
+ ivec25(face, int5(attribute3("boxId", "float")))
1146
1271
  ).toVar();
1147
1272
  const sampleUv = (p) => rect.xy.add(
1148
- vec24(dot3(p, right), dot3(p, up)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
1273
+ vec24(dot4(p, right), dot4(p, up)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
1149
1274
  );
1150
1275
  const decode = (d) => d.b.mul(65280).add(d.a.mul(255)).div(65535);
1151
1276
  const depthAt = (uv2) => decode(texture3(maps.normalDepth, uv2).level(0));
1152
- const projectedOrigin = vec24(dot3(o, right), dot3(o, up)).toVar();
1153
- const projectedRay = vec24(dot3(r, right), dot3(r, up)).toVar();
1154
- const axialOrigin = dot3(o, n).toVar();
1155
- const axialRay = dot3(r, n).toVar();
1277
+ const projectedOrigin = vec24(dot4(o, right), dot4(o, up)).toVar();
1278
+ const projectedRay = vec24(dot4(r, right), dot4(r, up)).toVar();
1279
+ const axialOrigin = dot4(o, n).toVar();
1280
+ const axialRay = dot4(r, n).toVar();
1156
1281
  const distance = (t) => {
1157
1282
  const uv2 = rect.xy.add(
1158
1283
  projectedOrigin.add(projectedRay.mul(t)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
@@ -1161,17 +1286,17 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1161
1286
  };
1162
1287
  const acceptHit = (hitT) => {
1163
1288
  const point2 = o.add(r.mul(hitT)), uv2 = sampleUv(point2);
1164
- const texel = ivec24(uv2.mul(descriptor.atlasResolution));
1165
- const nearest = textureLoad4(maps.normalDepth, texel);
1289
+ const texel = ivec25(uv2.mul(descriptor.atlasResolution));
1290
+ const nearest = textureLoad5(maps.normalDepth, texel);
1166
1291
  const capturedBase = texture3(maps.baseColor, uv2).level(0);
1167
- const occupied = textureLoad4(maps.orm, texel).a.greaterThan(0.5);
1292
+ const occupied = textureLoad5(maps.orm, texel).a.greaterThan(0.5);
1168
1293
  const residual = abs3(
1169
- dot3(point2, n).sub(float4(1).sub(decode(nearest).mul(2)))
1294
+ dot4(point2, n).sub(float4(1).sub(decode(nearest).mul(2)))
1170
1295
  );
1171
1296
  const tolerance = max4(
1172
- dot3(abs3(right), half).div(rect.z.mul(descriptor.atlasResolution)),
1173
- dot3(abs3(up), half).div(rect.w.mul(descriptor.atlasResolution))
1174
- ).div(dot3(abs3(n), half)).mul(4).add(4 / 65535);
1297
+ dot4(abs3(right), half).div(rect.z.mul(descriptor.atlasResolution)),
1298
+ dot4(abs3(up), half).div(rect.w.mul(descriptor.atlasResolution))
1299
+ ).div(dot4(abs3(n), half)).mul(4).add(4 / 65535);
1175
1300
  If4(
1176
1301
  residual.lessThanEqual(tolerance).and(occupied).and(
1177
1302
  capturedBase.a.greaterThanEqual(minimumAlpha)
@@ -1186,8 +1311,8 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1186
1311
  };
1187
1312
  if (preferFacingCapture) {
1188
1313
  const texel2 = max4(
1189
- dot3(abs3(right), half).div(rect.z),
1190
- dot3(abs3(up), half).div(rect.w)
1314
+ dot4(abs3(right), half).div(rect.z),
1315
+ dot4(abs3(up), half).div(rect.w)
1191
1316
  ).div(descriptor.atlasResolution);
1192
1317
  const uncertainty = min2(
1193
1318
  texel2,
@@ -1204,7 +1329,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1204
1329
  );
1205
1330
  };
1206
1331
  const traceInterval = (start, end, steps) => {
1207
- const footprint = vec24(dot3(r, right), dot3(r, up)).abs().mul(rect.zw).mul(descriptor.atlasResolution / 2).mul(end.sub(start));
1332
+ const footprint = vec24(dot4(r, right), dot4(r, up)).abs().mul(rect.zw).mul(descriptor.atlasResolution / 2).mul(end.sub(start));
1208
1333
  let density = steps / 8;
1209
1334
  if (descriptor.alphaMode === "blend" && transparency === "blended" && steps < 32) {
1210
1335
  density = i.equal(0).select(steps / 16, steps / 32);
@@ -1213,10 +1338,10 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1213
1338
  }
1214
1339
  const samples = max4(float4(steps), max4(footprint.x, footprint.y).mul(density)).div(2).ceil().mul(2).toVar();
1215
1340
  const step = end.sub(start).div(samples).toVar();
1216
- const pairs = int4(samples.div(2));
1341
+ const pairs = int5(samples.div(2));
1217
1342
  const pairAt = (t) => t.sub(start).div(max4(step.mul(2), 1e-15));
1218
- const firstPair = bounds ? int4(floor2(pairAt(bounds.start))).clamp(0, pairs).toVar() : int4(0);
1219
- const lastPair = bounds ? int4(pairAt(bounds.end).ceil()).clamp(0, pairs).toVar() : pairs;
1343
+ const firstPair = bounds ? int5(floor2(pairAt(bounds.start))).clamp(0, pairs).toVar() : int5(0);
1344
+ const lastPair = bounds ? int5(pairAt(bounds.end).ceil()).clamp(0, pairs).toVar() : pairs;
1220
1345
  const previous = start.toVar();
1221
1346
  if (bounds) If4(firstPair.greaterThan(0), () => {
1222
1347
  previous.assign(start.add(step.mul(float4(firstPair.mul(2)))));
@@ -1274,8 +1399,8 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1274
1399
  )
1275
1400
  );
1276
1401
  const interval = positive.select(depths.xy, depths.zw);
1277
- const initialDepth = float4(1).sub(dot3(o, n)).mul(0.5);
1278
- const depthSlope = dot3(r, n).negate().mul(0.5);
1402
+ const initialDepth = float4(1).sub(dot4(o, n)).mul(0.5);
1403
+ const depthSlope = dot4(r, n).negate().mul(0.5);
1279
1404
  const da = interval.x.sub(2 / 65535).sub(initialDepth).div(depthSlope);
1280
1405
  const db = interval.y.add(2 / 65535).sub(initialDepth).div(depthSlope);
1281
1406
  const start = max4(occupiedStart, min2(da, db));
@@ -1329,7 +1454,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1329
1454
  const sampled = texture3(maps.normalDepth, hit.get("uv")).level(0);
1330
1455
  const local = unpackNormal(sampled.rg.div(coverage));
1331
1456
  const direction = fromBox(right.mul(local.x).add(up.mul(local.y)).add(n.mul(local.z))).normalize().toVar();
1332
- return direction.mul(dot3(direction, ray).greaterThan(0).select(-1, 1));
1457
+ return direction.mul(dot4(direction, ray).greaterThan(0).select(-1, 1));
1333
1458
  })();
1334
1459
  let color = base.div(coverage), surfaceOrm = orm.rgb.div(coverage), emission = texture3(maps.emissive, hit.get("uv")).level(0).rgb.div(coverage), normal = hitNormal;
1335
1460
  if (settings.interpolation !== "none") {
@@ -1343,37 +1468,37 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1343
1468
  const look = toBox(
1344
1469
  perspective.select(normalize2(eye.sub(hit.get("position"))), viewAxis)
1345
1470
  );
1346
- const colors = vec45(0).toVar(), materials = vec34(0).toVar(), emissions = vec34(0).toVar(), normals = vec34(0).toVar(), weights = float4(0).toVar();
1471
+ const colors = vec46(0).toVar(), materials = vec35(0).toVar(), emissions = vec35(0).toVar(), normals = vec35(0).toVar(), weights = float4(0).toVar();
1347
1472
  for (let axis = 0; axis < 3; axis++) {
1348
1473
  const positive = look.element(axis).greaterThanEqual(0);
1349
1474
  const front = BOX_FACE_AXES[axis * 2], back = BOX_FACE_AXES[axis * 2 + 1];
1350
- const n = positive.select(vec34(...front.normal), vec34(...back.normal));
1475
+ const n = positive.select(vec35(...front.normal), vec35(...back.normal));
1351
1476
  const right = positive.select(
1352
- vec34(...front.right),
1353
- vec34(...back.right)
1477
+ vec35(...front.right),
1478
+ vec35(...back.right)
1354
1479
  );
1355
- const up = positive.select(vec34(...front.up), vec34(...back.up));
1356
- const face = positive.select(int4(axis * 2), int4(axis * 2 + 1));
1357
- const rect = textureLoad4(
1480
+ const up = positive.select(vec35(...front.up), vec35(...back.up));
1481
+ const face = positive.select(int5(axis * 2), int5(axis * 2 + 1));
1482
+ const rect = textureLoad5(
1358
1483
  rects,
1359
- ivec24(face, int4(attribute3("boxId", "float")))
1484
+ ivec25(face, int5(attribute3("boxId", "float")))
1360
1485
  );
1361
1486
  const uv2 = rect.xy.add(
1362
- vec24(dot3(point, right), dot3(point, up)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
1487
+ vec24(dot4(point, right), dot4(point, up)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
1363
1488
  );
1364
- const texel = ivec24(uv2.mul(descriptor.atlasResolution));
1365
- const captured = textureLoad4(maps.normalDepth, texel);
1489
+ const texel = ivec25(uv2.mul(descriptor.atlasResolution));
1490
+ const captured = textureLoad5(maps.normalDepth, texel);
1366
1491
  const sampled = texture3(maps.baseColor, uv2).level(0);
1367
1492
  const sampledOrm = texture3(maps.orm, uv2).level(0);
1368
- const occupied = textureLoad4(maps.orm, texel).a.greaterThan(0.5);
1493
+ const occupied = textureLoad5(maps.orm, texel).a.greaterThan(0.5);
1369
1494
  const depth = captured.b.mul(65280).add(captured.a.mul(255)).div(65535);
1370
1495
  const tolerance = max4(
1371
- dot3(abs3(right), half).div(rect.z.mul(descriptor.atlasResolution)),
1372
- dot3(abs3(up), half).div(rect.w.mul(descriptor.atlasResolution))
1373
- ).div(dot3(abs3(n), half)).mul(4).add(4 / 65535);
1496
+ dot4(abs3(right), half).div(rect.z.mul(descriptor.atlasResolution)),
1497
+ dot4(abs3(up), half).div(rect.w.mul(descriptor.atlasResolution))
1498
+ ).div(dot4(abs3(n), half)).mul(4).add(4 / 65535);
1374
1499
  If4(
1375
1500
  occupied.and(sampled.a.greaterThanEqual(minimumAlpha)).and(
1376
- abs3(dot3(point, n).sub(float4(1).sub(depth.mul(2)))).lessThanEqual(
1501
+ abs3(dot4(point, n).sub(float4(1).sub(depth.mul(2)))).lessThanEqual(
1377
1502
  tolerance
1378
1503
  )
1379
1504
  ),
@@ -1382,7 +1507,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1382
1507
  const filtered = filtering ? texture3(maps.normalDepth, uv2).level(0).toVar() : captured;
1383
1508
  let weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
1384
1509
  if (filtering) {
1385
- const error = abs3(dot3(point, n).sub(float4(1).sub(
1510
+ const error = abs3(dot4(point, n).sub(float4(1).sub(
1386
1511
  filtered.b.mul(65280).add(filtered.a.mul(255)).div(65535).mul(2)
1387
1512
  )));
1388
1513
  weight = weight.mul(float4(1).sub(error.div(tolerance)).clamp(0, 1));
@@ -1394,10 +1519,10 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1394
1519
  texture3(maps.emissive, uv2).level(0).rgb.div(coverage2).mul(weight)
1395
1520
  );
1396
1521
  let shadingNormal;
1397
- if (filtering) shadingNormal = filteredNormal(uv2, float4(1).sub(dot3(point, n)).mul(0.5), tolerance.mul(0.5), captured, sampled.a, filtered, sampledOrm.a);
1522
+ if (filtering) shadingNormal = filteredNormal(uv2, float4(1).sub(dot4(point, n)).mul(0.5), tolerance.mul(0.5), captured, sampled.a, filtered, sampledOrm.a);
1398
1523
  else {
1399
1524
  const packed = captured.rg.mul(2).sub(1);
1400
- const decoded = vec34(
1525
+ const decoded = vec35(
1401
1526
  packed,
1402
1527
  float4(1).sub(abs3(packed.x)).sub(abs3(packed.y))
1403
1528
  ).toVar();
@@ -1430,9 +1555,10 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1430
1555
  emission = blended.get("emission");
1431
1556
  normal = blended.get("normal");
1432
1557
  }
1433
- material.capturedPositionNode = hit.get("position");
1558
+ const position = meshPoint(hit.get("position")).toVar("boxHitMeshPosition");
1559
+ material.capturedPositionNode = position;
1434
1560
  material.colorNode = color.rgb;
1435
- material.normalNode = transformNormalToView3(normal);
1561
+ material.normalNode = transformNormalToView3(meshNormal(normal));
1436
1562
  material.roughnessNode = surfaceOrm.g;
1437
1563
  material.metalnessNode = surfaceOrm.b;
1438
1564
  material.aoNode = surfaceOrm.r;
@@ -1447,9 +1573,9 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
1447
1573
  }
1448
1574
  material.maskNode = hit.get("valid").greaterThan(0.5);
1449
1575
  material.maskShadowNode = material.maskNode;
1450
- material.depthNode = capturedSurfaceDepth(hit.get("position"));
1576
+ material.depthNode = capturedSurfaceDepth(position);
1451
1577
  material.receivedShadowPositionNode = modelWorldMatrix3.mul(
1452
- vec45(hit.get("position"), 1)
1578
+ vec46(position, 1)
1453
1579
  ).xyz;
1454
1580
  material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
1455
1581
  return material;