@needle-tools/three-mesh-effects 0.1.0-alpha → 0.1.0-alpha.0
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/LICENSE +21 -21
- package/README.md +94 -88
- package/build/deformation-runtime.js.map +1 -1
- package/build/impostor-runtime-needle-engine.js.map +1 -1
- package/build/impostor-runtime.js.map +1 -1
- package/build/internal/deformation/cloth-surface.js.map +1 -1
- package/build/internal/deformation/fur-surface.js.map +1 -1
- package/build/internal/deformation/ik-constraints.js.map +1 -1
- package/build/internal/deformation/joints-json.js.map +1 -1
- package/build/internal/deformation/limits.js.map +1 -1
- package/build/internal/deformation/motion-settings.js.map +1 -1
- package/build/internal/deformation/pin-cloth.js.map +1 -1
- package/build/internal/deformation/pin-ik.js.map +1 -1
- package/build/internal/deformation/pin-types.js.map +1 -1
- package/build/internal/deformation/pin-wiggle.js.map +1 -1
- package/build/internal/deformation/pose-slots.js.map +1 -1
- package/build/internal/deformation/pose-timeline.js.map +1 -1
- package/build/internal/deformation/rig-drivers.js.map +1 -1
- package/build/internal/deformation/rig-extension.js.map +1 -1
- package/build/internal/deformation/rig-runtime.js.map +1 -1
- package/build/internal/deformation/wind.js.map +1 -1
- package/build/internal/impostor/impostor-metadata.js.map +1 -1
- package/build/internal/impostor/impostor-node-material.js.map +1 -1
- package/build/internal/impostor/impostor-runtime-support.js.map +1 -1
- package/build/internal/impostor/octahedral.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-display.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +1 -1
- package/build/internal/rendering/skin-projection.js +52 -52
- package/build/internal/rendering/skin-projection.js.map +2 -2
- package/build/internal/rendering/surface-query.js +52 -52
- package/build/internal/rendering/surface-query.js.map +2 -2
- package/build/internal/rendering/wireframe.js.map +1 -1
- package/build/internal/rendering/wrap-preview-settings.js.map +1 -1
- package/build/internal/rendering/wrap-preview.js +52 -52
- package/build/internal/rendering/wrap-preview.js.map +2 -2
- package/build/internal/rendering/wrap-skin.js +52 -52
- package/build/internal/rendering/wrap-skin.js.map +2 -2
- package/build/pixel-impostor-runtime.js.map +1 -1
- package/build/wrap-effects.js +52 -52
- package/build/wrap-effects.js.map +2 -2
- package/package.json +107 -94
- package/src/deformation/cloth-surface.ts +223 -223
- package/src/deformation/fur-surface.ts +129 -129
- package/src/deformation/ik-constraints.ts +37 -37
- package/src/deformation/joints-json.ts +94 -94
- package/src/deformation/limits.ts +25 -25
- package/src/deformation/motion-settings.ts +9 -9
- package/src/deformation/pin-cloth.ts +149 -149
- package/src/deformation/pin-ik.ts +582 -582
- package/src/deformation/pin-types.ts +22 -22
- package/src/deformation/pin-wiggle.ts +119 -119
- package/src/deformation/pose-slots.ts +106 -106
- package/src/deformation/pose-timeline.ts +242 -242
- package/src/deformation/rig-drivers.ts +32 -32
- package/src/deformation/rig-extension.ts +87 -87
- package/src/deformation/rig-runtime.ts +416 -416
- package/src/deformation/wind.ts +36 -36
- package/src/impostor/impostor-metadata.js +137 -137
- package/src/impostor/impostor-node-material.js +1581 -1581
- package/src/impostor/impostor-runtime-support.js +139 -139
- package/src/impostor/octahedral.js +280 -280
- package/src/pixel-impostor/pixel-impostor-display.js +794 -794
- package/src/pixel-impostor/pixel-impostor-metadata.js +74 -74
- package/src/pixel-impostor/pixel-impostor-runtime.js +78 -78
- package/src/public/deformation-runtime.ts +3 -3
- package/src/public/impostor-runtime-needle-engine.js +56 -56
- package/src/public/impostor-runtime.js +21 -21
- package/src/public/pixel-impostor-runtime.js +5 -5
- package/src/public/wrap-effects.js +21 -21
- package/src/rendering/skin-projection.js +113 -113
- package/src/rendering/surface-query.js +88 -88
- package/src/rendering/wireframe.js +115 -115
- package/src/rendering/wrap-preview-settings.js +123 -123
- package/src/rendering/wrap-preview.js +645 -645
- package/src/rendering/wrap-skin.js +109 -109
- package/build/internal/public/deformation-runtime.js +0 -2143
- package/build/internal/public/deformation-runtime.js.map +0 -7
- package/build/internal/public/impostor-runtime-needle-engine.js +0 -42
- package/build/internal/public/impostor-runtime-needle-engine.js.map +0 -7
- package/build/internal/public/impostor-runtime.js +0 -1333
- package/build/internal/public/impostor-runtime.js.map +0 -7
- package/build/internal/public/pixel-impostor-runtime.js +0 -876
- package/build/internal/public/pixel-impostor-runtime.js.map +0 -7
- package/build/internal/public/wrap-effects.js +0 -5640
- package/build/internal/public/wrap-effects.js.map +0 -7
|
@@ -1,876 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
|
|
3
|
-
// src/pixel-impostor/pixel-impostor-runtime.js
|
|
4
|
-
import { ClampToEdgeWrapping, NearestFilter, NoColorSpace, SRGBColorSpace } from "three";
|
|
5
|
-
|
|
6
|
-
// src/pixel-impostor/pixel-impostor-metadata.js
|
|
7
|
-
var NEEDLE_PIXEL_IMPOSTOR = "NEEDLE_pixel_impostor";
|
|
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
|
-
);
|
|
63
|
-
}
|
|
64
|
-
function triplet(value) {
|
|
65
|
-
return Array.isArray(value) && value.length === 3 && value.every((item) => Number.isFinite(item));
|
|
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;
|
|
77
|
-
}
|
|
78
|
-
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
|
|
79
|
-
async afterRoot(result) {
|
|
80
|
-
const parser = (
|
|
81
|
-
/** @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>}} */
|
|
82
|
-
this.parser
|
|
83
|
-
);
|
|
84
|
-
const pending = (
|
|
85
|
-
/** @type {Promise<void>[]} */
|
|
86
|
-
[]
|
|
87
|
-
);
|
|
88
|
-
result.scene?.traverse((object) => {
|
|
89
|
-
if (!("isMesh" in object) || object.isMesh !== true) return;
|
|
90
|
-
const association = parser.associations.get(object);
|
|
91
|
-
if (association?.meshes === void 0 || association.primitives === void 0) return;
|
|
92
|
-
const raw = parser.json.meshes?.[association.meshes]?.primitives[association.primitives]?.extensions?.[NEEDLE_PIXEL_IMPOSTOR];
|
|
93
|
-
const descriptor = parsePixelImpostorDescriptor(raw);
|
|
94
|
-
if (!descriptor || !raw || typeof raw !== "object") return;
|
|
95
|
-
const record = (
|
|
96
|
-
/** @type {Record<string, unknown>} */
|
|
97
|
-
raw
|
|
98
|
-
);
|
|
99
|
-
const atlasInfo = record.atlas;
|
|
100
|
-
const atlasIndex = atlasInfo && typeof atlasInfo === "object" ? Reflect.get(atlasInfo, "index") : void 0;
|
|
101
|
-
if (!Number.isInteger(atlasIndex)) throw new Error("NEEDLE_pixel_impostor has no atlas binding.");
|
|
102
|
-
const loadTexture = async (role, colorSpace, required = false) => {
|
|
103
|
-
const info = record[role];
|
|
104
|
-
const index = info && typeof info === "object" ? Reflect.get(info, "index") : void 0;
|
|
105
|
-
if (!Number.isInteger(index)) {
|
|
106
|
-
if (required) throw new Error(`NEEDLE_pixel_impostor has no ${role} binding.`);
|
|
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
|
-
}));
|
|
138
|
-
});
|
|
139
|
-
await Promise.all(pending);
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
function registerNeedlePixelImpostorLoader(loader) {
|
|
143
|
-
loader.register((parser) => new NeedlePixelImpostorLoaderPlugin(parser));
|
|
144
|
-
return loader;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// src/pixel-impostor/pixel-impostor-display.js
|
|
148
|
-
import * as THREE from "three";
|
|
149
|
-
import { MeshBasicNodeMaterial, MeshStandardNodeMaterial } from "three/webgpu";
|
|
150
|
-
import {
|
|
151
|
-
Break,
|
|
152
|
-
Fn,
|
|
153
|
-
If,
|
|
154
|
-
Loop,
|
|
155
|
-
abs,
|
|
156
|
-
attribute,
|
|
157
|
-
cameraProjectionMatrix,
|
|
158
|
-
cameraProjectionMatrixInverse,
|
|
159
|
-
cameraFar,
|
|
160
|
-
cameraNear,
|
|
161
|
-
cameraPosition,
|
|
162
|
-
cameraWorldMatrix,
|
|
163
|
-
ceil,
|
|
164
|
-
clamp,
|
|
165
|
-
countOneBits,
|
|
166
|
-
float,
|
|
167
|
-
fwidth,
|
|
168
|
-
getViewPosition,
|
|
169
|
-
max,
|
|
170
|
-
min,
|
|
171
|
-
mix,
|
|
172
|
-
modelViewMatrix,
|
|
173
|
-
modelWorldMatrix,
|
|
174
|
-
modelWorldMatrixInverse,
|
|
175
|
-
mod,
|
|
176
|
-
normalMap as normalMapNode,
|
|
177
|
-
normalView,
|
|
178
|
-
smoothstep,
|
|
179
|
-
step,
|
|
180
|
-
sRGBTransferEOTF,
|
|
181
|
-
positionLocal,
|
|
182
|
-
screenCoordinate,
|
|
183
|
-
screenSize,
|
|
184
|
-
texture,
|
|
185
|
-
transformNormalToView,
|
|
186
|
-
uint,
|
|
187
|
-
uniform,
|
|
188
|
-
uvec4,
|
|
189
|
-
uv,
|
|
190
|
-
vec2,
|
|
191
|
-
vec3,
|
|
192
|
-
vec4,
|
|
193
|
-
vertexColor,
|
|
194
|
-
viewZToOrthographicDepth,
|
|
195
|
-
viewZToPerspectiveDepth
|
|
196
|
-
} from "three/tsl";
|
|
197
|
-
function createPixelImpostorRaycastPreview(asset, bounds, descriptor) {
|
|
198
|
-
const binding = descriptor;
|
|
199
|
-
descriptor = binding.descriptor && typeof binding.descriptor === "object" ? (
|
|
200
|
-
/** @type {Record<string, unknown>} */
|
|
201
|
-
binding.descriptor
|
|
202
|
-
) : descriptor;
|
|
203
|
-
const dimensions = Array.isArray(descriptor.dimensions) ? descriptor.dimensions.map(Number) : [];
|
|
204
|
-
const planar = Math.round(dimensions[0] ?? 0);
|
|
205
|
-
const layers = Math.round(dimensions[1] ?? 0);
|
|
206
|
-
if (!(planar > 0) || !(layers > 0) || Math.round(dimensions[2] ?? 0) !== planar) return null;
|
|
207
|
-
const atlasTextures = (
|
|
208
|
-
/** @type {THREE.Texture[]} */
|
|
209
|
-
[]
|
|
210
|
-
);
|
|
211
|
-
if (binding.atlas instanceof THREE.Texture) atlasTextures.push(binding.atlas);
|
|
212
|
-
asset.traverse((object) => {
|
|
213
|
-
if (atlasTextures.length > 0 || !(object instanceof THREE.Mesh)) return;
|
|
214
|
-
const materials = Array.isArray(object.material) ? object.material : [object.material];
|
|
215
|
-
for (const material of materials) {
|
|
216
|
-
const map = (
|
|
217
|
-
/** @type {THREE.MeshBasicMaterial} */
|
|
218
|
-
material.map
|
|
219
|
-
);
|
|
220
|
-
if (map) {
|
|
221
|
-
atlasTextures.push(map);
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
const atlas = atlasTextures[0];
|
|
227
|
-
if (!atlas) return null;
|
|
228
|
-
atlas.magFilter = THREE.NearestFilter;
|
|
229
|
-
atlas.minFilter = THREE.NearestFilter;
|
|
230
|
-
atlas.generateMipmaps = false;
|
|
231
|
-
atlas.needsUpdate = true;
|
|
232
|
-
const normalAtlas = binding.normalAtlas instanceof THREE.Texture ? binding.normalAtlas : null;
|
|
233
|
-
const ormAtlas = binding.ormAtlas instanceof THREE.Texture ? binding.ormAtlas : null;
|
|
234
|
-
const indexAtlas = binding.indexAtlas instanceof THREE.Texture ? binding.indexAtlas : null;
|
|
235
|
-
const sparse = descriptor.version === 3 && descriptor.storage === "sparse";
|
|
236
|
-
if (sparse && !indexAtlas) return null;
|
|
237
|
-
for (const map of [normalAtlas, ormAtlas, indexAtlas]) {
|
|
238
|
-
if (!map) continue;
|
|
239
|
-
map.colorSpace = THREE.NoColorSpace;
|
|
240
|
-
map.magFilter = THREE.NearestFilter;
|
|
241
|
-
map.minFilter = THREE.NearestFilter;
|
|
242
|
-
map.generateMipmaps = false;
|
|
243
|
-
map.needsUpdate = true;
|
|
244
|
-
}
|
|
245
|
-
const authoredMin = Array.isArray(descriptor.volumeMin) ? descriptor.volumeMin.map(Number) : null;
|
|
246
|
-
const authoredSize = Array.isArray(descriptor.volumeSize) ? descriptor.volumeSize.map(Number) : null;
|
|
247
|
-
const size = authoredSize?.length === 3 && authoredSize.every((value) => value > 0) ? new THREE.Vector3().fromArray(authoredSize) : bounds.getSize(new THREE.Vector3());
|
|
248
|
-
const center = authoredMin?.length === 3 ? new THREE.Vector3().fromArray(authoredMin).addScaledVector(size, 0.5) : bounds.getCenter(new THREE.Vector3());
|
|
249
|
-
const authoredOccupiedMin = Array.isArray(descriptor.occupiedMin) ? descriptor.occupiedMin.map(Number) : null;
|
|
250
|
-
const authoredOccupiedSize = Array.isArray(descriptor.occupiedSize) ? descriptor.occupiedSize.map(Number) : null;
|
|
251
|
-
const raycastSize = authoredOccupiedSize?.length === 3 && authoredOccupiedSize.every((value) => value > 0) ? new THREE.Vector3().fromArray(authoredOccupiedSize) : size.clone();
|
|
252
|
-
const raycastCenter = authoredOccupiedMin?.length === 3 ? new THREE.Vector3().fromArray(authoredOccupiedMin).addScaledVector(raycastSize, 0.5) : center.clone();
|
|
253
|
-
const raycastOffset = raycastCenter.clone().sub(center);
|
|
254
|
-
const atlasColumns = Math.max(1, Math.round(Number(descriptor.atlasColumns) || Math.ceil(Math.sqrt(layers))));
|
|
255
|
-
const atlasRows = Math.max(1, Math.round(Number(descriptor.atlasRows) || Math.ceil(layers / atlasColumns)));
|
|
256
|
-
const surfaceAtlasSize = Array.isArray(descriptor.surfaceAtlasSize) ? descriptor.surfaceAtlasSize.map(Number) : [atlas.image.width, atlas.image.height];
|
|
257
|
-
const indexAtlasSize = Array.isArray(descriptor.indexAtlasSize) ? descriptor.indexAtlasSize.map(Number) : [1, 1];
|
|
258
|
-
const sizeNode = uniform(size.clone());
|
|
259
|
-
const raycastHalfSize = uniform(raycastSize.clone().multiplyScalar(0.5));
|
|
260
|
-
const raycastOffsetNode = uniform(raycastOffset);
|
|
261
|
-
const screenPixelSize = uniform(1);
|
|
262
|
-
const splatPixelSize = uniform(5);
|
|
263
|
-
const splatScale = uniform(1);
|
|
264
|
-
const projectedNormalMix = uniform(1);
|
|
265
|
-
const unlitMix = uniform(0);
|
|
266
|
-
const snapMode = uniform(0);
|
|
267
|
-
const viewportSize = uniform(new THREE.Vector2(1, 1));
|
|
268
|
-
const dpr = uniform(1);
|
|
269
|
-
const volumeDimensions = vec3(planar, layers, planar);
|
|
270
|
-
const voxelHalfExtents = vec3(
|
|
271
|
-
size.x / planar * 0.5,
|
|
272
|
-
size.y / layers * 0.5,
|
|
273
|
-
size.z / planar * 0.5
|
|
274
|
-
);
|
|
275
|
-
const minimumCellSize = Math.min(size.x / planar, size.y / layers, size.z / planar);
|
|
276
|
-
const maximumSteps = planar * 2 + layers + 3;
|
|
277
|
-
const linearTextureUv = (index, dimensions2) => vec2(
|
|
278
|
-
mod(index, dimensions2[0]).add(0.5).div(dimensions2[0]),
|
|
279
|
-
index.div(dimensions2[0]).floor().add(0.5).div(dimensions2[1])
|
|
280
|
-
);
|
|
281
|
-
const sparseUint = (texel) => {
|
|
282
|
-
if (!indexAtlas) throw new Error("Sparse pixel impostor requires an index atlas");
|
|
283
|
-
const bytes = uvec4(texture(indexAtlas, linearTextureUv(texel, indexAtlasSize)).mul(255).round());
|
|
284
|
-
return bytes.x.bitOr(bytes.y.shiftLeft(8)).bitOr(bytes.z.shiftLeft(16)).bitOr(bytes.w.shiftLeft(24));
|
|
285
|
-
};
|
|
286
|
-
const traceVolumeNode = Fn(([sampleOffset]) => {
|
|
287
|
-
const rasterPixelSize = screenPixelSize.mul(dpr);
|
|
288
|
-
const sampleCoordinate = screenCoordinate.div(rasterPixelSize).floor().mul(rasterPixelSize).add(
|
|
289
|
-
rasterPixelSize.mul(vec2(0.5).add(sampleOffset))
|
|
290
|
-
);
|
|
291
|
-
const sampleUv = sampleCoordinate.div(screenSize);
|
|
292
|
-
const nearView = getViewPosition(sampleUv, float(0), cameraProjectionMatrixInverse);
|
|
293
|
-
const farView = getViewPosition(sampleUv, float(1), cameraProjectionMatrixInverse);
|
|
294
|
-
const nearWorld = cameraWorldMatrix.mul(vec4(nearView, 1));
|
|
295
|
-
const farWorld = cameraWorldMatrix.mul(vec4(farView, 1));
|
|
296
|
-
const rayOrigin = modelWorldMatrixInverse.mul(nearWorld).xyz;
|
|
297
|
-
const rayFar = modelWorldMatrixInverse.mul(farWorld).xyz;
|
|
298
|
-
const rayDirection = rayFar.sub(rayOrigin).normalize();
|
|
299
|
-
const distanceA = raycastHalfSize.negate().sub(rayOrigin).div(rayDirection);
|
|
300
|
-
const distanceB = raycastHalfSize.sub(rayOrigin).div(rayDirection);
|
|
301
|
-
const nearestDistance = max(
|
|
302
|
-
max(min(distanceA.x, distanceB.x), min(distanceA.y, distanceB.y)),
|
|
303
|
-
min(distanceA.z, distanceB.z)
|
|
304
|
-
).max(0);
|
|
305
|
-
const exitDistance = min(
|
|
306
|
-
min(max(distanceA.x, distanceB.x), max(distanceA.y, distanceB.y)),
|
|
307
|
-
max(distanceA.z, distanceB.z)
|
|
308
|
-
);
|
|
309
|
-
const entry = rayOrigin.add(rayDirection.mul(nearestDistance));
|
|
310
|
-
const gridVelocity = rayDirection.div(sizeNode).mul(volumeDimensions);
|
|
311
|
-
const gridPoint = entry.add(raycastOffsetNode).div(sizeNode).add(0.5).mul(volumeDimensions).add(gridVelocity.mul(minimumCellSize * 1e-3));
|
|
312
|
-
const cell = exitDistance.greaterThanEqual(nearestDistance).select(gridPoint.floor(), vec3(-1)).toVar();
|
|
313
|
-
const directionStep = step(0, gridVelocity).mul(2).sub(1);
|
|
314
|
-
const absoluteVelocity = abs(gridVelocity).max(1e-6);
|
|
315
|
-
const cellFraction = gridPoint.sub(cell);
|
|
316
|
-
const positiveAxis = step(0, gridVelocity);
|
|
317
|
-
const nextDistance = cellFraction.mul(vec3(1).sub(positiveAxis)).add(vec3(1).sub(cellFraction).mul(positiveAxis));
|
|
318
|
-
const nextCrossing = nextDistance.div(absoluteVelocity).toVar();
|
|
319
|
-
const crossingDelta = vec3(1).div(absoluteVelocity);
|
|
320
|
-
const result = vec4(0, 0, 0, 0).toVar();
|
|
321
|
-
const travelled = nearestDistance.toVar();
|
|
322
|
-
Loop(maximumSteps, () => {
|
|
323
|
-
const outside = cell.x.lessThan(0).or(cell.y.lessThan(0)).or(cell.z.lessThan(0)).or(cell.x.greaterThanEqual(planar)).or(cell.y.greaterThanEqual(layers)).or(cell.z.greaterThanEqual(planar));
|
|
324
|
-
If(outside, () => {
|
|
325
|
-
Break();
|
|
326
|
-
});
|
|
327
|
-
const linearCell = cell.x.add(cell.z.add(cell.y.mul(planar)).mul(planar));
|
|
328
|
-
let cellOccupied;
|
|
329
|
-
if (sparse) {
|
|
330
|
-
const wordIndex = linearCell.div(32).floor();
|
|
331
|
-
const word = sparseUint(wordIndex.mul(2));
|
|
332
|
-
const bit = uint(mod(linearCell, 32));
|
|
333
|
-
cellOccupied = word.shiftRight(bit).bitAnd(1).equal(1);
|
|
334
|
-
} else {
|
|
335
|
-
const atlasColumn = mod(cell.y, atlasColumns);
|
|
336
|
-
const atlasRow = cell.y.div(atlasColumns).floor();
|
|
337
|
-
const atlasUv = vec2(
|
|
338
|
-
atlasColumn.mul(planar).add(cell.x).add(0.5).div(atlasColumns * planar),
|
|
339
|
-
atlasRow.mul(planar).add(cell.z).add(0.5).div(atlasRows * planar)
|
|
340
|
-
);
|
|
341
|
-
cellOccupied = texture(atlas, atlasUv).a.greaterThan(0.5);
|
|
342
|
-
}
|
|
343
|
-
If(cellOccupied, () => {
|
|
344
|
-
result.assign(vec4(
|
|
345
|
-
rayOrigin.add(rayDirection.mul(travelled)),
|
|
346
|
-
linearCell.add(1)
|
|
347
|
-
));
|
|
348
|
-
Break();
|
|
349
|
-
});
|
|
350
|
-
const nearestCrossing = min(nextCrossing.x, min(nextCrossing.y, nextCrossing.z));
|
|
351
|
-
travelled.assign(nearestDistance.add(nearestCrossing));
|
|
352
|
-
If(nextCrossing.x.lessThanEqual(nearestCrossing.add(1e-6)), () => {
|
|
353
|
-
cell.x.addAssign(directionStep.x);
|
|
354
|
-
nextCrossing.x.addAssign(crossingDelta.x);
|
|
355
|
-
});
|
|
356
|
-
If(nextCrossing.y.lessThanEqual(nearestCrossing.add(1e-6)), () => {
|
|
357
|
-
cell.y.addAssign(directionStep.y);
|
|
358
|
-
nextCrossing.y.addAssign(crossingDelta.y);
|
|
359
|
-
});
|
|
360
|
-
If(nextCrossing.z.lessThanEqual(nearestCrossing.add(1e-6)), () => {
|
|
361
|
-
cell.z.addAssign(directionStep.z);
|
|
362
|
-
nextCrossing.z.addAssign(crossingDelta.z);
|
|
363
|
-
});
|
|
364
|
-
});
|
|
365
|
-
return result;
|
|
366
|
-
});
|
|
367
|
-
const traceVolume = (
|
|
368
|
-
/** @type {(sampleOffset: import('three/webgpu').Node<'vec2'>) => import('three/webgpu').Node<'vec4'>} */
|
|
369
|
-
/** @type {unknown} */
|
|
370
|
-
traceVolumeNode
|
|
371
|
-
);
|
|
372
|
-
const reconstructedDepth = Fn(([localPosition], builder) => {
|
|
373
|
-
const viewZ = modelViewMatrix.mul(vec4(localPosition, 1)).z;
|
|
374
|
-
return builder.camera.isPerspectiveCamera ? viewZToPerspectiveDepth(viewZ, cameraNear, cameraFar) : viewZToOrthographicDepth(viewZ, cameraNear, cameraFar);
|
|
375
|
-
}, "float");
|
|
376
|
-
const createRaycastMaterial = (antialias, pbr) => {
|
|
377
|
-
const centerHit = traceVolume(vec2(0));
|
|
378
|
-
const occupied = centerHit.w.greaterThan(0);
|
|
379
|
-
let coverage = float(1);
|
|
380
|
-
if (antialias) {
|
|
381
|
-
const sampleHits = [
|
|
382
|
-
traceVolume(vec2(-0.125, -0.375)),
|
|
383
|
-
traceVolume(vec2(0.375, -0.125)),
|
|
384
|
-
traceVolume(vec2(-0.375, 0.125)),
|
|
385
|
-
traceVolume(vec2(0.125, 0.375))
|
|
386
|
-
];
|
|
387
|
-
const sampleOccupancy = sampleHits.map((sampleHit) => sampleHit.w.greaterThan(0));
|
|
388
|
-
coverage = sampleOccupancy[0].select(1, 0).add(sampleOccupancy[1].select(1, 0)).add(sampleOccupancy[2].select(1, 0)).add(sampleOccupancy[3].select(1, 0)).mul(0.25).mul(occupied.select(1, 0));
|
|
389
|
-
}
|
|
390
|
-
const hit = centerHit.toVar(antialias ? "pixelImpostorAaHit" : "pixelImpostorFastHit");
|
|
391
|
-
const linearHitCell = hit.w.sub(1).max(0);
|
|
392
|
-
const hitLayer = linearHitCell.div(planar * planar).floor();
|
|
393
|
-
const hitSliceCell = mod(linearHitCell, planar * planar);
|
|
394
|
-
const hitCell = vec3(mod(hitSliceCell, planar), hitLayer, hitSliceCell.div(planar).floor());
|
|
395
|
-
let hitAtlasUv;
|
|
396
|
-
if (sparse) {
|
|
397
|
-
const wordIndex = linearHitCell.div(32).floor();
|
|
398
|
-
const word = sparseUint(wordIndex.mul(2));
|
|
399
|
-
const prefixRank = sparseUint(wordIndex.mul(2).add(1));
|
|
400
|
-
const bit = uint(mod(linearHitCell, 32));
|
|
401
|
-
const precedingMask = uint(1).shiftLeft(bit).sub(1);
|
|
402
|
-
const precedingCount = (
|
|
403
|
-
/** @type {ReturnType<typeof countOneBits> & import('three/webgpu').Node<'uint'>} */
|
|
404
|
-
countOneBits(word.bitAnd(precedingMask))
|
|
405
|
-
);
|
|
406
|
-
const record = prefixRank.add(precedingCount);
|
|
407
|
-
hitAtlasUv = linearTextureUv(float(record), surfaceAtlasSize);
|
|
408
|
-
} else {
|
|
409
|
-
const hitAtlasColumn = mod(hitCell.y, atlasColumns);
|
|
410
|
-
const hitAtlasRow = hitCell.y.div(atlasColumns).floor();
|
|
411
|
-
hitAtlasUv = vec2(
|
|
412
|
-
hitAtlasColumn.mul(planar).add(hitCell.x).add(0.5).div(atlasColumns * planar),
|
|
413
|
-
hitAtlasRow.mul(planar).add(hitCell.z).add(0.5).div(atlasRows * planar)
|
|
414
|
-
);
|
|
415
|
-
}
|
|
416
|
-
const baseSample = texture(atlas, hitAtlasUv);
|
|
417
|
-
const ormSample = ormAtlas ? texture(ormAtlas, hitAtlasUv) : vec4(1, 1, 0, 1);
|
|
418
|
-
const hitPosition = hit.xyz;
|
|
419
|
-
const hitToCamera = modelWorldMatrixInverse.mul(vec4(cameraPosition, 1)).xyz.sub(hitPosition);
|
|
420
|
-
const faceSign = step(0, hitToCamera).mul(2).sub(1);
|
|
421
|
-
const hitCellCenter = hitCell.add(0.5).div(volumeDimensions).sub(0.5).mul(sizeNode).sub(raycastOffsetNode);
|
|
422
|
-
const cellDelta = hitPosition.sub(hitCellCenter).div(sizeNode).mul(volumeDimensions);
|
|
423
|
-
const absoluteCellDelta = cellDelta.abs();
|
|
424
|
-
const boundaryDistance = max(
|
|
425
|
-
absoluteCellDelta.x,
|
|
426
|
-
max(absoluteCellDelta.y, absoluteCellDelta.z)
|
|
427
|
-
);
|
|
428
|
-
const faceDirection = hitToCamera.abs().normalize();
|
|
429
|
-
const xFaceScore = absoluteCellDelta.x.greaterThanEqual(boundaryDistance.sub(1e-4)).select(faceDirection.x, -1);
|
|
430
|
-
const yFaceScore = absoluteCellDelta.y.greaterThanEqual(boundaryDistance.sub(1e-4)).select(faceDirection.y, -1);
|
|
431
|
-
const zFaceScore = absoluteCellDelta.z.greaterThanEqual(boundaryDistance.sub(1e-4)).select(faceDirection.z, -1);
|
|
432
|
-
const geometricNormal = xFaceScore.greaterThanEqual(yFaceScore).and(xFaceScore.greaterThanEqual(zFaceScore)).select(vec3(faceSign.x, 0, 0), yFaceScore.greaterThanEqual(zFaceScore).select(vec3(0, faceSign.y, 0), vec3(0, 0, faceSign.z)));
|
|
433
|
-
const sampledNormal = normalAtlas ? texture(normalAtlas, hitAtlasUv).xyz.mul(2).sub(1) : geometricNormal;
|
|
434
|
-
const bakedNormal = sampledNormal.dot(sampledNormal).greaterThan(1e-8).select(sampledNormal.normalize(), geometricNormal);
|
|
435
|
-
const projectedNormal = bakedNormal.dot(geometricNormal).lessThan(0).select(bakedNormal.negate(), bakedNormal);
|
|
436
|
-
const shadingNormal = mix(geometricNormal, projectedNormal, projectedNormalMix).normalize();
|
|
437
|
-
const materialName = `Pixel impostor \xB7 volume raycast \xB7 ${antialias ? "AA" : "fast"} \xB7 ${pbr ? "PBR maps" : "plain lit"}`;
|
|
438
|
-
const material = new MeshStandardNodeMaterial({
|
|
439
|
-
name: materialName,
|
|
440
|
-
side: THREE.FrontSide,
|
|
441
|
-
depthTest: true,
|
|
442
|
-
depthWrite: true
|
|
443
|
-
});
|
|
444
|
-
material.colorNode = vec4(
|
|
445
|
-
baseSample.rgb.mul(float(1).sub(unlitMix)),
|
|
446
|
-
antialias ? baseSample.a.mul(coverage) : baseSample.a
|
|
447
|
-
);
|
|
448
|
-
material.emissiveNode = baseSample.rgb.mul(pbr ? ormSample.r : 1).mul(unlitMix);
|
|
449
|
-
material.normalNode = transformNormalToView(shadingNormal);
|
|
450
|
-
if (pbr) {
|
|
451
|
-
material.aoNode = ormSample.r;
|
|
452
|
-
material.roughnessNode = ormSample.g.max(0.04);
|
|
453
|
-
material.metalnessNode = ormSample.b;
|
|
454
|
-
}
|
|
455
|
-
material.receivedShadowPositionNode = modelWorldMatrix.mul(vec4(hitPosition, 1)).xyz;
|
|
456
|
-
material.maskNode = occupied.and(
|
|
457
|
-
baseSample.a.greaterThan(Number(descriptor.alphaCutoff) || 0.5)
|
|
458
|
-
);
|
|
459
|
-
material.maskShadowNode = antialias ? coverage.greaterThanEqual(0.5) : occupied;
|
|
460
|
-
material.alphaToCoverage = antialias;
|
|
461
|
-
material.depthNode = reconstructedDepth(hitPosition);
|
|
462
|
-
material.toneMapped = true;
|
|
463
|
-
material.userData.screenPixelSize = screenPixelSize;
|
|
464
|
-
material.userData.pixelImpostorRaycastAntialias = antialias;
|
|
465
|
-
material.userData.pixelImpostorRaySamples = antialias ? 5 : 1;
|
|
466
|
-
material.userData.pixelImpostorPbr = pbr;
|
|
467
|
-
return material;
|
|
468
|
-
};
|
|
469
|
-
const raycastMaterials = {
|
|
470
|
-
mapped: {
|
|
471
|
-
fast: createRaycastMaterial(false, true),
|
|
472
|
-
antialias: createRaycastMaterial(true, true)
|
|
473
|
-
},
|
|
474
|
-
plain: {
|
|
475
|
-
fast: createRaycastMaterial(false, false),
|
|
476
|
-
antialias: createRaycastMaterial(true, false)
|
|
477
|
-
}
|
|
478
|
-
};
|
|
479
|
-
const raycast = new THREE.Mesh(new THREE.BoxGeometry(
|
|
480
|
-
raycastSize.x,
|
|
481
|
-
raycastSize.y,
|
|
482
|
-
raycastSize.z
|
|
483
|
-
), raycastMaterials.mapped.antialias);
|
|
484
|
-
raycast.name = "Pixel impostor \xB7 volume raycast";
|
|
485
|
-
raycast.position.copy(raycastCenter);
|
|
486
|
-
raycast.castShadow = true;
|
|
487
|
-
raycast.receiveShadow = true;
|
|
488
|
-
raycast.userData.previewPixelImpostor = true;
|
|
489
|
-
raycast.userData.pixelImpostorDisplayMode = "raycast";
|
|
490
|
-
raycast.userData.pixelImpostorAlternateMaterials = [
|
|
491
|
-
...Object.values(raycastMaterials.mapped),
|
|
492
|
-
...Object.values(raycastMaterials.plain)
|
|
493
|
-
];
|
|
494
|
-
const surfacePoints = binding.surfacePoints instanceof THREE.BufferAttribute || binding.surfacePoints instanceof THREE.InterleavedBufferAttribute ? binding.surfacePoints : null;
|
|
495
|
-
let shell = null;
|
|
496
|
-
asset.traverse((object) => {
|
|
497
|
-
if (!shell && object instanceof THREE.Mesh && (object.geometry.getAttribute("_voxel_center") || surfacePoints)) {
|
|
498
|
-
shell = object;
|
|
499
|
-
}
|
|
500
|
-
});
|
|
501
|
-
if (!shell) return null;
|
|
502
|
-
const voxelShell = (
|
|
503
|
-
/** @type {THREE.Mesh} */
|
|
504
|
-
shell
|
|
505
|
-
);
|
|
506
|
-
const geometryBaseSample = texture(atlas, uv());
|
|
507
|
-
const geometryOrmSample = ormAtlas ? texture(ormAtlas, uv()) : vec4(1, 1, 0, 1);
|
|
508
|
-
const geometryNormal = attribute(
|
|
509
|
-
"normal",
|
|
510
|
-
/** @type {const} */
|
|
511
|
-
"vec3"
|
|
512
|
-
).normalize();
|
|
513
|
-
const geometryProjectedNormal = normalAtlas ? texture(normalAtlas, uv()).xyz.mul(2).sub(1).normalize() : geometryNormal;
|
|
514
|
-
const geometryAlignedNormal = geometryProjectedNormal.dot(geometryNormal).lessThan(0).select(geometryProjectedNormal.negate(), geometryProjectedNormal);
|
|
515
|
-
const createGeometryMaterial = (pbr) => {
|
|
516
|
-
const material = new MeshStandardNodeMaterial({
|
|
517
|
-
name: `Pixel impostor \xB7 voxel geometry \xB7 ${pbr ? "mapped" : "plain"}`,
|
|
518
|
-
side: THREE.FrontSide,
|
|
519
|
-
depthTest: true,
|
|
520
|
-
depthWrite: true
|
|
521
|
-
});
|
|
522
|
-
material.colorNode = vec4(
|
|
523
|
-
geometryBaseSample.rgb.mul(float(1).sub(unlitMix)),
|
|
524
|
-
geometryBaseSample.a
|
|
525
|
-
);
|
|
526
|
-
material.emissiveNode = geometryBaseSample.rgb.mul(pbr ? geometryOrmSample.r : 1).mul(unlitMix);
|
|
527
|
-
material.normalNode = transformNormalToView(
|
|
528
|
-
mix(geometryNormal, geometryAlignedNormal, projectedNormalMix).normalize()
|
|
529
|
-
);
|
|
530
|
-
if (pbr) {
|
|
531
|
-
material.aoNode = geometryOrmSample.r;
|
|
532
|
-
material.roughnessNode = geometryOrmSample.g.max(0.04);
|
|
533
|
-
material.metalnessNode = geometryOrmSample.b;
|
|
534
|
-
}
|
|
535
|
-
material.maskNode = geometryBaseSample.a.greaterThan(
|
|
536
|
-
Number(descriptor.alphaCutoff) || 0.5
|
|
537
|
-
);
|
|
538
|
-
material.toneMapped = true;
|
|
539
|
-
material.userData.pixelImpostorPbr = pbr;
|
|
540
|
-
return material;
|
|
541
|
-
};
|
|
542
|
-
const geometryMaterials = {
|
|
543
|
-
mapped: createGeometryMaterial(true),
|
|
544
|
-
plain: createGeometryMaterial(false)
|
|
545
|
-
};
|
|
546
|
-
const previousGeometryMaterial = voxelShell.material;
|
|
547
|
-
voxelShell.material = geometryMaterials.mapped;
|
|
548
|
-
voxelShell.userData.pixelImpostorAlternateMaterials = Object.values(geometryMaterials);
|
|
549
|
-
const skinnedSplats = voxelShell instanceof THREE.SkinnedMesh;
|
|
550
|
-
const useSurfacePoints = surfacePoints && !skinnedSplats;
|
|
551
|
-
let splatGeometry = skinnedSplats ? voxelShell.geometry.clone() : voxelShell.geometry;
|
|
552
|
-
if (skinnedSplats) {
|
|
553
|
-
const centers = voxelShell.geometry.getAttribute("_voxel_center");
|
|
554
|
-
if (!centers) throw new Error("Animated voxel splats require retained voxel centers.");
|
|
555
|
-
splatGeometry.setAttribute("position", centers.clone());
|
|
556
|
-
}
|
|
557
|
-
if (useSurfacePoints) {
|
|
558
|
-
const count = surfacePoints.count;
|
|
559
|
-
const coordinates = new Float32Array(count * 3);
|
|
560
|
-
const records = new Float32Array(count);
|
|
561
|
-
for (let index = 0; index < count; index++) {
|
|
562
|
-
coordinates[index * 3] = surfacePoints.getX(index);
|
|
563
|
-
coordinates[index * 3 + 1] = surfacePoints.getY(index);
|
|
564
|
-
coordinates[index * 3 + 2] = surfacePoints.getZ(index);
|
|
565
|
-
records[index] = index;
|
|
566
|
-
}
|
|
567
|
-
const instancedGeometry = new THREE.InstancedBufferGeometry();
|
|
568
|
-
instancedGeometry.instanceCount = count;
|
|
569
|
-
splatGeometry = instancedGeometry;
|
|
570
|
-
splatGeometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(12), 3));
|
|
571
|
-
splatGeometry.setAttribute("_voxel_corner", new THREE.BufferAttribute(new Float32Array([
|
|
572
|
-
-1,
|
|
573
|
-
-1,
|
|
574
|
-
1,
|
|
575
|
-
-1,
|
|
576
|
-
1,
|
|
577
|
-
1,
|
|
578
|
-
-1,
|
|
579
|
-
1
|
|
580
|
-
]), 2));
|
|
581
|
-
splatGeometry.setAttribute("_voxel_coord", new THREE.InstancedBufferAttribute(coordinates, 3));
|
|
582
|
-
splatGeometry.setAttribute("_voxel_record", new THREE.InstancedBufferAttribute(records, 1));
|
|
583
|
-
splatGeometry.setIndex([0, 1, 2, 0, 2, 3]);
|
|
584
|
-
splatGeometry.boundingBox = voxelShell.geometry.boundingBox?.clone() ?? bounds.clone();
|
|
585
|
-
splatGeometry.boundingSphere = voxelShell.geometry.boundingSphere?.clone() ?? splatGeometry.boundingBox.getBoundingSphere(new THREE.Sphere());
|
|
586
|
-
}
|
|
587
|
-
const voxelCoordinate = attribute(
|
|
588
|
-
"_voxel_coord",
|
|
589
|
-
/** @type {const} */
|
|
590
|
-
"vec3"
|
|
591
|
-
);
|
|
592
|
-
const voxelCenter = skinnedSplats ? positionLocal : useSurfacePoints ? voxelCoordinate.add(0.5).div(volumeDimensions).sub(0.5).mul(sizeNode).add(uniform(center.clone())) : attribute(
|
|
593
|
-
"_voxel_center",
|
|
594
|
-
/** @type {const} */
|
|
595
|
-
"vec3"
|
|
596
|
-
);
|
|
597
|
-
const voxelCorner = attribute(
|
|
598
|
-
"_voxel_corner",
|
|
599
|
-
/** @type {const} */
|
|
600
|
-
"vec2"
|
|
601
|
-
);
|
|
602
|
-
const centerView = modelViewMatrix.mul(vec4(voxelCenter, 1));
|
|
603
|
-
const centerClip = cameraProjectionMatrix.mul(centerView);
|
|
604
|
-
const centerNdc = centerClip.xy.div(centerClip.w);
|
|
605
|
-
const cssViewport = viewportSize;
|
|
606
|
-
const centerCss = centerNdc.mul(0.5).add(0.5).mul(cssViewport);
|
|
607
|
-
const projectVoxelPoint = (point) => {
|
|
608
|
-
const projected = cameraProjectionMatrix.mul(modelViewMatrix.mul(vec4(point, 1)));
|
|
609
|
-
return projected.xy.div(projected.w).mul(0.5).add(0.5).mul(cssViewport);
|
|
610
|
-
};
|
|
611
|
-
const projectedX = projectVoxelPoint(voxelCenter.add(vec3(voxelHalfExtents.x, 0, 0)));
|
|
612
|
-
const projectedY = projectVoxelPoint(voxelCenter.add(vec3(0, voxelHalfExtents.y, 0)));
|
|
613
|
-
const projectedZ = projectVoxelPoint(voxelCenter.add(vec3(0, 0, voxelHalfExtents.z)));
|
|
614
|
-
const projectedHalfFootprint = projectedX.sub(centerCss).abs().add(projectedY.sub(centerCss).abs()).add(projectedZ.sub(centerCss).abs());
|
|
615
|
-
const projectedVoxelSize = max(projectedHalfFootprint.x, projectedHalfFootprint.y).mul(2);
|
|
616
|
-
const projectedCellSpan = projectedVoxelSize.div(splatPixelSize);
|
|
617
|
-
const nearestCellSpan = ceil(projectedCellSpan.sub(0.5)).max(1);
|
|
618
|
-
const singleCell = snapMode.equal(1);
|
|
619
|
-
const centerOnly = snapMode.equal(2);
|
|
620
|
-
const cellSpan = singleCell.select(1, nearestCellSpan);
|
|
621
|
-
const cellCenter = centerCss.div(splatPixelSize).floor().mul(splatPixelSize).add(splatPixelSize.mul(0.5));
|
|
622
|
-
const gridIntersection = centerCss.div(splatPixelSize).add(0.5).floor().mul(splatPixelSize);
|
|
623
|
-
const oddCellSpan = mod(cellSpan, 2);
|
|
624
|
-
const parityAlignedCenter = oddCellSpan.greaterThan(0.5).select(cellCenter, gridIntersection);
|
|
625
|
-
const snappedCss = centerOnly.select(cellCenter, parityAlignedCenter);
|
|
626
|
-
const splatSize = cellSpan.mul(splatPixelSize).mul(splatScale);
|
|
627
|
-
const quadCss = snappedCss.add(voxelCorner.mul(splatSize.mul(0.52)));
|
|
628
|
-
const quadNdc = quadCss.div(cssViewport).mul(2).sub(1);
|
|
629
|
-
const quadClip = vec4(quadNdc.mul(centerClip.w), centerClip.z, centerClip.w);
|
|
630
|
-
const quadViewHomogeneous = cameraProjectionMatrixInverse.mul(quadClip);
|
|
631
|
-
const quadView = quadViewHomogeneous.xyz.div(quadViewHomogeneous.w);
|
|
632
|
-
const quadWorld = cameraWorldMatrix.mul(vec4(quadView, 1));
|
|
633
|
-
const quadLocalHomogeneous = modelWorldMatrixInverse.mul(quadWorld);
|
|
634
|
-
const quadLocal = quadLocalHomogeneous.xyz.div(quadLocalHomogeneous.w);
|
|
635
|
-
const splatUv = useSurfacePoints ? linearTextureUv(attribute(
|
|
636
|
-
"_voxel_record",
|
|
637
|
-
/** @type {const} */
|
|
638
|
-
"float"
|
|
639
|
-
), surfaceAtlasSize) : uv();
|
|
640
|
-
const splatColor = texture(atlas, splatUv);
|
|
641
|
-
const splatOrm = ormAtlas ? texture(ormAtlas, splatUv) : vec4(1, 1, 0, 1);
|
|
642
|
-
const cameraLocal = modelWorldMatrixInverse.mul(vec4(cameraPosition, 1)).xyz;
|
|
643
|
-
const viewDirection = cameraLocal.sub(voxelCenter).normalize();
|
|
644
|
-
const splatBakedNormal = normalAtlas ? texture(normalAtlas, splatUv).xyz.mul(2).sub(1).normalize() : viewDirection;
|
|
645
|
-
const splatProjectedNormal = splatBakedNormal.dot(viewDirection).lessThan(0).select(splatBakedNormal.negate(), splatBakedNormal);
|
|
646
|
-
const splatShadingNormal = mix(
|
|
647
|
-
viewDirection,
|
|
648
|
-
splatProjectedNormal,
|
|
649
|
-
projectedNormalMix
|
|
650
|
-
).normalize();
|
|
651
|
-
let splatMask;
|
|
652
|
-
if (useSurfacePoints) {
|
|
653
|
-
splatMask = splatColor.a.greaterThan(0.5);
|
|
654
|
-
} else {
|
|
655
|
-
const voxelFaceMask = float(attribute(
|
|
656
|
-
"_voxel_face_mask",
|
|
657
|
-
/** @type {const} */
|
|
658
|
-
"float"
|
|
659
|
-
));
|
|
660
|
-
const faceScores = [
|
|
661
|
-
viewDirection.x.negate(),
|
|
662
|
-
viewDirection.x,
|
|
663
|
-
viewDirection.z.negate(),
|
|
664
|
-
viewDirection.z,
|
|
665
|
-
viewDirection.y.negate(),
|
|
666
|
-
viewDirection.y
|
|
667
|
-
];
|
|
668
|
-
const exposedScore = (face) => {
|
|
669
|
-
const exposed = mod(voxelFaceMask.div(2 ** face).floor(), 2);
|
|
670
|
-
return mix(-2, faceScores[face], exposed);
|
|
671
|
-
};
|
|
672
|
-
const bestFaceScore = max(
|
|
673
|
-
max(max(exposedScore(0), exposedScore(1)), max(exposedScore(2), exposedScore(3))),
|
|
674
|
-
max(exposedScore(4), exposedScore(5))
|
|
675
|
-
);
|
|
676
|
-
const sourceFaceNormal = attribute(
|
|
677
|
-
"normal",
|
|
678
|
-
/** @type {const} */
|
|
679
|
-
"vec3"
|
|
680
|
-
);
|
|
681
|
-
splatMask = sourceFaceNormal.dot(viewDirection).greaterThanEqual(bestFaceScore.sub(1e-5)).and(splatColor.a.greaterThan(0.5));
|
|
682
|
-
}
|
|
683
|
-
const createSnappedMaterial = (pbr) => {
|
|
684
|
-
const material = new MeshStandardNodeMaterial({
|
|
685
|
-
name: `Pixel impostor \xB7 vertex-snapped shell \xB7 ${pbr ? "mapped" : "plain"}`,
|
|
686
|
-
side: THREE.FrontSide,
|
|
687
|
-
depthTest: true,
|
|
688
|
-
depthWrite: true
|
|
689
|
-
});
|
|
690
|
-
material.positionNode = quadLocal;
|
|
691
|
-
material.colorNode = vec4(splatColor.rgb.mul(float(1).sub(unlitMix)), splatColor.a);
|
|
692
|
-
material.emissiveNode = splatColor.rgb.mul(pbr ? splatOrm.r : 1).mul(unlitMix);
|
|
693
|
-
material.normalNode = transformNormalToView(splatShadingNormal);
|
|
694
|
-
if (pbr) {
|
|
695
|
-
material.aoNode = splatOrm.r;
|
|
696
|
-
material.roughnessNode = splatOrm.g.max(0.04);
|
|
697
|
-
material.metalnessNode = splatOrm.b;
|
|
698
|
-
}
|
|
699
|
-
material.maskNode = splatMask;
|
|
700
|
-
material.toneMapped = true;
|
|
701
|
-
material.userData.pixelImpostorPbr = pbr;
|
|
702
|
-
return material;
|
|
703
|
-
};
|
|
704
|
-
const snappedMaterials = {
|
|
705
|
-
mapped: createSnappedMaterial(true),
|
|
706
|
-
plain: createSnappedMaterial(false)
|
|
707
|
-
};
|
|
708
|
-
const snappedShell = skinnedSplats ? new THREE.SkinnedMesh(splatGeometry, snappedMaterials.mapped) : new THREE.Mesh(splatGeometry, snappedMaterials.mapped);
|
|
709
|
-
if (snappedShell instanceof THREE.SkinnedMesh && voxelShell instanceof THREE.SkinnedMesh) {
|
|
710
|
-
snappedShell.bindMode = voxelShell.bindMode;
|
|
711
|
-
snappedShell.bind(voxelShell.skeleton, voxelShell.bindMatrix);
|
|
712
|
-
snappedShell.bindMatrixInverse.copy(voxelShell.bindMatrixInverse);
|
|
713
|
-
}
|
|
714
|
-
asset.updateWorldMatrix(true, true);
|
|
715
|
-
voxelShell.updateWorldMatrix(true, false);
|
|
716
|
-
if (!useSurfacePoints) {
|
|
717
|
-
snappedShell.matrix.copy(asset.matrixWorld).invert().multiply(voxelShell.matrixWorld);
|
|
718
|
-
snappedShell.matrixAutoUpdate = false;
|
|
719
|
-
}
|
|
720
|
-
snappedShell.name = "Pixel impostor \xB7 vertex-snapped shell";
|
|
721
|
-
snappedShell.castShadow = false;
|
|
722
|
-
snappedShell.receiveShadow = false;
|
|
723
|
-
snappedShell.userData.previewPixelImpostor = true;
|
|
724
|
-
snappedShell.userData.pixelImpostorDisplayMode = "vertex-snap";
|
|
725
|
-
snappedShell.userData.pixelImpostorAlternateMaterials = Object.values(snappedMaterials);
|
|
726
|
-
const preview = new THREE.Group();
|
|
727
|
-
preview.name = "Pixel impostor shader previews";
|
|
728
|
-
preview.visible = false;
|
|
729
|
-
preview.userData.screenPixelSize = screenPixelSize;
|
|
730
|
-
preview.userData.splatPixelSize = splatPixelSize;
|
|
731
|
-
preview.userData.splatScale = splatScale;
|
|
732
|
-
preview.userData.projectedNormalMix = projectedNormalMix;
|
|
733
|
-
preview.userData.projectedNormalMixValue = 1;
|
|
734
|
-
preview.userData.unlitMix = unlitMix;
|
|
735
|
-
preview.userData.raycastMesh = raycast;
|
|
736
|
-
preview.userData.raycastMaterials = raycastMaterials;
|
|
737
|
-
preview.userData.geometryMesh = voxelShell;
|
|
738
|
-
preview.userData.previousGeometryMaterial = previousGeometryMaterial;
|
|
739
|
-
preview.userData.geometryMaterials = geometryMaterials;
|
|
740
|
-
preview.userData.snappedMesh = snappedShell;
|
|
741
|
-
preview.userData.snappedMaterials = snappedMaterials;
|
|
742
|
-
preview.userData.raycastAntialiasEnabled = true;
|
|
743
|
-
preview.userData.snapMode = snapMode;
|
|
744
|
-
preview.userData.viewportSize = viewportSize;
|
|
745
|
-
preview.userData.dpr = dpr;
|
|
746
|
-
preview.userData.pixelSizeValue = 5;
|
|
747
|
-
preview.add(raycast, snappedShell);
|
|
748
|
-
const textureBytes = textureFootprintBytes(atlas) + (normalAtlas ? textureFootprintBytes(normalAtlas) : 0) + (ormAtlas ? textureFootprintBytes(ormAtlas) : 0) + (indexAtlas ? textureFootprintBytes(indexAtlas) : 0);
|
|
749
|
-
const shellStats = pixelDisplayMeshStats(voxelShell, textureBytes);
|
|
750
|
-
const splatStats = pixelDisplayMeshStats(snappedShell, textureFootprintBytes(atlas));
|
|
751
|
-
const raycastStats = pixelDisplayMeshStats(raycast, textureBytes);
|
|
752
|
-
preview.userData.displayStats = {
|
|
753
|
-
geometry: shellStats,
|
|
754
|
-
"vertex-snap": splatStats,
|
|
755
|
-
raycast: raycastStats,
|
|
756
|
-
"screen-pixels": raycastStats
|
|
757
|
-
};
|
|
758
|
-
return preview;
|
|
759
|
-
}
|
|
760
|
-
function pixelDisplayMeshStats(mesh, textureBytes) {
|
|
761
|
-
const position = mesh.geometry.getAttribute("position");
|
|
762
|
-
const geometryBytes = bufferGeometryFootprintBytes(mesh.geometry);
|
|
763
|
-
const instances = mesh.geometry instanceof THREE.InstancedBufferGeometry ? mesh.geometry.instanceCount : 1;
|
|
764
|
-
return {
|
|
765
|
-
triangles: (mesh.geometry.getIndex()?.count ?? position?.count ?? 0) / 3 * instances,
|
|
766
|
-
vertices: (position?.count ?? 0) * instances,
|
|
767
|
-
drawCalls: 1,
|
|
768
|
-
geometryBytes,
|
|
769
|
-
textureBytes,
|
|
770
|
-
gpuBytes: geometryBytes + textureBytes
|
|
771
|
-
};
|
|
772
|
-
}
|
|
773
|
-
function bufferGeometryFootprintBytes(geometry) {
|
|
774
|
-
const buffers = /* @__PURE__ */ new Set();
|
|
775
|
-
let bytes = 0;
|
|
776
|
-
const add = (attribute2) => {
|
|
777
|
-
if (!attribute2) return;
|
|
778
|
-
const storage = attribute2 instanceof THREE.InterleavedBufferAttribute ? attribute2.data : attribute2;
|
|
779
|
-
if (buffers.has(storage)) return;
|
|
780
|
-
buffers.add(storage);
|
|
781
|
-
bytes += storage.array?.byteLength ?? 0;
|
|
782
|
-
};
|
|
783
|
-
add(geometry.getIndex());
|
|
784
|
-
for (const attribute2 of Object.values(geometry.attributes)) add(attribute2);
|
|
785
|
-
return bytes;
|
|
786
|
-
}
|
|
787
|
-
function textureFootprintBytes(atlas) {
|
|
788
|
-
const image = (
|
|
789
|
-
/** @type {{width?:number,height?:number,data?:{byteLength?:number}} | null} */
|
|
790
|
-
atlas.image
|
|
791
|
-
);
|
|
792
|
-
if (image?.data?.byteLength !== void 0) return image.data.byteLength;
|
|
793
|
-
const width = Number(image?.width ?? 0);
|
|
794
|
-
const height = Number(image?.height ?? 0);
|
|
795
|
-
return Number.isFinite(width) && Number.isFinite(height) ? width * height * 4 : 0;
|
|
796
|
-
}
|
|
797
|
-
function createPixelImpostorDisplay(asset, options = {}) {
|
|
798
|
-
let binding = options.binding ?? null;
|
|
799
|
-
asset.traverse((object) => {
|
|
800
|
-
if (!binding && object.userData.pixelImpostor) binding = object.userData.pixelImpostor;
|
|
801
|
-
});
|
|
802
|
-
if (!binding) throw new Error("Load a NEEDLE_pixel_impostor GLB with registerNeedlePixelImpostorLoader first.");
|
|
803
|
-
const preview = createPixelImpostorRaycastPreview(asset, new THREE.Box3().setFromObject(asset), binding);
|
|
804
|
-
if (!preview) throw new Error("The pixel impostor has no supported shader atlas.");
|
|
805
|
-
const originals = /* @__PURE__ */ new Map();
|
|
806
|
-
asset.traverse((object) => {
|
|
807
|
-
if (object instanceof THREE.Mesh) originals.set(object, object.visible);
|
|
808
|
-
});
|
|
809
|
-
if (options.attach !== false) asset.add(preview);
|
|
810
|
-
let mode = options.mode ?? "geometry";
|
|
811
|
-
let pbr = options.pbr ?? true;
|
|
812
|
-
let antialias = options.antialias ?? true;
|
|
813
|
-
const setMode = (next) => {
|
|
814
|
-
if (!["geometry", "vertex-snap", "raycast", "screen-pixels"].includes(next)) throw new Error(`Unknown pixel display mode: ${next}`);
|
|
815
|
-
mode = next;
|
|
816
|
-
preview.userData.displayMode = next;
|
|
817
|
-
for (const [mesh, visible] of originals) mesh.visible = next === "geometry" && visible;
|
|
818
|
-
preview.visible = next !== "geometry";
|
|
819
|
-
for (const child of preview.children) child.visible = child.userData.pixelImpostorDisplayMode === next || next === "screen-pixels" && child.userData.pixelImpostorDisplayMode === "raycast";
|
|
820
|
-
preview.userData.screenPixelSize.value = next === "screen-pixels" ? options.pixelSize ?? 5 : 1;
|
|
821
|
-
const raycast = preview.userData.raycastMesh;
|
|
822
|
-
const raycastMaterials = preview.userData.raycastMaterials;
|
|
823
|
-
if (raycast instanceof THREE.Mesh) raycast.material = raycastMaterials[pbr ? "mapped" : "plain"][next === "raycast" && antialias ? "antialias" : "fast"];
|
|
824
|
-
const snapped = preview.userData.snappedMesh;
|
|
825
|
-
if (snapped instanceof THREE.Mesh) snapped.material = preview.userData.snappedMaterials[pbr ? "mapped" : "plain"];
|
|
826
|
-
const shell = preview.userData.geometryMesh;
|
|
827
|
-
if (shell instanceof THREE.Mesh) shell.material = preview.userData.geometryMaterials[pbr ? "mapped" : "plain"];
|
|
828
|
-
return preview.userData.displayStats?.[next] ?? null;
|
|
829
|
-
};
|
|
830
|
-
const updateViewport = () => {
|
|
831
|
-
const canvas = options.renderer?.domElement;
|
|
832
|
-
const viewport = preview.userData.viewportSize.value;
|
|
833
|
-
if (viewport instanceof THREE.Vector2) viewport.set(Math.max(1, canvas?.clientWidth ?? 1), Math.max(1, canvas?.clientHeight ?? 1));
|
|
834
|
-
preview.userData.dpr.value = options.renderer?.getPixelRatio?.() ?? 1;
|
|
835
|
-
};
|
|
836
|
-
preview.userData.splatPixelSize.value = options.pixelSize ?? 5;
|
|
837
|
-
preview.userData.splatScale.value = options.splatScale ?? 1;
|
|
838
|
-
preview.userData.projectedNormalMix.value = options.projectedNormalMix ?? 1;
|
|
839
|
-
preview.userData.snapMode.value = options.snapMode === "single-cell" ? 1 : options.snapMode === "center-only" ? 2 : 0;
|
|
840
|
-
setMode(mode);
|
|
841
|
-
updateViewport();
|
|
842
|
-
return {
|
|
843
|
-
preview,
|
|
844
|
-
setMode,
|
|
845
|
-
updateViewport,
|
|
846
|
-
/** @param {boolean} enabled */
|
|
847
|
-
setPbr(enabled) {
|
|
848
|
-
pbr = Boolean(enabled);
|
|
849
|
-
setMode(mode);
|
|
850
|
-
},
|
|
851
|
-
/** @param {boolean} enabled */
|
|
852
|
-
setAntialias(enabled) {
|
|
853
|
-
antialias = Boolean(enabled);
|
|
854
|
-
setMode(mode);
|
|
855
|
-
},
|
|
856
|
-
dispose() {
|
|
857
|
-
preview.removeFromParent();
|
|
858
|
-
for (const [mesh, visible] of originals) mesh.visible = visible;
|
|
859
|
-
const shell = preview.userData.geometryMesh;
|
|
860
|
-
if (shell instanceof THREE.Mesh) shell.material = preview.userData.previousGeometryMaterial;
|
|
861
|
-
for (const child of preview.children) if (child instanceof THREE.Mesh && child.geometry !== preview.userData.geometryMesh?.geometry) child.geometry.dispose();
|
|
862
|
-
for (const materials of [preview.userData.raycastMaterials?.mapped, preview.userData.raycastMaterials?.plain, preview.userData.snappedMaterials, preview.userData.geometryMaterials]) {
|
|
863
|
-
if (materials) for (const material of Object.values(materials)) material.dispose();
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
};
|
|
867
|
-
}
|
|
868
|
-
export {
|
|
869
|
-
NEEDLE_PIXEL_IMPOSTOR,
|
|
870
|
-
NeedlePixelImpostorLoaderPlugin,
|
|
871
|
-
createPixelImpostorDisplay,
|
|
872
|
-
createPixelImpostorRaycastPreview,
|
|
873
|
-
parsePixelImpostorDescriptor,
|
|
874
|
-
registerNeedlePixelImpostorLoader
|
|
875
|
-
};
|
|
876
|
-
//# sourceMappingURL=pixel-impostor-runtime.js.map
|