@needle-tools/three-mesh-effects 0.3.3 → 0.3.5

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 (47) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +3 -2
  3. package/build/box-impostor-runtime.js +27 -2
  4. package/build/box-impostor-runtime.js.map +3 -3
  5. package/build/deformation-runtime.js +11 -9
  6. package/build/deformation-runtime.js.map +2 -2
  7. package/build/impostor-runtime.js +41 -19
  8. package/build/impostor-runtime.js.map +3 -3
  9. package/build/internal/box-impostor/box-impostor-display.js +10 -2
  10. package/build/internal/box-impostor/box-impostor-display.js.map +2 -2
  11. package/build/internal/box-impostor/box-impostor-faces.js.map +1 -1
  12. package/build/internal/box-impostor/box-impostor-geometry.js +4 -0
  13. package/build/internal/box-impostor/box-impostor-geometry.js.map +2 -2
  14. package/build/internal/box-impostor/box-impostor-material.js +10 -2
  15. package/build/internal/box-impostor/box-impostor-material.js.map +2 -2
  16. package/build/internal/box-impostor/box-impostor-metadata.js +4 -0
  17. package/build/internal/box-impostor/box-impostor-metadata.js.map +2 -2
  18. package/build/internal/box-impostor/box-impostor-projection.js.map +1 -1
  19. package/build/internal/box-impostor/box-impostor-runtime.js +27 -2
  20. package/build/internal/box-impostor/box-impostor-runtime.js.map +3 -3
  21. package/build/internal/deformation/pin-ik.js +11 -9
  22. package/build/internal/deformation/pin-ik.js.map +2 -2
  23. package/build/internal/deformation/pin-wiggle.js.map +1 -1
  24. package/build/internal/deformation/rig-extension.js.map +1 -1
  25. package/build/internal/deformation/rig-runtime.js +11 -9
  26. package/build/internal/deformation/rig-runtime.js.map +2 -2
  27. package/build/internal/impostor/impostor-node-material.js +41 -19
  28. package/build/internal/impostor/impostor-node-material.js.map +3 -3
  29. package/build/internal/pixel-impostor/pixel-impostor-display.js +9 -7
  30. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +2 -2
  31. package/build/internal/rendering/classic-webgl-support.js +12 -0
  32. package/build/internal/rendering/classic-webgl-support.js.map +7 -0
  33. package/build/internal/rendering/renderer-support.js +13 -0
  34. package/build/internal/rendering/renderer-support.js.map +3 -3
  35. package/build/pixel-impostor-runtime.js +9 -7
  36. package/build/pixel-impostor-runtime.js.map +2 -2
  37. package/build/types/box-impostor/box-impostor-metadata.d.ts +3 -1
  38. package/build/types/rendering/classic-webgl-support.d.ts +9 -0
  39. package/build/types/rendering/classic-webgl-support.js +1 -0
  40. package/package.json +2 -2
  41. package/src/box-impostor/box-impostor-material.js +10 -2
  42. package/src/box-impostor/box-impostor-metadata.js +5 -1
  43. package/src/deformation/pin-ik.ts +13 -9
  44. package/src/impostor/impostor-node-material.js +31 -15
  45. package/src/pixel-impostor/pixel-impostor-display.js +18 -7
  46. package/src/rendering/classic-webgl-support.js +15 -0
  47. package/src/rendering/renderer-support.js +5 -0
@@ -669,9 +669,11 @@ export function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures =
669
669
  // though its resolved alpha is below the authored 0.5 contour. Reprojection
670
670
  // must retain that depth candidate; the SDF and final alpha-to-coverage pass
671
671
  // still decide whether the pixel contributes to the visible silhouette.
672
+ // A literal, not a node expression: Three r186 caches a shared expression in
673
+ // the first branch that reads it, so other branches compared against 0.
672
674
  const reprojectionSurfaceCutoff = descriptor.coverageEncoding === 'sdf-base-alpha'
673
675
  ? float(0.5)
674
- : min(captureAlphaCutoff, float(0.25));
676
+ : float(Math.min(Number(descriptor.captureAlphaCutoff), 0.25));
675
677
  const blendCutoff = float((1 - profile.blendZone) * 0.5);
676
678
  const coarseSearchSteps = profile.raySamples;
677
679
  const fullSphere = descriptor.mapping === 'full';
@@ -1293,22 +1295,36 @@ export function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures =
1293
1295
  };
1294
1296
  // Share the expensive frame search across the three selected captures.
1295
1297
  // Resolve uniform-array bounds at the call site for both TSL backends.
