@needle-tools/three-mesh-effects 0.3.1 → 0.3.3
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 +13 -0
- package/README.md +22 -6
- package/SKILL.md +7 -5
- package/build/box-impostor-runtime.js +212 -87
- package/build/box-impostor-runtime.js.map +4 -4
- package/build/impostor-runtime.js +128 -48
- package/build/impostor-runtime.js.map +4 -4
- package/build/index.js +4 -0
- package/build/index.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-display.js +178 -81
- package/build/internal/box-impostor/box-impostor-display.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-material.js +163 -71
- package/build/internal/box-impostor/box-impostor-material.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-projection.js +38 -18
- package/build/internal/box-impostor/box-impostor-projection.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-runtime.js +193 -76
- package/build/internal/box-impostor/box-impostor-runtime.js.map +4 -4
- package/build/internal/impostor/impostor-node-material.js +128 -48
- package/build/internal/impostor/impostor-node-material.js.map +4 -4
- package/build/internal/pixel-impostor/pixel-impostor-display.js +84 -38
- package/build/internal/pixel-impostor/pixel-impostor-display.js.map +4 -4
- package/build/internal/rendering/captured-surface-depth.js +35 -0
- package/build/internal/rendering/captured-surface-depth.js.map +7 -0
- package/build/internal/rendering/captured-surface-material.js +28 -0
- package/build/internal/rendering/captured-surface-material.js.map +7 -0
- package/build/internal/rendering/renderer-support.js +31 -0
- package/build/internal/rendering/renderer-support.js.map +7 -0
- package/build/pixel-impostor-runtime.js +84 -38
- package/build/pixel-impostor-runtime.js.map +4 -4
- package/build/types/box-impostor/box-impostor-display.d.ts +9 -1
- package/build/types/box-impostor/box-impostor-material.d.ts +4 -0
- package/build/types/box-impostor/box-impostor-runtime.d.ts +5 -11
- package/build/types/impostor/impostor-node-material.d.ts +2 -15
- package/build/types/rendering/captured-surface-depth.d.ts +5 -0
- package/build/types/rendering/captured-surface-depth.js +1 -0
- package/build/types/rendering/captured-surface-material.d.ts +8 -0
- package/build/types/rendering/captured-surface-material.js +1 -0
- package/build/types/rendering/renderer-support.d.ts +27 -0
- package/build/types/rendering/renderer-support.js +1 -0
- package/package.json +3 -3
- package/src/box-impostor/box-impostor-display.js +23 -11
- package/src/box-impostor/box-impostor-material.js +94 -44
- package/src/box-impostor/box-impostor-projection.js +3 -14
- package/src/box-impostor/box-impostor-runtime.js +6 -4
- package/src/impostor/impostor-node-material.js +26 -43
- package/src/pixel-impostor/pixel-impostor-display.js +5 -13
- package/src/public/index.js +5 -0
- package/src/rendering/captured-surface-depth.js +55 -0
- package/src/rendering/captured-surface-material.js +27 -0
- package/src/rendering/renderer-support.js +50 -0
|
@@ -233,6 +233,36 @@ function createBoxImpostorGeometry(input) {
|
|
|
233
233
|
return geometry;
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
+
// src/rendering/captured-surface-depth.js
|
|
237
|
+
import {
|
|
238
|
+
Fn,
|
|
239
|
+
cameraFar,
|
|
240
|
+
cameraNear,
|
|
241
|
+
cameraProjectionMatrix,
|
|
242
|
+
modelViewMatrix,
|
|
243
|
+
vec4,
|
|
244
|
+
viewZToLogarithmicDepth,
|
|
245
|
+
viewZToOrthographicDepth,
|
|
246
|
+
viewZToPerspectiveDepth,
|
|
247
|
+
viewZToReversedOrthographicDepth,
|
|
248
|
+
viewZToReversedPerspectiveDepth
|
|
249
|
+
} from "three/tsl";
|
|
250
|
+
function capturedSurfaceDepth(position, shadowBias = 0) {
|
|
251
|
+
return Fn(
|
|
252
|
+
(_, builder) => {
|
|
253
|
+
const z = modelViewMatrix.mul(vec4(position, 1)).z.toVar("capturedSurfaceViewZ");
|
|
254
|
+
const reversed = builder.renderer.reversedDepthBuffer === true;
|
|
255
|
+
const perspective = cameraProjectionMatrix.mul(vec4(0, 0, 0, 1)).w.equal(0);
|
|
256
|
+
const perspectiveDepth = builder.renderer.logarithmicDepthBuffer === true ? viewZToLogarithmicDepth(z, cameraNear, cameraFar) : (reversed ? viewZToReversedPerspectiveDepth : viewZToPerspectiveDepth)(z, cameraNear, cameraFar);
|
|
257
|
+
const result = perspective.select(
|
|
258
|
+
perspectiveDepth,
|
|
259
|
+
(reversed ? viewZToReversedOrthographicDepth : viewZToOrthographicDepth)(z, cameraNear, cameraFar)
|
|
260
|
+
);
|
|
261
|
+
return shadowBias && "isShadowPassMaterial" in builder.material && builder.material.isShadowPassMaterial ? result.add(reversed ? -shadowBias : shadowBias).clamp(0, 1) : result;
|
|
262
|
+
}
|
|
263
|
+
)();
|
|
264
|
+
}
|
|
265
|
+
|
|
236
266
|
// src/box-impostor/box-impostor-material.js
|
|
237
267
|
import {
|
|
238
268
|
DataTexture as DataTexture3,
|
|
@@ -242,12 +272,12 @@ import {
|
|
|
242
272
|
UnsignedIntType,
|
|
243
273
|
NearestFilter as NearestFilter3,
|
|
244
274
|
LinearFilter,
|
|
245
|
-
MeshStandardNodeMaterial,
|
|
246
275
|
DoubleSide,
|
|
276
|
+
BackSide,
|
|
247
277
|
FrontSide
|
|
248
278
|
} from "three/webgpu";
|
|
249
279
|
import {
|
|
250
|
-
Fn as
|
|
280
|
+
Fn as Fn4,
|
|
251
281
|
If as If3,
|
|
252
282
|
Loop,
|
|
253
283
|
Break,
|
|
@@ -257,10 +287,10 @@ import {
|
|
|
257
287
|
modelWorldMatrixInverse,
|
|
258
288
|
cameraPosition,
|
|
259
289
|
cameraWorldMatrix,
|
|
260
|
-
cameraProjectionMatrix,
|
|
290
|
+
cameraProjectionMatrix as cameraProjectionMatrix2,
|
|
261
291
|
vec2 as vec23,
|
|
262
292
|
vec3 as vec33,
|
|
263
|
-
vec4 as
|
|
293
|
+
vec4 as vec44,
|
|
264
294
|
float as float3,
|
|
265
295
|
int as int3,
|
|
266
296
|
uint,
|
|
@@ -274,17 +304,37 @@ import {
|
|
|
274
304
|
ivec2 as ivec23,
|
|
275
305
|
normalize as normalize2,
|
|
276
306
|
floor,
|
|
277
|
-
|
|
278
|
-
modelWorldMatrix as
|
|
307
|
+
fract,
|
|
308
|
+
modelWorldMatrix as modelWorldMatrix3,
|
|
279
309
|
cameraNear as cameraNear2,
|
|
280
|
-
cameraFar as cameraFar2,
|
|
281
|
-
viewZToPerspectiveDepth as viewZToPerspectiveDepth2,
|
|
282
|
-
viewZToOrthographicDepth as viewZToOrthographicDepth2,
|
|
283
310
|
transformNormalToView as transformNormalToView3,
|
|
284
311
|
struct as struct2,
|
|
285
312
|
uniform
|
|
286
313
|
} from "three/tsl";
|
|
287
314
|
|
|
315
|
+
// src/rendering/captured-surface-material.js
|
|
316
|
+
import { MeshStandardNodeMaterial } from "three/webgpu";
|
|
317
|
+
import { context, modelViewMatrix as modelViewMatrix2, modelWorldMatrix, vec4 as vec42 } from "three/tsl";
|
|
318
|
+
var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
|
|
319
|
+
/** @type {import('three/webgpu').Node<'vec3'> | null} */
|
|
320
|
+
capturedPositionNode = null;
|
|
321
|
+
/**
|
|
322
|
+
* @override
|
|
323
|
+
* @param {import('three/webgpu').NodeBuilder} builder
|
|
324
|
+
*/
|
|
325
|
+
setupLighting(builder) {
|
|
326
|
+
const lighting = super.setupLighting(builder);
|
|
327
|
+
if (!this.capturedPositionNode) return lighting;
|
|
328
|
+
const world = modelWorldMatrix.mul(vec42(this.capturedPositionNode, 1)).xyz;
|
|
329
|
+
const view = modelViewMatrix2.mul(vec42(this.capturedPositionNode, 1)).xyz;
|
|
330
|
+
return context(lighting, {
|
|
331
|
+
positionView: view,
|
|
332
|
+
positionWorld: world,
|
|
333
|
+
shadowPositionWorld: world
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
288
338
|
// src/box-impostor/box-impostor-surface-search.js
|
|
289
339
|
function refineSurfaceCrossing({ float: float4, If: If4, Loop: Loop2 }, { lo, hi, loValue, hiValue, height, steps, secant }) {
|
|
290
340
|
const a = lo.toVar(), b = hi.toVar(), fa = loValue.toVar(), fb = secant ? hiValue.toVar() : null;
|
|
@@ -304,7 +354,7 @@ function refineSurfaceCrossing({ float: float4, If: If4, Loop: Loop2 }, { lo, hi
|
|
|
304
354
|
// src/box-impostor/box-impostor-faces.js
|
|
305
355
|
import { DataTexture, FloatType, RGBAFormat, NearestFilter } from "three";
|
|
306
356
|
import {
|
|
307
|
-
Fn,
|
|
357
|
+
Fn as Fn2,
|
|
308
358
|
If,
|
|
309
359
|
attribute,
|
|
310
360
|
positionLocal,
|
|
@@ -363,7 +413,7 @@ function applyBoxFaces({
|
|
|
363
413
|
const base = texture(maps.baseColor, uv()).level(0).toVar("boxFaceColor");
|
|
364
414
|
const orm = texture(maps.orm, uv()).level(0);
|
|
365
415
|
const coverage = max(orm.a, 1e-10);
|
|
366
|
-
const normal =
|
|
416
|
+
const normal = Fn2(() => {
|
|
367
417
|
const packed = textureLoad(
|
|
368
418
|
maps.normalDepth,
|
|
369
419
|
ivec2(uv().mul(descriptor.atlasResolution))
|
|
@@ -408,11 +458,11 @@ function applyBoxFaces({
|
|
|
408
458
|
// src/box-impostor/box-impostor-projection.js
|
|
409
459
|
import { DataTexture as DataTexture2, FloatType as FloatType2, RGBAFormat as RGBAFormat2, NearestFilter as NearestFilter2 } from "three";
|
|
410
460
|
import {
|
|
411
|
-
Fn as
|
|
461
|
+
Fn as Fn3,
|
|
412
462
|
If as If2,
|
|
413
463
|
vec2 as vec22,
|
|
414
464
|
vec3 as vec32,
|
|
415
|
-
vec4,
|
|
465
|
+
vec4 as vec43,
|
|
416
466
|
float as float2,
|
|
417
467
|
int as int2,
|
|
418
468
|
ivec2 as ivec22,
|
|
@@ -425,12 +475,7 @@ import {
|
|
|
425
475
|
textureLoad as textureLoad2,
|
|
426
476
|
attribute as attribute2,
|
|
427
477
|
struct,
|
|
428
|
-
|
|
429
|
-
modelWorldMatrix,
|
|
430
|
-
cameraNear,
|
|
431
|
-
cameraFar,
|
|
432
|
-
viewZToPerspectiveDepth,
|
|
433
|
-
viewZToOrthographicDepth,
|
|
478
|
+
modelWorldMatrix as modelWorldMatrix2,
|
|
434
479
|
transformNormalToView as transformNormalToView2
|
|
435
480
|
} from "three/tsl";
|
|
436
481
|
function applyBoxProjection({
|
|
@@ -494,7 +539,7 @@ function applyBoxProjection({
|
|
|
494
539
|
right.mul(decoded.x).add(up.mul(decoded.y)).add(n.mul(decoded.z))
|
|
495
540
|
).normalize();
|
|
496
541
|
};
|
|
497
|
-
const surface =
|
|
542
|
+
const surface = Fn3(() => {
|
|
498
543
|
const ray = perspective.select(
|
|
499
544
|
normalize(raster.sub(eye)),
|
|
500
545
|
viewAxis.negate()
|
|
@@ -517,8 +562,8 @@ function applyBoxProjection({
|
|
|
517
562
|
const weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
|
|
518
563
|
const candidate = settings.interpolation === "none" ? null : {
|
|
519
564
|
uv: vec22(0).toVar(),
|
|
520
|
-
base:
|
|
521
|
-
orm:
|
|
565
|
+
base: vec43(0).toVar(),
|
|
566
|
+
orm: vec43(0).toVar(),
|
|
522
567
|
t: float2(1e20).toVar(),
|
|
523
568
|
tolerance: float2(0).toVar(),
|
|
524
569
|
right,
|
|
@@ -617,6 +662,7 @@ function applyBoxProjection({
|
|
|
617
662
|
positions
|
|
618
663
|
);
|
|
619
664
|
})().toVar("boxProjectedSurface");
|
|
665
|
+
material.capturedPositionNode = surface.get("position");
|
|
620
666
|
material.colorNode = surface.get("color");
|
|
621
667
|
material.normalNode = transformNormalToView2(surface.get("normal"));
|
|
622
668
|
material.roughnessNode = surface.get("orm").g;
|
|
@@ -626,15 +672,9 @@ function applyBoxProjection({
|
|
|
626
672
|
material.maskNode = surface.get("alpha").greaterThanEqual(transparency === "cutout" ? alphaCutoff : float2(1 / 255));
|
|
627
673
|
material.maskShadowNode = material.maskNode;
|
|
628
674
|
if (transparency !== "cutout") material.opacityNode = surface.get("alpha");
|
|
629
|
-
material.depthNode =
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
viewZToPerspectiveDepth(z, cameraNear, cameraFar),
|
|
633
|
-
viewZToOrthographicDepth(z, cameraNear, cameraFar)
|
|
634
|
-
);
|
|
635
|
-
})();
|
|
636
|
-
material.receivedShadowPositionNode = modelWorldMatrix.mul(
|
|
637
|
-
vec4(surface.get("position"), 1)
|
|
675
|
+
material.depthNode = capturedSurfaceDepth(surface.get("position"));
|
|
676
|
+
material.receivedShadowPositionNode = modelWorldMatrix2.mul(
|
|
677
|
+
vec43(surface.get("position"), 1)
|
|
638
678
|
).xyz;
|
|
639
679
|
material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
|
|
640
680
|
return material;
|
|
@@ -642,10 +682,48 @@ function applyBoxProjection({
|
|
|
642
682
|
|
|
643
683
|
// src/box-impostor/box-impostor-material.js
|
|
644
684
|
function createBoxImpostorMaterial(descriptor, maps) {
|
|
685
|
+
return createBoxImpostorMaterialVariant(descriptor, maps, { filtering: true });
|
|
686
|
+
}
|
|
687
|
+
function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false } = {}) {
|
|
645
688
|
const settings = normalizeBoxImpostorDisplay(descriptor);
|
|
646
689
|
const alphaCutoff = uniform(settings.alphaCutoff);
|
|
647
690
|
const transparency = boxTransparency(descriptor, settings);
|
|
648
691
|
const minimumAlpha = transparency === "cutout" ? alphaCutoff : float3(1 / 255);
|
|
692
|
+
const surfaceCoverage = (ormAlpha, baseAlpha) => filtering && descriptor.alphaMode !== "blend" ? min(ormAlpha, baseAlpha) : ormAlpha;
|
|
693
|
+
const decodeNormal = (encoded) => {
|
|
694
|
+
const packed = encoded.mul(2).sub(1);
|
|
695
|
+
const normal2 = vec33(packed, float3(1).sub(abs3(packed.x)).sub(abs3(packed.y))).toVar();
|
|
696
|
+
If3(normal2.z.lessThan(0), () => {
|
|
697
|
+
normal2.xy.assign(vec23(1).sub(abs3(normal2.yx)).mul(sign3(normal2.xy.add(1e-20))));
|
|
698
|
+
});
|
|
699
|
+
return normal2.normalize();
|
|
700
|
+
};
|
|
701
|
+
const filteredNormal = (uv2, depth, tolerance, nearest, alpha, sampled = texture3(maps.normalDepth, uv2).level(0), ormAlpha = texture3(maps.orm, uv2).level(0).a) => {
|
|
702
|
+
sampled = sampled.toVar();
|
|
703
|
+
const coverage2 = max3(descriptor.alphaMode === "blend" ? ormAlpha : alpha, 1e-10);
|
|
704
|
+
const nearestNormal = decodeNormal(nearest.rg), result = decodeNormal(sampled.rg.div(coverage2)).toVar();
|
|
705
|
+
const sampledDepth = sampled.b.mul(65280).add(sampled.a.mul(255)).div(65535);
|
|
706
|
+
const nearestDepth = nearest.b.mul(65280).add(nearest.a.mul(255)).div(65535);
|
|
707
|
+
If3(nearestNormal.z.lessThan(0).or(dot3(result, nearestNormal).lessThan(0.75)).or(coverage2.lessThan(0.9999)).or(abs3(sampledDepth.sub(nearestDepth)).greaterThan(tolerance)), () => {
|
|
708
|
+
const p = uv2.mul(descriptor.atlasResolution).sub(0.5), lo = floor(p), f = fract(p);
|
|
709
|
+
const normal2 = vec33(0).toVar(), weights = float3(0).toVar();
|
|
710
|
+
for (const [x, y] of [[0, 0], [1, 0], [0, 1], [1, 1]]) {
|
|
711
|
+
const texel = ivec23(lo.add(vec23(x, y)).clamp(0, descriptor.atlasResolution - 1));
|
|
712
|
+
const packed = textureLoad3(maps.normalDepth, texel).toVar();
|
|
713
|
+
const alpha2 = textureLoad3(maps.baseColor, texel).a;
|
|
714
|
+
const coverage3 = descriptor.alphaMode === "blend" ? textureLoad3(maps.orm, texel).a : alpha2;
|
|
715
|
+
const capturedDepth = packed.b.mul(65280).add(packed.a.mul(255)).div(65535);
|
|
716
|
+
const supported = coverage3.greaterThan(1e-4).and(alpha2.greaterThanEqual(minimumAlpha.mul(coverage3))).and(abs3(capturedDepth.sub(depth)).lessThanEqual(tolerance));
|
|
717
|
+
If3(supported, () => {
|
|
718
|
+
const weight = (x ? f.x : float3(1).sub(f.x)).mul(y ? f.y : float3(1).sub(f.y)).mul(coverage3);
|
|
719
|
+
normal2.addAssign(decodeNormal(packed.rg.div(max3(coverage3, 1e-10))).mul(weight));
|
|
720
|
+
weights.addAssign(weight);
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
result.assign(weights.greaterThan(1e-10).select(normal2.normalize(), nearestNormal));
|
|
724
|
+
});
|
|
725
|
+
return result;
|
|
726
|
+
};
|
|
649
727
|
for (const map of Object.values(maps)) {
|
|
650
728
|
if (map.minFilter !== LinearFilter || map.magFilter !== LinearFilter) {
|
|
651
729
|
map.minFilter = map.magFilter = LinearFilter;
|
|
@@ -653,9 +731,9 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
653
731
|
map.needsUpdate = true;
|
|
654
732
|
}
|
|
655
733
|
}
|
|
656
|
-
const material = new
|
|
734
|
+
const material = new CapturedSurfaceMaterial({
|
|
657
735
|
name: "Box impostor",
|
|
658
|
-
side: descriptor.mode !== "fitted" ? DoubleSide :
|
|
736
|
+
side: descriptor.mode !== "fitted" ? DoubleSide : BackSide,
|
|
659
737
|
alphaHash: transparency === "dithered",
|
|
660
738
|
transparent: transparency === "blended",
|
|
661
739
|
depthWrite: transparency !== "blended",
|
|
@@ -689,25 +767,27 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
689
767
|
const ax = attribute3("boxAxisX", "vec3"), ay = attribute3("boxAxisY", "vec3"), az = attribute3("boxAxisZ", "vec3");
|
|
690
768
|
const toBox = (p) => vec33(dot3(p, ax), dot3(p, ay), dot3(p, az));
|
|
691
769
|
const fromBox = (p) => ax.mul(p.x).add(ay.mul(p.y)).add(az.mul(p.z));
|
|
692
|
-
const eye = modelWorldMatrixInverse.mul(
|
|
693
|
-
const viewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(
|
|
694
|
-
const perspective =
|
|
770
|
+
const eye = modelWorldMatrixInverse.mul(vec44(cameraPosition, 1)).xyz;
|
|
771
|
+
const viewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec44(0, 0, 1, 0))).xyz.normalize();
|
|
772
|
+
const perspective = cameraProjectionMatrix2.element(3).w.equal(0);
|
|
695
773
|
let proxy = positionLocal2;
|
|
696
774
|
if (descriptor.mode !== "fitted") {
|
|
697
|
-
proxy =
|
|
698
|
-
const
|
|
699
|
-
const
|
|
775
|
+
proxy = Fn4((_, builder) => {
|
|
776
|
+
const delta = eye.sub(center);
|
|
777
|
+
const forward = perspective.and(delta.length().greaterThan(1e-6)).select(normalize2(delta), viewAxis);
|
|
778
|
+
const right = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec44(0, 1, 0, 0))).xyz.cross(forward).normalize();
|
|
700
779
|
const up = forward.cross(right).normalize();
|
|
701
780
|
const radius = half.length();
|
|
702
781
|
const distance = dot3(eye.sub(center), forward);
|
|
703
|
-
const
|
|
782
|
+
const planeOffset = perspective.select(min(radius, distance.sub(cameraNear2.mul(2))), radius);
|
|
783
|
+
const plane = center.add(forward.mul(planeOffset));
|
|
704
784
|
const lo = vec23(1e20).toVar(), hi = vec23(-1e20).toVar();
|
|
705
785
|
for (const x of [-1, 1])
|
|
706
786
|
for (const y of [-1, 1])
|
|
707
787
|
for (const z of [-1, 1]) {
|
|
708
788
|
const corner2 = fromBox(half.mul(vec33(x, y, z)));
|
|
709
789
|
const scale = perspective.select(
|
|
710
|
-
max3(distance.sub(
|
|
790
|
+
max3(distance.sub(planeOffset), 1e-5).div(
|
|
711
791
|
max3(distance.sub(dot3(corner2, forward)), 1e-5)
|
|
712
792
|
),
|
|
713
793
|
float3(1)
|
|
@@ -724,7 +804,8 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
724
804
|
material.castShadowPositionNode = proxy;
|
|
725
805
|
}
|
|
726
806
|
const raster = varying(proxy, "boxRasterPosition");
|
|
727
|
-
if (boxDepthMode(descriptor, settings) === "projection" && descriptor.mode === "fitted" && settings.interpolation === "none")
|
|
807
|
+
if (boxDepthMode(descriptor, settings) === "projection" && descriptor.mode === "fitted" && settings.interpolation === "none") {
|
|
808
|
+
material.side = FrontSide;
|
|
728
809
|
return applyBoxFaces({
|
|
729
810
|
material,
|
|
730
811
|
descriptor,
|
|
@@ -735,6 +816,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
735
816
|
toBox,
|
|
736
817
|
fromBox
|
|
737
818
|
});
|
|
819
|
+
}
|
|
738
820
|
if (boxDepthMode(descriptor, settings) === "projection")
|
|
739
821
|
return applyBoxProjection({
|
|
740
822
|
material,
|
|
@@ -778,7 +860,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
778
860
|
{ uv: "vec2", position: "vec3", normal: "vec3", valid: "float" },
|
|
779
861
|
"BoxImpostorHit"
|
|
780
862
|
);
|
|
781
|
-
const raycast =
|
|
863
|
+
const raycast = Fn4((_, builder) => {
|
|
782
864
|
const ray = perspective.select(
|
|
783
865
|
normalize2(raster.sub(eye)),
|
|
784
866
|
viewAxis.negate()
|
|
@@ -889,6 +971,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
889
971
|
const point2 = o.add(r.mul(hitT)), uv2 = sampleUv(point2);
|
|
890
972
|
const texel = ivec23(uv2.mul(descriptor.atlasResolution));
|
|
891
973
|
const nearest = textureLoad3(maps.normalDepth, texel);
|
|
974
|
+
const capturedBase = texture3(maps.baseColor, uv2).level(0);
|
|
892
975
|
const occupied = textureLoad3(maps.orm, texel).a.greaterThan(0.5);
|
|
893
976
|
const residual = abs3(
|
|
894
977
|
dot3(point2, n).sub(float3(1).sub(decode(nearest).mul(2)))
|
|
@@ -899,7 +982,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
899
982
|
).div(dot3(abs3(n), half)).mul(4).add(4 / 65535);
|
|
900
983
|
If3(
|
|
901
984
|
residual.lessThanEqual(tolerance).and(occupied).and(
|
|
902
|
-
|
|
985
|
+
capturedBase.a.greaterThanEqual(minimumAlpha)
|
|
903
986
|
),
|
|
904
987
|
() => {
|
|
905
988
|
valid.assign(1);
|
|
@@ -907,7 +990,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
907
990
|
bestT.assign(hitT);
|
|
908
991
|
bestUv.assign(uv2);
|
|
909
992
|
found.assign(1);
|
|
910
|
-
const packed = texture3(maps.normalDepth, uv2).level(0).rg.div(max3(texture3(maps.orm, uv2).level(0).a, 1e-10)).mul(2).sub(1);
|
|
993
|
+
const packed = texture3(maps.normalDepth, uv2).level(0).rg.div(max3(surfaceCoverage(texture3(maps.orm, uv2).level(0).a, capturedBase.a), 1e-10)).mul(2).sub(1);
|
|
911
994
|
const normal2 = vec33(
|
|
912
995
|
packed.x,
|
|
913
996
|
packed.y,
|
|
@@ -1022,8 +1105,9 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
1022
1105
|
);
|
|
1023
1106
|
});
|
|
1024
1107
|
const hit = raycast().toVar("boxHit");
|
|
1108
|
+
hit.toStack();
|
|
1025
1109
|
const base = texture3(maps.baseColor, hit.get("uv")).level(0), orm = texture3(maps.orm, hit.get("uv")).level(0);
|
|
1026
|
-
const coverage = max3(orm.a, 1e-10);
|
|
1110
|
+
const coverage = max3(surfaceCoverage(orm.a, base.a), 1e-10);
|
|
1027
1111
|
let color = base.div(coverage), surfaceOrm = orm.rgb.div(coverage), emission = texture3(maps.emissive, hit.get("uv")).level(0).rgb.div(coverage), normal = hit.get("normal");
|
|
1028
1112
|
if (settings.interpolation !== "none") {
|
|
1029
1113
|
const fallbackColor = color, fallbackOrm = surfaceOrm, fallbackEmission = emission, fallbackNormal = normal;
|
|
@@ -1031,12 +1115,12 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
1031
1115
|
{ color: "vec4", orm: "vec3", emission: "vec3", normal: "vec3" },
|
|
1032
1116
|
"BoxSurfaceBlend"
|
|
1033
1117
|
);
|
|
1034
|
-
const blended =
|
|
1118
|
+
const blended = Fn4(() => {
|
|
1035
1119
|
const point = toBox(hit.get("position").sub(center)).div(half);
|
|
1036
1120
|
const look = toBox(
|
|
1037
1121
|
perspective.select(normalize2(eye.sub(hit.get("position"))), viewAxis)
|
|
1038
1122
|
);
|
|
1039
|
-
const colors =
|
|
1123
|
+
const colors = vec44(0).toVar(), materials = vec33(0).toVar(), emissions = vec33(0).toVar(), normals = vec33(0).toVar(), weights = float3(0).toVar();
|
|
1040
1124
|
for (let axis = 0; axis < 3; axis++) {
|
|
1041
1125
|
const positive = look.element(axis).greaterThanEqual(0);
|
|
1042
1126
|
const front = BOX_FACE_AXES[axis * 2], back = BOX_FACE_AXES[axis * 2 + 1];
|
|
@@ -1072,26 +1156,38 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
1072
1156
|
),
|
|
1073
1157
|
() => {
|
|
1074
1158
|
const angle = abs3(look.element(axis));
|
|
1075
|
-
const
|
|
1076
|
-
|
|
1159
|
+
const filtered = filtering ? texture3(maps.normalDepth, uv2).level(0).toVar() : captured;
|
|
1160
|
+
let weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
|
|
1161
|
+
if (filtering) {
|
|
1162
|
+
const error = abs3(dot3(point, n).sub(float3(1).sub(
|
|
1163
|
+
filtered.b.mul(65280).add(filtered.a.mul(255)).div(65535).mul(2)
|
|
1164
|
+
)));
|
|
1165
|
+
weight = weight.mul(float3(1).sub(error.div(tolerance)).clamp(0, 1));
|
|
1166
|
+
}
|
|
1167
|
+
const coverage2 = max3(surfaceCoverage(sampledOrm.a, sampled.a), 1e-10);
|
|
1077
1168
|
colors.addAssign(sampled.div(coverage2).mul(weight));
|
|
1078
1169
|
materials.addAssign(sampledOrm.rgb.div(coverage2).mul(weight));
|
|
1079
1170
|
emissions.addAssign(
|
|
1080
1171
|
texture3(maps.emissive, uv2).level(0).rgb.div(coverage2).mul(weight)
|
|
1081
1172
|
);
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
)
|
|
1091
|
-
|
|
1173
|
+
let shadingNormal;
|
|
1174
|
+
if (filtering) shadingNormal = filteredNormal(uv2, float3(1).sub(dot3(point, n)).mul(0.5), tolerance.mul(0.5), captured, sampled.a, filtered, sampledOrm.a);
|
|
1175
|
+
else {
|
|
1176
|
+
const packed = captured.rg.mul(2).sub(1);
|
|
1177
|
+
const decoded = vec33(
|
|
1178
|
+
packed,
|
|
1179
|
+
float3(1).sub(abs3(packed.x)).sub(abs3(packed.y))
|
|
1180
|
+
).toVar();
|
|
1181
|
+
If3(decoded.z.lessThan(0), () => {
|
|
1182
|
+
decoded.xy.assign(
|
|
1183
|
+
vec23(1).sub(abs3(decoded.yx)).mul(sign3(decoded.xy.add(1e-20)))
|
|
1184
|
+
);
|
|
1185
|
+
});
|
|
1186
|
+
shadingNormal = decoded;
|
|
1187
|
+
}
|
|
1092
1188
|
normals.addAssign(
|
|
1093
1189
|
fromBox(
|
|
1094
|
-
right.mul(
|
|
1190
|
+
right.mul(shadingNormal.x).add(up.mul(shadingNormal.y)).add(n.mul(shadingNormal.z))
|
|
1095
1191
|
).normalize().mul(weight)
|
|
1096
1192
|
);
|
|
1097
1193
|
weights.addAssign(weight);
|
|
@@ -1111,6 +1207,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
1111
1207
|
emission = blended.get("emission");
|
|
1112
1208
|
normal = blended.get("normal");
|
|
1113
1209
|
}
|
|
1210
|
+
material.capturedPositionNode = hit.get("position");
|
|
1114
1211
|
material.colorNode = color.rgb;
|
|
1115
1212
|
material.normalNode = transformNormalToView3(normal);
|
|
1116
1213
|
material.roughnessNode = surfaceOrm.g;
|
|
@@ -1120,15 +1217,9 @@ function createBoxImpostorMaterial(descriptor, maps) {
|
|
|
1120
1217
|
if (transparency !== "cutout") material.opacityNode = color.a.clamp(0, 1);
|
|
1121
1218
|
material.maskNode = hit.get("valid").greaterThan(0.5);
|
|
1122
1219
|
material.maskShadowNode = material.maskNode;
|
|
1123
|
-
material.depthNode =
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
viewZToPerspectiveDepth2(z, cameraNear2, cameraFar2),
|
|
1127
|
-
viewZToOrthographicDepth2(z, cameraNear2, cameraFar2)
|
|
1128
|
-
);
|
|
1129
|
-
})();
|
|
1130
|
-
material.receivedShadowPositionNode = modelWorldMatrix2.mul(
|
|
1131
|
-
vec42(hit.get("position"), 1)
|
|
1220
|
+
material.depthNode = capturedSurfaceDepth(hit.get("position"));
|
|
1221
|
+
material.receivedShadowPositionNode = modelWorldMatrix3.mul(
|
|
1222
|
+
vec44(hit.get("position"), 1)
|
|
1132
1223
|
).xyz;
|
|
1133
1224
|
material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
|
|
1134
1225
|
return material;
|
|
@@ -1142,9 +1233,36 @@ import {
|
|
|
1142
1233
|
NoColorSpace,
|
|
1143
1234
|
SRGBColorSpace
|
|
1144
1235
|
} from "three";
|
|
1236
|
+
|
|
1237
|
+
// src/rendering/renderer-support.js
|
|
1238
|
+
import * as TSL from "three/tsl";
|
|
1239
|
+
function rendererSupportsImpostorNodeMaterials(renderer) {
|
|
1240
|
+
if (typeof Reflect.get(TSL, "struct") !== "function") return false;
|
|
1241
|
+
return renderer?.isWebGPURenderer === true || renderer?.capabilities?.supportsNodeMaterials === true;
|
|
1242
|
+
}
|
|
1243
|
+
var pending = /* @__PURE__ */ new WeakMap();
|
|
1244
|
+
async function enableRendererNodeMaterials(renderer) {
|
|
1245
|
+
if (rendererSupportsImpostorNodeMaterials(renderer) || !renderer?.isWebGLRenderer || !renderer.setNodesHandler || typeof Reflect.get(TSL, "struct") !== "function") return;
|
|
1246
|
+
let setup = pending.get(renderer);
|
|
1247
|
+
if (!setup) {
|
|
1248
|
+
setup = import("three/addons/tsl/WebGLNodesHandler.js").then(({ WebGLNodesHandler }) => {
|
|
1249
|
+
if (rendererSupportsImpostorNodeMaterials(renderer)) return;
|
|
1250
|
+
renderer.setNodesHandler?.(new WebGLNodesHandler());
|
|
1251
|
+
renderer.capabilities ??= {};
|
|
1252
|
+
renderer.capabilities.supportsNodeMaterials = true;
|
|
1253
|
+
}).catch((error) => {
|
|
1254
|
+
pending.delete(renderer);
|
|
1255
|
+
throw error;
|
|
1256
|
+
});
|
|
1257
|
+
pending.set(renderer, setup);
|
|
1258
|
+
}
|
|
1259
|
+
await setup;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
// src/box-impostor/box-impostor-runtime.js
|
|
1145
1263
|
var NeedleBoxImpostorLoaderPlugin = class {
|
|
1146
1264
|
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTFParser} parser
|
|
1147
|
-
* @param {{renderer?:
|
|
1265
|
+
* @param {{renderer?:import('../rendering/renderer-support.js').ImpostorRenderer}} [options] */
|
|
1148
1266
|
constructor(parser, options = {}) {
|
|
1149
1267
|
this.name = NEEDLE_BOX_IMPOSTOR;
|
|
1150
1268
|
this.parser = parser;
|
|
@@ -1152,11 +1270,12 @@ var NeedleBoxImpostorLoaderPlugin = class {
|
|
|
1152
1270
|
}
|
|
1153
1271
|
/** @param {import('three/addons/loaders/GLTFLoader.js').GLTF} result */
|
|
1154
1272
|
async afterRoot(result) {
|
|
1273
|
+
await enableRendererNodeMaterials(this.options.renderer);
|
|
1155
1274
|
const seen = /* @__PURE__ */ new Set();
|
|
1156
1275
|
const geometries = /* @__PURE__ */ new Set();
|
|
1157
1276
|
const materials = /* @__PURE__ */ new Set();
|
|
1158
1277
|
const scenes = result.scenes ?? [result.scene];
|
|
1159
|
-
const
|
|
1278
|
+
const pending2 = (
|
|
1160
1279
|
/** @type {Promise<void>[]} */
|
|
1161
1280
|
[]
|
|
1162
1281
|
);
|
|
@@ -1176,15 +1295,15 @@ var NeedleBoxImpostorLoaderPlugin = class {
|
|
|
1176
1295
|
const raw = this.parser.json.meshes?.[a.meshes]?.primitives[a.primitives]?.extensions?.[NEEDLE_BOX_IMPOSTOR];
|
|
1177
1296
|
if (raw === void 0) return;
|
|
1178
1297
|
const descriptor = parseBoxImpostorDescriptor(raw);
|
|
1179
|
-
if (!this.options.renderer
|
|
1298
|
+
if (!rendererSupportsImpostorNodeMaterials(this.options.renderer)) {
|
|
1180
1299
|
if (this.parser.json.extensionsRequired?.includes(NEEDLE_BOX_IMPOSTOR))
|
|
1181
1300
|
throw new Error(
|
|
1182
|
-
"NEEDLE_box_impostor requires Three WebGPURenderer
|
|
1301
|
+
"NEEDLE_box_impostor requires Three WebGPURenderer or a renderer with compatible node-material support."
|
|
1183
1302
|
);
|
|
1184
1303
|
mesh.userData.boxImpostorFallback = true;
|
|
1185
1304
|
return;
|
|
1186
1305
|
}
|
|
1187
|
-
|
|
1306
|
+
pending2.push(
|
|
1188
1307
|
(async () => {
|
|
1189
1308
|
const entries = await Promise.all(
|
|
1190
1309
|
["baseColor", "normalDepth", "orm", "emissive"].map(
|
|
@@ -1221,7 +1340,7 @@ var NeedleBoxImpostorLoaderPlugin = class {
|
|
|
1221
1340
|
})()
|
|
1222
1341
|
);
|
|
1223
1342
|
});
|
|
1224
|
-
await Promise.all(
|
|
1343
|
+
await Promise.all(pending2);
|
|
1225
1344
|
for (const scene of scenes)
|
|
1226
1345
|
scene?.traverse((object) => {
|
|
1227
1346
|
const mesh = (
|
|
@@ -1259,24 +1378,30 @@ function setBoxImpostorDisplay(root, options) {
|
|
|
1259
1378
|
const keyFor = (s) => `${s.depthMode}/${s.depthMode !== "projection" ? s.raySteps : 0}/${s.interpolation}/${s.transparency}`;
|
|
1260
1379
|
let cache = variants.get(mesh);
|
|
1261
1380
|
if (!cache) {
|
|
1262
|
-
|
|
1263
|
-
keyFor(normalizeBoxImpostorDisplay(binding.descriptor)),
|
|
1381
|
+
const initial = (
|
|
1264
1382
|
/** @type {ReturnType<typeof createBoxImpostorMaterial>} */
|
|
1265
1383
|
mesh.material
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
const
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
owned.clear();
|
|
1384
|
+
);
|
|
1385
|
+
const materials = /* @__PURE__ */ new Map([[keyFor(normalizeBoxImpostorDisplay(binding.descriptor)), initial]]);
|
|
1386
|
+
const release = (event) => {
|
|
1387
|
+
const owned = [...materials.values()];
|
|
1388
|
+
materials.clear();
|
|
1272
1389
|
variants.delete(mesh);
|
|
1273
|
-
|
|
1390
|
+
for (const material2 of owned) {
|
|
1391
|
+
material2.removeEventListener("dispose", release);
|
|
1392
|
+
if (material2 !== event.target) material2.dispose();
|
|
1393
|
+
}
|
|
1394
|
+
};
|
|
1395
|
+
cache = { materials, release };
|
|
1396
|
+
variants.set(mesh, cache);
|
|
1397
|
+
initial.addEventListener("dispose", release);
|
|
1274
1398
|
}
|
|
1275
1399
|
const key = keyFor(settings);
|
|
1276
|
-
let material = cache.get(key);
|
|
1400
|
+
let material = cache.materials.get(key);
|
|
1277
1401
|
if (!material) {
|
|
1278
1402
|
material = createBoxImpostorMaterial(descriptor, binding.maps);
|
|
1279
|
-
|
|
1403
|
+
material.addEventListener("dispose", cache.release);
|
|
1404
|
+
cache.materials.set(key, material);
|
|
1280
1405
|
}
|
|
1281
1406
|
material.userData.boxImpostor.alphaCutoff.value = settings.alphaCutoff;
|
|
1282
1407
|
material.userData.boxImpostor.descriptor = descriptor;
|