@needle-tools/three-mesh-effects 0.3.7 → 0.4.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/CHANGELOG.md +7 -0
- package/README.md +14 -1
- package/build/box-impostor-runtime.js +321 -195
- package/build/box-impostor-runtime.js.map +4 -4
- package/build/impostor-runtime.js +944 -202
- package/build/impostor-runtime.js.map +4 -4
- package/build/index.js +7 -7
- package/build/index.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-display.js +319 -193
- package/build/internal/box-impostor/box-impostor-display.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-faces.js +8 -6
- package/build/internal/box-impostor/box-impostor-faces.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-material.js +319 -193
- package/build/internal/box-impostor/box-impostor-material.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-projection.js +8 -5
- package/build/internal/box-impostor/box-impostor-projection.js.map +2 -2
- package/build/internal/box-impostor/box-impostor-runtime.js +321 -195
- package/build/internal/box-impostor/box-impostor-runtime.js.map +4 -4
- package/build/internal/impostor/adaptive-impostor-renderer.js +434 -0
- package/build/internal/impostor/adaptive-impostor-renderer.js.map +7 -0
- package/build/internal/impostor/impostor-node-material.js +944 -202
- package/build/internal/impostor/impostor-node-material.js.map +4 -4
- package/build/internal/impostor/impostor-performance.js +136 -0
- package/build/internal/impostor/impostor-performance.js.map +7 -0
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js +6 -1
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +2 -2
- package/build/internal/rendering/instance-space.js +110 -0
- package/build/internal/rendering/instance-space.js.map +7 -0
- package/build/internal/rendering/renderer-support.js +6 -1
- package/build/internal/rendering/renderer-support.js.map +2 -2
- package/build/pixel-impostor-runtime.js +6 -1
- package/build/pixel-impostor-runtime.js.map +2 -2
- package/build/types/box-impostor/box-impostor-faces.d.ts +3 -1
- package/build/types/box-impostor/box-impostor-projection.d.ts +3 -1
- package/build/types/impostor/adaptive-impostor-renderer.d.ts +74 -0
- package/build/types/impostor/adaptive-impostor-renderer.js +1 -0
- package/build/types/impostor/impostor-node-material.d.ts +5 -1
- package/build/types/impostor/impostor-performance.d.ts +90 -0
- package/build/types/impostor/impostor-performance.js +1 -0
- package/build/types/public/impostor-runtime.d.ts +2 -0
- package/build/types/public/index.d.ts +3 -0
- package/build/types/rendering/instance-space.d.ts +26 -0
- package/build/types/rendering/instance-space.js +1 -0
- package/build/types/rendering/renderer-support.d.ts +13 -0
- package/package.json +2 -2
- package/src/box-impostor/box-impostor-faces.js +7 -5
- package/src/box-impostor/box-impostor-material.js +34 -19
- package/src/box-impostor/box-impostor-projection.js +7 -4
- package/src/impostor/adaptive-impostor-renderer.js +423 -0
- package/src/impostor/impostor-node-material.js +347 -157
- package/src/impostor/impostor-performance.js +152 -0
- package/src/public/impostor-runtime.js +2 -0
- package/src/public/index.js +12 -9
- package/src/rendering/instance-space.js +121 -0
- package/src/rendering/renderer-support.js +9 -0
|
@@ -98,8 +98,8 @@ function parseBoxImpostorDescriptor(raw) {
|
|
|
98
98
|
fail();
|
|
99
99
|
for (let i = 0; i < 3; i++)
|
|
100
100
|
for (let j = 0; j < 3; j++) {
|
|
101
|
-
const
|
|
102
|
-
if (Math.abs(
|
|
101
|
+
const dot5 = b.axes[i].reduce((s, v, k) => s + v * b.axes[j][k], 0);
|
|
102
|
+
if (Math.abs(dot5 - (i === j ? 1 : 0)) > 1e-4) fail();
|
|
103
103
|
}
|
|
104
104
|
const [x, y, z] = b.axes;
|
|
105
105
|
if ((x[1] * y[2] - x[2] * y[1]) * z[0] + (x[2] * y[0] - x[0] * y[2]) * z[1] + (x[0] * y[1] - x[1] * y[0]) * z[2] < 0.999)
|
|
@@ -316,9 +316,9 @@ function capturedSurfaceDepth(position, shadowBias = 0) {
|
|
|
316
316
|
|
|
317
317
|
// src/box-impostor/box-impostor-material.js
|
|
318
318
|
import {
|
|
319
|
-
DataTexture as
|
|
320
|
-
FloatType as
|
|
321
|
-
RGBAFormat as
|
|
319
|
+
DataTexture as DataTexture4,
|
|
320
|
+
FloatType as FloatType4,
|
|
321
|
+
RGBAFormat as RGBAFormat4,
|
|
322
322
|
RedIntegerFormat,
|
|
323
323
|
UnsignedIntType,
|
|
324
324
|
NearestFilter as NearestFilter3,
|
|
@@ -333,27 +333,27 @@ import {
|
|
|
333
333
|
Loop as Loop2,
|
|
334
334
|
Break,
|
|
335
335
|
attribute as attribute3,
|
|
336
|
-
varying,
|
|
337
|
-
|
|
336
|
+
varying as varying2,
|
|
337
|
+
positionGeometry as positionGeometry2,
|
|
338
338
|
positionViewDirection,
|
|
339
339
|
modelWorldMatrixInverse,
|
|
340
340
|
cameraPosition,
|
|
341
341
|
cameraWorldMatrix,
|
|
342
342
|
cameraProjectionMatrix as cameraProjectionMatrix2,
|
|
343
343
|
vec2 as vec24,
|
|
344
|
-
vec3 as
|
|
345
|
-
vec4 as
|
|
344
|
+
vec3 as vec35,
|
|
345
|
+
vec4 as vec46,
|
|
346
346
|
float as float4,
|
|
347
|
-
int as
|
|
347
|
+
int as int5,
|
|
348
348
|
uint as uint2,
|
|
349
|
-
dot as
|
|
349
|
+
dot as dot4,
|
|
350
350
|
abs as abs3,
|
|
351
351
|
max as max4,
|
|
352
352
|
min as min2,
|
|
353
353
|
sign as sign3,
|
|
354
354
|
texture as texture3,
|
|
355
|
-
textureLoad as
|
|
356
|
-
ivec2 as
|
|
355
|
+
textureLoad as textureLoad5,
|
|
356
|
+
ivec2 as ivec25,
|
|
357
357
|
normalize as normalize2,
|
|
358
358
|
floor as floor2,
|
|
359
359
|
fract,
|
|
@@ -364,9 +364,114 @@ import {
|
|
|
364
364
|
uniform
|
|
365
365
|
} from "three/tsl";
|
|
366
366
|
|
|
367
|
+
// src/rendering/instance-space.js
|
|
368
|
+
import { DataTexture, DynamicDrawUsage, FloatType, InstancedInterleavedBuffer, InterpolationSamplingType, NodeUpdateType, RGBAFormat } from "three/webgpu";
|
|
369
|
+
import { cross, dot, instanceIndex, instancedBufferAttribute, instancedDynamicBufferAttribute, int, ivec2, textureLoad, varying, vec3, vec4 as vec42 } from "three/tsl";
|
|
370
|
+
var spaces = /* @__PURE__ */ new WeakMap();
|
|
371
|
+
var webglSpaces = /* @__PURE__ */ new WeakMap();
|
|
372
|
+
function instanceColumns(object, matrix, useTexture) {
|
|
373
|
+
const columns = [0, 1, 2, 3];
|
|
374
|
+
if (useTexture) {
|
|
375
|
+
const rowSize = Math.ceil(Math.sqrt(matrix.count) / 2);
|
|
376
|
+
const rows = Math.ceil(matrix.count / rowSize);
|
|
377
|
+
const data = new Float32Array(rowSize * rows * 16);
|
|
378
|
+
const texture4 = new DataTexture(data, rowSize * 4, rows, RGBAFormat, FloatType);
|
|
379
|
+
const upload = () => {
|
|
380
|
+
data.set(matrix.array);
|
|
381
|
+
texture4.needsUpdate = true;
|
|
382
|
+
};
|
|
383
|
+
upload();
|
|
384
|
+
const release = () => {
|
|
385
|
+
object.removeEventListener("dispose", release);
|
|
386
|
+
texture4.dispose();
|
|
387
|
+
webglSpaces.delete(matrix);
|
|
388
|
+
};
|
|
389
|
+
object.addEventListener("dispose", release);
|
|
390
|
+
const x = instanceIndex.mod(rowSize).mul(4);
|
|
391
|
+
const y = instanceIndex.div(rowSize);
|
|
392
|
+
return { nodes: columns.map((column) => textureLoad(texture4, ivec2(int(x.add(column)), int(y)))), upload };
|
|
393
|
+
}
|
|
394
|
+
const interleaved = new InstancedInterleavedBuffer(matrix.array, 16, 1);
|
|
395
|
+
const read = matrix.usage === DynamicDrawUsage ? instancedDynamicBufferAttribute : instancedBufferAttribute;
|
|
396
|
+
return {
|
|
397
|
+
nodes: columns.map((column) => read(
|
|
398
|
+
interleaved,
|
|
399
|
+
/** @type {const} */
|
|
400
|
+
"vec4",
|
|
401
|
+
16,
|
|
402
|
+
column * 4
|
|
403
|
+
)),
|
|
404
|
+
upload: () => {
|
|
405
|
+
interleaved.needsUpdate = true;
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function instanceSpace(builder) {
|
|
410
|
+
const object = (
|
|
411
|
+
/** @type {InstancedMesh} */
|
|
412
|
+
builder.object
|
|
413
|
+
);
|
|
414
|
+
const matrix = object?.isInstancedMesh ? object.instanceMatrix : null;
|
|
415
|
+
if (!matrix) return null;
|
|
416
|
+
const backend = (
|
|
417
|
+
/** @type {{isWebGPUBackend?: boolean} | undefined} */
|
|
418
|
+
builder.renderer.backend
|
|
419
|
+
);
|
|
420
|
+
const useTexture = !backend?.isWebGPUBackend && matrix.count * 64 > (builder.getUniformBufferLimit?.() ?? 16384);
|
|
421
|
+
const cache = useTexture ? webglSpaces : spaces;
|
|
422
|
+
const cached = cache.get(matrix);
|
|
423
|
+
if (cached) return cached;
|
|
424
|
+
const { nodes, upload } = instanceColumns(object, matrix, useTexture);
|
|
425
|
+
let version = matrix.version;
|
|
426
|
+
nodes[0].updateBeforeType = NodeUpdateType.OBJECT;
|
|
427
|
+
nodes[0].updateBefore = () => {
|
|
428
|
+
if (matrix.version !== version) {
|
|
429
|
+
version = matrix.version;
|
|
430
|
+
upload();
|
|
431
|
+
}
|
|
432
|
+
return void 0;
|
|
433
|
+
};
|
|
434
|
+
const [a, b, c, d] = nodes.map((node) => node.xyz);
|
|
435
|
+
const determinant = dot(a, cross(b, c));
|
|
436
|
+
const rows = [cross(b, c), cross(c, a), cross(a, b)].map((row) => row.div(determinant));
|
|
437
|
+
const toLocalDirection = (v) => vec3(dot(rows[0], v), dot(rows[1], v), dot(rows[2], v));
|
|
438
|
+
const toMeshDirection = (v) => a.mul(v.x).add(b.mul(v.y)).add(c.mul(v.z));
|
|
439
|
+
const translation = [d.x, d.y, d.z];
|
|
440
|
+
const packed = [a, b, c].map(
|
|
441
|
+
(column, k) => varying(vec42(column, translation[k]), `instanceSpace${k}`).setInterpolation(InterpolationSamplingType.FLAT)
|
|
442
|
+
);
|
|
443
|
+
const [fa, fb, fc] = packed.map((column) => column.xyz);
|
|
444
|
+
const cofactors = () => [cross(fb, fc), cross(fc, fa), cross(fa, fb)];
|
|
445
|
+
const space = {
|
|
446
|
+
toLocalPoint: (p) => toLocalDirection(p.sub(d)),
|
|
447
|
+
toLocalDirection,
|
|
448
|
+
toMeshPoint: (p) => toMeshDirection(p).add(d),
|
|
449
|
+
toMeshDirection,
|
|
450
|
+
fragment: {
|
|
451
|
+
toMeshPoint: (p) => fa.mul(p.x).add(fb.mul(p.y)).add(fc.mul(p.z)).add(vec3(packed[0].w, packed[1].w, packed[2].w)),
|
|
452
|
+
toMeshDirection: (v) => fa.mul(v.x).add(fb.mul(v.y)).add(fc.mul(v.z)),
|
|
453
|
+
toMeshNormal: (n) => {
|
|
454
|
+
const [x, y, z] = cofactors();
|
|
455
|
+
return x.mul(n.x).add(y.mul(n.y)).add(z.mul(n.z)).mul(dot(fa, x).sign()).normalize();
|
|
456
|
+
},
|
|
457
|
+
toLocalDirection: (v) => {
|
|
458
|
+
const [x, y, z] = cofactors();
|
|
459
|
+
return vec3(dot(x, v), dot(y, v), dot(z, v)).div(dot(fa, x));
|
|
460
|
+
},
|
|
461
|
+
toLocalPoint: (p) => {
|
|
462
|
+
const [x, y, z] = cofactors();
|
|
463
|
+
const v = p.sub(vec3(packed[0].w, packed[1].w, packed[2].w));
|
|
464
|
+
return vec3(dot(x, v), dot(y, v), dot(z, v)).div(dot(fa, x));
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
cache.set(matrix, space);
|
|
469
|
+
return space;
|
|
470
|
+
}
|
|
471
|
+
|
|
367
472
|
// src/rendering/captured-surface-material.js
|
|
368
473
|
import { MeshStandardNodeMaterial } from "three/webgpu";
|
|
369
|
-
import { context, modelViewMatrix as modelViewMatrix2, modelWorldMatrix, vec4 as
|
|
474
|
+
import { context, modelViewMatrix as modelViewMatrix2, modelWorldMatrix, vec4 as vec43 } from "three/tsl";
|
|
370
475
|
var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
|
|
371
476
|
/** @type {import('three/webgpu').Node<'vec3'> | null} */
|
|
372
477
|
capturedPositionNode = null;
|
|
@@ -377,8 +482,8 @@ var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
|
|
|
377
482
|
setupLighting(builder) {
|
|
378
483
|
const lighting = super.setupLighting(builder);
|
|
379
484
|
if (!this.capturedPositionNode) return lighting;
|
|
380
|
-
const world = modelWorldMatrix.mul(
|
|
381
|
-
const view = modelViewMatrix2.mul(
|
|
485
|
+
const world = modelWorldMatrix.mul(vec43(this.capturedPositionNode, 1)).xyz;
|
|
486
|
+
const view = modelViewMatrix2.mul(vec43(this.capturedPositionNode, 1)).xyz;
|
|
382
487
|
return context(lighting, {
|
|
383
488
|
positionView: view,
|
|
384
489
|
positionWorld: world,
|
|
@@ -388,7 +493,7 @@ var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
|
|
|
388
493
|
};
|
|
389
494
|
|
|
390
495
|
// src/box-impostor/box-impostor-ray-bounds.js
|
|
391
|
-
import { Matrix4, NodeUpdateType, StorageBufferAttribute, Vector3 } from "three/webgpu";
|
|
496
|
+
import { Matrix4, NodeUpdateType as NodeUpdateType2, StorageBufferAttribute, Vector3 } from "three/webgpu";
|
|
392
497
|
import {
|
|
393
498
|
Fn as Fn2,
|
|
394
499
|
If,
|
|
@@ -398,17 +503,17 @@ import {
|
|
|
398
503
|
clamp,
|
|
399
504
|
float,
|
|
400
505
|
floor,
|
|
401
|
-
instanceIndex,
|
|
402
|
-
int,
|
|
403
|
-
ivec2,
|
|
506
|
+
instanceIndex as instanceIndex2,
|
|
507
|
+
int as int2,
|
|
508
|
+
ivec2 as ivec22,
|
|
404
509
|
max,
|
|
405
510
|
min,
|
|
406
511
|
storage,
|
|
407
|
-
textureLoad,
|
|
512
|
+
textureLoad as textureLoad2,
|
|
408
513
|
uint,
|
|
409
514
|
vec2,
|
|
410
|
-
vec3,
|
|
411
|
-
vec4 as
|
|
515
|
+
vec3 as vec32,
|
|
516
|
+
vec4 as vec44
|
|
412
517
|
} from "three/tsl";
|
|
413
518
|
var MIN_TILE_SIZE = 3;
|
|
414
519
|
var MAX_CAPTURE_TILES = 512 * 1024;
|
|
@@ -451,18 +556,18 @@ function buildPatches(descriptor, maps, rects, tiles, size, output) {
|
|
|
451
556
|
const descriptions = storage(tiles, "vec4", tiles.count).toReadOnly();
|
|
452
557
|
const patches = storage(output, "vec4", output.count);
|
|
453
558
|
return Fn2(() => {
|
|
454
|
-
const meta = descriptions.element(
|
|
455
|
-
const key =
|
|
456
|
-
const rect =
|
|
457
|
-
const extent =
|
|
458
|
-
const origin =
|
|
559
|
+
const meta = descriptions.element(instanceIndex2).toVar();
|
|
560
|
+
const key = int2(meta.x), face = key.mod(6), axis = face.div(2);
|
|
561
|
+
const rect = textureLoad2(rects, ivec22(face, key.div(6))).mul(descriptor.atlasResolution).round().toVar();
|
|
562
|
+
const extent = ivec22(rect.zw.add(1).sub(meta.yz)).min(ivec22(size));
|
|
563
|
+
const origin = ivec22(rect.xy.add(meta.yz));
|
|
459
564
|
const near = float(1).toVar(), far = float(0).toVar();
|
|
460
565
|
function visit(process) {
|
|
461
|
-
Loop({ start:
|
|
462
|
-
Loop({ start:
|
|
463
|
-
const pixel = origin.add(
|
|
464
|
-
If(
|
|
465
|
-
const packed =
|
|
566
|
+
Loop({ start: int2(0), end: extent.y, type: "int", name: "row" }, ({ row }) => {
|
|
567
|
+
Loop({ start: int2(0), end: extent.x, type: "int", name: "column" }, ({ column }) => {
|
|
568
|
+
const pixel = origin.add(ivec22(column, row));
|
|
569
|
+
If(textureLoad2(maps.orm, pixel).a.greaterThan(0.5), () => {
|
|
570
|
+
const packed = textureLoad2(maps.normalDepth, pixel);
|
|
466
571
|
const depth = packed.b.mul(65280).add(packed.a.mul(255)).div(DEPTH_MAX).toVar();
|
|
467
572
|
process(column, row, depth);
|
|
468
573
|
});
|
|
@@ -474,11 +579,11 @@ function buildPatches(descriptor, maps, rects, tiles, size, output) {
|
|
|
474
579
|
far.assign(max(far, depth));
|
|
475
580
|
});
|
|
476
581
|
const middle = near.add(far).mul(0.5).toVar();
|
|
477
|
-
const low = [
|
|
582
|
+
const low = [vec32(2).toVar(), vec32(2).toVar()], high = [vec32(-2).toVar(), vec32(-2).toVar()];
|
|
478
583
|
visit((x, y, depth) => {
|
|
479
584
|
const uv2 = meta.yz.add(vec2(x, y)).div(rect.zw).mul(2).sub(1);
|
|
480
|
-
const lo =
|
|
481
|
-
const hi =
|
|
585
|
+
const lo = vec32(uv2.clamp(-1, 1), float(1).sub(depth.mul(2)).sub(meta.w).max(-1)).toVar();
|
|
586
|
+
const hi = vec32(
|
|
482
587
|
uv2.add(vec2(2).div(rect.zw)).clamp(-1, 1),
|
|
483
588
|
float(1).sub(depth.mul(2)).add(meta.w).min(1)
|
|
484
589
|
).toVar();
|
|
@@ -495,38 +600,38 @@ function buildPatches(descriptor, maps, rects, tiles, size, output) {
|
|
|
495
600
|
});
|
|
496
601
|
});
|
|
497
602
|
const facing = face.mod(2).equal(0).select(1, -1);
|
|
498
|
-
const right = axis.equal(0).select(
|
|
499
|
-
const up = axis.equal(1).select(
|
|
603
|
+
const right = axis.equal(0).select(vec32(0, 0, facing.negate()), axis.equal(1).select(vec32(1, 0, 0), vec32(facing, 0, 0)));
|
|
604
|
+
const up = axis.equal(1).select(vec32(0, 0, facing.negate()), vec32(0, 1, 0));
|
|
500
605
|
const normal = right.cross(up);
|
|
501
606
|
const rotate = (p) => right.mul(p.x).add(up.mul(p.y)).add(normal.mul(p.z));
|
|
502
607
|
for (let group = 0; group < 2; group++) {
|
|
503
|
-
const a = rotate(low[group]), b = rotate(high[group]), slot =
|
|
608
|
+
const a = rotate(low[group]), b = rotate(high[group]), slot = instanceIndex2.mul(4).add(group * 2);
|
|
504
609
|
const valid = low[group].x.lessThanEqual(high[group].x);
|
|
505
|
-
patches.element(slot).assign(
|
|
506
|
-
patches.element(slot.add(1)).assign(
|
|
610
|
+
patches.element(slot).assign(vec44(min(a, b), valid.select(meta.x, -1)));
|
|
611
|
+
patches.element(slot.add(1)).assign(vec44(max(a, b), 0));
|
|
507
612
|
}
|
|
508
613
|
})().compute(tiles.count);
|
|
509
614
|
}
|
|
510
615
|
function makeViewPasses(patchBuffer, views, ranges, count, resolution) {
|
|
511
616
|
const table = storage(ranges, "uint", count).toAtomic();
|
|
512
617
|
const reset = Fn2(() => {
|
|
513
|
-
atomicStore(table.element(
|
|
618
|
+
atomicStore(table.element(instanceIndex2), uint(DEPTH_MAX));
|
|
514
619
|
})().compute(count);
|
|
515
620
|
const patches = storage(patchBuffer, "vec4", patchBuffer.count).toReadOnly();
|
|
516
621
|
const scatter = Fn2(() => {
|
|
517
|
-
const a = patches.element(
|
|
622
|
+
const a = patches.element(instanceIndex2.mul(2)).toVar(), b = patches.element(instanceIndex2.mul(2).add(1)).toVar();
|
|
518
623
|
If(a.w.greaterThanEqual(0), () => {
|
|
519
|
-
const key =
|
|
624
|
+
const key = int2(a.w), box = key.div(6);
|
|
520
625
|
const mapping = views.element(box.mul(2)), reference = views.element(box.mul(2).add(1));
|
|
521
626
|
If(reference.w.greaterThanEqual(0), () => {
|
|
522
|
-
const axis =
|
|
627
|
+
const axis = int2(reference.w).mod(3), x = axis.add(1).mod(3), y = axis.add(2).mod(3);
|
|
523
628
|
const e = vec2(reference.element(x), reference.element(y)), ez = reference.element(axis);
|
|
524
629
|
const za = a.element(axis), zb = b.element(axis);
|
|
525
630
|
const weight = reference.w.lessThan(3).select(1, 0).toVar();
|
|
526
631
|
const eyeSlope = e.div(ez).toVar(), inverseDistance = weight.div(ez).toVar();
|
|
527
632
|
const project = (v, z) => v.sub(eyeSlope.mul(z)).div(float(1).sub(inverseDistance.mul(z)));
|
|
528
633
|
const l = vec2(a.element(x), a.element(y)), h = vec2(b.element(x), b.element(y));
|
|
529
|
-
const cell = (v, bias) =>
|
|
634
|
+
const cell = (v, bias) => ivec22(floor(v.sub(mapping.xy).mul(mapping.zw).add(bias)).clamp(0, resolution - 1));
|
|
530
635
|
const lo = cell(min(project(l, za), project(l, zb)), -5e-3);
|
|
531
636
|
const hi = cell(max(project(h, za), project(h, zb)), 5e-3);
|
|
532
637
|
const padding = vec2(0.506).div(mapping.zw).mul(inverseDistance.abs().add(1)).add(2e-6);
|
|
@@ -678,7 +783,7 @@ function createBoxRayBounds(descriptor, maps, rects, material) {
|
|
|
678
783
|
slot.ortho = ortho;
|
|
679
784
|
slot.ready = true;
|
|
680
785
|
}
|
|
681
|
-
views.updateBeforeType =
|
|
786
|
+
views.updateBeforeType = NodeUpdateType2.OBJECT;
|
|
682
787
|
views.updateBefore = prepare;
|
|
683
788
|
material.addEventListener("dispose", () => {
|
|
684
789
|
for (const state of states.values()) {
|
|
@@ -700,7 +805,7 @@ function createBoxRayBounds(descriptor, maps, rects, material) {
|
|
|
700
805
|
if (!builder.renderer.backend?.isWebGPUBackend) return null;
|
|
701
806
|
const mapping = views.element(id.mul(2)).toVar();
|
|
702
807
|
const ref = views.element(id.mul(2).add(1)).toVar();
|
|
703
|
-
const axis =
|
|
808
|
+
const axis = int2(max(ref.w, 0)).mod(3), x = axis.add(1).mod(3), y = axis.add(2).mod(3);
|
|
704
809
|
const rz = r.element(axis).toVar(), oz = o.element(axis).toVar();
|
|
705
810
|
const ez = ref.element(axis), e = vec2(ref.element(x), ref.element(y));
|
|
706
811
|
const eyeSlope = e.div(ez), inverseDistance = ref.w.lessThan(3).select(1, 0).div(ez);
|
|
@@ -710,9 +815,9 @@ function createBoxRayBounds(descriptor, maps, rects, material) {
|
|
|
710
815
|
return p.sub(mapping.xy).mul(mapping.zw);
|
|
711
816
|
};
|
|
712
817
|
const a = project(enter).toVar(), b = project(exit).toVar();
|
|
713
|
-
const cellLo =
|
|
714
|
-
const cellHi =
|
|
715
|
-
const valid = ref.w.greaterThanEqual(0).and(rz.abs().greaterThan(1e-12)).and(cellHi.sub(cellLo).lessThanEqual(
|
|
818
|
+
const cellLo = ivec22(floor(min(a, b).sub(1e-3)).clamp(0, resolution - 1)).toVar();
|
|
819
|
+
const cellHi = ivec22(floor(max(a, b).add(1e-3)).clamp(0, resolution - 1)).toVar();
|
|
820
|
+
const valid = ref.w.greaterThanEqual(0).and(rz.abs().greaterThan(1e-12)).and(cellHi.sub(cellLo).lessThanEqual(ivec22(2)).all()).toVar();
|
|
716
821
|
return (face, enter2, exit2) => {
|
|
717
822
|
const start = enter2.toVar(), end = exit2.toVar();
|
|
718
823
|
If(valid, () => {
|
|
@@ -756,21 +861,21 @@ function refineSurfaceCrossing({ float: float5, If: If5, Loop: Loop3 }, { lo, hi
|
|
|
756
861
|
}
|
|
757
862
|
|
|
758
863
|
// src/box-impostor/box-impostor-faces.js
|
|
759
|
-
import { DataTexture, FloatType, RGBAFormat, NearestFilter } from "three";
|
|
864
|
+
import { DataTexture as DataTexture2, FloatType as FloatType2, RGBAFormat as RGBAFormat2, NearestFilter } from "three";
|
|
760
865
|
import {
|
|
761
866
|
Fn as Fn3,
|
|
762
867
|
If as If2,
|
|
763
868
|
attribute,
|
|
764
|
-
|
|
869
|
+
positionGeometry,
|
|
765
870
|
uv,
|
|
766
871
|
texture,
|
|
767
|
-
textureLoad as
|
|
768
|
-
ivec2 as
|
|
872
|
+
textureLoad as textureLoad3,
|
|
873
|
+
ivec2 as ivec23,
|
|
769
874
|
vec2 as vec22,
|
|
770
|
-
vec3 as
|
|
875
|
+
vec3 as vec33,
|
|
771
876
|
float as float2,
|
|
772
|
-
int as
|
|
773
|
-
dot,
|
|
877
|
+
int as int3,
|
|
878
|
+
dot as dot2,
|
|
774
879
|
abs,
|
|
775
880
|
max as max2,
|
|
776
881
|
sign,
|
|
@@ -784,45 +889,47 @@ function applyBoxFaces({
|
|
|
784
889
|
transparency,
|
|
785
890
|
half,
|
|
786
891
|
toBox,
|
|
787
|
-
fromBox
|
|
892
|
+
fromBox,
|
|
893
|
+
meshVertex,
|
|
894
|
+
meshNormal
|
|
788
895
|
}) {
|
|
789
896
|
const values = new Float32Array(descriptor.boxes.length * 6 * 4);
|
|
790
897
|
descriptor.boxes.forEach((box, i) => {
|
|
791
898
|
for (let face2 = 0; face2 < 6; face2++)
|
|
792
899
|
values[(i * 6 + face2) * 4] = box.planes?.[face2] ?? 0.5;
|
|
793
900
|
});
|
|
794
|
-
const planes = new
|
|
901
|
+
const planes = new DataTexture2(
|
|
795
902
|
values,
|
|
796
903
|
6,
|
|
797
904
|
descriptor.boxes.length,
|
|
798
|
-
|
|
799
|
-
|
|
905
|
+
RGBAFormat2,
|
|
906
|
+
FloatType2
|
|
800
907
|
);
|
|
801
908
|
planes.minFilter = planes.magFilter = NearestFilter;
|
|
802
909
|
planes.needsUpdate = true;
|
|
803
910
|
material.addEventListener("dispose", () => planes.dispose());
|
|
804
|
-
const face =
|
|
911
|
+
const face = int3(attribute("boxFace", "float"));
|
|
805
912
|
const n = attribute("normal", "vec3");
|
|
806
|
-
const depth =
|
|
913
|
+
const depth = textureLoad3(
|
|
807
914
|
planes,
|
|
808
|
-
|
|
915
|
+
ivec23(face, int3(attribute("boxId", "float")))
|
|
809
916
|
).r;
|
|
810
|
-
const proxy =
|
|
917
|
+
const proxy = positionGeometry.sub(
|
|
811
918
|
n.mul(
|
|
812
|
-
|
|
919
|
+
dot2(abs(toBox(n)), half).mul(depth).mul(2)
|
|
813
920
|
)
|
|
814
921
|
);
|
|
815
|
-
material.positionNode = proxy;
|
|
816
|
-
material.castShadowPositionNode =
|
|
922
|
+
material.positionNode = meshVertex(proxy);
|
|
923
|
+
material.castShadowPositionNode = material.positionNode;
|
|
817
924
|
const base = texture(maps.baseColor, uv()).level(0).toVar("boxFaceColor");
|
|
818
925
|
const orm = texture(maps.orm, uv()).level(0);
|
|
819
926
|
const coverage = max2(orm.a, 1e-10);
|
|
820
927
|
const normal = Fn3(() => {
|
|
821
|
-
const packed =
|
|
928
|
+
const packed = textureLoad3(
|
|
822
929
|
maps.normalDepth,
|
|
823
|
-
|
|
930
|
+
ivec23(uv().mul(descriptor.atlasResolution))
|
|
824
931
|
).rg.mul(2).sub(1);
|
|
825
|
-
const decoded =
|
|
932
|
+
const decoded = vec33(
|
|
826
933
|
packed,
|
|
827
934
|
float2(1).sub(abs(packed.x)).sub(abs(packed.y))
|
|
828
935
|
).toVar();
|
|
@@ -831,13 +938,13 @@ function applyBoxFaces({
|
|
|
831
938
|
vec22(1).sub(abs(decoded.yx)).mul(sign(decoded.xy.add(1e-20)))
|
|
832
939
|
);
|
|
833
940
|
});
|
|
834
|
-
const result =
|
|
941
|
+
const result = vec33(0).toVar();
|
|
835
942
|
for (let f = 0; f < 6; f++) {
|
|
836
943
|
const a = BOX_FACE_AXES[f];
|
|
837
944
|
If2(face.equal(f), () => {
|
|
838
945
|
result.assign(
|
|
839
946
|
fromBox(
|
|
840
|
-
|
|
947
|
+
vec33(...a.right).mul(decoded.x).add(vec33(...a.up).mul(decoded.y)).add(vec33(...a.normal).mul(decoded.z))
|
|
841
948
|
).normalize()
|
|
842
949
|
);
|
|
843
950
|
});
|
|
@@ -845,7 +952,7 @@ function applyBoxFaces({
|
|
|
845
952
|
return result;
|
|
846
953
|
})();
|
|
847
954
|
material.colorNode = base.rgb.div(coverage);
|
|
848
|
-
material.normalNode = transformNormalToView(normal);
|
|
955
|
+
material.normalNode = transformNormalToView(meshNormal(normal));
|
|
849
956
|
material.roughnessNode = orm.g.div(coverage);
|
|
850
957
|
material.metalnessNode = orm.b.div(coverage);
|
|
851
958
|
material.aoNode = orm.r.div(coverage);
|
|
@@ -860,23 +967,23 @@ function applyBoxFaces({
|
|
|
860
967
|
}
|
|
861
968
|
|
|
862
969
|
// src/box-impostor/box-impostor-projection.js
|
|
863
|
-
import { DataTexture as
|
|
970
|
+
import { DataTexture as DataTexture3, FloatType as FloatType3, RGBAFormat as RGBAFormat3, NearestFilter as NearestFilter2 } from "three";
|
|
864
971
|
import {
|
|
865
972
|
Fn as Fn4,
|
|
866
973
|
If as If3,
|
|
867
974
|
vec2 as vec23,
|
|
868
|
-
vec3 as
|
|
869
|
-
vec4 as
|
|
975
|
+
vec3 as vec34,
|
|
976
|
+
vec4 as vec45,
|
|
870
977
|
float as float3,
|
|
871
|
-
int as
|
|
872
|
-
ivec2 as
|
|
873
|
-
dot as
|
|
978
|
+
int as int4,
|
|
979
|
+
ivec2 as ivec24,
|
|
980
|
+
dot as dot3,
|
|
874
981
|
abs as abs2,
|
|
875
982
|
max as max3,
|
|
876
983
|
sign as sign2,
|
|
877
984
|
normalize,
|
|
878
985
|
texture as texture2,
|
|
879
|
-
textureLoad as
|
|
986
|
+
textureLoad as textureLoad4,
|
|
880
987
|
attribute as attribute2,
|
|
881
988
|
struct,
|
|
882
989
|
modelWorldMatrix as modelWorldMatrix2,
|
|
@@ -897,19 +1004,21 @@ function applyBoxProjection({
|
|
|
897
1004
|
eye,
|
|
898
1005
|
viewAxis,
|
|
899
1006
|
perspective,
|
|
900
|
-
raster
|
|
1007
|
+
raster,
|
|
1008
|
+
meshPoint,
|
|
1009
|
+
meshNormal
|
|
901
1010
|
}) {
|
|
902
1011
|
const data = new Float32Array(descriptor.boxes.length * 24);
|
|
903
1012
|
descriptor.boxes.forEach((box, i) => {
|
|
904
1013
|
for (let face = 0; face < 6; face++)
|
|
905
1014
|
data[(i * 6 + face) * 4] = box.planes?.[face] ?? 0.5;
|
|
906
1015
|
});
|
|
907
|
-
const planes = new
|
|
1016
|
+
const planes = new DataTexture3(
|
|
908
1017
|
data,
|
|
909
1018
|
6,
|
|
910
1019
|
descriptor.boxes.length,
|
|
911
|
-
|
|
912
|
-
|
|
1020
|
+
RGBAFormat3,
|
|
1021
|
+
FloatType3
|
|
913
1022
|
);
|
|
914
1023
|
planes.minFilter = planes.magFilter = NearestFilter2;
|
|
915
1024
|
planes.needsUpdate = true;
|
|
@@ -926,11 +1035,11 @@ function applyBoxProjection({
|
|
|
926
1035
|
"BoxProjectedSurface"
|
|
927
1036
|
);
|
|
928
1037
|
const readNormal = (uv2, right, up, n) => {
|
|
929
|
-
const packed =
|
|
1038
|
+
const packed = textureLoad4(
|
|
930
1039
|
maps.normalDepth,
|
|
931
|
-
|
|
1040
|
+
ivec24(uv2.mul(descriptor.atlasResolution))
|
|
932
1041
|
).rg.mul(2).sub(1);
|
|
933
|
-
const decoded =
|
|
1042
|
+
const decoded = vec34(
|
|
934
1043
|
packed,
|
|
935
1044
|
float3(1).sub(abs2(packed.x)).sub(abs2(packed.y))
|
|
936
1045
|
).toVar();
|
|
@@ -954,20 +1063,20 @@ function applyBoxProjection({
|
|
|
954
1063
|
);
|
|
955
1064
|
const o = toBox(origin.sub(center)).div(half), r = toBox(ray).div(half);
|
|
956
1065
|
const look = normalize(toBox(ray).negate()), angles = abs2(look);
|
|
957
|
-
const positions =
|
|
958
|
-
const selectedUv = vec23(0).toVar(), selectedRight =
|
|
1066
|
+
const positions = vec34(0).toVar();
|
|
1067
|
+
const selectedUv = vec23(0).toVar(), selectedRight = vec34(1, 0, 0).toVar(), selectedUp = vec34(0, 1, 0).toVar(), selectedNormal = vec34(0, 0, 1).toVar();
|
|
959
1068
|
const nearest = float3(1e20).toVar("boxPlaneDistance");
|
|
960
1069
|
const opacity = float3(0).toVar();
|
|
961
1070
|
const candidates = [];
|
|
962
1071
|
for (let axis = 0; axis < 3; axis++) {
|
|
963
1072
|
const angle = angles.element(axis);
|
|
964
1073
|
const positive = look.element(axis).greaterThanEqual(0), a = BOX_FACE_AXES[axis * 2], b = BOX_FACE_AXES[axis * 2 + 1];
|
|
965
|
-
const n = positive.select(
|
|
1074
|
+
const n = positive.select(vec34(...a.normal), vec34(...b.normal)), right = positive.select(vec34(...a.right), vec34(...b.right)), up = positive.select(vec34(...a.up), vec34(...b.up));
|
|
966
1075
|
const weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
|
|
967
1076
|
const candidate = settings.interpolation === "none" ? null : {
|
|
968
1077
|
uv: vec23(0).toVar(),
|
|
969
|
-
base:
|
|
970
|
-
orm:
|
|
1078
|
+
base: vec45(0).toVar(),
|
|
1079
|
+
orm: vec45(0).toVar(),
|
|
971
1080
|
t: float3(1e20).toVar(),
|
|
972
1081
|
tolerance: float3(0).toVar(),
|
|
973
1082
|
right,
|
|
@@ -977,17 +1086,17 @@ function applyBoxProjection({
|
|
|
977
1086
|
};
|
|
978
1087
|
if (candidate) candidates.push(candidate);
|
|
979
1088
|
If3(angle.greaterThan(1e-4), () => {
|
|
980
|
-
const face = positive.select(
|
|
1089
|
+
const face = positive.select(int4(axis * 2), int4(axis * 2 + 1)), id = int4(attribute2("boxId", "float"));
|
|
981
1090
|
const plane = float3(1).sub(
|
|
982
|
-
|
|
1091
|
+
textureLoad4(planes, ivec24(face, id)).r.mul(2)
|
|
983
1092
|
);
|
|
984
|
-
const divisor2 =
|
|
985
|
-
const t = plane.sub(
|
|
986
|
-
const xy = vec23(
|
|
1093
|
+
const divisor2 = dot3(r, n), safe = sign2(divisor2.add(1e-20)).mul(max3(abs2(divisor2), 1e-10));
|
|
1094
|
+
const t = plane.sub(dot3(o, n)).div(safe), point = o.add(r.mul(t));
|
|
1095
|
+
const xy = vec23(dot3(point, right), dot3(point, up)).mul(0.5).add(0.5);
|
|
987
1096
|
let supported = t.greaterThanEqual(0).and(xy.x.greaterThanEqual(0)).and(xy.y.greaterThanEqual(0)).and(xy.x.lessThanEqual(1)).and(xy.y.lessThanEqual(1));
|
|
988
1097
|
if (!candidate) supported = supported.and(t.lessThan(nearest));
|
|
989
1098
|
If3(supported, () => {
|
|
990
|
-
const rect =
|
|
1099
|
+
const rect = textureLoad4(rects, ivec24(face, id)), uv2 = rect.xy.add(xy.mul(rect.zw));
|
|
991
1100
|
const base = texture2(maps.baseColor, uv2).level(0), orm = texture2(maps.orm, uv2).level(0);
|
|
992
1101
|
const alpha2 = base.a.clamp(0, 1);
|
|
993
1102
|
If3(
|
|
@@ -1011,10 +1120,10 @@ function applyBoxProjection({
|
|
|
1011
1120
|
candidate.t.assign(t);
|
|
1012
1121
|
candidate.tolerance.assign(
|
|
1013
1122
|
max3(
|
|
1014
|
-
|
|
1123
|
+
dot3(abs2(right), half).div(
|
|
1015
1124
|
rect.z.mul(descriptor.atlasResolution)
|
|
1016
1125
|
),
|
|
1017
|
-
|
|
1126
|
+
dot3(abs2(up), half).div(
|
|
1018
1127
|
rect.w.mul(descriptor.atlasResolution)
|
|
1019
1128
|
)
|
|
1020
1129
|
).mul(4).div(max3(angle, 0.1))
|
|
@@ -1037,7 +1146,7 @@ function applyBoxProjection({
|
|
|
1037
1146
|
positions
|
|
1038
1147
|
);
|
|
1039
1148
|
}
|
|
1040
|
-
const colors =
|
|
1149
|
+
const colors = vec34(0).toVar(), orms = vec34(0).toVar(), emissions = vec34(0).toVar(), normals = vec34(0).toVar();
|
|
1041
1150
|
const weights = float3(0).toVar(), support = float3(0).toVar(), alpha = float3(0).toVar();
|
|
1042
1151
|
for (const c of candidates) {
|
|
1043
1152
|
If3(
|
|
@@ -1066,9 +1175,10 @@ function applyBoxProjection({
|
|
|
1066
1175
|
positions
|
|
1067
1176
|
);
|
|
1068
1177
|
})().toVar("boxProjectedSurface");
|
|
1069
|
-
|
|
1178
|
+
const position = meshPoint(surface.get("position")).toVar("boxProjectedMeshPosition");
|
|
1179
|
+
material.capturedPositionNode = position;
|
|
1070
1180
|
material.colorNode = surface.get("color");
|
|
1071
|
-
material.normalNode = transformNormalToView2(surface.get("normal"));
|
|
1181
|
+
material.normalNode = transformNormalToView2(meshNormal(surface.get("normal")));
|
|
1072
1182
|
material.roughnessNode = surface.get("orm").g;
|
|
1073
1183
|
material.metalnessNode = surface.get("orm").b;
|
|
1074
1184
|
material.aoNode = surface.get("orm").r;
|
|
@@ -1076,9 +1186,9 @@ function applyBoxProjection({
|
|
|
1076
1186
|
material.maskNode = surface.get("alpha").greaterThanEqual(transparency === "cutout" ? alphaCutoff : float3(1 / 255));
|
|
1077
1187
|
material.maskShadowNode = material.maskNode;
|
|
1078
1188
|
if (transparency !== "cutout") material.opacityNode = surface.get("alpha");
|
|
1079
|
-
material.depthNode = capturedSurfaceDepth(
|
|
1189
|
+
material.depthNode = capturedSurfaceDepth(position);
|
|
1080
1190
|
material.receivedShadowPositionNode = modelWorldMatrix2.mul(
|
|
1081
|
-
|
|
1191
|
+
vec45(position, 1)
|
|
1082
1192
|
).xyz;
|
|
1083
1193
|
material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
|
|
1084
1194
|
return material;
|
|
@@ -1096,7 +1206,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1096
1206
|
const surfaceCoverage = (ormAlpha, baseAlpha) => filtering && descriptor.alphaMode !== "blend" ? min2(ormAlpha, baseAlpha) : ormAlpha;
|
|
1097
1207
|
const unpackNormal = (encoded) => {
|
|
1098
1208
|
const packed = encoded.mul(2).sub(1);
|
|
1099
|
-
const normal2 =
|
|
1209
|
+
const normal2 = vec35(packed, float4(1).sub(abs3(packed.x)).sub(abs3(packed.y))).toVar();
|
|
1100
1210
|
If4(normal2.z.lessThan(0), () => {
|
|
1101
1211
|
normal2.xy.assign(vec24(1).sub(abs3(normal2.yx)).mul(sign3(normal2.xy.add(1e-20))));
|
|
1102
1212
|
});
|
|
@@ -1109,14 +1219,14 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1109
1219
|
const nearestNormal = decodeNormal(nearest.rg), result = decodeNormal(sampled.rg.div(coverage2)).toVar();
|
|
1110
1220
|
const sampledDepth = sampled.b.mul(65280).add(sampled.a.mul(255)).div(65535);
|
|
1111
1221
|
const nearestDepth = nearest.b.mul(65280).add(nearest.a.mul(255)).div(65535);
|
|
1112
|
-
If4(nearestNormal.z.lessThan(0).or(
|
|
1222
|
+
If4(nearestNormal.z.lessThan(0).or(dot4(result, nearestNormal).lessThan(0.75)).or(coverage2.lessThan(0.9999)).or(abs3(sampledDepth.sub(nearestDepth)).greaterThan(tolerance)), () => {
|
|
1113
1223
|
const p = uv2.mul(descriptor.atlasResolution).sub(0.5), lo = floor2(p), f = fract(p);
|
|
1114
|
-
const normal2 =
|
|
1224
|
+
const normal2 = vec35(0).toVar(), weights = float4(0).toVar();
|
|
1115
1225
|
for (const [x, y] of [[0, 0], [1, 0], [0, 1], [1, 1]]) {
|
|
1116
|
-
const texel =
|
|
1117
|
-
const packed =
|
|
1118
|
-
const alpha2 =
|
|
1119
|
-
const coverage3 = descriptor.alphaMode === "blend" ?
|
|
1226
|
+
const texel = ivec25(lo.add(vec24(x, y)).clamp(0, descriptor.atlasResolution - 1));
|
|
1227
|
+
const packed = textureLoad5(maps.normalDepth, texel).toVar();
|
|
1228
|
+
const alpha2 = textureLoad5(maps.baseColor, texel).a;
|
|
1229
|
+
const coverage3 = descriptor.alphaMode === "blend" ? textureLoad5(maps.orm, texel).a : alpha2;
|
|
1120
1230
|
const capturedDepth = packed.b.mul(65280).add(packed.a.mul(255)).div(65535);
|
|
1121
1231
|
const supported = coverage3.greaterThan(1e-4).and(alpha2.greaterThanEqual(minimumAlpha.mul(coverage3))).and(abs3(capturedDepth.sub(depth)).lessThanEqual(tolerance));
|
|
1122
1232
|
If4(supported, () => {
|
|
@@ -1158,46 +1268,57 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1158
1268
|
)
|
|
1159
1269
|
)
|
|
1160
1270
|
);
|
|
1161
|
-
const rects = new
|
|
1271
|
+
const rects = new DataTexture4(
|
|
1162
1272
|
rectData,
|
|
1163
1273
|
6,
|
|
1164
1274
|
descriptor.boxes.length,
|
|
1165
|
-
|
|
1166
|
-
|
|
1275
|
+
RGBAFormat4,
|
|
1276
|
+
FloatType4
|
|
1167
1277
|
);
|
|
1168
1278
|
rects.needsUpdate = true;
|
|
1169
1279
|
rects.magFilter = rects.minFilter = NearestFilter3;
|
|
1170
1280
|
material.addEventListener("dispose", () => rects.dispose());
|
|
1171
1281
|
const center = attribute3("boxCenter", "vec3"), half = attribute3("boxHalfSize", "vec3");
|
|
1172
1282
|
const ax = attribute3("boxAxisX", "vec3"), ay = attribute3("boxAxisY", "vec3"), az = attribute3("boxAxisZ", "vec3");
|
|
1173
|
-
const toBox = (p) =>
|
|
1283
|
+
const toBox = (p) => vec35(dot4(p, ax), dot4(p, ay), dot4(p, az));
|
|
1174
1284
|
const fromBox = (p) => ax.mul(p.x).add(ay.mul(p.y)).add(az.mul(p.z));
|
|
1175
|
-
const
|
|
1176
|
-
const
|
|
1285
|
+
const meshVertex = (p) => Fn5((_, builder) => instanceSpace(builder)?.toMeshPoint(p) ?? p)();
|
|
1286
|
+
const meshPoint = (p) => Fn5((_, builder) => instanceSpace(builder)?.fragment.toMeshPoint(p) ?? p)();
|
|
1287
|
+
const meshNormal = (n) => Fn5((_, builder) => instanceSpace(builder)?.fragment.toMeshNormal(n) ?? n)();
|
|
1288
|
+
const meshEye = modelWorldMatrixInverse.mul(vec46(cameraPosition, 1)).xyz;
|
|
1289
|
+
const meshViewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec46(0, 0, 1, 0))).xyz;
|
|
1290
|
+
const eye = Fn5((_, builder) => instanceSpace(builder)?.fragment.toLocalPoint(meshEye) ?? meshEye)();
|
|
1291
|
+
const viewAxis = Fn5((_, builder) => instanceSpace(builder)?.fragment.toLocalDirection(meshViewAxis) ?? meshViewAxis)().normalize();
|
|
1292
|
+
const vertexEye = Fn5((_, builder) => instanceSpace(builder)?.toLocalPoint(meshEye) ?? meshEye)();
|
|
1293
|
+
const vertexViewAxis = Fn5((_, builder) => instanceSpace(builder)?.toLocalDirection(meshViewAxis) ?? meshViewAxis)().normalize();
|
|
1294
|
+
const cameraUp = Fn5((_, builder) => {
|
|
1295
|
+
const up = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec46(0, 1, 0, 0))).xyz;
|
|
1296
|
+
return instanceSpace(builder)?.toLocalDirection(up) ?? up;
|
|
1297
|
+
})();
|
|
1177
1298
|
const perspective = cameraProjectionMatrix2.element(3).w.equal(0);
|
|
1178
|
-
let proxy =
|
|
1299
|
+
let proxy = positionGeometry2;
|
|
1179
1300
|
if (descriptor.mode !== "fitted") {
|
|
1180
1301
|
proxy = Fn5((_, builder) => {
|
|
1181
|
-
const delta =
|
|
1182
|
-
const forward = perspective.and(delta.length().greaterThan(1e-6)).select(normalize2(delta),
|
|
1183
|
-
const right =
|
|
1302
|
+
const delta = vertexEye.sub(center);
|
|
1303
|
+
const forward = perspective.and(delta.length().greaterThan(1e-6)).select(normalize2(delta), vertexViewAxis);
|
|
1304
|
+
const right = cameraUp.cross(forward).normalize();
|
|
1184
1305
|
const up = forward.cross(right).normalize();
|
|
1185
1306
|
const radius = half.length();
|
|
1186
|
-
const distance =
|
|
1307
|
+
const distance = dot4(vertexEye.sub(center), forward);
|
|
1187
1308
|
const planeOffset = perspective.select(min2(radius, distance.sub(cameraNear2.mul(2))), radius);
|
|
1188
1309
|
const plane = center.add(forward.mul(planeOffset));
|
|
1189
1310
|
const lo = vec24(1e20).toVar(), hi = vec24(-1e20).toVar();
|
|
1190
1311
|
for (const x of [-1, 1])
|
|
1191
1312
|
for (const y of [-1, 1])
|
|
1192
1313
|
for (const z of [-1, 1]) {
|
|
1193
|
-
const corner2 = fromBox(half.mul(
|
|
1314
|
+
const corner2 = fromBox(half.mul(vec35(x, y, z)));
|
|
1194
1315
|
const scale = perspective.select(
|
|
1195
1316
|
max4(distance.sub(planeOffset), 1e-5).div(
|
|
1196
|
-
max4(distance.sub(
|
|
1317
|
+
max4(distance.sub(dot4(corner2, forward)), 1e-5)
|
|
1197
1318
|
),
|
|
1198
1319
|
float4(1)
|
|
1199
1320
|
);
|
|
1200
|
-
const p = vec24(
|
|
1321
|
+
const p = vec24(dot4(corner2, right), dot4(corner2, up)).mul(scale);
|
|
1201
1322
|
lo.assign(min2(lo, p));
|
|
1202
1323
|
hi.assign(max4(hi, p));
|
|
1203
1324
|
}
|
|
@@ -1205,10 +1326,10 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1205
1326
|
const xy = lo.add(hi.sub(lo).mul(corner));
|
|
1206
1327
|
return plane.add(right.mul(xy.x)).add(up.mul(xy.y));
|
|
1207
1328
|
})();
|
|
1208
|
-
material.positionNode = proxy;
|
|
1209
|
-
material.castShadowPositionNode =
|
|
1329
|
+
material.positionNode = meshVertex(proxy);
|
|
1330
|
+
material.castShadowPositionNode = material.positionNode;
|
|
1210
1331
|
}
|
|
1211
|
-
const raster =
|
|
1332
|
+
const raster = varying2(proxy, "boxRasterPosition");
|
|
1212
1333
|
if (boxDepthMode(descriptor, settings) === "projection" && descriptor.mode === "fitted" && settings.interpolation === "none") {
|
|
1213
1334
|
material.side = FrontSide;
|
|
1214
1335
|
return applyBoxFaces({
|
|
@@ -1219,7 +1340,9 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1219
1340
|
transparency,
|
|
1220
1341
|
half,
|
|
1221
1342
|
toBox,
|
|
1222
|
-
fromBox
|
|
1343
|
+
fromBox,
|
|
1344
|
+
meshVertex,
|
|
1345
|
+
meshNormal
|
|
1223
1346
|
});
|
|
1224
1347
|
}
|
|
1225
1348
|
if (boxDepthMode(descriptor, settings) === "projection")
|
|
@@ -1238,7 +1361,9 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1238
1361
|
eye,
|
|
1239
1362
|
viewAxis,
|
|
1240
1363
|
perspective,
|
|
1241
|
-
raster
|
|
1364
|
+
raster,
|
|
1365
|
+
meshPoint,
|
|
1366
|
+
meshNormal
|
|
1242
1367
|
});
|
|
1243
1368
|
const accelerated = descriptor.boxes.every((b) => b.depthBounds);
|
|
1244
1369
|
const gridSize = BOX_OCCUPANCY_RESOLUTION;
|
|
@@ -1251,7 +1376,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1251
1376
|
descriptor.boxes.forEach(
|
|
1252
1377
|
(box, index) => words.set(box.occupancy, index * wordsPerBox)
|
|
1253
1378
|
);
|
|
1254
|
-
occupancy = new
|
|
1379
|
+
occupancy = new DataTexture4(
|
|
1255
1380
|
words,
|
|
1256
1381
|
wordsPerBox,
|
|
1257
1382
|
descriptor.boxes.length,
|
|
@@ -1268,16 +1393,16 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1268
1393
|
"BoxImpostorHit"
|
|
1269
1394
|
);
|
|
1270
1395
|
const faceBasis = (axis, facing) => {
|
|
1271
|
-
const n =
|
|
1396
|
+
const n = vec35(
|
|
1272
1397
|
axis.equal(0).select(facing, 0),
|
|
1273
1398
|
axis.equal(1).select(facing, 0),
|
|
1274
1399
|
axis.equal(2).select(facing, 0)
|
|
1275
1400
|
).toVar();
|
|
1276
1401
|
const right = axis.equal(0).select(
|
|
1277
|
-
|
|
1278
|
-
axis.equal(1).select(
|
|
1402
|
+
vec35(0, 0, facing.negate()),
|
|
1403
|
+
axis.equal(1).select(vec35(1, 0, 0), vec35(facing, 0, 0))
|
|
1279
1404
|
).toVar();
|
|
1280
|
-
const up = axis.equal(1).select(
|
|
1405
|
+
const up = axis.equal(1).select(vec35(0, 0, facing.negate()), vec35(0, 1, 0)).toVar();
|
|
1281
1406
|
return { n, right, up };
|
|
1282
1407
|
};
|
|
1283
1408
|
const ray = perspective.select(normalize2(raster.sub(eye)), viewAxis.negate());
|
|
@@ -1288,17 +1413,17 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1288
1413
|
);
|
|
1289
1414
|
const o = toBox(origin.sub(center)).div(half), r = toBox(ray).div(half);
|
|
1290
1415
|
const safe = sign3(r.add(1e-20)).mul(max4(abs3(r), 1e-10));
|
|
1291
|
-
const a =
|
|
1416
|
+
const a = vec35(-1).sub(o).div(safe), b = vec35(1).sub(o).div(safe);
|
|
1292
1417
|
const enter = max4(max4(min2(a, b).x, min2(a, b).y), min2(a, b).z).max(0);
|
|
1293
1418
|
const exit = min2(min2(max4(a, b).x, max4(a, b).y), max4(a, b).z);
|
|
1294
|
-
const rayBounds = viewBounds?.(builder,
|
|
1419
|
+
const rayBounds = viewBounds?.(builder, int5(attribute3("boxId", "float")), o, r, enter, exit);
|
|
1295
1420
|
const occupiedStart = enter.toVar();
|
|
1296
1421
|
if (occupancy) {
|
|
1297
1422
|
const gridOrigin = o.add(1).mul(gridSize / 2);
|
|
1298
1423
|
const gridRay = r.mul(gridSize / 2);
|
|
1299
1424
|
const step = sign3(gridRay.add(1e-20));
|
|
1300
1425
|
const cell = floor2(gridOrigin.add(gridRay.mul(enter))).clamp(0, gridSize - 1).toVar();
|
|
1301
|
-
const inverse =
|
|
1426
|
+
const inverse = vec35(1).div(safe.mul(gridSize / 2));
|
|
1302
1427
|
const delta = abs3(inverse);
|
|
1303
1428
|
const next = cell.add(step.add(1).mul(0.5)).sub(gridOrigin).mul(inverse).toVar();
|
|
1304
1429
|
const t = enter.toVar();
|
|
@@ -1310,9 +1435,9 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1310
1435
|
const cellIndex = uint2(
|
|
1311
1436
|
cell.x.add(cell.y.mul(gridSize)).add(cell.z.mul(gridSize ** 2))
|
|
1312
1437
|
).toVar();
|
|
1313
|
-
const word =
|
|
1438
|
+
const word = textureLoad5(
|
|
1314
1439
|
occupancy,
|
|
1315
|
-
|
|
1440
|
+
ivec25(int5(cellIndex.shiftRight(5)), int5(attribute3("boxId", "float")))
|
|
1316
1441
|
).r;
|
|
1317
1442
|
const support = word.shiftRight(cellIndex.bitAnd(31)).bitAnd(1);
|
|
1318
1443
|
If4(support.notEqual(uint2(0)), () => {
|
|
@@ -1320,7 +1445,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1320
1445
|
Break();
|
|
1321
1446
|
});
|
|
1322
1447
|
const edge = min2(min2(next.x, next.y), next.z).toVar();
|
|
1323
|
-
const advance =
|
|
1448
|
+
const advance = vec35(
|
|
1324
1449
|
next.x.lessThanEqual(edge).select(1, 0),
|
|
1325
1450
|
next.y.lessThanEqual(edge).select(1, 0),
|
|
1326
1451
|
next.z.lessThanEqual(edge).select(1, 0)
|
|
@@ -1337,15 +1462,15 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1337
1462
|
});
|
|
1338
1463
|
}
|
|
1339
1464
|
const look = toBox(ray).negate();
|
|
1340
|
-
const bestT = exit.add(1).toVar(), bestUv = vec24(0).toVar(), bestFace =
|
|
1465
|
+
const bestT = exit.add(1).toVar(), bestUv = vec24(0).toVar(), bestFace = int5(0).toVar(), appearanceNear = preferFacingCapture ? float4(0).toVar() : null, valid = float4(0).toVar(), found = float4(0).toVar();
|
|
1341
1466
|
const angles = abs3(look).toVar();
|
|
1342
1467
|
const major = angles.x.greaterThanEqual(angles.y).and(angles.x.greaterThanEqual(angles.z)).select(
|
|
1343
|
-
|
|
1344
|
-
angles.y.greaterThanEqual(angles.z).select(
|
|
1468
|
+
int5(0),
|
|
1469
|
+
angles.y.greaterThanEqual(angles.z).select(int5(1), int5(2))
|
|
1345
1470
|
).toVar();
|
|
1346
1471
|
const nextAxis = major.add(1).mod(3), lastAxis = major.add(2).mod(3);
|
|
1347
1472
|
const second = angles.element(nextAxis).greaterThanEqual(angles.element(lastAxis)).select(nextAxis, lastAxis).toVar();
|
|
1348
|
-
const third =
|
|
1473
|
+
const third = int5(3).sub(major).sub(second).toVar();
|
|
1349
1474
|
Loop2(
|
|
1350
1475
|
{ start: 0, end: 6, type: "int", name: "viewIndex" },
|
|
1351
1476
|
({ viewIndex: i }) => {
|
|
@@ -1359,24 +1484,24 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1359
1484
|
look.element(axis).greaterThanEqual(0),
|
|
1360
1485
|
look.element(axis).lessThan(0)
|
|
1361
1486
|
);
|
|
1362
|
-
const face = axis.mul(2).add(positive.select(
|
|
1487
|
+
const face = axis.mul(2).add(positive.select(int5(0), int5(1)));
|
|
1363
1488
|
const bounds = rayBounds?.(face, enter, exit);
|
|
1364
1489
|
const facing = positive.select(1, -1);
|
|
1365
1490
|
const { n, right, up } = faceBasis(axis, facing);
|
|
1366
1491
|
const direction = i.lessThan(3).select(1, -1);
|
|
1367
|
-
const rect =
|
|
1492
|
+
const rect = textureLoad5(
|
|
1368
1493
|
rects,
|
|
1369
|
-
|
|
1494
|
+
ivec25(face, int5(attribute3("boxId", "float")))
|
|
1370
1495
|
).toVar();
|
|
1371
1496
|
const sampleUv = (p) => rect.xy.add(
|
|
1372
|
-
vec24(
|
|
1497
|
+
vec24(dot4(p, right), dot4(p, up)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
|
|
1373
1498
|
);
|
|
1374
1499
|
const decode = (d) => d.b.mul(65280).add(d.a.mul(255)).div(65535);
|
|
1375
1500
|
const depthAt = (uv2) => decode(texture3(maps.normalDepth, uv2).level(0));
|
|
1376
|
-
const projectedOrigin = vec24(
|
|
1377
|
-
const projectedRay = vec24(
|
|
1378
|
-
const axialOrigin =
|
|
1379
|
-
const axialRay =
|
|
1501
|
+
const projectedOrigin = vec24(dot4(o, right), dot4(o, up)).toVar();
|
|
1502
|
+
const projectedRay = vec24(dot4(r, right), dot4(r, up)).toVar();
|
|
1503
|
+
const axialOrigin = dot4(o, n).toVar();
|
|
1504
|
+
const axialRay = dot4(r, n).toVar();
|
|
1380
1505
|
const distance = (t) => {
|
|
1381
1506
|
const uv2 = rect.xy.add(
|
|
1382
1507
|
projectedOrigin.add(projectedRay.mul(t)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
|
|
@@ -1385,17 +1510,17 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1385
1510
|
};
|
|
1386
1511
|
const acceptHit = (hitT) => {
|
|
1387
1512
|
const point2 = o.add(r.mul(hitT)), uv2 = sampleUv(point2);
|
|
1388
|
-
const texel =
|
|
1389
|
-
const nearest =
|
|
1513
|
+
const texel = ivec25(uv2.mul(descriptor.atlasResolution));
|
|
1514
|
+
const nearest = textureLoad5(maps.normalDepth, texel);
|
|
1390
1515
|
const capturedBase = texture3(maps.baseColor, uv2).level(0);
|
|
1391
|
-
const occupied =
|
|
1516
|
+
const occupied = textureLoad5(maps.orm, texel).a.greaterThan(0.5);
|
|
1392
1517
|
const residual = abs3(
|
|
1393
|
-
|
|
1518
|
+
dot4(point2, n).sub(float4(1).sub(decode(nearest).mul(2)))
|
|
1394
1519
|
);
|
|
1395
1520
|
const tolerance = max4(
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
).div(
|
|
1521
|
+
dot4(abs3(right), half).div(rect.z.mul(descriptor.atlasResolution)),
|
|
1522
|
+
dot4(abs3(up), half).div(rect.w.mul(descriptor.atlasResolution))
|
|
1523
|
+
).div(dot4(abs3(n), half)).mul(4).add(4 / 65535);
|
|
1399
1524
|
If4(
|
|
1400
1525
|
residual.lessThanEqual(tolerance).and(occupied).and(
|
|
1401
1526
|
capturedBase.a.greaterThanEqual(minimumAlpha)
|
|
@@ -1410,8 +1535,8 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1410
1535
|
};
|
|
1411
1536
|
if (preferFacingCapture) {
|
|
1412
1537
|
const texel2 = max4(
|
|
1413
|
-
|
|
1414
|
-
|
|
1538
|
+
dot4(abs3(right), half).div(rect.z),
|
|
1539
|
+
dot4(abs3(up), half).div(rect.w)
|
|
1415
1540
|
).div(descriptor.atlasResolution);
|
|
1416
1541
|
const uncertainty = min2(
|
|
1417
1542
|
texel2,
|
|
@@ -1428,7 +1553,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1428
1553
|
);
|
|
1429
1554
|
};
|
|
1430
1555
|
const traceInterval = (start, end, steps) => {
|
|
1431
|
-
const footprint = vec24(
|
|
1556
|
+
const footprint = vec24(dot4(r, right), dot4(r, up)).abs().mul(rect.zw).mul(descriptor.atlasResolution / 2).mul(end.sub(start));
|
|
1432
1557
|
let density = steps / 8;
|
|
1433
1558
|
if (descriptor.alphaMode === "blend" && transparency === "blended" && steps < 32) {
|
|
1434
1559
|
density = i.equal(0).select(steps / 16, steps / 32);
|
|
@@ -1437,10 +1562,10 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1437
1562
|
}
|
|
1438
1563
|
const samples = max4(float4(steps), max4(footprint.x, footprint.y).mul(density)).div(2).ceil().mul(2).toVar();
|
|
1439
1564
|
const step = end.sub(start).div(samples).toVar();
|
|
1440
|
-
const pairs =
|
|
1565
|
+
const pairs = int5(samples.div(2));
|
|
1441
1566
|
const pairAt = (t) => t.sub(start).div(max4(step.mul(2), 1e-15));
|
|
1442
|
-
const firstPair = bounds ?
|
|
1443
|
-
const lastPair = bounds ?
|
|
1567
|
+
const firstPair = bounds ? int5(floor2(pairAt(bounds.start))).clamp(0, pairs).toVar() : int5(0);
|
|
1568
|
+
const lastPair = bounds ? int5(pairAt(bounds.end).ceil()).clamp(0, pairs).toVar() : pairs;
|
|
1444
1569
|
const previous = start.toVar();
|
|
1445
1570
|
if (bounds) If4(firstPair.greaterThan(0), () => {
|
|
1446
1571
|
previous.assign(start.add(step.mul(float4(firstPair.mul(2)))));
|
|
@@ -1498,8 +1623,8 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1498
1623
|
)
|
|
1499
1624
|
);
|
|
1500
1625
|
const interval = positive.select(depths.xy, depths.zw);
|
|
1501
|
-
const initialDepth = float4(1).sub(
|
|
1502
|
-
const depthSlope =
|
|
1626
|
+
const initialDepth = float4(1).sub(dot4(o, n)).mul(0.5);
|
|
1627
|
+
const depthSlope = dot4(r, n).negate().mul(0.5);
|
|
1503
1628
|
const da = interval.x.sub(2 / 65535).sub(initialDepth).div(depthSlope);
|
|
1504
1629
|
const db = interval.y.add(2 / 65535).sub(initialDepth).div(depthSlope);
|
|
1505
1630
|
const start = max4(occupiedStart, min2(da, db));
|
|
@@ -1553,7 +1678,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1553
1678
|
const sampled = texture3(maps.normalDepth, hit.get("uv")).level(0);
|
|
1554
1679
|
const local = unpackNormal(sampled.rg.div(coverage));
|
|
1555
1680
|
const direction = fromBox(right.mul(local.x).add(up.mul(local.y)).add(n.mul(local.z))).normalize().toVar();
|
|
1556
|
-
return direction.mul(
|
|
1681
|
+
return direction.mul(dot4(direction, ray).greaterThan(0).select(-1, 1));
|
|
1557
1682
|
})();
|
|
1558
1683
|
let color = base.div(coverage), surfaceOrm = orm.rgb.div(coverage), emission = texture3(maps.emissive, hit.get("uv")).level(0).rgb.div(coverage), normal = hitNormal;
|
|
1559
1684
|
if (settings.interpolation !== "none") {
|
|
@@ -1567,37 +1692,37 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1567
1692
|
const look = toBox(
|
|
1568
1693
|
perspective.select(normalize2(eye.sub(hit.get("position"))), viewAxis)
|
|
1569
1694
|
);
|
|
1570
|
-
const colors =
|
|
1695
|
+
const colors = vec46(0).toVar(), materials = vec35(0).toVar(), emissions = vec35(0).toVar(), normals = vec35(0).toVar(), weights = float4(0).toVar();
|
|
1571
1696
|
for (let axis = 0; axis < 3; axis++) {
|
|
1572
1697
|
const positive = look.element(axis).greaterThanEqual(0);
|
|
1573
1698
|
const front = BOX_FACE_AXES[axis * 2], back = BOX_FACE_AXES[axis * 2 + 1];
|
|
1574
|
-
const n = positive.select(
|
|
1699
|
+
const n = positive.select(vec35(...front.normal), vec35(...back.normal));
|
|
1575
1700
|
const right = positive.select(
|
|
1576
|
-
|
|
1577
|
-
|
|
1701
|
+
vec35(...front.right),
|
|
1702
|
+
vec35(...back.right)
|
|
1578
1703
|
);
|
|
1579
|
-
const up = positive.select(
|
|
1580
|
-
const face = positive.select(
|
|
1581
|
-
const rect =
|
|
1704
|
+
const up = positive.select(vec35(...front.up), vec35(...back.up));
|
|
1705
|
+
const face = positive.select(int5(axis * 2), int5(axis * 2 + 1));
|
|
1706
|
+
const rect = textureLoad5(
|
|
1582
1707
|
rects,
|
|
1583
|
-
|
|
1708
|
+
ivec25(face, int5(attribute3("boxId", "float")))
|
|
1584
1709
|
);
|
|
1585
1710
|
const uv2 = rect.xy.add(
|
|
1586
|
-
vec24(
|
|
1711
|
+
vec24(dot4(point, right), dot4(point, up)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
|
|
1587
1712
|
);
|
|
1588
|
-
const texel =
|
|
1589
|
-
const captured =
|
|
1713
|
+
const texel = ivec25(uv2.mul(descriptor.atlasResolution));
|
|
1714
|
+
const captured = textureLoad5(maps.normalDepth, texel);
|
|
1590
1715
|
const sampled = texture3(maps.baseColor, uv2).level(0);
|
|
1591
1716
|
const sampledOrm = texture3(maps.orm, uv2).level(0);
|
|
1592
|
-
const occupied =
|
|
1717
|
+
const occupied = textureLoad5(maps.orm, texel).a.greaterThan(0.5);
|
|
1593
1718
|
const depth = captured.b.mul(65280).add(captured.a.mul(255)).div(65535);
|
|
1594
1719
|
const tolerance = max4(
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
).div(
|
|
1720
|
+
dot4(abs3(right), half).div(rect.z.mul(descriptor.atlasResolution)),
|
|
1721
|
+
dot4(abs3(up), half).div(rect.w.mul(descriptor.atlasResolution))
|
|
1722
|
+
).div(dot4(abs3(n), half)).mul(4).add(4 / 65535);
|
|
1598
1723
|
If4(
|
|
1599
1724
|
occupied.and(sampled.a.greaterThanEqual(minimumAlpha)).and(
|
|
1600
|
-
abs3(
|
|
1725
|
+
abs3(dot4(point, n).sub(float4(1).sub(depth.mul(2)))).lessThanEqual(
|
|
1601
1726
|
tolerance
|
|
1602
1727
|
)
|
|
1603
1728
|
),
|
|
@@ -1606,7 +1731,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1606
1731
|
const filtered = filtering ? texture3(maps.normalDepth, uv2).level(0).toVar() : captured;
|
|
1607
1732
|
let weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
|
|
1608
1733
|
if (filtering) {
|
|
1609
|
-
const error = abs3(
|
|
1734
|
+
const error = abs3(dot4(point, n).sub(float4(1).sub(
|
|
1610
1735
|
filtered.b.mul(65280).add(filtered.a.mul(255)).div(65535).mul(2)
|
|
1611
1736
|
)));
|
|
1612
1737
|
weight = weight.mul(float4(1).sub(error.div(tolerance)).clamp(0, 1));
|
|
@@ -1618,10 +1743,10 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1618
1743
|
texture3(maps.emissive, uv2).level(0).rgb.div(coverage2).mul(weight)
|
|
1619
1744
|
);
|
|
1620
1745
|
let shadingNormal;
|
|
1621
|
-
if (filtering) shadingNormal = filteredNormal(uv2, float4(1).sub(
|
|
1746
|
+
if (filtering) shadingNormal = filteredNormal(uv2, float4(1).sub(dot4(point, n)).mul(0.5), tolerance.mul(0.5), captured, sampled.a, filtered, sampledOrm.a);
|
|
1622
1747
|
else {
|
|
1623
1748
|
const packed = captured.rg.mul(2).sub(1);
|
|
1624
|
-
const decoded =
|
|
1749
|
+
const decoded = vec35(
|
|
1625
1750
|
packed,
|
|
1626
1751
|
float4(1).sub(abs3(packed.x)).sub(abs3(packed.y))
|
|
1627
1752
|
).toVar();
|
|
@@ -1654,9 +1779,10 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1654
1779
|
emission = blended.get("emission");
|
|
1655
1780
|
normal = blended.get("normal");
|
|
1656
1781
|
}
|
|
1657
|
-
|
|
1782
|
+
const position = meshPoint(hit.get("position")).toVar("boxHitMeshPosition");
|
|
1783
|
+
material.capturedPositionNode = position;
|
|
1658
1784
|
material.colorNode = color.rgb;
|
|
1659
|
-
material.normalNode = transformNormalToView3(normal);
|
|
1785
|
+
material.normalNode = transformNormalToView3(meshNormal(normal));
|
|
1660
1786
|
material.roughnessNode = surfaceOrm.g;
|
|
1661
1787
|
material.metalnessNode = surfaceOrm.b;
|
|
1662
1788
|
material.aoNode = surfaceOrm.r;
|
|
@@ -1671,9 +1797,9 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false,
|
|
|
1671
1797
|
}
|
|
1672
1798
|
material.maskNode = hit.get("valid").greaterThan(0.5);
|
|
1673
1799
|
material.maskShadowNode = material.maskNode;
|
|
1674
|
-
material.depthNode = capturedSurfaceDepth(
|
|
1800
|
+
material.depthNode = capturedSurfaceDepth(position);
|
|
1675
1801
|
material.receivedShadowPositionNode = modelWorldMatrix3.mul(
|
|
1676
|
-
|
|
1802
|
+
vec46(position, 1)
|
|
1677
1803
|
).xyz;
|
|
1678
1804
|
material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
|
|
1679
1805
|
return material;
|