1296
- const sampleFrame = Fn(buildFrameSample).setLayout({
1297
- name: withReprojection ? 'sampleReprojectedImpostorFrame' : 'samplePlanarImpostorFrame',
1298
- type: 'ImpostorFrameSample',
1299
- inputs: [
1300
- { name: 'frame', type: 'vec2' },
1301
- { name: 'planePosition', type: 'vec3' },
1302
- { name: 'rayDirection', type: 'vec3' },
1303
- { name: 'bounds', type: 'vec4' },
1304
- ],
1305
- });
1306
- return (frame, planePosition, rayDirection) => sampleFrame(frame, planePosition, rayDirection,
1307
- viewBounds.element(int(frame.y.mul(frames).add(frame.x))));
1298
+ // Three reuses a layout function's generated code for every builder of a
1299
+ // backend, but this one reads the atlases from its closure: a second
1300
+ // builder, such as the shadow pass, would get code naming texture bindings
1301
+ // it never declared. One function per builder still shares it per shader.
1302
+ /** @type {WeakMap<object, ReturnType<typeof Fn>>} */
1303
+ const functions = new WeakMap();
1304
+ return (/** @type {object} */ builder) => {
1305
+ let sampleFrame = functions.get(builder);
1306
+ if (!sampleFrame) {
1307
+ sampleFrame = Fn(buildFrameSample).setLayout({
1308
+ name: withReprojection ? 'sampleReprojectedImpostorFrame' : 'samplePlanarImpostorFrame',
1309
+ type: 'ImpostorFrameSample',
1310
+ inputs: [
1311
+ { name: 'frame', type: 'vec2' },
1312
+ { name: 'planePosition', type: 'vec3' },
1313
+ { name: 'rayDirection', type: 'vec3' },
1314
+ { name: 'bounds', type: 'vec4' },
1315
+ ],
1316
+ });
1317
+ functions.set(builder, sampleFrame);
1318
+ }
1319
+ return (frame, planePosition, rayDirection) => sampleFrame(frame, planePosition, rayDirection,
1320
+ viewBounds.element(int(frame.y.mul(frames).add(frame.x))));
1321
+ };
1308
1322
  };
1309
- const samplePlanarFrame = createFrameSampler(false);
1310
- const sampleReprojectedFrame = features.reprojection ? createFrameSampler(true) : null;
1323
+ const planarFrameSamplers = createFrameSampler(false);
1324
+ const reprojectedFrameSamplers = features.reprojection ? createFrameSampler(true) : null;
1311
1325
  const buildBlendedSample = ([], builder) => {
1326
+ const samplePlanarFrame = planarFrameSamplers(builder);
1327
+ const sampleReprojectedFrame = reprojectedFrameSamplers ? reprojectedFrameSamplers(builder) : null;
1312
1328
  // An upper-hemisphere atlas has no authored views below the horizon.
1313
1329
  // Project both frame selection and depth rays onto that horizon together;
1314
1330
  // clamping selection alone while retaining a below-ground ray makes the
@@ -39,6 +39,7 @@ import {
39
39
  transformNormalToView,
40
40
  uint,
41
41
  uniform,
42
+ uvec2,
42
43
  uvec4,
43
44
  uv,
44
45
  vec2,
@@ -146,9 +147,16 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
146
147
  mod(index, dimensions[0]).add(0.5).div(dimensions[0]),
147
148
  index.div(dimensions[0]).floor().add(0.5).div(dimensions[1]),
148
149
  );
150
+ // The ray march reads these atlases texel by texel. Explicit integer loads
151
+ // avoid a nearest-filtered sample's shared size lookup: before r187, Three
152
+ // computes it once inside the first trace loop, after the loop's early exit,
153
+ // and later traces read it unassigned when that first ray leaves early.
154
+ // Same texel a nearest sample selects: floor(uv * the texture's own size).
155
+ const loadTexel = (/** @type {THREE.Texture} */ map, /** @type {import('three/webgpu').Node<'vec2'>} */ atlasUv) =>
156
+ texture(map).load(uvec2(atlasUv.mul(vec2(Number(map.image.width), Number(map.image.height))).floor()));
149
157
  const sparseUint = (/** @type {import('three/webgpu').Node<'float'>} */ texel) => {
150
158
  if (!indexAtlas) throw new Error('Sparse pixel impostor requires an index atlas');
151
- const bytes = uvec4(texture(indexAtlas, linearTextureUv(texel, indexAtlasSize)).mul(255).round());
159
+ const bytes = uvec4(loadTexel(indexAtlas, linearTextureUv(texel, indexAtlasSize)).mul(255).round());
152
160
  return bytes.x
153
161
  .bitOr(bytes.y.shiftLeft(8))
154
162
  .bitOr(bytes.z.shiftLeft(16))
@@ -190,14 +198,17 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
190
198
  // its invalid slab entry into the voxel traversal (which drew box lines).
191
199
  const cell = exitDistance.greaterThanEqual(nearestDistance)
192
200
  .select(gridPoint.floor(), vec3(-1)).toVar();
193
- const directionStep = step(0, gridVelocity).mul(2).sub(1);
201
+ // Declared before the loop: Three r186 caches a sub-expression shared by
202
+ // the per-axis branches below into a variable assigned only inside the
203
+ // first branch that uses it, so the z step read a stale value.
204
+ const directionStep = step(0, gridVelocity).mul(2).sub(1).toVar();
194
205
  const absoluteVelocity = abs(gridVelocity).max(1e-6);
195
206
  const cellFraction = gridPoint.sub(cell);
196
207
  const positiveAxis = step(0, gridVelocity);
197
208
  const nextDistance = cellFraction.mul(vec3(1).sub(positiveAxis))
198
209
  .add(vec3(1).sub(cellFraction).mul(positiveAxis));
199
210
  const nextCrossing = nextDistance.div(absoluteVelocity).toVar();
200
- const crossingDelta = vec3(1).div(absoluteVelocity);
211
+ const crossingDelta = vec3(1).div(absoluteVelocity).toVar();
201
212
  // xyz is the reconstructed local hit; w is the linear cell index + 1.
202
213
  // Keeping both in one vec4 avoids rebuilding the camera ray after traversal.
203
214
  const result = vec4(0, 0, 0, 0).toVar();
@@ -222,7 +233,7 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
222
233
  atlasColumn.mul(planar).add(cell.x).add(0.5).div(atlasColumns * planar),
223
234
  atlasRow.mul(planar).add(cell.z).add(0.5).div(atlasRows * planar),
224
235
  );
225
- cellOccupied = texture(atlas, atlasUv).a.greaterThan(0.5);
236
+ cellOccupied = loadTexel(atlas, atlasUv).a.greaterThan(0.5);
226
237
  }
