@needle-tools/three-mesh-effects 0.3.4 → 0.3.6
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.
- package/CHANGELOG.md +32 -27
- package/README.md +63 -231
- package/SKILL.md +11 -68
- package/build/box-impostor-runtime.js +14 -2
- package/build/box-impostor-runtime.js.map +2 -2
- package/build/deformation-runtime.js +1 -1
- package/build/deformation-runtime.js.map +1 -1
- package/build/hull-effects.js.map +1 -1
- package/build/impostor-runtime.js +2 -2
- package/build/impostor-runtime.js.map +2 -2
- package/build/index.js +2 -2
- package/build/index.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-display.js +10 -2
- package/build/internal/box-impostor/box-impostor-display.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-faces.js.map +1 -1
- package/build/internal/box-impostor/box-impostor-geometry.js +4 -0
- package/build/internal/box-impostor/box-impostor-geometry.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-material.js +10 -2
- package/build/internal/box-impostor/box-impostor-material.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-metadata.js +4 -0
- package/build/internal/box-impostor/box-impostor-metadata.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-projection.js.map +1 -1
- package/build/internal/box-impostor/box-impostor-runtime.js +14 -2
- package/build/internal/box-impostor/box-impostor-runtime.js.map +2 -2
- package/build/internal/deformation/rig-runtime.js +1 -1
- package/build/internal/deformation/rig-runtime.js.map +1 -1
- package/build/internal/impostor/impostor-node-material.js +2 -2
- package/build/internal/impostor/impostor-node-material.js.map +2 -2
- package/build/internal/impostor/octahedral.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-display.js +39 -16
- package/build/internal/pixel-impostor/pixel-impostor-display.js.map +2 -2
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js +61 -6
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +2 -2
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js +980 -26
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +4 -4
- package/build/internal/rendering/hull-effect-runtime.js.map +1 -1
- package/build/internal/rendering/renderer-support.js.map +1 -1
- package/build/internal/rendering/skin-projection.js.map +1 -1
- package/build/internal/rendering/surface-query.js.map +1 -1
- package/build/internal/rendering/wrap-preview.js.map +1 -1
- package/build/internal/rendering/wrap-skin.js.map +1 -1
- package/build/pixel-impostor-runtime.js +362 -172
- package/build/pixel-impostor-runtime.js.map +4 -4
- package/build/types/box-impostor/box-impostor-metadata.d.ts +3 -1
- package/build/types/deformation/rig-runtime.d.ts +1 -1
- package/build/types/impostor/octahedral.d.ts +1 -1
- package/build/types/pixel-impostor/pixel-impostor-display.d.ts +3 -3
- package/build/types/pixel-impostor/pixel-impostor-metadata.d.ts +76 -5
- package/build/types/pixel-impostor/pixel-impostor-runtime.d.ts +19 -7
- package/build/types/public/pixel-impostor-runtime.d.ts +1 -1
- package/build/types/rendering/renderer-support.d.ts +1 -1
- package/build/types/rendering/surface-query.d.ts +2 -2
- package/package.json +2 -2
- package/src/box-impostor/box-impostor-material.js +10 -2
- package/src/box-impostor/box-impostor-metadata.js +5 -1
- package/src/deformation/rig-runtime.ts +1 -1
- package/src/impostor/impostor-node-material.js +2 -2
- package/src/impostor/octahedral.js +1 -1
- package/src/pixel-impostor/pixel-impostor-display.js +32 -18
- package/src/pixel-impostor/pixel-impostor-metadata.js +69 -9
- package/src/pixel-impostor/pixel-impostor-runtime.js +32 -9
- package/src/public/index.js +2 -2
- package/src/public/pixel-impostor-runtime.js +1 -1
- package/src/rendering/renderer-support.js +1 -1
- package/src/rendering/surface-query.js +2 -2
|
@@ -1,18 +1,30 @@
|
|
|
1
|
-
/** Register pixel atlas loading before calling `loadAsync`; returns the same loader.
|
|
1
|
+
/** Register pixel atlas loading before calling `loadAsync`; returns the same loader. With a
|
|
2
|
+
* renderer, files show their stored display.
|
|
2
3
|
* @template {import('three/addons/loaders/GLTFLoader.js').GLTFLoader} T
|
|
3
|
-
* @param {T} loader @returns {T}
|
|
4
|
+
* @param {T} loader @param {{renderer?: import('../rendering/renderer-support.js').ImpostorRenderer}} [options] @returns {T}
|
|
4
5
|
*/
|
|
5
|
-
export function registerNeedlePixelImpostorLoader<T extends import("three/addons/loaders/GLTFLoader.js").GLTFLoader>(loader: T
|
|
6
|
+
export function registerNeedlePixelImpostorLoader<T extends import("three/addons/loaders/GLTFLoader.js").GLTFLoader>(loader: T, options?: {
|
|
7
|
+
renderer?: import("../rendering/renderer-support.js").ImpostorRenderer;
|
|
8
|
+
}): T;
|
|
6
9
|
/**
|
|
7
10
|
* Three.js GLTFLoader plugin for versioned pixel impostor atlas bindings.
|
|
8
|
-
*
|
|
9
|
-
*
|
|
11
|
+
* Given a renderer, it shows a file's stored display (`display` in the extension) with
|
|
12
|
+
* `createPixelImpostorDisplay`, and keeps the display's handle in the scene's
|
|
13
|
+
* `userData.pixelImpostorDisplay`. Without a renderer, or for files without a stored
|
|
14
|
+
* display, it leaves the file's mesh as it is; call `createPixelImpostorDisplay` to choose a mode.
|
|
10
15
|
*/
|
|
11
16
|
export class NeedlePixelImpostorLoaderPlugin {
|
|
12
|
-
/** @param {unknown} parser */
|
|
13
|
-
constructor(parser: unknown
|
|
17
|
+
/** @param {unknown} parser @param {{renderer?: import('../rendering/renderer-support.js').ImpostorRenderer}} [options] */
|
|
18
|
+
constructor(parser: unknown, options?: {
|
|
19
|
+
renderer?: import("../rendering/renderer-support.js").ImpostorRenderer;
|
|
20
|
+
});
|
|
14
21
|
name: string;
|
|
15
22
|
parser: unknown;
|
|
23
|
+
options: {
|
|
24
|
+
renderer?: import("../rendering/renderer-support.js").ImpostorRenderer;
|
|
25
|
+
};
|
|
16
26
|
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
|
|
17
27
|
afterRoot(result: import("three/addons/loaders/GLTFLoader.js").GLTF): Promise<void>;
|
|
28
|
+
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
|
|
29
|
+
applyStoredDisplay(result: import("three/addons/loaders/GLTFLoader.js").GLTF): Promise<void>;
|
|
18
30
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { NeedlePixelImpostorLoaderPlugin, registerNeedlePixelImpostorLoader } from "../pixel-impostor/pixel-impostor-runtime.js";
|
|
2
|
-
export { NEEDLE_PIXEL_IMPOSTOR, parsePixelImpostorDescriptor } from "../pixel-impostor/pixel-impostor-metadata.js";
|
|
2
|
+
export { NEEDLE_PIXEL_IMPOSTOR, parsePixelImpostorDescriptor, normalizePixelImpostorDisplay, DEFAULT_PIXEL_IMPOSTOR_DISPLAY, PIXEL_IMPOSTOR_VOLUME_MODES } from "../pixel-impostor/pixel-impostor-metadata.js";
|
|
3
3
|
export { createPixelImpostorDisplay, createPixelImpostorRaycastPreview } from "../pixel-impostor/pixel-impostor-display.js";
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @property {((handler: import('three/addons/tsl/WebGLNodesHandler.js').WebGLNodesHandler) => void)=} setNodesHandler
|
|
7
7
|
*/
|
|
8
8
|
/**
|
|
9
|
-
* Reports whether a renderer can compile
|
|
9
|
+
* Reports whether a renderer can compile TSL impostor materials.
|
|
10
10
|
* WebGPURenderer supports node materials directly. Classic WebGLRenderer
|
|
11
11
|
* integrations must advertise that they installed a compatible nodes handler.
|
|
12
12
|
*
|
|
@@ -28,8 +28,8 @@ export function createSurfaceQuery(mesh: QueryMesh, options?: {
|
|
|
28
28
|
dispose(): void;
|
|
29
29
|
};
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
32
|
-
* aligned with the source triangle arrays.
|
|
31
|
+
* Reuse allocations when querying a prebuilt BVH. Indirect indexing keeps BVH
|
|
32
|
+
* face IDs aligned with the source triangle arrays.
|
|
33
33
|
*
|
|
34
34
|
* @param {BufferGeometry} geometry
|
|
35
35
|
* @param {MeshBVH} bvh
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@needle-tools/three-mesh-effects",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "Three.js loaders and TSL
|
|
3
|
+
"version": "0.3.6",
|
|
4
|
+
"description": "Three.js loaders and TSL materials for glTF impostors, hull effects, and deformation rigs.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"three.js",
|
|
7
7
|
"gltf",
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
attribute,
|
|
22
22
|
varying,
|
|
23
23
|
positionLocal,
|
|
24
|
+
positionViewDirection,
|
|
24
25
|
modelWorldMatrixInverse,
|
|
25
26
|
cameraPosition,
|
|
26
27
|
cameraWorldMatrix,
|
|
@@ -758,8 +759,15 @@ export function createBoxImpostorMaterialVariant(descriptor, maps, {filtering =
|
|
|
758
759
|
material.roughnessNode = surfaceOrm.g;
|
|
759
760
|
material.metalnessNode = surfaceOrm.b;
|
|
760
761
|
material.aoNode = surfaceOrm.r;
|
|
761
|
-
material.emissiveNode = emission;
|
|
762
|
-
if (transparency !== "cutout")
|
|
762
|
+
material.emissiveNode = emission.mul(descriptor.emissiveStrength ?? 1);
|
|
763
|
+
if (transparency !== "cutout") {
|
|
764
|
+
let opacity = color.a.clamp(0, 1);
|
|
765
|
+
if (descriptor.fresnelOpacity) {
|
|
766
|
+
const cosine = material.normalNode.dot(positionViewDirection).abs().clamp(0, 1);
|
|
767
|
+
opacity = opacity.add(opacity.oneMinus().mul(cosine.oneMinus().pow(5)));
|
|
768
|
+
}
|
|
769
|
+
material.opacityNode = opacity;
|
|
770
|
+
}
|
|
763
771
|
material.maskNode = hit.get("valid").greaterThan(0.5);
|
|
764
772
|
material.maskShadowNode = material.maskNode;
|
|
765
773
|
material.depthNode = capturedSurfaceDepth(hit.get("position"));
|
|
@@ -5,7 +5,7 @@ export const BOX_OCCUPANCY_RESOLUTION = 8;
|
|
|
5
5
|
export const BOX_OCCUPANCY_WORDS = BOX_OCCUPANCY_RESOLUTION ** 3 / 32;
|
|
6
6
|
export { BOX_FACE_AXES } from "./box-impostor-face-axes.js";
|
|
7
7
|
/** @typedef {{center:number[],axes:number[][],halfSize:number[],views:number[][],planes?:number[],planeErrors?:number[],depthBounds?:number[],occupancy?:number[]}} ImpostorBox */
|
|
8
|
-
/** @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 */
|
|
8
|
+
/** @typedef {{version:1,fresnelOpacity?:boolean,emissiveStrength?:number,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 */
|
|
9
9
|
/** @param {unknown} raw @returns {BoxImpostorDescriptor} */
|
|
10
10
|
export function parseBoxImpostorDescriptor(raw) {
|
|
11
11
|
const d = /** @type {BoxImpostorDescriptor} */ (raw);
|
|
@@ -38,6 +38,8 @@ export function parseBoxImpostorDescriptor(raw) {
|
|
|
38
38
|
!["opaque", "mask", "blend"].includes(d.alphaMode)
|
|
39
39
|
)
|
|
40
40
|
fail();
|
|
41
|
+
if (d.fresnelOpacity !== undefined && typeof d.fresnelOpacity !== 'boolean') fail();
|
|
42
|
+
if (d.emissiveStrength !== undefined && (!Number.isFinite(d.emissiveStrength) || d.emissiveStrength < 1)) fail();
|
|
41
43
|
if (d.mode === "basic" && d.boxes.length !== 1) fail();
|
|
42
44
|
for (const b of d.boxes) {
|
|
43
45
|
if (
|
|
@@ -116,6 +118,8 @@ export function parseBoxImpostorDescriptor(raw) {
|
|
|
116
118
|
atlasResolution: d.atlasResolution,
|
|
117
119
|
padding: d.padding,
|
|
118
120
|
boxes: d.boxes,
|
|
121
|
+
...(d.fresnelOpacity !== undefined ? {fresnelOpacity: d.fresnelOpacity} : {}),
|
|
122
|
+
...(d.emissiveStrength !== undefined ? {emissiveStrength: d.emissiveStrength} : {}),
|
|
119
123
|
...(d.alphaMode ? { alphaMode: d.alphaMode } : {}),
|
|
120
124
|
...normalizeBoxImpostorDisplay(d),
|
|
121
125
|
};
|
|
@@ -222,7 +222,7 @@ export class DeformationRigRuntime {
|
|
|
222
222
|
}
|
|
223
223
|
this.apply(pins); this.rememberAnimationFrame();
|
|
224
224
|
}
|
|
225
|
-
/** Display a caller-supplied fitted frame.
|
|
225
|
+
/** Display a caller-supplied fitted frame. */
|
|
226
226
|
previewPins(pins: readonly PinRecord[]) {
|
|
227
227
|
if (pins.length !== this.bones.size || pins.some(p => !this.bones.has(p.id))) throw new Error('Rig layout changed; rebuild the GLB.');
|
|
228
228
|
if (this.mixingStaticPoses) { this.mixer.stopAllAction(); this.mixingStaticPoses = false; }
|
|
@@ -208,7 +208,7 @@ export function upgradeImpostorScene(root, bindings = new Map(), inputFeatures =
|
|
|
208
208
|
}
|
|
209
209
|
const descriptor = parseImpostorDescriptor(metadata);
|
|
210
210
|
if (!descriptor) {
|
|
211
|
-
throw new Error('Unsupported
|
|
211
|
+
throw new Error('Unsupported NEEDLE_mesh_impostor descriptor.');
|
|
212
212
|
}
|
|
213
213
|
if (!binding) {
|
|
214
214
|
throw new Error('NEEDLE_mesh_impostor textures require the registered GLTFLoader plugin.');
|
|
@@ -381,7 +381,7 @@ export class NeedleImpostorLoaderPlugin {
|
|
|
381
381
|
const definition = /** @type {Record<string, unknown>} */ (raw);
|
|
382
382
|
const descriptor = parseImpostorDescriptor(definition);
|
|
383
383
|
if (!descriptor) {
|
|
384
|
-
throw new Error('Unsupported NEEDLE_mesh_impostor descriptor
|
|
384
|
+
throw new Error('Unsupported NEEDLE_mesh_impostor descriptor.');
|
|
385
385
|
}
|
|
386
386
|
const rawTextures = definition.textures;
|
|
387
387
|
if (!rawTextures || typeof rawTextures !== 'object') {
|
|
@@ -61,7 +61,7 @@ export function intersectRayWithCaptureDepth(
|
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* Decodes a regular or upper-hemisphere octahedral grid coordinate.
|
|
64
|
-
* Y
|
|
64
|
+
* Uses Y as the up axis.
|
|
65
65
|
*
|
|
66
66
|
* @param {[number, number]} coordinate normalized 0..1 grid coordinate
|
|
67
67
|
* @param {'hemisphere' | 'full'} mapping
|
|
@@ -87,7 +87,7 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
87
87
|
const normalAtlas = binding.normalAtlas instanceof THREE.Texture ? binding.normalAtlas : null;
|
|
88
88
|
const ormAtlas = binding.ormAtlas instanceof THREE.Texture ? binding.ormAtlas : null;
|
|
89
89
|
const indexAtlas = binding.indexAtlas instanceof THREE.Texture ? binding.indexAtlas : null;
|
|
90
|
-
const sparse = descriptor.
|
|
90
|
+
const sparse = descriptor.storage === 'sparse';
|
|
91
91
|
if (sparse && !indexAtlas) return null;
|
|
92
92
|
for (const map of [normalAtlas, ormAtlas, indexAtlas]) {
|
|
93
93
|
if (!map) continue;
|
|
@@ -414,16 +414,18 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
414
414
|
// Keep the portable face-culled shell as one ordinary draw. Each vertex is
|
|
415
415
|
// marked with its source voxel center, so every corner from one voxel gets
|
|
416
416
|
// exactly the same screen-space displacement and the cell remains coherent.
|
|
417
|
+
// An export for a volume display mode has no shell: its primitive is the volume's box (voxelShell: false).
|
|
418
|
+
const shellless = descriptor.voxelShell === false;
|
|
417
419
|
/** @type {THREE.Mesh | null} */
|
|
418
420
|
let shell = null;
|
|
419
|
-
asset.traverse((object) => {
|
|
421
|
+
if (!shellless) asset.traverse((object) => {
|
|
420
422
|
if (!shell && object instanceof THREE.Mesh
|
|
421
423
|
&& (object.geometry.getAttribute('_voxel_center') || surfacePoints)) {
|
|
422
424
|
shell = object;
|
|
423
425
|
}
|
|
424
426
|
});
|
|
425
|
-
if (!shell) return null;
|
|
426
|
-
const voxelShell = /** @type {THREE.Mesh} */ (shell);
|
|
427
|
+
if (!shell && !shellless) return null;
|
|
428
|
+
const voxelShell = /** @type {THREE.Mesh | null} */ (shell);
|
|
427
429
|
const geometryBaseSample = texture(atlas, uv());
|
|
428
430
|
const geometryOrmSample = ormAtlas ? texture(ormAtlas, uv()) : vec4(1, 1, 0, 1);
|
|
429
431
|
const geometryNormal = attribute('normal', /** @type {const} */ ('vec3')).normalize();
|
|
@@ -461,18 +463,22 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
461
463
|
mapped: createGeometryMaterial(true),
|
|
462
464
|
plain: createGeometryMaterial(false),
|
|
463
465
|
};
|
|
464
|
-
const previousGeometryMaterial = voxelShell
|
|
465
|
-
voxelShell
|
|
466
|
-
|
|
466
|
+
const previousGeometryMaterial = voxelShell?.material ?? null;
|
|
467
|
+
if (voxelShell) {
|
|
468
|
+
voxelShell.material = geometryMaterials.mapped;
|
|
469
|
+
voxelShell.userData.pixelImpostorAlternateMaterials = Object.values(geometryMaterials);
|
|
470
|
+
}
|
|
467
471
|
const skinnedSplats = voxelShell instanceof THREE.SkinnedMesh;
|
|
468
472
|
// Sparse instancing deliberately omits the carrier mesh's per-vertex data.
|
|
469
473
|
// Animated exports retain that data, so use their dense carriers and let
|
|
470
474
|
// standard Three.js skinning deform each voxel center before screen-aligning
|
|
471
475
|
// its quad. Static sparse files keep the smaller instanced path.
|
|
472
476
|
const useSurfacePoints = surfacePoints && !skinnedSplats;
|
|
473
|
-
|
|
477
|
+
// Without a shell, splats need the sparse surface points; otherwise the splat mode is unavailable.
|
|
478
|
+
const splatsAvailable = Boolean(voxelShell || useSurfacePoints);
|
|
479
|
+
let splatGeometry = skinnedSplats ? /** @type {THREE.Mesh} */ (voxelShell).geometry.clone() : voxelShell?.geometry ?? new THREE.BufferGeometry();
|
|
474
480
|
if (skinnedSplats) {
|
|
475
|
-
const centers = voxelShell.geometry.getAttribute('_voxel_center');
|
|
481
|
+
const centers = /** @type {THREE.Mesh} */ (voxelShell).geometry.getAttribute('_voxel_center');
|
|
476
482
|
if (!centers) throw new Error('Animated voxel splats require retained voxel centers.');
|
|
477
483
|
splatGeometry.setAttribute('position', centers.clone());
|
|
478
484
|
}
|
|
@@ -496,8 +502,8 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
496
502
|
splatGeometry.setAttribute('_voxel_coord', new THREE.InstancedBufferAttribute(coordinates, 3));
|
|
497
503
|
splatGeometry.setAttribute('_voxel_record', new THREE.InstancedBufferAttribute(records, 1));
|
|
498
504
|
splatGeometry.setIndex([0, 1, 2, 0, 2, 3]);
|
|
499
|
-
splatGeometry.boundingBox = voxelShell
|
|
500
|
-
splatGeometry.boundingSphere = voxelShell
|
|
505
|
+
splatGeometry.boundingBox = voxelShell?.geometry.boundingBox?.clone() ?? bounds.clone();
|
|
506
|
+
splatGeometry.boundingSphere = voxelShell?.geometry.boundingSphere?.clone()
|
|
501
507
|
?? splatGeometry.boundingBox.getBoundingSphere(new THREE.Sphere());
|
|
502
508
|
}
|
|
503
509
|
const voxelCoordinate = attribute('_voxel_coord', /** @type {const} */ ('vec3'));
|
|
@@ -626,8 +632,8 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
626
632
|
snappedShell.bindMatrixInverse.copy(voxelShell.bindMatrixInverse);
|
|
627
633
|
}
|
|
628
634
|
asset.updateWorldMatrix(true, true);
|
|
629
|
-
voxelShell
|
|
630
|
-
if (!useSurfacePoints) {
|
|
635
|
+
voxelShell?.updateWorldMatrix(true, false);
|
|
636
|
+
if (!useSurfacePoints && voxelShell) {
|
|
631
637
|
snappedShell.matrix.copy(asset.matrixWorld).invert().multiply(voxelShell.matrixWorld);
|
|
632
638
|
snappedShell.matrixAutoUpdate = false;
|
|
633
639
|
}
|
|
@@ -664,8 +670,11 @@ export function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
664
670
|
+ (normalAtlas ? textureFootprintBytes(normalAtlas) : 0)
|
|
665
671
|
+ (ormAtlas ? textureFootprintBytes(ormAtlas) : 0)
|
|
666
672
|
+ (indexAtlas ? textureFootprintBytes(indexAtlas) : 0);
|
|
667
|
-
const shellStats = pixelDisplayMeshStats(voxelShell, textureBytes);
|
|
668
|
-
const splatStats = pixelDisplayMeshStats(snappedShell, textureFootprintBytes(atlas));
|
|
673
|
+
const shellStats = voxelShell ? pixelDisplayMeshStats(voxelShell, textureBytes) : null;
|
|
674
|
+
const splatStats = splatsAvailable ? pixelDisplayMeshStats(snappedShell, textureFootprintBytes(atlas)) : null;
|
|
675
|
+
preview.userData.availableModes = [
|
|
676
|
+
...(voxelShell ? ['geometry'] : []), ...(splatsAvailable ? ['vertex-snap'] : []), 'raycast', 'screen-pixels',
|
|
677
|
+
];
|
|
669
678
|
const raycastStats = pixelDisplayMeshStats(raycast, textureBytes);
|
|
670
679
|
preview.userData.displayStats = {
|
|
671
680
|
geometry: shellStats,
|
|
@@ -718,8 +727,8 @@ function textureFootprintBytes(atlas) {
|
|
|
718
727
|
}
|
|
719
728
|
|
|
720
729
|
/**
|
|
721
|
-
*
|
|
722
|
-
*
|
|
730
|
+
* Create a voxel, screen-space, or raycast display for a loaded pixel impostor.
|
|
731
|
+
* Register the pixel glTF loader before loading the GLB. Call
|
|
723
732
|
* `updateViewport()` after a resize and `dispose()` before discarding the scene.
|
|
724
733
|
*
|
|
725
734
|
* @param {THREE.Object3D} asset Loaded glTF scene.
|
|
@@ -742,11 +751,16 @@ export function createPixelImpostorDisplay(asset, options = {}) {
|
|
|
742
751
|
if (object instanceof THREE.Mesh) originals.set(object, object.visible);
|
|
743
752
|
});
|
|
744
753
|
if (options.attach !== false) asset.add(preview);
|
|
745
|
-
|
|
754
|
+
// Unset options take the display stored in the file, then the defaults.
|
|
755
|
+
const stored = /** @type {Partial<import('./pixel-impostor-metadata.js').PixelImpostorDisplay>} */ (
|
|
756
|
+
/** @type {{display?: object}} */ (binding.descriptor ?? {}).display ?? {});
|
|
757
|
+
options = { ...stored, ...Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined)) };
|
|
758
|
+
let mode = options.mode ?? (preview.userData.availableModes.includes('geometry') ? 'geometry' : 'screen-pixels');
|
|
746
759
|
let pbr = options.pbr ?? true;
|
|
747
760
|
let antialias = options.antialias ?? true;
|
|
748
761
|
const setMode = (/** @type {typeof mode} */ next) => {
|
|
749
762
|
if (!['geometry', 'vertex-snap', 'raycast', 'screen-pixels'].includes(next)) throw new Error(`Unknown pixel display mode: ${next}`);
|
|
763
|
+
if (!preview.userData.availableModes.includes(next)) throw new Error(`This pixel impostor has no voxel shell, so it cannot use the ${next} display mode.`);
|
|
750
764
|
mode = next;
|
|
751
765
|
preview.userData.displayMode = next;
|
|
752
766
|
for (const [mesh, visible] of originals) mesh.visible = next === 'geometry' && visible;
|
|
@@ -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
|
-
|
|
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.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
*
|
|
9
|
-
*
|
|
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.
|
|
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
|
}
|
package/src/public/index.js
CHANGED
|
@@ -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
|
|
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
|
-
*
|
|
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
|