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

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 (63) hide show
  1. package/CHANGELOG.md +33 -28
  2. package/README.md +63 -231
  3. package/SKILL.md +11 -68
  4. package/build/box-impostor-runtime.js +652 -257
  5. package/build/box-impostor-runtime.js.map +4 -4
  6. package/build/deformation-runtime.js +1 -1
  7. package/build/deformation-runtime.js.map +1 -1
  8. package/build/hull-effects.js.map +1 -1
  9. package/build/impostor-runtime.js +2 -2
  10. package/build/impostor-runtime.js.map +2 -2
  11. package/build/index.js +2 -2
  12. package/build/index.js.map +2 -2
  13. package/build/internal/box-impostor/box-impostor-display.js +662 -265
  14. package/build/internal/box-impostor/box-impostor-display.js.map +4 -4
  15. package/build/internal/box-impostor/box-impostor-material.js +662 -265
  16. package/build/internal/box-impostor/box-impostor-material.js.map +4 -4
  17. package/build/internal/box-impostor/box-impostor-ray-bounds.js +369 -0
  18. package/build/internal/box-impostor/box-impostor-ray-bounds.js.map +7 -0
  19. package/build/internal/box-impostor/box-impostor-runtime.js +652 -257
  20. package/build/internal/box-impostor/box-impostor-runtime.js.map +4 -4
  21. package/build/internal/deformation/rig-runtime.js +1 -1
  22. package/build/internal/deformation/rig-runtime.js.map +1 -1
  23. package/build/internal/impostor/impostor-node-material.js +2 -2
  24. package/build/internal/impostor/impostor-node-material.js.map +2 -2
  25. package/build/internal/impostor/octahedral.js.map +1 -1
  26. package/build/internal/pixel-impostor/pixel-impostor-display.js +39 -16
  27. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +2 -2
  28. package/build/internal/pixel-impostor/pixel-impostor-metadata.js +61 -6
  29. package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +2 -2
  30. package/build/internal/pixel-impostor/pixel-impostor-runtime.js +980 -26
  31. package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +4 -4
  32. package/build/internal/rendering/hull-effect-runtime.js.map +1 -1
  33. package/build/internal/rendering/renderer-support.js.map +1 -1
  34. package/build/internal/rendering/skin-projection.js.map +1 -1
  35. package/build/internal/rendering/surface-query.js.map +1 -1
  36. package/build/internal/rendering/wrap-preview.js.map +1 -1
  37. package/build/internal/rendering/wrap-skin.js.map +1 -1
  38. package/build/pixel-impostor-runtime.js +362 -172
  39. package/build/pixel-impostor-runtime.js.map +4 -4
  40. package/build/types/box-impostor/box-impostor-material.d.ts +2 -1
  41. package/build/types/box-impostor/box-impostor-ray-bounds.d.ts +6 -0
  42. package/build/types/box-impostor/box-impostor-ray-bounds.js +1 -0
  43. package/build/types/deformation/rig-runtime.d.ts +1 -1
  44. package/build/types/impostor/octahedral.d.ts +1 -1
  45. package/build/types/pixel-impostor/pixel-impostor-display.d.ts +3 -3
  46. package/build/types/pixel-impostor/pixel-impostor-metadata.d.ts +76 -5
  47. package/build/types/pixel-impostor/pixel-impostor-runtime.d.ts +19 -7
  48. package/build/types/public/pixel-impostor-runtime.d.ts +1 -1
  49. package/build/types/rendering/renderer-support.d.ts +1 -1
  50. package/build/types/rendering/surface-query.d.ts +2 -2
  51. package/package.json +2 -2
  52. package/src/box-impostor/box-impostor-material.js +128 -90
  53. package/src/box-impostor/box-impostor-ray-bounds.js +452 -0
  54. package/src/deformation/rig-runtime.ts +1 -1
  55. package/src/impostor/impostor-node-material.js +2 -2
  56. package/src/impostor/octahedral.js +1 -1
  57. package/src/pixel-impostor/pixel-impostor-display.js +32 -18
  58. package/src/pixel-impostor/pixel-impostor-metadata.js +69 -9
  59. package/src/pixel-impostor/pixel-impostor-runtime.js +32 -9
  60. package/src/public/index.js +2 -2
  61. package/src/public/pixel-impostor-runtime.js +1 -1
  62. package/src/rendering/renderer-support.js +1 -1
  63. package/src/rendering/surface-query.js +2 -2
