@needle-tools/three-mesh-effects 0.2.3 → 0.3.1

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 (92) hide show
  1. package/CHANGELOG.md +25 -8
  2. package/README.md +68 -1
  3. package/SKILL.md +9 -1
  4. package/build/box-impostor-runtime.js +1298 -0
  5. package/build/box-impostor-runtime.js.map +7 -0
  6. package/build/deformation-runtime.js +30 -19
  7. package/build/deformation-runtime.js.map +3 -3
  8. package/build/hull-effects.js +60 -28
  9. package/build/hull-effects.js.map +3 -3
  10. package/build/impostor-runtime.js +152 -87
  11. package/build/impostor-runtime.js.map +3 -3
  12. package/build/index.js +4 -0
  13. package/build/index.js.map +2 -2
  14. package/build/internal/box-impostor/box-impostor-display-settings.js +50 -0
  15. package/build/internal/box-impostor/box-impostor-display-settings.js.map +7 -0
  16. package/build/internal/box-impostor/box-impostor-display.js +1005 -0
  17. package/build/internal/box-impostor/box-impostor-display.js.map +7 -0
  18. package/build/internal/box-impostor/box-impostor-face-axes.js +15 -0
  19. package/build/internal/box-impostor/box-impostor-face-axes.js.map +7 -0
  20. package/build/internal/box-impostor/box-impostor-faces.js +125 -0
  21. package/build/internal/box-impostor/box-impostor-faces.js.map +7 -0
  22. package/build/internal/box-impostor/box-impostor-geometry.js +230 -0
  23. package/build/internal/box-impostor/box-impostor-geometry.js.map +7 -0
  24. package/build/internal/box-impostor/box-impostor-material.js +964 -0
  25. package/build/internal/box-impostor/box-impostor-material.js.map +7 -0
  26. package/build/internal/box-impostor/box-impostor-metadata.js +108 -0
  27. package/build/internal/box-impostor/box-impostor-metadata.js.map +7 -0
  28. package/build/internal/box-impostor/box-impostor-projection.js +256 -0
  29. package/build/internal/box-impostor/box-impostor-projection.js.map +7 -0
  30. package/build/internal/box-impostor/box-impostor-runtime.js +1252 -0
  31. package/build/internal/box-impostor/box-impostor-runtime.js.map +7 -0
  32. package/build/internal/box-impostor/box-impostor-surface-search.js +21 -0
  33. package/build/internal/box-impostor/box-impostor-surface-search.js.map +7 -0
  34. package/build/internal/deformation/cloth-surface.js +23 -17
  35. package/build/internal/deformation/cloth-surface.js.map +3 -3
  36. package/build/internal/deformation/pin-cloth.js +7 -2
  37. package/build/internal/deformation/pin-cloth.js.map +2 -2
  38. package/build/internal/deformation/rig-runtime.js +30 -19
  39. package/build/internal/deformation/rig-runtime.js.map +3 -3
  40. package/build/internal/impostor/impostor-node-material.js +152 -87
  41. package/build/internal/impostor/impostor-node-material.js.map +3 -3
  42. package/build/internal/rendering/hull-effect-runtime.js +59 -27
  43. package/build/internal/rendering/hull-effect-runtime.js.map +3 -3
  44. package/build/internal/rendering/wireframe.js +41 -9
  45. package/build/internal/rendering/wireframe.js.map +2 -2
  46. package/build/internal/rendering/wrap-preview.js +59 -27
  47. package/build/internal/rendering/wrap-preview.js.map +3 -3
  48. package/build/types/box-impostor/box-impostor-display-settings.d.ts +30 -0
  49. package/build/types/box-impostor/box-impostor-display-settings.js +1 -0
  50. package/build/types/box-impostor/box-impostor-display.d.ts +5 -0
  51. package/build/types/box-impostor/box-impostor-display.js +1 -0
  52. package/build/types/box-impostor/box-impostor-face-axes.d.ts +5 -0
  53. package/build/types/box-impostor/box-impostor-face-axes.js +1 -0
  54. package/build/types/box-impostor/box-impostor-faces.d.ts +11 -0
  55. package/build/types/box-impostor/box-impostor-faces.js +1 -0
  56. package/build/types/box-impostor/box-impostor-geometry.d.ts +5 -0
  57. package/build/types/box-impostor/box-impostor-geometry.js +1 -0
  58. package/build/types/box-impostor/box-impostor-material.d.ts +10 -0
  59. package/build/types/box-impostor/box-impostor-material.js +1 -0
  60. package/build/types/box-impostor/box-impostor-metadata.d.ts +31 -0
  61. package/build/types/box-impostor/box-impostor-metadata.js +1 -0
  62. package/build/types/box-impostor/box-impostor-projection.d.ts +19 -0
  63. package/build/types/box-impostor/box-impostor-projection.js +1 -0
  64. package/build/types/box-impostor/box-impostor-runtime.d.ts +25 -0
  65. package/build/types/box-impostor/box-impostor-runtime.js +1 -0
  66. package/build/types/box-impostor/box-impostor-surface-search.d.ts +13 -0
  67. package/build/types/box-impostor/box-impostor-surface-search.js +1 -0
  68. package/build/types/deformation/cloth-surface.d.ts +1 -0
  69. package/build/types/deformation/pin-cloth.d.ts +1 -0
  70. package/build/types/public/box-impostor-runtime.d.ts +6 -0
  71. package/build/types/public/box-impostor-runtime.js +1 -0
  72. package/build/types/public/hull-effects.d.ts +1 -0
  73. package/build/types/public/index.d.ts +1 -1
  74. package/build/types/rendering/wireframe.d.ts +2 -2
  75. package/package.json +27 -6
  76. package/src/box-impostor/box-impostor-display-settings.js +54 -0
  77. package/src/box-impostor/box-impostor-display.js +38 -0
  78. package/src/box-impostor/box-impostor-face-axes.js +11 -0
  79. package/src/box-impostor/box-impostor-faces.js +118 -0
  80. package/src/box-impostor/box-impostor-geometry.js +140 -0
  81. package/src/box-impostor/box-impostor-material.js +721 -0
  82. package/src/box-impostor/box-impostor-metadata.js +122 -0
  83. package/src/box-impostor/box-impostor-projection.js +287 -0
  84. package/src/box-impostor/box-impostor-runtime.js +122 -0
  85. package/src/box-impostor/box-impostor-surface-search.js +11 -0
  86. package/src/deformation/cloth-surface.ts +28 -17
  87. package/src/deformation/pin-cloth.ts +9 -3
  88. package/src/impostor/impostor-node-material.js +177 -117
  89. package/src/public/box-impostor-runtime.js +14 -0
  90. package/src/public/hull-effects.js +1 -0
  91. package/src/public/index.js +5 -1
  92. package/src/rendering/wireframe.js +51 -13