227
238
  If(cellOccupied, () => {
228
239
  result.assign(vec4(
@@ -307,8 +318,8 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
307
318
  hitAtlasRow.mul(planar).add(hitCell.z).add(0.5).div(atlasRows * planar),
308
319
  );
309
320
  }
310
- const baseSample = texture(atlas, hitAtlasUv);
311
- const ormSample = ormAtlas ? texture(ormAtlas, hitAtlasUv) : vec4(1, 1, 0, 1);
321
+ const baseSample = loadTexel(atlas, hitAtlasUv);
322
+ const ormSample = ormAtlas ? loadTexel(ormAtlas, hitAtlasUv) : vec4(1, 1, 0, 1);
312
323
  const hitPosition = hit.xyz;
313
324
  const hitToCamera = modelWorldMatrixInverse.mul(vec4(cameraPosition, 1)).xyz.sub(hitPosition);
314
325
  const faceSign = step(0, hitToCamera).mul(2).sub(1);
@@ -333,7 +344,7 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
333
344
  .select(vec3(faceSign.x, 0, 0), yFaceScore.greaterThanEqual(zFaceScore)
334
345
  .select(vec3(0, faceSign.y, 0), vec3(0, 0, faceSign.z)));
335
346
  const sampledNormal = normalAtlas
336
- ? texture(normalAtlas, hitAtlasUv).xyz.mul(2).sub(1)
347
+ ? loadTexel(normalAtlas, hitAtlasUv).xyz.mul(2).sub(1)
337
348
  : geometricNormal;
338
349
  const bakedNormal = sampledNormal.dot(sampledNormal).greaterThan(1e-8)
339
350
  .select(sampledNormal.normalize(), geometricNormal);
@@ -0,0 +1,15 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * Reports whether WebGLRenderer from a three revision can keep many node
5
+ * materials alive. Before r187 every uniform block holds one of a few binding
6
+ * points until its material is disposed, so node materials stop drawing after
7
+ * about a dozen. The Needle fork backports the fix from 0.186.2-alpha.2.
8
+ *
9
+ * @param {string} revision three's `REVISION`, such as `'187'` or `'186.2-alpha.2'`
10
+ */
11
+ export function classicWebGLSupportsNodeMaterials(revision) {
12
+ const fork = /^186\.2-alpha\.(\d+)$/.exec(revision);
13
+ if (fork) return Number(fork[1]) >= 2;
14
+ return Number.parseInt(revision, 10) >= 187;
15
+ }
@@ -1,5 +1,7 @@
1
1
  // @ts-check
2
+ import {REVISION} from 'three';
2
3
  import * as TSL from 'three/tsl';
4
+ import {classicWebGLSupportsNodeMaterials} from './classic-webgl-support.js';
3
5
 
4
6
  /**
5
7
  * @typedef {object} ImpostorRenderer
@@ -36,6 +38,9 @@ export async function enableRendererNodeMaterials(renderer) {
36
38
  if (!setup) {
37
39
  setup = import('three/addons/tsl/WebGLNodesHandler.js').then(({WebGLNodesHandler}) => {
38
40
  if (rendererSupportsImpostorNodeMaterials(renderer)) return;
41
+ if (!classicWebGLSupportsNodeMaterials(REVISION)) {
42
+ console.warn(`Mesh Effects: WebGLRenderer from three r${REVISION} stops drawing node materials after about a dozen are in use. Use three r187 or later, or WebGPURenderer.`);
43
+ }
39
44
  renderer.setNodesHandler?.(new WebGLNodesHandler());
40
45
  renderer.capabilities ??= {};
41
46
  renderer.capabilities.supportsNodeMaterials = true;