@@ -4,7 +4,10 @@ export const NEEDLE_PIXEL_IMPOSTOR = 'NEEDLE_pixel_impostor';
4
4
 
5
5
  /**
6
6
  * @typedef {object} PixelImpostorDescriptor
7
- * @property {1 | 2 | 3} version
7
+ * @property {1 | 2 | 3 | string} version The integers 1 and 2 (dense storage) and 3 (sparse storage), or
8
+ * `"<major>.<minor>"` from 4.0: either storage, and the primitive can be the volume's box (`voxelShell`).
9
+ * Readers read every minor version of a major version they know, ignoring properties they do not
10
+ * know, and ignore other major versions (older readers ignore every string version).
8
11
  * @property {[number, number, number]} dimensions
9
12
  * @property {number} atlasColumns
10
13
  * @property {number} atlasRows
@@ -17,13 +20,60 @@ export const NEEDLE_PIXEL_IMPOSTOR = 'NEEDLE_pixel_impostor';
17
20
  * @property {number=} surfaceCount
18
21
  * @property {[number, number]=} surfaceAtlasSize
19
22
  * @property {[number, number]=} indexAtlasSize
23
+ * @property {PixelImpostorDisplay=} display How the asset is meant to be shown; loaders given a renderer apply it.
24
+ * @property {false=} voxelShell Version 4.x: `false` when the primitive is the volume's box rather than the
25
+ * voxel shell, written for the `raycast` and `screen-pixels` modes, which trace the volume. Such a
26
+ * file has no `geometry` display, and viewers without the extension draw the box.
20
27
  */
21
28
 
29
+ /**
30
+ * @typedef {object} PixelImpostorDisplay
31
+ * @property {'geometry' | 'vertex-snap' | 'raycast' | 'screen-pixels'} mode `geometry` draws the voxel shell;
32
+ * the other modes are shaders. `raycast` and `screen-pixels` trace the volume and need no shell.
33
+ * @property {number} pixelSize Superpixel size in CSS pixels (`vertex-snap`, `screen-pixels`).
34
+ * @property {number} splatScale Splat size relative to the voxel footprint (`vertex-snap`).
35
+ * @property {'nearest-grid' | 'single-cell' | 'center-only'} snapMode Splat grid mode (`vertex-snap`).
36
+ * @property {boolean} antialias Four rotated subpixel rays per pixel (`raycast`).
37
+ * @property {boolean} pbr Use the baked occlusion, roughness and metallic data.
38
+ * @property {number} projectedNormalMix Strength of the baked object-space normals, 0 to 1.
39
+ */
40
+
41
+ /** @type {Readonly<PixelImpostorDisplay>} */
42
+ export const DEFAULT_PIXEL_IMPOSTOR_DISPLAY = Object.freeze({
43
+ mode: /** @type {PixelImpostorDisplay['mode']} */ ('geometry'),
44
+ pixelSize: 5,
45
+ splatScale: 1,
46
+ snapMode: /** @type {PixelImpostorDisplay['snapMode']} */ ('nearest-grid'),
47
+ antialias: true,
48
+ pbr: true,
49
+ projectedNormalMix: 1,
50
+ });
51
+
52
+ /** Display modes that trace the volume: they draw a box and need no voxel shell. */
53
+ export const PIXEL_IMPOSTOR_VOLUME_MODES = Object.freeze(/** @type {const} */ (['raycast', 'screen-pixels']));
54
+
55
+ /** @param {Partial<PixelImpostorDisplay>} [input] @returns {PixelImpostorDisplay} */
56
+ export function normalizePixelImpostorDisplay(input = {}) {
57
+ const value = { ...DEFAULT_PIXEL_IMPOSTOR_DISPLAY, ...input };
58
+ if (!['geometry', 'vertex-snap', 'raycast', 'screen-pixels'].includes(value.mode)
59
+ || !Number.isInteger(value.pixelSize) || value.pixelSize < 1 || value.pixelSize > 64
60
+ || !Number.isFinite(value.splatScale) || value.splatScale <= 0 || value.splatScale > 4
61
+ || !['nearest-grid', 'single-cell', 'center-only'].includes(value.snapMode)
62
+ || typeof value.antialias !== 'boolean' || typeof value.pbr !== 'boolean'
63
+ || !Number.isFinite(value.projectedNormalMix) || value.projectedNormalMix < 0 || value.projectedNormalMix > 1)
64
+ throw new Error('Invalid pixel impostor display settings.');
65
+ return {
66
+ mode: value.mode, pixelSize: value.pixelSize, splatScale: value.splatScale, snapMode: value.snapMode,
67
+ antialias: value.antialias, pbr: value.pbr, projectedNormalMix: value.projectedNormalMix,
68
+ };
69
+ }
70
+
22
71
  /** @param {unknown} value @returns {PixelImpostorDescriptor | null} */