@@ -0,0 +1,30 @@
1
+ /** @param {Partial<BoxImpostorDisplay>} [input] @returns {BoxImpostorDisplay} */
2
+ export function normalizeBoxImpostorDisplay(input?: Partial<BoxImpostorDisplay>): BoxImpostorDisplay;
3
+ /** @param {{alphaMode?:'opaque'|'mask'|'blend'}} descriptor @param {BoxImpostorDisplay} settings */
4
+ export function boxTransparency(descriptor: {
5
+ alphaMode?: "opaque" | "mask" | "blend";
6
+ }, settings: BoxImpostorDisplay): "cutout" | "dithered" | "blended";
7
+ /** Auto only uses planes when every capture is flat within its stored depth bound.
8
+ * Captures without a flatness bound use depth reconstruction.
9
+ * @param {{boxes:{planes?:number[],planeErrors?:number[]}[]}} descriptor @param {BoxImpostorDisplay} settings */
10
+ export function boxDepthMode(descriptor: {
11
+ boxes: {
12
+ planes?: number[];
13
+ planeErrors?: number[];
14
+ }[];
15
+ }, settings: BoxImpostorDisplay): "projection" | "depth";
16
+ /** @typedef {{depthMode:'auto'|'projection'|'depth',raySteps:8|16|32,alphaCutoff:number,interpolation:'none'|'linear'|'smooth',transparency:'auto'|'cutout'|'dithered'|'blended'}} BoxImpostorDisplay */
17
+ export const DEFAULT_BOX_IMPOSTOR_DISPLAY: Readonly<{
18
+ depthMode: "auto" | "projection" | "depth";
19
+ raySteps: 8 | 16 | 32;
20
+ alphaCutoff: 0.5;
21
+ interpolation: "none" | "linear" | "smooth";
22
+ transparency: "auto" | "cutout" | "dithered" | "blended";
23
+ }>;
24
+ export type BoxImpostorDisplay = {
25
+ depthMode: "auto" | "projection" | "depth";
26
+ raySteps: 8 | 16 | 32;
27
+ alphaCutoff: number;
28
+ interpolation: "none" | "linear" | "smooth";
29
+ transparency: "auto" | "cutout" | "dithered" | "blended";
30
+ };
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-display-settings.js';
@@ -0,0 +1,5 @@
1
+ /** Changes display without touching captured textures. Expensive settings select
2
+ * cached shader graphs; alpha cutoff updates a uniform. Geometry owns the cache.
3
+ * @param {import('three').Object3D} root @param {Partial<BoxImpostorDisplay>} options */
4
+ export function setBoxImpostorDisplay(root: import("three").Object3D, options: Partial<BoxImpostorDisplay>): void;
5
+ export type BoxImpostorDisplay = import("./box-impostor-display-settings.js").BoxImpostorDisplay;
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-display.js';
@@ -0,0 +1,5 @@
1
+ export const BOX_FACE_AXES: readonly {
2
+ normal: number[];
3
+ right: number[];
4
+ up: number[];
5
+ }[];
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-face-axes.js';
@@ -0,0 +1,11 @@
1
+ /** Rasterization resolves fitted faces without a fragment ray or depth override. */
2
+ export function applyBoxFaces({ material, descriptor, maps, alphaCutoff, transparency, half, toBox, fromBox, }: {
3
+ material: any;
4
+ descriptor: any;
5
+ maps: any;
6
+ alphaCutoff: any;
7
+ transparency: any;
8
+ half: any;
9
+ toBox: any;
10
+ fromBox: any;
11
+ }): any;
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-faces.js';
@@ -0,0 +1,5 @@
1
+ /** Portable six-face boxes (or a front card) with atlas UVs. Runtime attributes
2
+ * repeat each box's transform so all boxes can use a single material and draw.
3
+ * @param {import('./box-impostor-metadata.js').BoxImpostorDescriptor} input */
4
+ export function createBoxImpostorGeometry(input: import("./box-impostor-metadata.js").BoxImpostorDescriptor): BufferGeometry<import("three").NormalBufferAttributes, import("three").BufferGeometryEventMap>;
5
+ import { BufferGeometry } from 'three';
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-geometry.js';
@@ -0,0 +1,10 @@
1
+ /** Six orthographic depth fields, one compiled ray budget and one shared PBR
2
+ * material. The same graph runs on native WebGPU and Three's WebGL2 backend.
3
+ * @param {import('./box-impostor-metadata.js').BoxImpostorDescriptor} descriptor
4
+ * @param {{baseColor:import('three').Texture,normalDepth:import('three').Texture,orm:import('three').Texture,emissive:import('three').Texture}} maps */
5
+ export function createBoxImpostorMaterial(descriptor: import("./box-impostor-metadata.js").BoxImpostorDescriptor, maps: {
6
+ baseColor: import("three").Texture;
7
+ normalDepth: import("three").Texture;
8
+ orm: import("three").Texture;
9
+ emissive: import("three").Texture;
10
+ }): any;
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-material.js';
@@ -0,0 +1,31 @@
1
+ /** @typedef {{center:number[],axes:number[][],halfSize:number[],views:number[][],planes?:number[],planeErrors?:number[],depthBounds?:number[],occupancy?:number[]}} ImpostorBox */
2
+ /** @typedef {{version:1,depthMode?:'auto'|'projection'|'depth',alphaMode?:'opaque'|'mask'|'blend',interpolation?:'none'|'linear'|'smooth',transparency?:'auto'|'cutout'|'dithered'|'blended',mode:'basic'|'cards'|'fitted',atlasResolution:number,padding:number,raySteps:8|16|32,alphaCutoff:number,boxes:ImpostorBox[]}} BoxImpostorDescriptor */
3
+ /** @param {unknown} raw @returns {BoxImpostorDescriptor} */
4
+ export function parseBoxImpostorDescriptor(raw: unknown): BoxImpostorDescriptor;
5
+ export const NEEDLE_BOX_IMPOSTOR: "NEEDLE_box_impostor";
6
+ export const BOX_OCCUPANCY_RESOLUTION: 8;
7
+ export const BOX_OCCUPANCY_WORDS: number;
8
+ export { BOX_FACE_AXES } from "./box-impostor-face-axes.js";
9
+ export type ImpostorBox = {
10
+ center: number[];
11
+ axes: number[][];
12
+ halfSize: number[];
13
+ views: number[][];
14
+ planes?: number[];
15
+ planeErrors?: number[];
16
+ depthBounds?: number[];
17
+ occupancy?: number[];
18
+ };
19
+ export type BoxImpostorDescriptor = {
20
+ version: 1;
21
+ depthMode?: "auto" | "projection" | "depth";
22
+ alphaMode?: "opaque" | "mask" | "blend";
23
+ interpolation?: "none" | "linear" | "smooth";
24
+ transparency?: "auto" | "cutout" | "dithered" | "blended";
25
+ mode: "basic" | "cards" | "fitted";
26
+ atlasResolution: number;
27
+ padding: number;
28
+ raySteps: 8 | 16 | 32;
29
+ alphaCutoff: number;
30
+ boxes: ImpostorBox[];
31
+ };
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-metadata.js';
@@ -0,0 +1,19 @@
1
+ /** A ray/plane division supplies each UV directly. This graph deliberately has
2
+ * no depth search, refinement or depth-consistency texture fetches. */
3
+ export function applyBoxProjection({ material, descriptor, maps, settings, transparency, alphaCutoff, rects, center, half, toBox, fromBox, eye, viewAxis, perspective, raster, }: {
4
+ material: any;
5
+ descriptor: any;
6
+ maps: any;
7
+ settings: any;
8
+ transparency: any;
9
+ alphaCutoff: any;
10
+ rects: any;
11
+ center: any;
12
+ half: any;
13
+ toBox: any;
14
+ fromBox: any;
15
+ eye: any;
16
+ viewAxis: any;
17
+ perspective: any;
18
+ raster: any;
19
+ }): any;
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-projection.js';
@@ -0,0 +1,25 @@
1
+ /** @template {import('three/addons/loaders/GLTFLoader.js').GLTFLoader} T
2
+ * @param {T} loader @param {{renderer?:{isWebGPURenderer?:boolean}}} [options] @returns {T} */
3
+ export function registerNeedleBoxImpostorLoader<T extends import("three/addons/loaders/GLTFLoader.js").GLTFLoader>(loader: T, options?: {
4
+ renderer?: {
5
+ isWebGPURenderer?: boolean;
6
+ };
7
+ }): T;
8
+ export class NeedleBoxImpostorLoaderPlugin {
9
+ /** @param {import('three/addons/loaders/GLTFLoader.js').GLTFParser} parser
10
+ * @param {{renderer?:{isWebGPURenderer?:boolean}}} [options] */
11
+ constructor(parser: import("three/addons/loaders/GLTFLoader.js").GLTFParser, options?: {
12
+ renderer?: {
13
+ isWebGPURenderer?: boolean;
14
+ };
15
+ });
16
+ name: string;
17
+ parser: import("three/addons/loaders/GLTFLoader.js").GLTFParser;
18
+ options: {
19
+ renderer?: {
20
+ isWebGPURenderer?: boolean;
21
+ };
22
+ };
23
+ /** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
24
+ afterRoot(result: import("three/addons/loaders/GLTFLoader.js").GLTF): Promise<void>;
25
+ }
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-runtime.js';
@@ -0,0 +1,13 @@
1
+ export function refineSurfaceCrossing({ float, If, Loop }: {
2
+ float: any;
3
+ If: any;
4
+ Loop: any;
5
+ }, { lo, hi, loValue, hiValue, height, steps, secant }: {
6
+ lo: any;
7
+ hi: any;
8
+ loValue: any;
9
+ hiValue: any;
10
+ height: any;
11
+ steps: any;
12
+ secant: any;
13
+ }): any;
@@ -0,0 +1 @@
1
+ export * from '../../../src/box-impostor/box-impostor-surface-search.js';
@@ -31,6 +31,7 @@ export declare class ClothSurface {
31
31
  readonly edges: Edge[];
32
32
  readonly scale: number;
33
33
  private time;
34
+ private accumulator;
34
35
  constructor(restVertices: Float32Array, indices: ArrayLike<number>, pins: readonly PinRecord[]);
35
36
  get movableCount(): number;
36
37
  reset(): void;
@@ -20,6 +20,7 @@ export declare class PinCloth {
20
20
  private particles;
21
21
  private layout;
22
22
  private windTime;
23
+ private accumulator;
23
24
  reset(): void;
24
25
  step(pins: readonly PinRecord[], parents: PinParents, seconds: number, links: readonly ClothLink[], colliders?: readonly ClothCollider[], windStrength?: number): {
25
26
  pins: readonly PinRecord[];
@@ -0,0 +1,6 @@
1
+ export { createBoxImpostorGeometry } from "../box-impostor/box-impostor-geometry.js";
2
+ export { createBoxImpostorMaterial } from "../box-impostor/box-impostor-material.js";
3
+ export { setBoxImpostorDisplay } from "../box-impostor/box-impostor-display.js";
4
+ export { NEEDLE_BOX_IMPOSTOR, parseBoxImpostorDescriptor } from "../box-impostor/box-impostor-metadata.js";
5
+ export { NeedleBoxImpostorLoaderPlugin, registerNeedleBoxImpostorLoader } from "../box-impostor/box-impostor-runtime.js";
6
+ export { DEFAULT_BOX_IMPOSTOR_DISPLAY, normalizeBoxImpostorDisplay } from "../box-impostor/box-impostor-display-settings.js";
@@ -0,0 +1 @@
1
+ export * from '../../../src/public/box-impostor-runtime.js';
@@ -1,4 +1,5 @@
1
1
  export { captureWrapSkinPose } from "../rendering/wrap-skin.js";
2
+ export type WireframeBlendMode = import("../rendering/wireframe.js").WireframeBlendMode;
2
3
  export { bindHull, getHullEffect, NeedleHullEffectLoaderPlugin } from "../rendering/hull-effect-runtime.js";
3
4
  export { NEEDLE_HULL_EFFECT, parseHullEffectDescriptor } from "../rendering/hull-effect-metadata.js";
4
5
  export { createHullEffect, prepareIceNormalAttributes, prepareAuraGeometry, prepareRayGeometry } from "../rendering/wrap-preview.js";
@@ -17,7 +17,7 @@ export type GLTFLoader = import("three/addons/loaders/GLTFLoader.js").GLTFLoader
17
17
  export type GLTFParser = import("three/addons/loaders/GLTFLoader.js").GLTFParser;
18
18
  export type GLTF = import("three/addons/loaders/GLTFLoader.js").GLTF;
19
19
  export type Renderer = import("../impostor/impostor-node-material.js").ImpostorRenderer;
20
- export type NeedleExtension = "NEEDLE_deformation_rig" | "NEEDLE_mesh_impostor" | "NEEDLE_pixel_impostor" | "NEEDLE_hull_effect";
20
+ export type NeedleExtension = "NEEDLE_deformation_rig" | "NEEDLE_mesh_impostor" | "NEEDLE_pixel_impostor" | "NEEDLE_hull_effect" | "NEEDLE_box_impostor";
21
21
  export type LoaderOptions = {
22
22
  disable?: readonly NeedleExtension[];
23
23
  };
@@ -1,4 +1,4 @@
1
- /** @typedef {'black' | 'white' | 'invert'} WireframeBlendMode */
1
+ /** @typedef {'high-contrast' | 'black' | 'white' | 'invert' | 'cyan' | 'lime' | 'magenta'} WireframeBlendMode */
2
2
  /**
3
3
  * Produces a triangle overlay with a screen-space barycentric edge mask. Unlike
4
4
  * hardware line primitives this supports stable, adjustable pixel thickness.
@@ -20,6 +20,6 @@ export function applyPreviewWireframeBlendMode(material: THREE.Material, mode: W
20
20
  * @returns {THREE.BufferGeometry}
21
21
  */
22
22
  export function createBarycentricWireframeGeometry(source: THREE.BufferGeometry): THREE.BufferGeometry;
23
- export type WireframeBlendMode = "black" | "white" | "invert";
23
+ export type WireframeBlendMode = "high-contrast" | "black" | "white" | "invert" | "cyan" | "lime" | "magenta";
24
24
  import * as THREE from 'three';
25
25
  import { MeshBasicNodeMaterial } from 'three/webgpu';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/three-mesh-effects",
3
- "version": "0.2.3",
3
+ "version": "0.3.1",
4
4
  "description": "Three.js loaders and TSL effects for Needle Mesh Baker outputs.",
5
5
  "keywords": [
6
6
  "three.js",
@@ -24,7 +24,9 @@
24
24
  },
25
25
  "type": "module",
26
26
  "sideEffects": false,
27
- "engines": { "node": ">=22" },
27
+ "engines": {
28
+ "node": ">=22"
29
+ },
28
30
  "exports": {
29
31
  ".": {
30
32
  "deno": "./src/public/index.js",
@@ -76,12 +78,29 @@
76
78
  "types": "./build/types/rendering/*.d.ts",
77
79
  "import": "./build/internal/rendering/*.js"
78
80
  },
79
- "./package.json": "./package.json"
81
+ "./package.json": "./package.json",
82
+ "./box-impostor-runtime": {
83
+ "deno": "./src/public/box-impostor-runtime.js",
84
+ "types": "./build/types/public/box-impostor-runtime.d.ts",
85
+ "import": "./build/box-impostor-runtime.js"
86
+ },
87
+ "./internal/box-impostor/*": {
88
+ "deno": "./src/box-impostor/*.js",
89
+ "types": "./build/types/box-impostor/*.d.ts",
90
+ "import": "./build/internal/box-impostor/*.js"
91
+ }
80
92
  },
81
- "files": ["build/", "src/", "README.md", "CHANGELOG.md", "SKILL.md", "LICENSE"],
93
+ "files": [
94
+ "build/",
95
+ "src/",
96
+ "README.md",
97
+ "CHANGELOG.md",
98
+ "SKILL.md",
99
+ "LICENSE"
100
+ ],
82
101
  "scripts": {
83
102
  "build": "node scripts/build.mjs",
84
- "test": "npm run build && node --test tests/use-loader.test.js",
103
+ "test": "npm run build && node --test tests/use-loader.test.js tests/box-impostor-runtime.test.js",
85
104
  "prepare": "node scripts/prepare.mjs",
86
105
  "prepack": "npm run build",
87
106
  "test:packed-consumer": "node scripts/test-packed-consumer.mjs"
@@ -103,7 +122,9 @@
103
122
  "@needle-tools/engine": "^6.0.0-alpha.2"
104
123
  },
105
124
  "peerDependenciesMeta": {
106
- "@needle-tools/engine": { "optional": true }
125
+ "@needle-tools/engine": {
126
+ "optional": true
127
+ }
107
128
  },
108
129
  "repository": {
109
130
  "type": "git",
@@ -0,0 +1,54 @@
1
+ // @ts-check
2
+ /** @typedef {{depthMode:'auto'|'projection'|'depth',raySteps:8|16|32,alphaCutoff:number,interpolation:'none'|'linear'|'smooth',transparency:'auto'|'cutout'|'dithered'|'blended'}} BoxImpostorDisplay */
3
+ export const DEFAULT_BOX_IMPOSTOR_DISPLAY = Object.freeze({
4
+ depthMode: /** @type {'auto'|'projection'|'depth'} */ ('auto'),
5
+ raySteps: /** @type {8|16|32} */ (16),
6
+ alphaCutoff: 0.5,
7
+ interpolation: /** @type {'none'|'linear'|'smooth'} */ ('none'),
8
+ transparency: /** @type {'auto'|'cutout'|'dithered'|'blended'} */ ('auto'),
9
+ });
10
+ /** @param {Partial<BoxImpostorDisplay>} [input] @returns {BoxImpostorDisplay} */
11
+ export function normalizeBoxImpostorDisplay(input = {}) {
12
+ const value = { ...DEFAULT_BOX_IMPOSTOR_DISPLAY, ...input };
13
+ if (
14
+ !['auto', 'projection', 'depth'].includes(value.depthMode) ||
15
+ ![8, 16, 32].includes(value.raySteps) ||
16
+ !Number.isFinite(value.alphaCutoff) ||
17
+ value.alphaCutoff <= 0 ||
18
+ value.alphaCutoff >= 1 ||
19
+ !['none', 'linear', 'smooth'].includes(value.interpolation) ||
20
+ !['auto', 'cutout', 'dithered', 'blended'].includes(value.transparency)
21
+ )
22
+ throw new Error('Invalid box impostor display settings.');
23
+ return {
24
+ depthMode: value.depthMode,
25
+ raySteps: value.raySteps,
26
+ alphaCutoff: value.alphaCutoff,
27
+ interpolation: value.interpolation,
28
+ transparency: value.transparency,
29
+ };
30
+ }
31
+
32
+ /** @param {{alphaMode?:'opaque'|'mask'|'blend'}} descriptor @param {BoxImpostorDisplay} settings */
33
+ export function boxTransparency(descriptor, settings) {
34
+ return settings.transparency === 'auto'
35
+ ? descriptor.alphaMode === 'blend'
36
+ ? 'blended'
37
+ : 'cutout'
38
+ : settings.transparency;
39
+ }
40
+
41
+ /** Auto only uses planes when every capture is flat within its stored depth bound.
42
+ * Captures without a flatness bound use depth reconstruction.
43
+ * @param {{boxes:{planes?:number[],planeErrors?:number[]}[]}} descriptor @param {BoxImpostorDisplay} settings */
44
+ export function boxDepthMode(descriptor, settings) {
45
+ if (settings.depthMode !== 'auto') return settings.depthMode;
46
+ return descriptor.boxes.every(
47
+ (box) =>
48
+ box.planes?.length === 6 &&
49
+ box.planeErrors?.length === 6 &&
50
+ box.planeErrors.every((error) => error <= 2 / 255),
51
+ )
52
+ ? 'projection'
53
+ : 'depth';
54
+ }
@@ -0,0 +1,38 @@
1
+ // @ts-check
2
+ import { createBoxImpostorMaterial } from './box-impostor-material.js';
3
+
4
+ import { normalizeBoxImpostorDisplay } from './box-impostor-display-settings.js';
5
+ /** @typedef {import('./box-impostor-display-settings.js').BoxImpostorDisplay} BoxImpostorDisplay */
6
+ /** @type {WeakMap<import('three').Mesh, Map<string, ReturnType<typeof createBoxImpostorMaterial>>>} */
7
+ const variants = new WeakMap();
8
+ /** Changes display without touching captured textures. Expensive settings select
9
+ * cached shader graphs; alpha cutoff updates a uniform. Geometry owns the cache.
10
+ * @param {import('three').Object3D} root @param {Partial<BoxImpostorDisplay>} options */
11
+ export function setBoxImpostorDisplay(root, options) {
12
+ root.traverse(object => {
13
+ const mesh = /** @type {import('three').Mesh} */ (object);
14
+ const binding = mesh.userData.boxImpostor;
15
+ if (!mesh.isMesh || !binding) return;
16
+ const settings = normalizeBoxImpostorDisplay({...binding.descriptor, ...options});
17
+ const descriptor = {...binding.descriptor, ...settings};
18
+ const keyFor = (/** @type {BoxImpostorDisplay} */ s) => `${s.depthMode}/${s.depthMode !== 'projection' ? s.raySteps : 0}/${s.interpolation}/${s.transparency}`;
19
+ let cache = variants.get(mesh);
20
+ if (!cache) {
21
+ cache = new Map([[keyFor(normalizeBoxImpostorDisplay(binding.descriptor)), /** @type {ReturnType<typeof createBoxImpostorMaterial>} */ (mesh.material)]]);
22
+ variants.set(mesh, cache);
23
+ const owned = cache;
24
+ mesh.geometry.addEventListener('dispose', () => {
25
+ for (const material of owned.values()) material.dispose();
26
+ owned.clear();
27
+ variants.delete(mesh);
28
+ });
29
+ }
30
+ const key = keyFor(settings);
31
+ let material = cache.get(key);
32
+ if (!material) { material = createBoxImpostorMaterial(descriptor, binding.maps); cache.set(key, material); }
33
+ material.userData.boxImpostor.alphaCutoff.value = settings.alphaCutoff;
34
+ material.userData.boxImpostor.descriptor = descriptor;
35
+ mesh.material = material;
36
+ binding.descriptor = descriptor;
37
+ });
38
+ }
@@ -0,0 +1,11 @@
1
+ // @ts-check
2
+ // Shared capture coordinates; keep this module free of renderer dependencies so
3
+ // standalone bundles can use the same convention without importing an engine.
4
+ export const BOX_FACE_AXES = Object.freeze([
5
+ { normal: [1, 0, 0], right: [0, 0, -1], up: [0, 1, 0] },
6
+ { normal: [-1, 0, 0], right: [0, 0, 1], up: [0, 1, 0] },
7
+ { normal: [0, 1, 0], right: [1, 0, 0], up: [0, 0, -1] },
8
+ { normal: [0, -1, 0], right: [1, 0, 0], up: [0, 0, 1] },
9
+ { normal: [0, 0, 1], right: [1, 0, 0], up: [0, 1, 0] },
10
+ { normal: [0, 0, -1], right: [-1, 0, 0], up: [0, 1, 0] },
11
+ ]);
@@ -0,0 +1,118 @@
1
+ // TSL graph properties are checked by rendered WebGPU/WebGL regressions.
2
+ // @ts-nocheck
3
+ import { DataTexture, FloatType, RGBAFormat, NearestFilter } from 'three';
4
+ import {
5
+ Fn,
6
+ If,
7
+ attribute,
8
+ positionLocal,
9
+ uv,
10
+ texture,
11
+ textureLoad,
12
+ ivec2,
13
+ vec2,
14
+ vec3,
15
+ float,
16
+ int,
17
+ dot,
18
+ abs,
19
+ max,
20
+ sign,
21
+ transformNormalToView,
22
+ } from 'three/tsl';
23
+ import { BOX_FACE_AXES } from './box-impostor-metadata.js';
24
+
25
+ /** Rasterization resolves fitted faces without a fragment ray or depth override. */
26
+ export function applyBoxFaces({
27
+ material,
28
+ descriptor,
29
+ maps,
30
+ alphaCutoff,
31
+ transparency,
32
+ half,
33
+ toBox,
34
+ fromBox,
35
+ }) {
36
+ const values = new Float32Array(descriptor.boxes.length * 6 * 4);
37
+ descriptor.boxes.forEach((box, i) => {
38
+ for (let face = 0; face < 6; face++)
39
+ values[(i * 6 + face) * 4] = box.planes?.[face] ?? 0.5;
40
+ });
41
+ const planes = new DataTexture(
42
+ values,
43
+ 6,
44
+ descriptor.boxes.length,
45
+ RGBAFormat,
46
+ FloatType,
47
+ );
48
+ planes.minFilter = planes.magFilter = NearestFilter;
49
+ planes.needsUpdate = true;
50
+ material.addEventListener('dispose', () => planes.dispose());
51
+ const face = int(attribute('boxFace', 'float'));
52
+ const n = attribute('normal', 'vec3');
53
+ const depth = textureLoad(
54
+ planes,
55
+ ivec2(face, int(attribute('boxId', 'float'))),
56
+ ).r;
57
+ const proxy = positionLocal.sub(
58
+ n.mul(
59
+ dot(abs(toBox(n)), half)
60
+ .mul(depth)
61
+ .mul(2),
62
+ ),
63
+ );
64
+ material.positionNode = proxy;
65
+ material.castShadowPositionNode = proxy;
66
+ const base = texture(maps.baseColor, uv()).level(0).toVar('boxFaceColor');
67
+ const orm = texture(maps.orm, uv()).level(0);
68
+ const coverage = max(orm.a, 1e-10);
69
+ const normal = Fn(() => {
70
+ const packed = textureLoad(
71
+ maps.normalDepth,
72
+ ivec2(uv().mul(descriptor.atlasResolution)),
73
+ )
74
+ .rg.mul(2)
75
+ .sub(1);
76
+ const decoded = vec3(
77
+ packed,
78
+ float(1).sub(abs(packed.x)).sub(abs(packed.y)),
79
+ ).toVar();
80
+ If(decoded.z.lessThan(0), () => {
81
+ decoded.xy.assign(
82
+ vec2(1)
83
+ .sub(abs(decoded.yx))
84
+ .mul(sign(decoded.xy.add(1e-20))),
85
+ );
86
+ });
87
+ const result = vec3(0).toVar();
88
+ for (let f = 0; f < 6; f++) {
89
+ const a = BOX_FACE_AXES[f];
90
+ If(face.equal(f), () => {
91
+ result.assign(
92
+ fromBox(
93
+ vec3(...a.right)
94
+ .mul(decoded.x)
95
+ .add(vec3(...a.up).mul(decoded.y))
96
+ .add(vec3(...a.normal).mul(decoded.z)),
97
+ ).normalize(),
98
+ );
99
+ });
100
+ }
101
+ return result;
102
+ })();
103
+ material.colorNode = base.rgb.div(coverage);
104
+ material.normalNode = transformNormalToView(normal);
105
+ material.roughnessNode = orm.g.div(coverage);
106
+ material.metalnessNode = orm.b.div(coverage);
107
+ material.aoNode = orm.r.div(coverage);
108
+ material.emissiveNode = texture(maps.emissive, uv())
109
+ .level(0)
110
+ .rgb.div(coverage);
111
+ material.maskNode = base.a.greaterThanEqual(
112
+ transparency === 'cutout' ? alphaCutoff : float(1 / 255),
113
+ );
114
+ material.maskShadowNode = material.maskNode;
115
+ if (transparency !== 'cutout') material.opacityNode = base.a;
116
+ material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
117
+ return material;
118
+ }