@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,171 +1,95 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __esm = (fn, res) => function __init() {
|
|
5
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
6
|
+
};
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
2
11
|
|
|
3
|
-
// src/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function parsePixelImpostorDescriptor(value) {
|
|
9
|
-
if (!value || typeof value !== "object") return null;
|
|
10
|
-
const raw = (
|
|
11
|
-
/** @type {Record<string, unknown>} */
|
|
12
|
-
value
|
|
13
|
-
);
|
|
14
|
-
if (raw.version !== 1 && raw.version !== 2 && raw.version !== 3 || !triplet(raw.dimensions) || !triplet(raw.volumeMin) || !triplet(raw.volumeSize)) return null;
|
|
15
|
-
const dimensions = (
|
|
16
|
-
/** @type {number[]} */
|
|
17
|
-
raw.dimensions
|
|
18
|
-
);
|
|
19
|
-
const volumeSize = (
|
|
20
|
-
/** @type {number[]} */
|
|
21
|
-
raw.volumeSize
|
|
22
|
-
);
|
|
23
|
-
const occupiedMin = triplet(raw.occupiedMin) ? (
|
|
24
|
-
/** @type {number[]} */
|
|
25
|
-
raw.occupiedMin
|
|
26
|
-
) : (
|
|
27
|
-
/** @type {number[]} */
|
|
28
|
-
raw.volumeMin
|
|
29
|
-
);
|
|
30
|
-
const occupiedSize = triplet(raw.occupiedSize) ? (
|
|
31
|
-
/** @type {number[]} */
|
|
32
|
-
raw.occupiedSize
|
|
33
|
-
) : volumeSize;
|
|
34
|
-
if (!dimensions.every((item) => Number.isInteger(item) && item > 0) || !volumeSize.every((item) => Number.isFinite(item) && item > 0) || !occupiedSize.every((item) => Number.isFinite(item) && item > 0) || raw.version !== 3 && (!Number.isInteger(raw.atlasColumns) || Number(raw.atlasColumns) < 1 || !Number.isInteger(raw.atlasRows) || Number(raw.atlasRows) < 1)) return null;
|
|
35
|
-
const sparse = raw.version === 3 && raw.storage === "sparse";
|
|
36
|
-
if (raw.version === 3 && (!sparse || !Number.isInteger(raw.surfaceCount) || Number(raw.surfaceCount) < 1 || !pair(raw.surfaceAtlasSize) || !pair(raw.indexAtlasSize))) return null;
|
|
37
|
-
return (
|
|
38
|
-
/** @type {PixelImpostorDescriptor} */
|
|
39
|
-
{
|
|
40
|
-
version: (
|
|
41
|
-
/** @type {1 | 2 | 3} */
|
|
42
|
-
raw.version
|
|
43
|
-
),
|
|
44
|
-
dimensions: [...dimensions],
|
|
45
|
-
atlasColumns: sparse ? 0 : Number(raw.atlasColumns),
|
|
46
|
-
atlasRows: sparse ? 0 : Number(raw.atlasRows),
|
|
47
|
-
volumeMin: [.../** @type {number[]} */
|
|
48
|
-
raw.volumeMin],
|
|
49
|
-
volumeSize: [...volumeSize],
|
|
50
|
-
occupiedMin: [...occupiedMin],
|
|
51
|
-
occupiedSize: [...occupiedSize],
|
|
52
|
-
alphaCutoff: Number.isFinite(raw.alphaCutoff) ? Number(raw.alphaCutoff) : 0.5,
|
|
53
|
-
storage: sparse ? "sparse" : "dense",
|
|
54
|
-
...sparse ? {
|
|
55
|
-
surfaceCount: Number(raw.surfaceCount),
|
|
56
|
-
surfaceAtlasSize: [.../** @type {number[]} */
|
|
57
|
-
raw.surfaceAtlasSize],
|
|
58
|
-
indexAtlasSize: [.../** @type {number[]} */
|
|
59
|
-
raw.indexAtlasSize]
|
|
60
|
-
} : {}
|
|
61
|
-
}
|
|
62
|
-
);
|
|
12
|
+
// src/rendering/classic-webgl-support.js
|
|
13
|
+
function classicWebGLSupportsNodeMaterials(revision) {
|
|
14
|
+
const fork = /^186\.2-alpha\.(\d+)$/.exec(revision);
|
|
15
|
+
if (fork) return Number(fork[1]) >= 2;
|
|
16
|
+
return Number.parseInt(revision, 10) >= 187;
|
|
63
17
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
function pair(value) {
|
|
68
|
-
return Array.isArray(value) && value.length === 2 && value.every((item) => Number.isInteger(item) && item > 0);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// src/pixel-impostor/pixel-impostor-runtime.js
|
|
72
|
-
var NeedlePixelImpostorLoaderPlugin = class {
|
|
73
|
-
/** @param {unknown} parser */
|
|
74
|
-
constructor(parser) {
|
|
75
|
-
this.name = NEEDLE_PIXEL_IMPOSTOR;
|
|
76
|
-
this.parser = parser;
|
|
18
|
+
var init_classic_webgl_support = __esm({
|
|
19
|
+
"src/rendering/classic-webgl-support.js"() {
|
|
20
|
+
"use strict";
|
|
77
21
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
const texture2 = await parser.getDependency("texture", Number(index));
|
|
110
|
-
texture2.colorSpace = colorSpace;
|
|
111
|
-
texture2.magFilter = NearestFilter;
|
|
112
|
-
texture2.minFilter = NearestFilter;
|
|
113
|
-
texture2.wrapS = ClampToEdgeWrapping;
|
|
114
|
-
texture2.wrapT = ClampToEdgeWrapping;
|
|
115
|
-
texture2.generateMipmaps = false;
|
|
116
|
-
texture2.needsUpdate = true;
|
|
117
|
-
return texture2;
|
|
118
|
-
};
|
|
119
|
-
const surfacePointsIndex = record.surfacePoints;
|
|
120
|
-
const surfacePointsPromise = Number.isInteger(surfacePointsIndex) ? parser.getDependency("accessor", Number(surfacePointsIndex)) : Promise.resolve(null);
|
|
121
|
-
pending.push(Promise.all([
|
|
122
|
-
loadTexture("atlas", SRGBColorSpace, true),
|
|
123
|
-
loadTexture("normalAtlas", NoColorSpace),
|
|
124
|
-
loadTexture("ormAtlas", NoColorSpace),
|
|
125
|
-
loadTexture("indexAtlas", NoColorSpace, descriptor.version === 3),
|
|
126
|
-
surfacePointsPromise
|
|
127
|
-
]).then(([atlas, normalAtlas, ormAtlas, indexAtlas, surfacePoints]) => {
|
|
128
|
-
if (!atlas) throw new Error("NEEDLE_pixel_impostor atlas is unavailable.");
|
|
129
|
-
object.userData.pixelImpostor = {
|
|
130
|
-
descriptor,
|
|
131
|
-
atlas,
|
|
132
|
-
normalAtlas,
|
|
133
|
-
ormAtlas,
|
|
134
|
-
indexAtlas,
|
|
135
|
-
surfacePoints
|
|
136
|
-
};
|
|
137
|
-
}));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// src/rendering/renderer-support.js
|
|
25
|
+
var renderer_support_exports = {};
|
|
26
|
+
__export(renderer_support_exports, {
|
|
27
|
+
enableRendererNodeMaterials: () => enableRendererNodeMaterials,
|
|
28
|
+
rendererSupportsImpostorNodeMaterials: () => rendererSupportsImpostorNodeMaterials
|
|
29
|
+
});
|
|
30
|
+
import { REVISION } from "three";
|
|
31
|
+
import * as TSL from "three/tsl";
|
|
32
|
+
function rendererSupportsImpostorNodeMaterials(renderer) {
|
|
33
|
+
if (typeof Reflect.get(TSL, "struct") !== "function") return false;
|
|
34
|
+
return renderer?.isWebGPURenderer === true || renderer?.capabilities?.supportsNodeMaterials === true;
|
|
35
|
+
}
|
|
36
|
+
async function enableRendererNodeMaterials(renderer) {
|
|
37
|
+
if (rendererSupportsImpostorNodeMaterials(renderer) || !renderer?.isWebGLRenderer || !renderer.setNodesHandler || typeof Reflect.get(TSL, "struct") !== "function") return;
|
|
38
|
+
let setup = pending.get(renderer);
|
|
39
|
+
if (!setup) {
|
|
40
|
+
setup = import("three/addons/tsl/WebGLNodesHandler.js").then(({ WebGLNodesHandler }) => {
|
|
41
|
+
if (rendererSupportsImpostorNodeMaterials(renderer)) return;
|
|
42
|
+
if (!classicWebGLSupportsNodeMaterials(REVISION)) {
|
|
43
|
+
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.`);
|
|
44
|
+
}
|
|
45
|
+
renderer.setNodesHandler?.(new WebGLNodesHandler());
|
|
46
|
+
renderer.capabilities ??= {};
|
|
47
|
+
renderer.capabilities.supportsNodeMaterials = true;
|
|
48
|
+
}).catch((error) => {
|
|
49
|
+
pending.delete(renderer);
|
|
50
|
+
throw error;
|
|
138
51
|
});
|
|
139
|
-
|
|
52
|
+
pending.set(renderer, setup);
|
|
140
53
|
}
|
|
141
|
-
|
|
142
|
-
function registerNeedlePixelImpostorLoader(loader) {
|
|
143
|
-
loader.register((parser) => new NeedlePixelImpostorLoaderPlugin(parser));
|
|
144
|
-
return loader;
|
|
54
|
+
await setup;
|
|
145
55
|
}
|
|
56
|
+
var pending;
|
|
57
|
+
var init_renderer_support = __esm({
|
|
58
|
+
"src/rendering/renderer-support.js"() {
|
|
59
|
+
"use strict";
|
|
60
|
+
init_classic_webgl_support();
|
|
61
|
+
pending = /* @__PURE__ */ new WeakMap();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
146
64
|
|
|
147
65
|
// src/rendering/captured-surface-material.js
|
|
148
66
|
import { MeshStandardNodeMaterial } from "three/webgpu";
|
|
149
67
|
import { context, modelViewMatrix, modelWorldMatrix, vec4 } from "three/tsl";
|
|
150
|
-
var CapturedSurfaceMaterial
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
68
|
+
var CapturedSurfaceMaterial;
|
|
69
|
+
var init_captured_surface_material = __esm({
|
|
70
|
+
"src/rendering/captured-surface-material.js"() {
|
|
71
|
+
"use strict";
|
|
72
|
+
CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
|
|
73
|
+
/** @type {import('three/webgpu').Node<'vec3'> | null} */
|
|
74
|
+
capturedPositionNode = null;
|
|
75
|
+
/**
|
|
76
|
+
* @override
|
|
77
|
+
* @param {import('three/webgpu').NodeBuilder} builder
|
|
78
|
+
*/
|
|
79
|
+
setupLighting(builder) {
|
|
80
|
+
const lighting = super.setupLighting(builder);
|
|
81
|
+
if (!this.capturedPositionNode) return lighting;
|
|
82
|
+
const world = modelWorldMatrix.mul(vec4(this.capturedPositionNode, 1)).xyz;
|
|
83
|
+
const view = modelViewMatrix.mul(vec4(this.capturedPositionNode, 1)).xyz;
|
|
84
|
+
return context(lighting, {
|
|
85
|
+
positionView: view,
|
|
86
|
+
positionWorld: world,
|
|
87
|
+
shadowPositionWorld: world
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
167
91
|
}
|
|
168
|
-
};
|
|
92
|
+
});
|
|
169
93
|
|
|
170
94
|
// src/rendering/captured-surface-depth.js
|
|
171
95
|
import {
|
|
@@ -196,8 +120,18 @@ function capturedSurfaceDepth(position, shadowBias = 0) {
|
|
|
196
120
|
}
|
|
197
121
|
)();
|
|
198
122
|
}
|
|
123
|
+
var init_captured_surface_depth = __esm({
|
|
124
|
+
"src/rendering/captured-surface-depth.js"() {
|
|
125
|
+
"use strict";
|
|
126
|
+
}
|
|
127
|
+
});
|
|
199
128
|
|
|
200
129
|
// src/pixel-impostor/pixel-impostor-display.js
|
|
130
|
+
var pixel_impostor_display_exports = {};
|
|
131
|
+
__export(pixel_impostor_display_exports, {
|
|
132
|
+
createPixelImpostorDisplay: () => createPixelImpostorDisplay,
|
|
133
|
+
createPixelImpostorRaycastPreview: () => createPixelImpostorRaycastPreview
|
|
134
|
+
});
|
|
201
135
|
import * as THREE from "three";
|
|
202
136
|
import { MeshBasicNodeMaterial, MeshStandardNodeMaterial as MeshStandardNodeMaterial2 } from "three/webgpu";
|
|
203
137
|
import {
|
|
@@ -282,7 +216,7 @@ function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
282
216
|
const normalAtlas = binding.normalAtlas instanceof THREE.Texture ? binding.normalAtlas : null;
|
|
283
217
|
const ormAtlas = binding.ormAtlas instanceof THREE.Texture ? binding.ormAtlas : null;
|
|
284
218
|
const indexAtlas = binding.indexAtlas instanceof THREE.Texture ? binding.indexAtlas : null;
|
|
285
|
-
const sparse = descriptor.
|
|
219
|
+
const sparse = descriptor.storage === "sparse";
|
|
286
220
|
if (sparse && !indexAtlas) return null;
|
|
287
221
|
for (const map of [normalAtlas, ormAtlas, indexAtlas]) {
|
|
288
222
|
if (!map) continue;
|
|
@@ -540,15 +474,16 @@ function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
540
474
|
...Object.values(raycastMaterials.plain)
|
|
541
475
|
];
|
|
542
476
|
const surfacePoints = binding.surfacePoints instanceof THREE.BufferAttribute || binding.surfacePoints instanceof THREE.InterleavedBufferAttribute ? binding.surfacePoints : null;
|
|
477
|
+
const shellless = descriptor.voxelShell === false;
|
|
543
478
|
let shell = null;
|
|
544
|
-
asset.traverse((object) => {
|
|
479
|
+
if (!shellless) asset.traverse((object) => {
|
|
545
480
|
if (!shell && object instanceof THREE.Mesh && (object.geometry.getAttribute("_voxel_center") || surfacePoints)) {
|
|
546
481
|
shell = object;
|
|
547
482
|
}
|
|
548
483
|
});
|
|
549
|
-
if (!shell) return null;
|
|
484
|
+
if (!shell && !shellless) return null;
|
|
550
485
|
const voxelShell = (
|
|
551
|
-
/** @type {THREE.Mesh} */
|
|
486
|
+
/** @type {THREE.Mesh | null} */
|
|
552
487
|
shell
|
|
553
488
|
);
|
|
554
489
|
const geometryBaseSample = texture(atlas, uv());
|
|
@@ -591,14 +526,23 @@ function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
591
526
|
mapped: createGeometryMaterial(true),
|
|
592
527
|
plain: createGeometryMaterial(false)
|
|
593
528
|
};
|
|
594
|
-
const previousGeometryMaterial = voxelShell
|
|
595
|
-
voxelShell
|
|
596
|
-
|
|
529
|
+
const previousGeometryMaterial = voxelShell?.material ?? null;
|
|
530
|
+
if (voxelShell) {
|
|
531
|
+
voxelShell.material = geometryMaterials.mapped;
|
|
532
|
+
voxelShell.userData.pixelImpostorAlternateMaterials = Object.values(geometryMaterials);
|
|
533
|
+
}
|
|
597
534
|
const skinnedSplats = voxelShell instanceof THREE.SkinnedMesh;
|
|
598
535
|
const useSurfacePoints = surfacePoints && !skinnedSplats;
|
|
599
|
-
|
|
536
|
+
const splatsAvailable = Boolean(voxelShell || useSurfacePoints);
|
|
537
|
+
let splatGeometry = skinnedSplats ? (
|
|
538
|
+
/** @type {THREE.Mesh} */
|
|
539
|
+
voxelShell.geometry.clone()
|
|
540
|
+
) : voxelShell?.geometry ?? new THREE.BufferGeometry();
|
|
600
541
|
if (skinnedSplats) {
|
|
601
|
-
const centers =
|
|
542
|
+
const centers = (
|
|
543
|
+
/** @type {THREE.Mesh} */
|
|
544
|
+
voxelShell.geometry.getAttribute("_voxel_center")
|
|
545
|
+
);
|
|
602
546
|
if (!centers) throw new Error("Animated voxel splats require retained voxel centers.");
|
|
603
547
|
splatGeometry.setAttribute("position", centers.clone());
|
|
604
548
|
}
|
|
@@ -629,8 +573,8 @@ function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
629
573
|
splatGeometry.setAttribute("_voxel_coord", new THREE.InstancedBufferAttribute(coordinates, 3));
|
|
630
574
|
splatGeometry.setAttribute("_voxel_record", new THREE.InstancedBufferAttribute(records, 1));
|
|
631
575
|
splatGeometry.setIndex([0, 1, 2, 0, 2, 3]);
|
|
632
|
-
splatGeometry.boundingBox = voxelShell
|
|
633
|
-
splatGeometry.boundingSphere = voxelShell
|
|
576
|
+
splatGeometry.boundingBox = voxelShell?.geometry.boundingBox?.clone() ?? bounds.clone();
|
|
577
|
+
splatGeometry.boundingSphere = voxelShell?.geometry.boundingSphere?.clone() ?? splatGeometry.boundingBox.getBoundingSphere(new THREE.Sphere());
|
|
634
578
|
}
|
|
635
579
|
const voxelCoordinate = attribute(
|
|
636
580
|
"_voxel_coord",
|
|
@@ -760,8 +704,8 @@ function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
760
704
|
snappedShell.bindMatrixInverse.copy(voxelShell.bindMatrixInverse);
|
|
761
705
|
}
|
|
762
706
|
asset.updateWorldMatrix(true, true);
|
|
763
|
-
voxelShell
|
|
764
|
-
if (!useSurfacePoints) {
|
|
707
|
+
voxelShell?.updateWorldMatrix(true, false);
|
|
708
|
+
if (!useSurfacePoints && voxelShell) {
|
|
765
709
|
snappedShell.matrix.copy(asset.matrixWorld).invert().multiply(voxelShell.matrixWorld);
|
|
766
710
|
snappedShell.matrixAutoUpdate = false;
|
|
767
711
|
}
|
|
@@ -794,8 +738,14 @@ function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
|
794
738
|
preview.userData.pixelSizeValue = 5;
|
|
795
739
|
preview.add(raycast, snappedShell);
|
|
796
740
|
const textureBytes = textureFootprintBytes(atlas) + (normalAtlas ? textureFootprintBytes(normalAtlas) : 0) + (ormAtlas ? textureFootprintBytes(ormAtlas) : 0) + (indexAtlas ? textureFootprintBytes(indexAtlas) : 0);
|
|
797
|
-
const shellStats = pixelDisplayMeshStats(voxelShell, textureBytes);
|
|
798
|
-
const splatStats = pixelDisplayMeshStats(snappedShell, textureFootprintBytes(atlas));
|
|
741
|
+
const shellStats = voxelShell ? pixelDisplayMeshStats(voxelShell, textureBytes) : null;
|
|
742
|
+
const splatStats = splatsAvailable ? pixelDisplayMeshStats(snappedShell, textureFootprintBytes(atlas)) : null;
|
|
743
|
+
preview.userData.availableModes = [
|
|
744
|
+
...voxelShell ? ["geometry"] : [],
|
|
745
|
+
...splatsAvailable ? ["vertex-snap"] : [],
|
|
746
|
+
"raycast",
|
|
747
|
+
"screen-pixels"
|
|
748
|
+
];
|
|
799
749
|
const raycastStats = pixelDisplayMeshStats(raycast, textureBytes);
|
|
800
750
|
preview.userData.displayStats = {
|
|
801
751
|
geometry: shellStats,
|
|
@@ -855,11 +805,18 @@ function createPixelImpostorDisplay(asset, options = {}) {
|
|
|
855
805
|
if (object instanceof THREE.Mesh) originals.set(object, object.visible);
|
|
856
806
|
});
|
|
857
807
|
if (options.attach !== false) asset.add(preview);
|
|
858
|
-
|
|
808
|
+
const stored = (
|
|
809
|
+
/** @type {Partial<import('./pixel-impostor-metadata.js').PixelImpostorDisplay>} */
|
|
810
|
+
/** @type {{display?: object}} */
|
|
811
|
+
(binding.descriptor ?? {}).display ?? {}
|
|
812
|
+
);
|
|
813
|
+
options = { ...stored, ...Object.fromEntries(Object.entries(options).filter(([, value]) => value !== void 0)) };
|
|
814
|
+
let mode = options.mode ?? (preview.userData.availableModes.includes("geometry") ? "geometry" : "screen-pixels");
|
|
859
815
|
let pbr = options.pbr ?? true;
|
|
860
816
|
let antialias = options.antialias ?? true;
|
|
861
817
|
const setMode = (next) => {
|
|
862
818
|
if (!["geometry", "vertex-snap", "raycast", "screen-pixels"].includes(next)) throw new Error(`Unknown pixel display mode: ${next}`);
|
|
819
|
+
if (!preview.userData.availableModes.includes(next)) throw new Error(`This pixel impostor has no voxel shell, so it cannot use the ${next} display mode.`);
|
|
863
820
|
mode = next;
|
|
864
821
|
preview.userData.displayMode = next;
|
|
865
822
|
for (const [mesh, visible] of originals) mesh.visible = next === "geometry" && visible;
|
|
@@ -913,11 +870,244 @@ function createPixelImpostorDisplay(asset, options = {}) {
|
|
|
913
870
|
}
|
|
914
871
|
};
|
|
915
872
|
}
|
|
873
|
+
var init_pixel_impostor_display = __esm({
|
|
874
|
+
"src/pixel-impostor/pixel-impostor-display.js"() {
|
|
875
|
+
"use strict";
|
|
876
|
+
init_captured_surface_material();
|
|
877
|
+
init_captured_surface_depth();
|
|
878
|
+
}
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
// src/pixel-impostor/pixel-impostor-runtime.js
|
|
882
|
+
import { ClampToEdgeWrapping, NearestFilter as NearestFilter2, NoColorSpace as NoColorSpace2, SRGBColorSpace } from "three";
|
|
883
|
+
|
|
884
|
+
// src/pixel-impostor/pixel-impostor-metadata.js
|
|
885
|
+
var NEEDLE_PIXEL_IMPOSTOR = "NEEDLE_pixel_impostor";
|
|
886
|
+
var DEFAULT_PIXEL_IMPOSTOR_DISPLAY = Object.freeze({
|
|
887
|
+
mode: (
|
|
888
|
+
/** @type {PixelImpostorDisplay['mode']} */
|
|
889
|
+
"geometry"
|
|
890
|
+
),
|
|
891
|
+
pixelSize: 5,
|
|
892
|
+
splatScale: 1,
|
|
893
|
+
snapMode: (
|
|
894
|
+
/** @type {PixelImpostorDisplay['snapMode']} */
|
|
895
|
+
"nearest-grid"
|
|
896
|
+
),
|
|
897
|
+
antialias: true,
|
|
898
|
+
pbr: true,
|
|
899
|
+
projectedNormalMix: 1
|
|
900
|
+
});
|
|
901
|
+
var PIXEL_IMPOSTOR_VOLUME_MODES = Object.freeze(
|
|
902
|
+
/** @type {const} */
|
|
903
|
+
["raycast", "screen-pixels"]
|
|
904
|
+
);
|
|
905
|
+
function normalizePixelImpostorDisplay(input = {}) {
|
|
906
|
+
const value = { ...DEFAULT_PIXEL_IMPOSTOR_DISPLAY, ...input };
|
|
907
|
+
if (!["geometry", "vertex-snap", "raycast", "screen-pixels"].includes(value.mode) || !Number.isInteger(value.pixelSize) || value.pixelSize < 1 || value.pixelSize > 64 || !Number.isFinite(value.splatScale) || value.splatScale <= 0 || value.splatScale > 4 || !["nearest-grid", "single-cell", "center-only"].includes(value.snapMode) || typeof value.antialias !== "boolean" || typeof value.pbr !== "boolean" || !Number.isFinite(value.projectedNormalMix) || value.projectedNormalMix < 0 || value.projectedNormalMix > 1)
|
|
908
|
+
throw new Error("Invalid pixel impostor display settings.");
|
|
909
|
+
return {
|
|
910
|
+
mode: value.mode,
|
|
911
|
+
pixelSize: value.pixelSize,
|
|
912
|
+
splatScale: value.splatScale,
|
|
913
|
+
snapMode: value.snapMode,
|
|
914
|
+
antialias: value.antialias,
|
|
915
|
+
pbr: value.pbr,
|
|
916
|
+
projectedNormalMix: value.projectedNormalMix
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
function parsePixelImpostorDescriptor(value) {
|
|
920
|
+
if (!value || typeof value !== "object") return null;
|
|
921
|
+
const raw = (
|
|
922
|
+
/** @type {Record<string, unknown>} */
|
|
923
|
+
value
|
|
924
|
+
);
|
|
925
|
+
const v4 = typeof raw.version === "string" && /^4\.(?:0|[1-9][0-9]*)$/.test(raw.version);
|
|
926
|
+
if (![1, 2, 3].includes(
|
|
927
|
+
/** @type {number} */
|
|
928
|
+
raw.version
|
|
929
|
+
) && !v4 || !triplet(raw.dimensions) || !triplet(raw.volumeMin) || !triplet(raw.volumeSize)) return null;
|
|
930
|
+
const dimensions = (
|
|
931
|
+
/** @type {number[]} */
|
|
932
|
+
raw.dimensions
|
|
933
|
+
);
|
|
934
|
+
const volumeSize = (
|
|
935
|
+
/** @type {number[]} */
|
|
936
|
+
raw.volumeSize
|
|
937
|
+
);
|
|
938
|
+
const occupiedMin = triplet(raw.occupiedMin) ? (
|
|
939
|
+
/** @type {number[]} */
|
|
940
|
+
raw.occupiedMin
|
|
941
|
+
) : (
|
|
942
|
+
/** @type {number[]} */
|
|
943
|
+
raw.volumeMin
|
|
944
|
+
);
|
|
945
|
+
const occupiedSize = triplet(raw.occupiedSize) ? (
|
|
946
|
+
/** @type {number[]} */
|
|
947
|
+
raw.occupiedSize
|
|
948
|
+
) : volumeSize;
|
|
949
|
+
if (!dimensions.every((item) => Number.isInteger(item) && item > 0) || !volumeSize.every((item) => Number.isFinite(item) && item > 0) || !occupiedSize.every((item) => Number.isFinite(item) && item > 0) || v4 && raw.storage !== "sparse" && raw.storage !== "dense") return null;
|
|
950
|
+
if (!((raw.version === 3 || v4) && raw.storage === "sparse") && (!Number.isInteger(raw.atlasColumns) || Number(raw.atlasColumns) < 1 || !Number.isInteger(raw.atlasRows) || Number(raw.atlasRows) < 1)) return null;
|
|
951
|
+
const sparse = (raw.version === 3 || v4) && raw.storage === "sparse";
|
|
952
|
+
let display = null;
|
|
953
|
+
try {
|
|
954
|
+
display = raw.display && typeof raw.display === "object" ? normalizePixelImpostorDisplay(
|
|
955
|
+
/** @type {Partial<PixelImpostorDisplay>} */
|
|
956
|
+
raw.display
|
|
957
|
+
) : null;
|
|
958
|
+
} catch {
|
|
959
|
+
display = null;
|
|
960
|
+
}
|
|
961
|
+
if (raw.version === 3 && !sparse || sparse && (!Number.isInteger(raw.surfaceCount) || Number(raw.surfaceCount) < 1 || !pair(raw.surfaceAtlasSize) || !pair(raw.indexAtlasSize))) return null;
|
|
962
|
+
return (
|
|
963
|
+
/** @type {PixelImpostorDescriptor} */
|
|
964
|
+
{
|
|
965
|
+
version: (
|
|
966
|
+
/** @type {1 | 2 | 3 | string} */
|
|
967
|
+
raw.version
|
|
968
|
+
),
|
|
969
|
+
dimensions: [...dimensions],
|
|
970
|
+
atlasColumns: sparse ? 0 : Number(raw.atlasColumns),
|
|
971
|
+
atlasRows: sparse ? 0 : Number(raw.atlasRows),
|
|
972
|
+
volumeMin: [.../** @type {number[]} */
|
|
973
|
+
raw.volumeMin],
|
|
974
|
+
volumeSize: [...volumeSize],
|
|
975
|
+
occupiedMin: [...occupiedMin],
|
|
976
|
+
occupiedSize: [...occupiedSize],
|
|
977
|
+
alphaCutoff: Number.isFinite(raw.alphaCutoff) ? Number(raw.alphaCutoff) : 0.5,
|
|
978
|
+
storage: sparse ? "sparse" : "dense",
|
|
979
|
+
...sparse ? {
|
|
980
|
+
surfaceCount: Number(raw.surfaceCount),
|
|
981
|
+
surfaceAtlasSize: [.../** @type {number[]} */
|
|
982
|
+
raw.surfaceAtlasSize],
|
|
983
|
+
indexAtlasSize: [.../** @type {number[]} */
|
|
984
|
+
raw.indexAtlasSize]
|
|
985
|
+
} : {},
|
|
986
|
+
...display ? { display } : {},
|
|
987
|
+
...v4 && raw.voxelShell === false ? { voxelShell: (
|
|
988
|
+
/** @type {const} */
|
|
989
|
+
false
|
|
990
|
+
) } : {}
|
|
991
|
+
}
|
|
992
|
+
);
|
|
993
|
+
}
|
|
994
|
+
function triplet(value) {
|
|
995
|
+
return Array.isArray(value) && value.length === 3 && value.every((item) => Number.isFinite(item));
|
|
996
|
+
}
|
|
997
|
+
function pair(value) {
|
|
998
|
+
return Array.isArray(value) && value.length === 2 && value.every((item) => Number.isInteger(item) && item > 0);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// src/pixel-impostor/pixel-impostor-runtime.js
|
|
1002
|
+
var NeedlePixelImpostorLoaderPlugin = class {
|
|
1003
|
+
/** @param {unknown} parser @param {{renderer?: import('../rendering/renderer-support.js').ImpostorRenderer}} [options] */
|
|
1004
|
+
constructor(parser, options = {}) {
|
|
1005
|
+
this.name = NEEDLE_PIXEL_IMPOSTOR;
|
|
1006
|
+
this.parser = parser;
|
|
1007
|
+
this.options = options;
|
|
1008
|
+
}
|
|
1009
|
+
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
|
|
1010
|
+
async afterRoot(result) {
|
|
1011
|
+
const parser = (
|
|
1012
|
+
/** @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>}} */
|
|
1013
|
+
this.parser
|
|
1014
|
+
);
|
|
1015
|
+
const pending2 = (
|
|
1016
|
+
/** @type {Promise<void>[]} */
|
|
1017
|
+
[]
|
|
1018
|
+
);
|
|
1019
|
+
result.scene?.traverse((object) => {
|
|
1020
|
+
if (!("isMesh" in object) || object.isMesh !== true) return;
|
|
1021
|
+
const association = parser.associations.get(object);
|
|
1022
|
+
if (association?.meshes === void 0 || association.primitives === void 0) return;
|
|
1023
|
+
const raw = parser.json.meshes?.[association.meshes]?.primitives[association.primitives]?.extensions?.[NEEDLE_PIXEL_IMPOSTOR];
|
|
1024
|
+
const descriptor = parsePixelImpostorDescriptor(raw);
|
|
1025
|
+
if (!descriptor || !raw || typeof raw !== "object") return;
|
|
1026
|
+
const record = (
|
|
1027
|
+
/** @type {Record<string, unknown>} */
|
|
1028
|
+
raw
|
|
1029
|
+
);
|
|
1030
|
+
const atlasInfo = record.atlas;
|
|
1031
|
+
const atlasIndex = atlasInfo && typeof atlasInfo === "object" ? Reflect.get(atlasInfo, "index") : void 0;
|
|
1032
|
+
if (!Number.isInteger(atlasIndex)) throw new Error("NEEDLE_pixel_impostor has no atlas binding.");
|
|
1033
|
+
const loadTexture = async (role, colorSpace, required = false) => {
|
|
1034
|
+
const info = record[role];
|
|
1035
|
+
const index = info && typeof info === "object" ? Reflect.get(info, "index") : void 0;
|
|
1036
|
+
if (!Number.isInteger(index)) {
|
|
1037
|
+
if (required) throw new Error(`NEEDLE_pixel_impostor has no ${role} binding.`);
|
|
1038
|
+
return null;
|
|
1039
|
+
}
|
|
1040
|
+
const texture2 = await parser.getDependency("texture", Number(index));
|
|
1041
|
+
texture2.colorSpace = colorSpace;
|
|
1042
|
+
texture2.magFilter = NearestFilter2;
|
|
1043
|
+
texture2.minFilter = NearestFilter2;
|
|
1044
|
+
texture2.wrapS = ClampToEdgeWrapping;
|
|
1045
|
+
texture2.wrapT = ClampToEdgeWrapping;
|
|
1046
|
+
texture2.generateMipmaps = false;
|
|
1047
|
+
texture2.needsUpdate = true;
|
|
1048
|
+
return texture2;
|
|
1049
|
+
};
|
|
1050
|
+
const surfacePointsIndex = record.surfacePoints;
|
|
1051
|
+
const surfacePointsPromise = Number.isInteger(surfacePointsIndex) ? parser.getDependency("accessor", Number(surfacePointsIndex)) : Promise.resolve(null);
|
|
1052
|
+
pending2.push(Promise.all([
|
|
1053
|
+
loadTexture("atlas", SRGBColorSpace, true),
|
|
1054
|
+
loadTexture("normalAtlas", NoColorSpace2),
|
|
1055
|
+
loadTexture("ormAtlas", NoColorSpace2),
|
|
1056
|
+
loadTexture("indexAtlas", NoColorSpace2, descriptor.storage === "sparse"),
|
|
1057
|
+
surfacePointsPromise
|
|
1058
|
+
]).then(([atlas, normalAtlas, ormAtlas, indexAtlas, surfacePoints]) => {
|
|
1059
|
+
if (!atlas) throw new Error("NEEDLE_pixel_impostor atlas is unavailable.");
|
|
1060
|
+
object.userData.pixelImpostor = {
|
|
1061
|
+
descriptor,
|
|
1062
|
+
atlas,
|
|
1063
|
+
normalAtlas,
|
|
1064
|
+
ormAtlas,
|
|
1065
|
+
indexAtlas,
|
|
1066
|
+
surfacePoints
|
|
1067
|
+
};
|
|
1068
|
+
}));
|
|
1069
|
+
});
|
|
1070
|
+
await Promise.all(pending2);
|
|
1071
|
+
await this.applyStoredDisplay(result);
|
|
1072
|
+
}
|
|
1073
|
+
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
|
|
1074
|
+
async applyStoredDisplay(result) {
|
|
1075
|
+
const renderer = this.options.renderer, scene = result.scene;
|
|
1076
|
+
if (!renderer || !scene) return;
|
|
1077
|
+
let binding = null;
|
|
1078
|
+
scene.traverse((object) => {
|
|
1079
|
+
if (!binding && object.userData.pixelImpostor?.descriptor?.display) binding = object.userData.pixelImpostor;
|
|
1080
|
+
});
|
|
1081
|
+
if (!binding) return;
|
|
1082
|
+
const { rendererSupportsImpostorNodeMaterials: rendererSupportsImpostorNodeMaterials2 } = await Promise.resolve().then(() => (init_renderer_support(), renderer_support_exports));
|
|
1083
|
+
if (!rendererSupportsImpostorNodeMaterials2(renderer)) {
|
|
1084
|
+
console.warn("[three-mesh-effects] NEEDLE_pixel_impostor displays need WebGPURenderer or a renderer with node-material support; drawing the file's mesh.");
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
const { createPixelImpostorDisplay: createPixelImpostorDisplay2 } = await Promise.resolve().then(() => (init_pixel_impostor_display(), pixel_impostor_display_exports));
|
|
1088
|
+
const display = createPixelImpostorDisplay2(scene, { binding, renderer: (
|
|
1089
|
+
/** @type {any} */
|
|
1090
|
+
renderer
|
|
1091
|
+
) });
|
|
1092
|
+
for (const child of display.preview.children) child.onBeforeRender = () => display.updateViewport();
|
|
1093
|
+
scene.userData.pixelImpostorDisplay = display;
|
|
1094
|
+
}
|
|
1095
|
+
};
|
|
1096
|
+
function registerNeedlePixelImpostorLoader(loader, options = {}) {
|
|
1097
|
+
loader.register((parser) => new NeedlePixelImpostorLoaderPlugin(parser, options));
|
|
1098
|
+
return loader;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
// src/public/pixel-impostor-runtime.js
|
|
1102
|
+
init_pixel_impostor_display();
|
|
916
1103
|
export {
|
|
1104
|
+
DEFAULT_PIXEL_IMPOSTOR_DISPLAY,
|
|
917
1105
|
NEEDLE_PIXEL_IMPOSTOR,
|
|
918
1106
|
NeedlePixelImpostorLoaderPlugin,
|
|
1107
|
+
PIXEL_IMPOSTOR_VOLUME_MODES,
|
|
919
1108
|
createPixelImpostorDisplay,
|
|
920
1109
|
createPixelImpostorRaycastPreview,
|
|
1110
|
+
normalizePixelImpostorDisplay,
|
|
921
1111
|
parsePixelImpostorDescriptor,
|
|
922
1112
|
registerNeedlePixelImpostorLoader
|
|
923
1113
|
};
|