23
72
  export function parsePixelImpostorDescriptor(value) {
24
73
  if (!value || typeof value !== 'object') return null;
25
74
  const raw = /** @type {Record<string, unknown>} */ (value);
26
- if ((raw.version !== 1 && raw.version !== 2 && raw.version !== 3) || !triplet(raw.dimensions) || !triplet(raw.volumeMin)
75
+ const v4 = typeof raw.version === 'string' && /^4\.(?:0|[1-9][0-9]*)$/.test(raw.version);
76
+ if ((![1, 2, 3].includes(/** @type {number} */ (raw.version)) && !v4) || !triplet(raw.dimensions) || !triplet(raw.volumeMin)
27
77
  || !triplet(raw.volumeSize)) return null;
28
78
  const dimensions = /** @type {number[]} */ (raw.dimensions);
29
79
  const volumeSize = /** @type {number[]} */ (raw.volumeSize);
@@ -36,14 +86,22 @@ export function parsePixelImpostorDescriptor(value) {
36
86
  if (!dimensions.every((item) => Number.isInteger(item) && item > 0)
37
87
  || !volumeSize.every((item) => Number.isFinite(item) && item > 0)
38
88
  || !occupiedSize.every((item) => Number.isFinite(item) && item > 0)
39
- || (raw.version !== 3 && (!Number.isInteger(raw.atlasColumns) || Number(raw.atlasColumns) < 1
40
- || !Number.isInteger(raw.atlasRows) || Number(raw.atlasRows) < 1))) return null;
41
- const sparse = raw.version === 3 && raw.storage === 'sparse';
42
- if (raw.version === 3 && (!sparse
43
- || !Number.isInteger(raw.surfaceCount) || Number(raw.surfaceCount) < 1
44
- || !pair(raw.surfaceAtlasSize) || !pair(raw.indexAtlasSize))) return null;
89
+ || (v4 && raw.storage !== 'sparse' && raw.storage !== 'dense')) return null;
90
+ if (!((raw.version === 3 || v4) && raw.storage === 'sparse')
91
+ && (!Number.isInteger(raw.atlasColumns) || Number(raw.atlasColumns) < 1
92
+ || !Number.isInteger(raw.atlasRows) || Number(raw.atlasRows) < 1)) return null;
93
+ // version 3 is sparse; version 4.x names its storage
94
+ const sparse = (raw.version === 3 || v4) && raw.storage === 'sparse';
95
+ /** @type {PixelImpostorDisplay | null} */
96
+ let display = null;
97
+ try {
98
+ display = raw.display && typeof raw.display === 'object' ? normalizePixelImpostorDisplay(/** @type {Partial<PixelImpostorDisplay>} */ (raw.display)) : null;
99
+ } catch { display = null; } // an unknown display falls back to the reader's default
100
+ if ((raw.version === 3 && !sparse) || (sparse && (
101
+ !Number.isInteger(raw.surfaceCount) || Number(raw.surfaceCount) < 1
102
+ || !pair(raw.surfaceAtlasSize) || !pair(raw.indexAtlasSize)))) return null;
45
103
  return /** @type {PixelImpostorDescriptor} */ ({
46
- version: /** @type {1 | 2 | 3} */ (raw.version),
104
+ version: /** @type {1 | 2 | 3 | string} */ (raw.version),
47
105
  dimensions: [...dimensions],
48
106
  atlasColumns: sparse ? 0 : Number(raw.atlasColumns),
49
107
  atlasRows: sparse ? 0 : Number(raw.atlasRows),
@@ -58,6 +116,8 @@ export function parsePixelImpostorDescriptor(value) {
58
116
  surfaceAtlasSize: [.../** @type {number[]} */ (raw.surfaceAtlasSize)],
59
117
  indexAtlasSize: [.../** @type {number[]} */ (raw.indexAtlasSize)],
60
118
  } : {}),
119
+ ...(display ? { display } : {}),
120
+ ...(v4 && raw.voxelShell === false ? { voxelShell: /** @type {const} */ (false) } : {}),
61
121
  });
62
122
  }
63
123
 
@@ -5,12 +5,14 @@ import { NEEDLE_PIXEL_IMPOSTOR, parsePixelImpostorDescriptor } from './pixel-imp
5
5
 
6
6
  /**
7
7
  * Three.js GLTFLoader plugin for versioned pixel impostor atlas bindings.
8
- * It leaves the portable voxel shell intact. Use `createPixelImpostorDisplay`
9
- * after loading to select the workbench's TSL display modes.
8
+ * Given a renderer, it shows a file's stored display (`display` in the extension) with
9
+ * `createPixelImpostorDisplay`, and keeps the display's handle in the scene's
10
+ * `userData.pixelImpostorDisplay`. Without a renderer, or for files without a stored
11
+ * display, it leaves the file's mesh as it is; call `createPixelImpostorDisplay` to choose a mode.
10
12
  */
11
13
  export class NeedlePixelImpostorLoaderPlugin {
12
- /** @param {unknown} parser */
13
- constructor(parser) { this.name = NEEDLE_PIXEL_IMPOSTOR; this.parser = parser; }
14
+ /** @param {unknown} parser @param {{renderer?: import('../rendering/renderer-support.js').ImpostorRenderer}} [options] */
15
+ constructor(parser, options = {}) { this.name = NEEDLE_PIXEL_IMPOSTOR; this.parser = parser; this.options = options; }
14
16
  /** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
15
17
  async afterRoot(result) {
16
18
  const parser = /** @type {{json:{meshes?:Array<{primitives:Array<{extensions?:Record<string,unknown>}>}>},associations:Map<import('three').Object3D,{meshes?:number,primitives?:number}>,getDependency(type:'texture',index:number):Promise<import('three').Texture>,getDependency(type:'accessor',index:number):Promise<import('three').BufferAttribute>}} */ (this.parser);
@@ -55,7 +57,7 @@ export class NeedlePixelImpostorLoaderPlugin {
55
57
  loadTexture('atlas', SRGBColorSpace, true),
56
58
  loadTexture('normalAtlas', NoColorSpace),
57
59
  loadTexture('ormAtlas', NoColorSpace),
58
- loadTexture('indexAtlas', NoColorSpace, descriptor.version === 3),
60
+ loadTexture('indexAtlas', NoColorSpace, descriptor.storage === 'sparse'),
59
61
  surfacePointsPromise,
60
62
  ]).then(([atlas, normalAtlas, ormAtlas, indexAtlas, surfacePoints]) => {
61
63
  if (!atlas) throw new Error('NEEDLE_pixel_impostor atlas is unavailable.');
@@ -65,14 +67,35 @@ export class NeedlePixelImpostorLoaderPlugin {
65
67
  }));
66
68
  });
67
69
  await Promise.all(pending);
70
+ await this.applyStoredDisplay(result);
71
+ }
72
+ /** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
73
+ async applyStoredDisplay(result) {
74
+ const renderer = this.options.renderer, scene = result.scene;
75
+ if (!renderer || !scene) return;
76
+ /** @type {{descriptor: import('./pixel-impostor-metadata.js').PixelImpostorDescriptor} | null} */
77
+ let binding = null;
78
+ scene.traverse((object) => { if (!binding && object.userData.pixelImpostor?.descriptor?.display) binding = object.userData.pixelImpostor; });
79
+ if (!binding) return;
80
+ const { rendererSupportsImpostorNodeMaterials } = await import('../rendering/renderer-support.js');
81
+ if (!rendererSupportsImpostorNodeMaterials(renderer)) {
82
+ console.warn('[three-mesh-effects] NEEDLE_pixel_impostor displays need WebGPURenderer or a renderer with node-material support; drawing the file\'s mesh.');
83
+ return;
84
+ }
85
+ const { createPixelImpostorDisplay } = await import('./pixel-impostor-display.js');
86
+ const display = createPixelImpostorDisplay(scene, { binding, renderer: /** @type {any} */ (renderer) });
87
+ // the canvas size and pixel ratio can change at any time: read them before each frame
88
+ for (const child of display.preview.children) child.onBeforeRender = () => display.updateViewport();
89
+ scene.userData.pixelImpostorDisplay = display;
68
90
  }
69
91
  }
70
92
 
71
- /** Register pixel atlas loading before calling `loadAsync`; returns the same loader.
93
+ /** Register pixel atlas loading before calling `loadAsync`; returns the same loader. With a
94
+ * renderer, files show their stored display.
72
95
  * @template {import('three/addons/loaders/GLTFLoader.js').GLTFLoader} T
73
- * @param {T} loader @returns {T}
96
+ * @param {T} loader @param {{renderer?: import('../rendering/renderer-support.js').ImpostorRenderer}} [options] @returns {T}
74
97
  */
75
- export function registerNeedlePixelImpostorLoader(loader) {
76
- loader.register((parser) => new NeedlePixelImpostorLoaderPlugin(parser));
98
+ export function registerNeedlePixelImpostorLoader(loader, options = {}) {
99
+ loader.register((parser) => new NeedlePixelImpostorLoaderPlugin(parser, options));
77
100
  return loader;
78
101
  }
@@ -66,9 +66,9 @@ export function useLoader(loader, renderer, options = {}) {
66
66
  const { NeedleImpostorLoaderPlugin } = await import('@needle-tools/three-mesh-effects/impostor-runtime');
67
67
  await new NeedleImpostorLoaderPlugin(parser, { renderer }).afterRoot(result);
68
68
  });
69
- register('NEEDLE_pixel_impostor', async (parser, result) => {
69
+ register('NEEDLE_pixel_impostor', async (parser, result, renderer) => {
70
70
  const { NeedlePixelImpostorLoaderPlugin } = await import('@needle-tools/three-mesh-effects/pixel-impostor-runtime');
71
- await new NeedlePixelImpostorLoaderPlugin(parser).afterRoot(result);
71
+ await new NeedlePixelImpostorLoaderPlugin(parser, { renderer }).afterRoot(result);
72
72
  });
73
73
  register('NEEDLE_hull_effect', async (parser, result) => {
74
74
  const { NeedleHullEffectLoaderPlugin } = await import('@needle-tools/three-mesh-effects/hull-effects');
@@ -1,5 +1,5 @@
1
1
  // @ts-check
2
2
 
3
3
  export { NeedlePixelImpostorLoaderPlugin, registerNeedlePixelImpostorLoader } from '../pixel-impostor/pixel-impostor-runtime.js';
4
- export { NEEDLE_PIXEL_IMPOSTOR, parsePixelImpostorDescriptor } from '../pixel-impostor/pixel-impostor-metadata.js';
4
+ export { NEEDLE_PIXEL_IMPOSTOR, parsePixelImpostorDescriptor, normalizePixelImpostorDisplay, DEFAULT_PIXEL_IMPOSTOR_DISPLAY, PIXEL_IMPOSTOR_VOLUME_MODES } from '../pixel-impostor/pixel-impostor-metadata.js';
5
5
  export { createPixelImpostorDisplay, createPixelImpostorRaycastPreview } from '../pixel-impostor/pixel-impostor-display.js';
@@ -12,7 +12,7 @@ import {classicWebGLSupportsNodeMaterials} from './classic-webgl-support.js';
12
12
  */
13
13
 
14
14
  /**
15
- * Reports whether a renderer can compile Baker's TSL impostor material.
15
+ * Reports whether a renderer can compile TSL impostor materials.
16
16
  * WebGPURenderer supports node materials directly. Classic WebGLRenderer
17
17
  * integrations must advertise that they installed a compatible nodes handler.
18
18
  *
@@ -32,8 +32,8 @@ export function createSurfaceQuery(mesh, options = {}) {
32
32
  }
33
33
 
34
34
  /**
35
- * Wraps a prebuilt BVH with the allocation-reusing query API used by the wrap skin preview and baker. The geometry and BVH must use indirect indexing so face IDs stay
36
- * aligned with the source triangle arrays.
35
+ * Reuse allocations when querying a prebuilt BVH. Indirect indexing keeps BVH
36
+ * face IDs aligned with the source triangle arrays.
37
37
  *
38
38
  * @param {BufferGeometry} geometry
39
39
  * @param {MeshBVH} bvh