@needle-tools/three-mesh-effects 0.3.6 → 0.3.7
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 +4 -0
- package/build/box-impostor-runtime.js +652 -257
- package/build/box-impostor-runtime.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-display.js +662 -265
- package/build/internal/box-impostor/box-impostor-display.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-material.js +662 -265
- package/build/internal/box-impostor/box-impostor-material.js.map +4 -4
- package/build/internal/box-impostor/box-impostor-ray-bounds.js +369 -0
- package/build/internal/box-impostor/box-impostor-ray-bounds.js.map +7 -0
- package/build/internal/box-impostor/box-impostor-runtime.js +652 -257
- package/build/internal/box-impostor/box-impostor-runtime.js.map +4 -4
- package/build/types/box-impostor/box-impostor-material.d.ts +2 -1
- package/build/types/box-impostor/box-impostor-ray-bounds.d.ts +6 -0
- package/build/types/box-impostor/box-impostor-ray-bounds.js +1 -0
- package/package.json +1 -1
- package/src/box-impostor/box-impostor-material.js +128 -90
- package/src/box-impostor/box-impostor-ray-bounds.js +452 -0
|
@@ -258,7 +258,7 @@ function createBoxImpostorGeometry(input) {
|
|
|
258
258
|
(sum, name) => sum + geometry.getAttribute(name).itemSize,
|
|
259
259
|
0
|
|
260
260
|
);
|
|
261
|
-
const data = new Float32Array(centers.length / 3 * stride),
|
|
261
|
+
const data = new Float32Array(centers.length / 3 * stride), buffer2 = new InterleavedBuffer(data, stride);
|
|
262
262
|
let offset = 0;
|
|
263
263
|
for (const name of names) {
|
|
264
264
|
const attribute4 = geometry.getAttribute(name);
|
|
@@ -270,7 +270,7 @@ function createBoxImpostorGeometry(input) {
|
|
|
270
270
|
);
|
|
271
271
|
geometry.setAttribute(
|
|
272
272
|
name,
|
|
273
|
-
new InterleavedBufferAttribute(
|
|
273
|
+
new InterleavedBufferAttribute(buffer2, attribute4.itemSize, offset)
|
|
274
274
|
);
|
|
275
275
|
offset += attribute4.itemSize;
|
|
276
276
|
}
|
|
@@ -328,9 +328,9 @@ import {
|
|
|
328
328
|
FrontSide
|
|
329
329
|
} from "three/webgpu";
|
|
330
330
|
import {
|
|
331
|
-
Fn as
|
|
332
|
-
If as
|
|
333
|
-
Loop,
|
|
331
|
+
Fn as Fn5,
|
|
332
|
+
If as If4,
|
|
333
|
+
Loop as Loop2,
|
|
334
334
|
Break,
|
|
335
335
|
attribute as attribute3,
|
|
336
336
|
varying,
|
|
@@ -340,22 +340,22 @@ import {
|
|
|
340
340
|
cameraPosition,
|
|
341
341
|
cameraWorldMatrix,
|
|
342
342
|
cameraProjectionMatrix as cameraProjectionMatrix2,
|
|
343
|
-
vec2 as
|
|
344
|
-
vec3 as
|
|
345
|
-
vec4 as
|
|
346
|
-
float as
|
|
347
|
-
int as
|
|
348
|
-
uint,
|
|
343
|
+
vec2 as vec24,
|
|
344
|
+
vec3 as vec34,
|
|
345
|
+
vec4 as vec45,
|
|
346
|
+
float as float4,
|
|
347
|
+
int as int4,
|
|
348
|
+
uint as uint2,
|
|
349
349
|
dot as dot3,
|
|
350
350
|
abs as abs3,
|
|
351
|
-
max as
|
|
352
|
-
min,
|
|
351
|
+
max as max4,
|
|
352
|
+
min as min2,
|
|
353
353
|
sign as sign3,
|
|
354
354
|
texture as texture3,
|
|
355
|
-
textureLoad as
|
|
356
|
-
ivec2 as
|
|
355
|
+
textureLoad as textureLoad4,
|
|
356
|
+
ivec2 as ivec24,
|
|
357
357
|
normalize as normalize2,
|
|
358
|
-
floor,
|
|
358
|
+
floor as floor2,
|
|
359
359
|
fract,
|
|
360
360
|
modelWorldMatrix as modelWorldMatrix3,
|
|
361
361
|
cameraNear as cameraNear2,
|
|
@@ -387,12 +387,364 @@ var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
|
|
|
387
387
|
}
|
|
388
388
|
};
|
|
389
389
|
|
|
390
|
+
// src/box-impostor/box-impostor-ray-bounds.js
|
|
391
|
+
import { Matrix4, NodeUpdateType, StorageBufferAttribute, Vector3 } from "three/webgpu";
|
|
392
|
+
import {
|
|
393
|
+
Fn as Fn2,
|
|
394
|
+
If,
|
|
395
|
+
Loop,
|
|
396
|
+
atomicMin,
|
|
397
|
+
atomicStore,
|
|
398
|
+
clamp,
|
|
399
|
+
float,
|
|
400
|
+
floor,
|
|
401
|
+
instanceIndex,
|
|
402
|
+
int,
|
|
403
|
+
ivec2,
|
|
404
|
+
max,
|
|
405
|
+
min,
|
|
406
|
+
storage,
|
|
407
|
+
textureLoad,
|
|
408
|
+
uint,
|
|
409
|
+
vec2,
|
|
410
|
+
vec3,
|
|
411
|
+
vec4 as vec43
|
|
412
|
+
} from "three/tsl";
|
|
413
|
+
var MIN_TILE_SIZE = 3;
|
|
414
|
+
var MAX_CAPTURE_TILES = 512 * 1024;
|
|
415
|
+
var REFRESH_ANGLE_SQUARED = Math.tan(Math.PI / 360) ** 2;
|
|
416
|
+
var DEPTH_MAX = 65535;
|
|
417
|
+
var MAX_VIEW_BYTES = 16 * 1024 * 1024;
|
|
418
|
+
var MAX_CACHED_VIEW_BYTES = 64 * 1024 * 1024;
|
|
419
|
+
function buffer(length, components = 1, Type = Float32Array) {
|
|
420
|
+
return new StorageBufferAttribute(length, components, Type);
|
|
421
|
+
}
|
|
422
|
+
function captureTiles(descriptor) {
|
|
423
|
+
const faces = descriptor.boxes.flatMap(
|
|
424
|
+
(box, id) => BOX_FACE_AXES.map((axes, face) => {
|
|
425
|
+
const [, , width, height] = box.views[face];
|
|
426
|
+
const w = width - 2 * descriptor.padding, h = height - 2 * descriptor.padding;
|
|
427
|
+
const right = axes.right.findIndex(Boolean), up = axes.up.findIndex(Boolean);
|
|
428
|
+
const tolerance = 4 * Math.max(box.halfSize[right] / w, box.halfSize[up] / h) / box.halfSize[face >> 1] + 5 / DEPTH_MAX;
|
|
429
|
+
return { key: id * 6 + face, w, h, tolerance };
|
|
430
|
+
})
|
|
431
|
+
);
|
|
432
|
+
const countFor = (size2) => faces.reduce((n, f) => n + Math.ceil((f.w + 2) / size2) * Math.ceil((f.h + 2) / size2), 0);
|
|
433
|
+
let size = MIN_TILE_SIZE;
|
|
434
|
+
while (countFor(size) > MAX_CAPTURE_TILES) size *= 2;
|
|
435
|
+
const tiles = buffer(countFor(size), 4);
|
|
436
|
+
let offset = 0;
|
|
437
|
+
for (const { key, w, h, tolerance } of faces) {
|
|
438
|
+
for (let y = -1; y <= h; y += size) {
|
|
439
|
+
for (let x = -1; x <= w; x += size) {
|
|
440
|
+
tiles.array[offset] = key;
|
|
441
|
+
tiles.array[offset + 1] = x;
|
|
442
|
+
tiles.array[offset + 2] = y;
|
|
443
|
+
tiles.array[offset + 3] = tolerance;
|
|
444
|
+
offset += 4;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return { tiles, size };
|
|
449
|
+
}
|
|
450
|
+
function buildPatches(descriptor, maps, rects, tiles, size, output) {
|
|
451
|
+
const descriptions = storage(tiles, "vec4", tiles.count).toReadOnly();
|
|
452
|
+
const patches = storage(output, "vec4", output.count);
|
|
453
|
+
return Fn2(() => {
|
|
454
|
+
const meta = descriptions.element(instanceIndex).toVar();
|
|
455
|
+
const key = int(meta.x), face = key.mod(6), axis = face.div(2);
|
|
456
|
+
const rect = textureLoad(rects, ivec2(face, key.div(6))).mul(descriptor.atlasResolution).round().toVar();
|
|
457
|
+
const extent = ivec2(rect.zw.add(1).sub(meta.yz)).min(ivec2(size));
|
|
458
|
+
const origin = ivec2(rect.xy.add(meta.yz));
|
|
459
|
+
const near = float(1).toVar(), far = float(0).toVar();
|
|
460
|
+
function visit(process) {
|
|
461
|
+
Loop({ start: int(0), end: extent.y, type: "int", name: "row" }, ({ row }) => {
|
|
462
|
+
Loop({ start: int(0), end: extent.x, type: "int", name: "column" }, ({ column }) => {
|
|
463
|
+
const pixel = origin.add(ivec2(column, row));
|
|
464
|
+
If(textureLoad(maps.orm, pixel).a.greaterThan(0.5), () => {
|
|
465
|
+
const packed = textureLoad(maps.normalDepth, pixel);
|
|
466
|
+
const depth = packed.b.mul(65280).add(packed.a.mul(255)).div(DEPTH_MAX).toVar();
|
|
467
|
+
process(column, row, depth);
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
visit((x, y, depth) => {
|
|
473
|
+
near.assign(min(near, depth));
|
|
474
|
+
far.assign(max(far, depth));
|
|
475
|
+
});
|
|
476
|
+
const middle = near.add(far).mul(0.5).toVar();
|
|
477
|
+
const low = [vec3(2).toVar(), vec3(2).toVar()], high = [vec3(-2).toVar(), vec3(-2).toVar()];
|
|
478
|
+
visit((x, y, depth) => {
|
|
479
|
+
const uv2 = meta.yz.add(vec2(x, y)).div(rect.zw).mul(2).sub(1);
|
|
480
|
+
const lo = vec3(uv2.clamp(-1, 1), float(1).sub(depth.mul(2)).sub(meta.w).max(-1)).toVar();
|
|
481
|
+
const hi = vec3(
|
|
482
|
+
uv2.add(vec2(2).div(rect.zw)).clamp(-1, 1),
|
|
483
|
+
float(1).sub(depth.mul(2)).add(meta.w).min(1)
|
|
484
|
+
).toVar();
|
|
485
|
+
lo.toStack();
|
|
486
|
+
hi.toStack();
|
|
487
|
+
const extend = (group) => {
|
|
488
|
+
low[group].assign(min(low[group], lo));
|
|
489
|
+
high[group].assign(max(high[group], hi));
|
|
490
|
+
};
|
|
491
|
+
If(depth.lessThanEqual(middle), () => {
|
|
492
|
+
extend(0);
|
|
493
|
+
}).Else(() => {
|
|
494
|
+
extend(1);
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
const facing = face.mod(2).equal(0).select(1, -1);
|
|
498
|
+
const right = axis.equal(0).select(vec3(0, 0, facing.negate()), axis.equal(1).select(vec3(1, 0, 0), vec3(facing, 0, 0)));
|
|
499
|
+
const up = axis.equal(1).select(vec3(0, 0, facing.negate()), vec3(0, 1, 0));
|
|
500
|
+
const normal = right.cross(up);
|
|
501
|
+
const rotate = (p) => right.mul(p.x).add(up.mul(p.y)).add(normal.mul(p.z));
|
|
502
|
+
for (let group = 0; group < 2; group++) {
|
|
503
|
+
const a = rotate(low[group]), b = rotate(high[group]), slot = instanceIndex.mul(4).add(group * 2);
|
|
504
|
+
const valid = low[group].x.lessThanEqual(high[group].x);
|
|
505
|
+
patches.element(slot).assign(vec43(min(a, b), valid.select(meta.x, -1)));
|
|
506
|
+
patches.element(slot.add(1)).assign(vec43(max(a, b), 0));
|
|
507
|
+
}
|
|
508
|
+
})().compute(tiles.count);
|
|
509
|
+
}
|
|
510
|
+
function makeViewPasses(patchBuffer, views, ranges, count, resolution) {
|
|
511
|
+
const table = storage(ranges, "uint", count).toAtomic();
|
|
512
|
+
const reset = Fn2(() => {
|
|
513
|
+
atomicStore(table.element(instanceIndex), uint(DEPTH_MAX));
|
|
514
|
+
})().compute(count);
|
|
515
|
+
const patches = storage(patchBuffer, "vec4", patchBuffer.count).toReadOnly();
|
|
516
|
+
const scatter = Fn2(() => {
|
|
517
|
+
const a = patches.element(instanceIndex.mul(2)).toVar(), b = patches.element(instanceIndex.mul(2).add(1)).toVar();
|
|
518
|
+
If(a.w.greaterThanEqual(0), () => {
|
|
519
|
+
const key = int(a.w), box = key.div(6);
|
|
520
|
+
const mapping = views.element(box.mul(2)), reference = views.element(box.mul(2).add(1));
|
|
521
|
+
If(reference.w.greaterThanEqual(0), () => {
|
|
522
|
+
const axis = int(reference.w).mod(3), x = axis.add(1).mod(3), y = axis.add(2).mod(3);
|
|
523
|
+
const e = vec2(reference.element(x), reference.element(y)), ez = reference.element(axis);
|
|
524
|
+
const za = a.element(axis), zb = b.element(axis);
|
|
525
|
+
const weight = reference.w.lessThan(3).select(1, 0).toVar();
|
|
526
|
+
const eyeSlope = e.div(ez).toVar(), inverseDistance = weight.div(ez).toVar();
|
|
527
|
+
const project = (v, z) => v.sub(eyeSlope.mul(z)).div(float(1).sub(inverseDistance.mul(z)));
|
|
528
|
+
const l = vec2(a.element(x), a.element(y)), h = vec2(b.element(x), b.element(y));
|
|
529
|
+
const cell = (v, bias) => ivec2(floor(v.sub(mapping.xy).mul(mapping.zw).add(bias)).clamp(0, resolution - 1));
|
|
530
|
+
const lo = cell(min(project(l, za), project(l, zb)), -5e-3);
|
|
531
|
+
const hi = cell(max(project(h, za), project(h, zb)), 5e-3);
|
|
532
|
+
const padding = vec2(0.506).div(mapping.zw).mul(inverseDistance.abs().add(1)).add(2e-6);
|
|
533
|
+
Loop({ start: lo.y, end: hi.y.add(1), type: "int", name: "row" }, ({ row }) => {
|
|
534
|
+
Loop({ start: lo.x, end: hi.x.add(1), type: "int", name: "column" }, ({ column }) => {
|
|
535
|
+
const uv2 = vec2(column, row).add(0.5).div(mapping.zw).add(mapping.xy);
|
|
536
|
+
const slope = eyeSlope.sub(uv2.mul(inverseDistance)).toVar();
|
|
537
|
+
const safe = slope.add(1e-30).sign().mul(slope.abs().max(1e-12));
|
|
538
|
+
const ta = l.sub(padding).sub(uv2).div(safe).toVar(), tb = h.add(padding).sub(uv2).div(safe).toVar();
|
|
539
|
+
const begin = max(za, max(min(ta.x, tb.x), min(ta.y, tb.y))), end = min(zb, min(max(ta.x, tb.x), max(ta.y, tb.y)));
|
|
540
|
+
If(begin.lessThanEqual(end), () => {
|
|
541
|
+
const depths = floor(
|
|
542
|
+
vec2(begin.add(1), float(1).sub(end)).mul(DEPTH_MAX / 2).sub(0.02)
|
|
543
|
+
).max(0);
|
|
544
|
+
const i = key.mul(resolution).add(row).mul(resolution).add(column).mul(2);
|
|
545
|
+
atomicMin(table.element(i), uint(depths.x));
|
|
546
|
+
atomicMin(table.element(i.add(1)), uint(depths.y));
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
});
|
|
551
|
+
});
|
|
552
|
+
})().compute(patchBuffer.count / 2);
|
|
553
|
+
return { reset, scatter, table };
|
|
554
|
+
}
|
|
555
|
+
function setProjection(data, descriptor, reference, orthographic, resolution) {
|
|
556
|
+
for (const [id, box] of descriptor.boxes.entries()) {
|
|
557
|
+
const p = box.axes.map(
|
|
558
|
+
(axis2, i) => axis2.reduce(
|
|
559
|
+
(sum, v, k) => sum + v * (orthographic ? reference[k] : reference[k] - box.center[k]),
|
|
560
|
+
0
|
|
561
|
+
) / box.halfSize[i]
|
|
562
|
+
);
|
|
563
|
+
const axis = p.map(Math.abs).indexOf(Math.max(...p.map(Math.abs)));
|
|
564
|
+
const ez = p[axis], offset = id * 8;
|
|
565
|
+
if (orthographic ? Math.abs(ez) < 1e-12 : Math.abs(ez) <= 1.01) {
|
|
566
|
+
data[offset + 7] = -1;
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
for (let j = 0; j < 2; j++) {
|
|
570
|
+
const e = p[(axis + 1 + j) % 3];
|
|
571
|
+
const project = (v, z) => orthographic ? v - z * e / ez : (v * ez - z * e) / (ez - z);
|
|
572
|
+
const low = Math.min(project(-1, -1), project(-1, 1));
|
|
573
|
+
const high = Math.max(project(1, -1), project(1, 1));
|
|
574
|
+
data[offset + j] = low;
|
|
575
|
+
data[offset + j + 2] = resolution / (high - low);
|
|
576
|
+
}
|
|
577
|
+
data.set(p, offset + 4);
|
|
578
|
+
data[offset + 7] = axis + (orthographic ? 3 : 0);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
function createBoxRayBounds(descriptor, maps, rects, material) {
|
|
582
|
+
const boxes = descriptor.boxes.length;
|
|
583
|
+
const resolution = Math.min(128, Math.floor(Math.sqrt(MAX_VIEW_BYTES / (48 * boxes))));
|
|
584
|
+
if (resolution < 32 || descriptor.atlasResolution < 512 || [maps.normalDepth, maps.orm].some((map) => map.isRenderTargetTexture || map.isVideoTexture))
|
|
585
|
+
return null;
|
|
586
|
+
const count = boxes * 6 * resolution ** 2 * 2;
|
|
587
|
+
const maxViews = Math.max(1, Math.floor(MAX_CACHED_VIEW_BYTES / (count * 4 + boxes * 32)));
|
|
588
|
+
const inactiveView = buffer(boxes * 2, 4);
|
|
589
|
+
for (let i = 0; i < boxes; i++) inactiveView.array.set([0, 0, 1, 1, 2, 0, 0, -1], i * 8);
|
|
590
|
+
const inactiveRanges = buffer(1, 1, Uint32Array);
|
|
591
|
+
const views = storage(inactiveView, "vec4", boxes * 2).toReadOnly();
|
|
592
|
+
const ranges = storage(inactiveRanges, "uint").toReadOnly();
|
|
593
|
+
const inverse = new Matrix4(), eye = new Vector3(), center = new Vector3();
|
|
594
|
+
const states = /* @__PURE__ */ new Map();
|
|
595
|
+
let tiles, patches, reduce, passes;
|
|
596
|
+
function prepare({ renderer, camera, object }) {
|
|
597
|
+
views.value = inactiveView;
|
|
598
|
+
ranges.value = inactiveRanges;
|
|
599
|
+
if (!renderer.backend?.isWebGPUBackend || renderer.xr?.isPresenting || object.isInstancedMesh || !camera.isPerspectiveCamera && !camera.isOrthographicCamera || camera.isArrayCamera)
|
|
600
|
+
return;
|
|
601
|
+
inverse.copy(object.matrixWorld).invert();
|
|
602
|
+
const ortho = camera.isOrthographicCamera === true;
|
|
603
|
+
if (ortho) camera.getWorldDirection(eye).transformDirection(inverse);
|
|
604
|
+
else eye.setFromMatrixPosition(camera.matrixWorld).applyMatrix4(inverse);
|
|
605
|
+
if (!Number.isFinite(eye.lengthSq())) return;
|
|
606
|
+
let state = states.get(renderer);
|
|
607
|
+
if (!state) {
|
|
608
|
+
state = { slots: [], initialized: false };
|
|
609
|
+
states.set(renderer, state);
|
|
610
|
+
}
|
|
611
|
+
if (!tiles) {
|
|
612
|
+
const capture = captureTiles(descriptor);
|
|
613
|
+
tiles = capture.tiles;
|
|
614
|
+
patches = buffer(tiles.count * 4, 4);
|
|
615
|
+
reduce = buildPatches(descriptor, maps, rects, tiles, capture.size, patches);
|
|
616
|
+
const computeViews = storage(inactiveView, "vec4", boxes * 2).toReadOnly();
|
|
617
|
+
passes = {
|
|
618
|
+
...makeViewPasses(patches, computeViews, inactiveRanges, count, resolution),
|
|
619
|
+
views: computeViews
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
if (!state.initialized || state.depthVersion !== maps.normalDepth.version || state.coverageVersion !== maps.orm.version) {
|
|
623
|
+
renderer.compute(reduce);
|
|
624
|
+
for (const slot2 of state.slots) slot2.ready = false;
|
|
625
|
+
state.depthVersion = maps.normalDepth.version;
|
|
626
|
+
state.coverageVersion = maps.orm.version;
|
|
627
|
+
state.initialized = true;
|
|
628
|
+
}
|
|
629
|
+
const frame = renderer.info.render.calls;
|
|
630
|
+
const compatible = (v) => v.ready && v.ortho === ortho && v.eye.distanceToSquared(eye) <= v.radiusSq;
|
|
631
|
+
let slot = state.slots.find((v) => v.object.deref() === object && v.camera.deref() === camera);
|
|
632
|
+
if (!slot || !compatible(slot)) {
|
|
633
|
+
const shared = state.slots.find(compatible);
|
|
634
|
+
if (shared) {
|
|
635
|
+
shared.frame = frame;
|
|
636
|
+
views.value = shared.view;
|
|
637
|
+
ranges.value = shared.ranges;
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
if (slot?.frame === frame) slot = void 0;
|
|
641
|
+
}
|
|
642
|
+
if (!slot) {
|
|
643
|
+
if (state.slots.length < maxViews) {
|
|
644
|
+
slot = { view: buffer(boxes * 2, 4), ranges: buffer(count, 1, Uint32Array), eye: new Vector3() };
|
|
645
|
+
state.slots.push(slot);
|
|
646
|
+
} else {
|
|
647
|
+
slot = state.slots.reduce((oldest, v) => v.frame < oldest.frame ? v : oldest);
|
|
648
|
+
if (slot.frame === frame) {
|
|
649
|
+
let nearest = Infinity;
|
|
650
|
+
for (const v of state.slots) {
|
|
651
|
+
if (!v.ready || v.ortho !== ortho) continue;
|
|
652
|
+
const distance = v.eye.distanceToSquared(eye);
|
|
653
|
+
if (distance < nearest) {
|
|
654
|
+
nearest = distance;
|
|
655
|
+
views.value = v.view;
|
|
656
|
+
ranges.value = v.ranges;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
slot.object = new WeakRef(object);
|
|
663
|
+
slot.camera = new WeakRef(camera);
|
|
664
|
+
slot.ready = false;
|
|
665
|
+
}
|
|
666
|
+
slot.frame = frame;
|
|
667
|
+
views.value = slot.view;
|
|
668
|
+
ranges.value = slot.ranges;
|
|
669
|
+
if (compatible(slot)) return;
|
|
670
|
+
setProjection(slot.view.array, descriptor, eye.toArray(), ortho, resolution);
|
|
671
|
+
slot.view.needsUpdate = true;
|
|
672
|
+
passes.views.value = slot.view;
|
|
673
|
+
passes.table.value = slot.ranges;
|
|
674
|
+
renderer.compute([passes.reset, passes.scatter]);
|
|
675
|
+
const distanceSquared = ortho ? 1 : Math.min(...descriptor.boxes.map((box) => eye.distanceToSquared(center.fromArray(box.center))));
|
|
676
|
+
slot.radiusSq = distanceSquared * REFRESH_ANGLE_SQUARED;
|
|
677
|
+
slot.eye.copy(eye);
|
|
678
|
+
slot.ortho = ortho;
|
|
679
|
+
slot.ready = true;
|
|
680
|
+
}
|
|
681
|
+
views.updateBeforeType = NodeUpdateType.OBJECT;
|
|
682
|
+
views.updateBefore = prepare;
|
|
683
|
+
material.addEventListener("dispose", () => {
|
|
684
|
+
for (const state of states.values()) {
|
|
685
|
+
for (const slot of state.slots) {
|
|
686
|
+
slot.view.dispose();
|
|
687
|
+
slot.ranges.dispose();
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
states.clear();
|
|
691
|
+
reduce?.dispose();
|
|
692
|
+
passes?.reset.dispose();
|
|
693
|
+
passes?.scatter.dispose();
|
|
694
|
+
tiles?.dispose();
|
|
695
|
+
patches?.dispose();
|
|
696
|
+
inactiveView.dispose();
|
|
697
|
+
inactiveRanges.dispose();
|
|
698
|
+
});
|
|
699
|
+
return (builder, id, o, r, enter, exit) => {
|
|
700
|
+
if (!builder.renderer.backend?.isWebGPUBackend) return null;
|
|
701
|
+
const mapping = views.element(id.mul(2)).toVar();
|
|
702
|
+
const ref = views.element(id.mul(2).add(1)).toVar();
|
|
703
|
+
const axis = int(max(ref.w, 0)).mod(3), x = axis.add(1).mod(3), y = axis.add(2).mod(3);
|
|
704
|
+
const rz = r.element(axis).toVar(), oz = o.element(axis).toVar();
|
|
705
|
+
const ez = ref.element(axis), e = vec2(ref.element(x), ref.element(y));
|
|
706
|
+
const eyeSlope = e.div(ez), inverseDistance = ref.w.lessThan(3).select(1, 0).div(ez);
|
|
707
|
+
const project = (t) => {
|
|
708
|
+
const point = o.add(r.mul(t)), v = vec2(point.element(x), point.element(y)), z = point.element(axis);
|
|
709
|
+
const p = v.sub(eyeSlope.mul(z)).div(float(1).sub(inverseDistance.mul(z)));
|
|
710
|
+
return p.sub(mapping.xy).mul(mapping.zw);
|
|
711
|
+
};
|
|
712
|
+
const a = project(enter).toVar(), b = project(exit).toVar();
|
|
713
|
+
const cellLo = ivec2(floor(min(a, b).sub(1e-3)).clamp(0, resolution - 1)).toVar();
|
|
714
|
+
const cellHi = ivec2(floor(max(a, b).add(1e-3)).clamp(0, resolution - 1)).toVar();
|
|
715
|
+
const valid = ref.w.greaterThanEqual(0).and(rz.abs().greaterThan(1e-12)).and(cellHi.sub(cellLo).lessThanEqual(ivec2(2)).all()).toVar();
|
|
716
|
+
return (face, enter2, exit2) => {
|
|
717
|
+
const start = enter2.toVar(), end = exit2.toVar();
|
|
718
|
+
If(valid, () => {
|
|
719
|
+
const base = id.mul(6).add(face).mul(resolution ** 2);
|
|
720
|
+
const at = (u, v) => base.add(v.mul(resolution)).add(u).mul(2);
|
|
721
|
+
const idx = at(cellLo.x, cellLo.y).toVar();
|
|
722
|
+
const lower = ranges.element(idx).toVar(), upper = ranges.element(idx.add(1)).toVar();
|
|
723
|
+
If(cellHi.notEqual(cellLo).any(), () => {
|
|
724
|
+
Loop({ start: cellLo.y, end: cellHi.y.add(1), type: "int", name: "by" }, ({ by }) => {
|
|
725
|
+
Loop({ start: cellLo.x, end: cellHi.x.add(1), type: "int", name: "bx" }, ({ bx }) => {
|
|
726
|
+
const p = at(bx, by);
|
|
727
|
+
lower.assign(min(lower, ranges.element(p)));
|
|
728
|
+
upper.assign(min(upper, ranges.element(p.add(1))));
|
|
729
|
+
});
|
|
730
|
+
});
|
|
731
|
+
});
|
|
732
|
+
const lo = float(lower).mul(2 / DEPTH_MAX).sub(1), hi = float(1).sub(float(upper).mul(2 / DEPTH_MAX));
|
|
733
|
+
const a2 = lo.sub(oz).div(rz), b2 = hi.sub(oz).div(rz);
|
|
734
|
+
start.assign(min(a2, b2));
|
|
735
|
+
end.assign(lo.lessThanEqual(hi).select(max(a2, b2), enter2.sub(1)));
|
|
736
|
+
});
|
|
737
|
+
return { start, end };
|
|
738
|
+
};
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
|
|
390
742
|
// src/box-impostor/box-impostor-surface-search.js
|
|
391
|
-
function refineSurfaceCrossing({ float:
|
|
743
|
+
function refineSurfaceCrossing({ float: float5, If: If5, Loop: Loop3 }, { lo, hi, loValue, hiValue, height, steps, secant }) {
|
|
392
744
|
const a = lo.toVar(), b = hi.toVar(), fa = loValue.toVar(), fb = secant ? hiValue.toVar() : null;
|
|
393
|
-
|
|
745
|
+
Loop3(steps, () => {
|
|
394
746
|
const mid = a.add(b).mul(0.5).toVar(), d = height(mid).toVar();
|
|
395
|
-
|
|
747
|
+
If5(d.mul(fa).greaterThan(0), () => {
|
|
396
748
|
a.assign(mid);
|
|
397
749
|
if (secant) fa.assign(d);
|
|
398
750
|
}).Else(() => {
|
|
@@ -406,21 +758,21 @@ function refineSurfaceCrossing({ float: float4, If: If4, Loop: Loop2 }, { lo, hi
|
|
|
406
758
|
// src/box-impostor/box-impostor-faces.js
|
|
407
759
|
import { DataTexture, FloatType, RGBAFormat, NearestFilter } from "three";
|
|
408
760
|
import {
|
|
409
|
-
Fn as
|
|
410
|
-
If,
|
|
761
|
+
Fn as Fn3,
|
|
762
|
+
If as If2,
|
|
411
763
|
attribute,
|
|
412
764
|
positionLocal,
|
|
413
765
|
uv,
|
|
414
766
|
texture,
|
|
415
|
-
textureLoad,
|
|
416
|
-
ivec2,
|
|
417
|
-
vec2,
|
|
418
|
-
vec3,
|
|
419
|
-
float,
|
|
420
|
-
int,
|
|
767
|
+
textureLoad as textureLoad2,
|
|
768
|
+
ivec2 as ivec22,
|
|
769
|
+
vec2 as vec22,
|
|
770
|
+
vec3 as vec32,
|
|
771
|
+
float as float2,
|
|
772
|
+
int as int2,
|
|
421
773
|
dot,
|
|
422
774
|
abs,
|
|
423
|
-
max,
|
|
775
|
+
max as max2,
|
|
424
776
|
sign,
|
|
425
777
|
transformNormalToView
|
|
426
778
|
} from "three/tsl";
|
|
@@ -449,11 +801,11 @@ function applyBoxFaces({
|
|
|
449
801
|
planes.minFilter = planes.magFilter = NearestFilter;
|
|
450
802
|
planes.needsUpdate = true;
|
|
451
803
|
material.addEventListener("dispose", () => planes.dispose());
|
|
452
|
-
const face =
|
|
804
|
+
const face = int2(attribute("boxFace", "float"));
|
|
453
805
|
const n = attribute("normal", "vec3");
|
|
454
|
-
const depth =
|
|
806
|
+
const depth = textureLoad2(
|
|
455
807
|
planes,
|
|
456
|
-
|
|
808
|
+
ivec22(face, int2(attribute("boxId", "float")))
|
|
457
809
|
).r;
|
|
458
810
|
const proxy = positionLocal.sub(
|
|
459
811
|
n.mul(
|
|
@@ -464,28 +816,28 @@ function applyBoxFaces({
|
|
|
464
816
|
material.castShadowPositionNode = proxy;
|
|
465
817
|
const base = texture(maps.baseColor, uv()).level(0).toVar("boxFaceColor");
|
|
466
818
|
const orm = texture(maps.orm, uv()).level(0);
|
|
467
|
-
const coverage =
|
|
468
|
-
const normal =
|
|
469
|
-
const packed =
|
|
819
|
+
const coverage = max2(orm.a, 1e-10);
|
|
820
|
+
const normal = Fn3(() => {
|
|
821
|
+
const packed = textureLoad2(
|
|
470
822
|
maps.normalDepth,
|
|
471
|
-
|
|
823
|
+
ivec22(uv().mul(descriptor.atlasResolution))
|
|
472
824
|
).rg.mul(2).sub(1);
|
|
473
|
-
const decoded =
|
|
825
|
+
const decoded = vec32(
|
|
474
826
|
packed,
|
|
475
|
-
|
|
827
|
+
float2(1).sub(abs(packed.x)).sub(abs(packed.y))
|
|
476
828
|
).toVar();
|
|
477
|
-
|
|
829
|
+
If2(decoded.z.lessThan(0), () => {
|
|
478
830
|
decoded.xy.assign(
|
|
479
|
-
|
|
831
|
+
vec22(1).sub(abs(decoded.yx)).mul(sign(decoded.xy.add(1e-20)))
|
|
480
832
|
);
|
|
481
833
|
});
|
|
482
|
-
const result =
|
|
834
|
+
const result = vec32(0).toVar();
|
|
483
835
|
for (let f = 0; f < 6; f++) {
|
|
484
836
|
const a = BOX_FACE_AXES[f];
|
|
485
|
-
|
|
837
|
+
If2(face.equal(f), () => {
|
|
486
838
|
result.assign(
|
|
487
839
|
fromBox(
|
|
488
|
-
|
|
840
|
+
vec32(...a.right).mul(decoded.x).add(vec32(...a.up).mul(decoded.y)).add(vec32(...a.normal).mul(decoded.z))
|
|
489
841
|
).normalize()
|
|
490
842
|
);
|
|
491
843
|
});
|
|
@@ -499,7 +851,7 @@ function applyBoxFaces({
|
|
|
499
851
|
material.aoNode = orm.r.div(coverage);
|
|
500
852
|
material.emissiveNode = texture(maps.emissive, uv()).level(0).rgb.div(coverage);
|
|
501
853
|
material.maskNode = base.a.greaterThanEqual(
|
|
502
|
-
transparency === "cutout" ? alphaCutoff :
|
|
854
|
+
transparency === "cutout" ? alphaCutoff : float2(1 / 255)
|
|
503
855
|
);
|
|
504
856
|
material.maskShadowNode = material.maskNode;
|
|
505
857
|
if (transparency !== "cutout") material.opacityNode = base.a;
|
|
@@ -510,21 +862,21 @@ function applyBoxFaces({
|
|
|
510
862
|
// src/box-impostor/box-impostor-projection.js
|
|
511
863
|
import { DataTexture as DataTexture2, FloatType as FloatType2, RGBAFormat as RGBAFormat2, NearestFilter as NearestFilter2 } from "three";
|
|
512
864
|
import {
|
|
513
|
-
Fn as
|
|
514
|
-
If as
|
|
515
|
-
vec2 as
|
|
516
|
-
vec3 as
|
|
517
|
-
vec4 as
|
|
518
|
-
float as
|
|
519
|
-
int as
|
|
520
|
-
ivec2 as
|
|
865
|
+
Fn as Fn4,
|
|
866
|
+
If as If3,
|
|
867
|
+
vec2 as vec23,
|
|
868
|
+
vec3 as vec33,
|
|
869
|
+
vec4 as vec44,
|
|
870
|
+
float as float3,
|
|
871
|
+
int as int3,
|
|
872
|
+
ivec2 as ivec23,
|
|
521
873
|
dot as dot2,
|
|
522
874
|
abs as abs2,
|
|
523
|
-
max as
|
|
875
|
+
max as max3,
|
|
524
876
|
sign as sign2,
|
|
525
877
|
normalize,
|
|
526
878
|
texture as texture2,
|
|
527
|
-
textureLoad as
|
|
879
|
+
textureLoad as textureLoad3,
|
|
528
880
|
attribute as attribute2,
|
|
529
881
|
struct,
|
|
530
882
|
modelWorldMatrix as modelWorldMatrix2,
|
|
@@ -574,24 +926,24 @@ function applyBoxProjection({
|
|
|
574
926
|
"BoxProjectedSurface"
|
|
575
927
|
);
|
|
576
928
|
const readNormal = (uv2, right, up, n) => {
|
|
577
|
-
const packed =
|
|
929
|
+
const packed = textureLoad3(
|
|
578
930
|
maps.normalDepth,
|
|
579
|
-
|
|
931
|
+
ivec23(uv2.mul(descriptor.atlasResolution))
|
|
580
932
|
).rg.mul(2).sub(1);
|
|
581
|
-
const decoded =
|
|
933
|
+
const decoded = vec33(
|
|
582
934
|
packed,
|
|
583
|
-
|
|
935
|
+
float3(1).sub(abs2(packed.x)).sub(abs2(packed.y))
|
|
584
936
|
).toVar();
|
|
585
|
-
|
|
937
|
+
If3(decoded.z.lessThan(0), () => {
|
|
586
938
|
decoded.xy.assign(
|
|
587
|
-
|
|
939
|
+
vec23(1).sub(abs2(decoded.yx)).mul(sign2(decoded.xy.add(1e-20)))
|
|
588
940
|
);
|
|
589
941
|
});
|
|
590
942
|
return fromBox(
|
|
591
943
|
right.mul(decoded.x).add(up.mul(decoded.y)).add(n.mul(decoded.z))
|
|
592
944
|
).normalize();
|
|
593
945
|
};
|
|
594
|
-
const surface =
|
|
946
|
+
const surface = Fn4(() => {
|
|
595
947
|
const ray = perspective.select(
|
|
596
948
|
normalize(raster.sub(eye)),
|
|
597
949
|
viewAxis.negate()
|
|
@@ -602,48 +954,48 @@ function applyBoxProjection({
|
|
|
602
954
|
);
|
|
603
955
|
const o = toBox(origin.sub(center)).div(half), r = toBox(ray).div(half);
|
|
604
956
|
const look = normalize(toBox(ray).negate()), angles = abs2(look);
|
|
605
|
-
const positions =
|
|
606
|
-
const selectedUv =
|
|
607
|
-
const nearest =
|
|
608
|
-
const opacity =
|
|
957
|
+
const positions = vec33(0).toVar();
|
|
958
|
+
const selectedUv = vec23(0).toVar(), selectedRight = vec33(1, 0, 0).toVar(), selectedUp = vec33(0, 1, 0).toVar(), selectedNormal = vec33(0, 0, 1).toVar();
|
|
959
|
+
const nearest = float3(1e20).toVar("boxPlaneDistance");
|
|
960
|
+
const opacity = float3(0).toVar();
|
|
609
961
|
const candidates = [];
|
|
610
962
|
for (let axis = 0; axis < 3; axis++) {
|
|
611
963
|
const angle = angles.element(axis);
|
|
612
964
|
const positive = look.element(axis).greaterThanEqual(0), a = BOX_FACE_AXES[axis * 2], b = BOX_FACE_AXES[axis * 2 + 1];
|
|
613
|
-
const n = positive.select(
|
|
965
|
+
const n = positive.select(vec33(...a.normal), vec33(...b.normal)), right = positive.select(vec33(...a.right), vec33(...b.right)), up = positive.select(vec33(...a.up), vec33(...b.up));
|
|
614
966
|
const weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
|
|
615
967
|
const candidate = settings.interpolation === "none" ? null : {
|
|
616
|
-
uv:
|
|
617
|
-
base:
|
|
618
|
-
orm:
|
|
619
|
-
t:
|
|
620
|
-
tolerance:
|
|
968
|
+
uv: vec23(0).toVar(),
|
|
969
|
+
base: vec44(0).toVar(),
|
|
970
|
+
orm: vec44(0).toVar(),
|
|
971
|
+
t: float3(1e20).toVar(),
|
|
972
|
+
tolerance: float3(0).toVar(),
|
|
621
973
|
right,
|
|
622
974
|
up,
|
|
623
975
|
n,
|
|
624
976
|
weight
|
|
625
977
|
};
|
|
626
978
|
if (candidate) candidates.push(candidate);
|
|
627
|
-
|
|
628
|
-
const face = positive.select(
|
|
629
|
-
const plane =
|
|
630
|
-
|
|
979
|
+
If3(angle.greaterThan(1e-4), () => {
|
|
980
|
+
const face = positive.select(int3(axis * 2), int3(axis * 2 + 1)), id = int3(attribute2("boxId", "float"));
|
|
981
|
+
const plane = float3(1).sub(
|
|
982
|
+
textureLoad3(planes, ivec23(face, id)).r.mul(2)
|
|
631
983
|
);
|
|
632
|
-
const divisor2 = dot2(r, n), safe = sign2(divisor2.add(1e-20)).mul(
|
|
984
|
+
const divisor2 = dot2(r, n), safe = sign2(divisor2.add(1e-20)).mul(max3(abs2(divisor2), 1e-10));
|
|
633
985
|
const t = plane.sub(dot2(o, n)).div(safe), point = o.add(r.mul(t));
|
|
634
|
-
const xy =
|
|
986
|
+
const xy = vec23(dot2(point, right), dot2(point, up)).mul(0.5).add(0.5);
|
|
635
987
|
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));
|
|
636
988
|
if (!candidate) supported = supported.and(t.lessThan(nearest));
|
|
637
|
-
|
|
638
|
-
const rect =
|
|
989
|
+
If3(supported, () => {
|
|
990
|
+
const rect = textureLoad3(rects, ivec23(face, id)), uv2 = rect.xy.add(xy.mul(rect.zw));
|
|
639
991
|
const base = texture2(maps.baseColor, uv2).level(0), orm = texture2(maps.orm, uv2).level(0);
|
|
640
992
|
const alpha2 = base.a.clamp(0, 1);
|
|
641
|
-
|
|
993
|
+
If3(
|
|
642
994
|
alpha2.greaterThanEqual(
|
|
643
|
-
transparency === "cutout" ? alphaCutoff :
|
|
995
|
+
transparency === "cutout" ? alphaCutoff : float3(1 / 255)
|
|
644
996
|
).and(orm.a.greaterThan(0)),
|
|
645
997
|
() => {
|
|
646
|
-
|
|
998
|
+
If3(t.lessThan(nearest), () => {
|
|
647
999
|
nearest.assign(t);
|
|
648
1000
|
selectedUv.assign(uv2);
|
|
649
1001
|
opacity.assign(alpha2);
|
|
@@ -658,14 +1010,14 @@ function applyBoxProjection({
|
|
|
658
1010
|
candidate.orm.assign(orm);
|
|
659
1011
|
candidate.t.assign(t);
|
|
660
1012
|
candidate.tolerance.assign(
|
|
661
|
-
|
|
1013
|
+
max3(
|
|
662
1014
|
dot2(abs2(right), half).div(
|
|
663
1015
|
rect.z.mul(descriptor.atlasResolution)
|
|
664
1016
|
),
|
|
665
1017
|
dot2(abs2(up), half).div(
|
|
666
1018
|
rect.w.mul(descriptor.atlasResolution)
|
|
667
1019
|
)
|
|
668
|
-
).mul(4).div(
|
|
1020
|
+
).mul(4).div(max3(angle, 0.1))
|
|
669
1021
|
);
|
|
670
1022
|
}
|
|
671
1023
|
}
|
|
@@ -675,7 +1027,7 @@ function applyBoxProjection({
|
|
|
675
1027
|
}
|
|
676
1028
|
if (settings.interpolation === "none") {
|
|
677
1029
|
const base = texture2(maps.baseColor, selectedUv).level(0), orm = texture2(maps.orm, selectedUv).level(0);
|
|
678
|
-
const coverage =
|
|
1030
|
+
const coverage = max3(orm.a, 1e-10);
|
|
679
1031
|
return Surface(
|
|
680
1032
|
base.rgb.div(coverage),
|
|
681
1033
|
opacity,
|
|
@@ -685,15 +1037,15 @@ function applyBoxProjection({
|
|
|
685
1037
|
positions
|
|
686
1038
|
);
|
|
687
1039
|
}
|
|
688
|
-
const colors =
|
|
689
|
-
const weights =
|
|
1040
|
+
const colors = vec33(0).toVar(), orms = vec33(0).toVar(), emissions = vec33(0).toVar(), normals = vec33(0).toVar();
|
|
1041
|
+
const weights = float3(0).toVar(), support = float3(0).toVar(), alpha = float3(0).toVar();
|
|
690
1042
|
for (const c of candidates) {
|
|
691
|
-
|
|
1043
|
+
If3(
|
|
692
1044
|
c.base.a.greaterThan(0).and(c.t.lessThanEqual(nearest.add(c.tolerance))),
|
|
693
1045
|
() => {
|
|
694
|
-
const coverage =
|
|
1046
|
+
const coverage = max3(c.orm.a, 1e-10), w = c.base.a.mul(c.weight);
|
|
695
1047
|
weights.addAssign(c.weight.mul(coverage));
|
|
696
|
-
support.assign(
|
|
1048
|
+
support.assign(max3(support, coverage));
|
|
697
1049
|
colors.addAssign(c.base.rgb.div(coverage).mul(w));
|
|
698
1050
|
orms.addAssign(c.orm.rgb.div(coverage).mul(w));
|
|
699
1051
|
emissions.addAssign(
|
|
@@ -704,13 +1056,13 @@ function applyBoxProjection({
|
|
|
704
1056
|
}
|
|
705
1057
|
);
|
|
706
1058
|
}
|
|
707
|
-
const divisor =
|
|
1059
|
+
const divisor = max3(alpha, 1e-10);
|
|
708
1060
|
return Surface(
|
|
709
1061
|
colors.div(divisor),
|
|
710
|
-
alpha.div(
|
|
1062
|
+
alpha.div(max3(weights, 1e-10)).mul(support),
|
|
711
1063
|
orms.div(divisor),
|
|
712
1064
|
emissions.div(divisor),
|
|
713
|
-
normals.div(
|
|
1065
|
+
normals.div(max3(normals.length(), 1e-10)),
|
|
714
1066
|
positions
|
|
715
1067
|
);
|
|
716
1068
|
})().toVar("boxProjectedSurface");
|
|
@@ -721,12 +1073,12 @@ function applyBoxProjection({
|
|
|
721
1073
|
material.metalnessNode = surface.get("orm").b;
|
|
722
1074
|
material.aoNode = surface.get("orm").r;
|
|
723
1075
|
material.emissiveNode = surface.get("emission");
|
|
724
|
-
material.maskNode = surface.get("alpha").greaterThanEqual(transparency === "cutout" ? alphaCutoff :
|
|
1076
|
+
material.maskNode = surface.get("alpha").greaterThanEqual(transparency === "cutout" ? alphaCutoff : float3(1 / 255));
|
|
725
1077
|
material.maskShadowNode = material.maskNode;
|
|
726
1078
|
if (transparency !== "cutout") material.opacityNode = surface.get("alpha");
|
|
727
1079
|
material.depthNode = capturedSurfaceDepth(surface.get("position"));
|
|
728
1080
|
material.receivedShadowPositionNode = modelWorldMatrix2.mul(
|
|
729
|
-
|
|
1081
|
+
vec44(surface.get("position"), 1)
|
|
730
1082
|
).xyz;
|
|
731
1083
|
material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
|
|
732
1084
|
return material;
|
|
@@ -736,39 +1088,40 @@ function applyBoxProjection({
|
|
|
736
1088
|
function createBoxImpostorMaterial(descriptor, maps) {
|
|
737
1089
|
return createBoxImpostorMaterialVariant(descriptor, maps, { filtering: true });
|
|
738
1090
|
}
|
|
739
|
-
function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false } = {}) {
|
|
1091
|
+
function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false, boundSearch = true } = {}) {
|
|
740
1092
|
const settings = normalizeBoxImpostorDisplay(descriptor);
|
|
741
1093
|
const alphaCutoff = uniform(settings.alphaCutoff);
|
|
742
1094
|
const transparency = boxTransparency(descriptor, settings);
|
|
743
|
-
const minimumAlpha = transparency === "cutout" ? alphaCutoff :
|
|
744
|
-
const surfaceCoverage = (ormAlpha, baseAlpha) => filtering && descriptor.alphaMode !== "blend" ?
|
|
745
|
-
const
|
|
1095
|
+
const minimumAlpha = transparency === "cutout" ? alphaCutoff : float4(1 / 255);
|
|
1096
|
+
const surfaceCoverage = (ormAlpha, baseAlpha) => filtering && descriptor.alphaMode !== "blend" ? min2(ormAlpha, baseAlpha) : ormAlpha;
|
|
1097
|
+
const unpackNormal = (encoded) => {
|
|
746
1098
|
const packed = encoded.mul(2).sub(1);
|
|
747
|
-
const normal2 =
|
|
748
|
-
|
|
749
|
-
normal2.xy.assign(
|
|
1099
|
+
const normal2 = vec34(packed, float4(1).sub(abs3(packed.x)).sub(abs3(packed.y))).toVar();
|
|
1100
|
+
If4(normal2.z.lessThan(0), () => {
|
|
1101
|
+
normal2.xy.assign(vec24(1).sub(abs3(normal2.yx)).mul(sign3(normal2.xy.add(1e-20))));
|
|
750
1102
|
});
|
|
751
|
-
return normal2
|
|
1103
|
+
return normal2;
|
|
752
1104
|
};
|
|
1105
|
+
const decodeNormal = (encoded) => unpackNormal(encoded).normalize();
|
|
753
1106
|
const filteredNormal = (uv2, depth, tolerance, nearest, alpha, sampled = texture3(maps.normalDepth, uv2).level(0), ormAlpha = texture3(maps.orm, uv2).level(0).a) => {
|
|
754
1107
|
sampled = sampled.toVar();
|
|
755
|
-
const coverage2 =
|
|
1108
|
+
const coverage2 = max4(descriptor.alphaMode === "blend" ? ormAlpha : alpha, 1e-10);
|
|
756
1109
|
const nearestNormal = decodeNormal(nearest.rg), result = decodeNormal(sampled.rg.div(coverage2)).toVar();
|
|
757
1110
|
const sampledDepth = sampled.b.mul(65280).add(sampled.a.mul(255)).div(65535);
|
|
758
1111
|
const nearestDepth = nearest.b.mul(65280).add(nearest.a.mul(255)).div(65535);
|
|
759
|
-
|
|
760
|
-
const p = uv2.mul(descriptor.atlasResolution).sub(0.5), lo =
|
|
761
|
-
const normal2 =
|
|
1112
|
+
If4(nearestNormal.z.lessThan(0).or(dot3(result, nearestNormal).lessThan(0.75)).or(coverage2.lessThan(0.9999)).or(abs3(sampledDepth.sub(nearestDepth)).greaterThan(tolerance)), () => {
|
|
1113
|
+
const p = uv2.mul(descriptor.atlasResolution).sub(0.5), lo = floor2(p), f = fract(p);
|
|
1114
|
+
const normal2 = vec34(0).toVar(), weights = float4(0).toVar();
|
|
762
1115
|
for (const [x, y] of [[0, 0], [1, 0], [0, 1], [1, 1]]) {
|
|
763
|
-
const texel =
|
|
764
|
-
const packed =
|
|
765
|
-
const alpha2 =
|
|
766
|
-
const coverage3 = descriptor.alphaMode === "blend" ?
|
|
1116
|
+
const texel = ivec24(lo.add(vec24(x, y)).clamp(0, descriptor.atlasResolution - 1));
|
|
1117
|
+
const packed = textureLoad4(maps.normalDepth, texel).toVar();
|
|
1118
|
+
const alpha2 = textureLoad4(maps.baseColor, texel).a;
|
|
1119
|
+
const coverage3 = descriptor.alphaMode === "blend" ? textureLoad4(maps.orm, texel).a : alpha2;
|
|
767
1120
|
const capturedDepth = packed.b.mul(65280).add(packed.a.mul(255)).div(65535);
|
|
768
1121
|
const supported = coverage3.greaterThan(1e-4).and(alpha2.greaterThanEqual(minimumAlpha.mul(coverage3))).and(abs3(capturedDepth.sub(depth)).lessThanEqual(tolerance));
|
|
769
|
-
|
|
770
|
-
const weight = (x ? f.x :
|
|
771
|
-
normal2.addAssign(decodeNormal(packed.rg.div(
|
|
1122
|
+
If4(supported, () => {
|
|
1123
|
+
const weight = (x ? f.x : float4(1).sub(f.x)).mul(y ? f.y : float4(1).sub(f.y)).mul(coverage3);
|
|
1124
|
+
normal2.addAssign(decodeNormal(packed.rg.div(max4(coverage3, 1e-10))).mul(weight));
|
|
772
1125
|
weights.addAssign(weight);
|
|
773
1126
|
});
|
|
774
1127
|
}
|
|
@@ -817,36 +1170,36 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
817
1170
|
material.addEventListener("dispose", () => rects.dispose());
|
|
818
1171
|
const center = attribute3("boxCenter", "vec3"), half = attribute3("boxHalfSize", "vec3");
|
|
819
1172
|
const ax = attribute3("boxAxisX", "vec3"), ay = attribute3("boxAxisY", "vec3"), az = attribute3("boxAxisZ", "vec3");
|
|
820
|
-
const toBox = (p) =>
|
|
1173
|
+
const toBox = (p) => vec34(dot3(p, ax), dot3(p, ay), dot3(p, az));
|
|
821
1174
|
const fromBox = (p) => ax.mul(p.x).add(ay.mul(p.y)).add(az.mul(p.z));
|
|
822
|
-
const eye = modelWorldMatrixInverse.mul(
|
|
823
|
-
const viewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(
|
|
1175
|
+
const eye = modelWorldMatrixInverse.mul(vec45(cameraPosition, 1)).xyz;
|
|
1176
|
+
const viewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec45(0, 0, 1, 0))).xyz.normalize();
|
|
824
1177
|
const perspective = cameraProjectionMatrix2.element(3).w.equal(0);
|
|
825
1178
|
let proxy = positionLocal2;
|
|
826
1179
|
if (descriptor.mode !== "fitted") {
|
|
827
|
-
proxy =
|
|
1180
|
+
proxy = Fn5((_, builder) => {
|
|
828
1181
|
const delta = eye.sub(center);
|
|
829
1182
|
const forward = perspective.and(delta.length().greaterThan(1e-6)).select(normalize2(delta), viewAxis);
|
|
830
|
-
const right = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(
|
|
1183
|
+
const right = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec45(0, 1, 0, 0))).xyz.cross(forward).normalize();
|
|
831
1184
|
const up = forward.cross(right).normalize();
|
|
832
1185
|
const radius = half.length();
|
|
833
1186
|
const distance = dot3(eye.sub(center), forward);
|
|
834
|
-
const planeOffset = perspective.select(
|
|
1187
|
+
const planeOffset = perspective.select(min2(radius, distance.sub(cameraNear2.mul(2))), radius);
|
|
835
1188
|
const plane = center.add(forward.mul(planeOffset));
|
|
836
|
-
const lo =
|
|
1189
|
+
const lo = vec24(1e20).toVar(), hi = vec24(-1e20).toVar();
|
|
837
1190
|
for (const x of [-1, 1])
|
|
838
1191
|
for (const y of [-1, 1])
|
|
839
1192
|
for (const z of [-1, 1]) {
|
|
840
|
-
const corner2 = fromBox(half.mul(
|
|
1193
|
+
const corner2 = fromBox(half.mul(vec34(x, y, z)));
|
|
841
1194
|
const scale = perspective.select(
|
|
842
|
-
|
|
843
|
-
|
|
1195
|
+
max4(distance.sub(planeOffset), 1e-5).div(
|
|
1196
|
+
max4(distance.sub(dot3(corner2, forward)), 1e-5)
|
|
844
1197
|
),
|
|
845
|
-
|
|
1198
|
+
float4(1)
|
|
846
1199
|
);
|
|
847
|
-
const p =
|
|
848
|
-
lo.assign(
|
|
849
|
-
hi.assign(
|
|
1200
|
+
const p = vec24(dot3(corner2, right), dot3(corner2, up)).mul(scale);
|
|
1201
|
+
lo.assign(min2(lo, p));
|
|
1202
|
+
hi.assign(max4(hi, p));
|
|
850
1203
|
}
|
|
851
1204
|
const corner = attribute3("cardCorner", "vec2").add(1).mul(0.5);
|
|
852
1205
|
const xy = lo.add(hi.sub(lo).mul(corner));
|
|
@@ -908,53 +1261,66 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
908
1261
|
occupancy.needsUpdate = true;
|
|
909
1262
|
material.addEventListener("dispose", () => occupancy.dispose());
|
|
910
1263
|
}
|
|
1264
|
+
const preferFacingCapture = filtering && descriptor.alphaMode === "opaque" && settings.interpolation === "none";
|
|
1265
|
+
const viewBounds = boundSearch ? createBoxRayBounds(descriptor, maps, rects, material) : null;
|
|
911
1266
|
const Result = struct2(
|
|
912
|
-
{ uv: "vec2", position: "vec3",
|
|
1267
|
+
{ uv: "vec2", position: "vec3", face: "int", valid: "float" },
|
|
913
1268
|
"BoxImpostorHit"
|
|
914
1269
|
);
|
|
915
|
-
const
|
|
916
|
-
const
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
1270
|
+
const faceBasis = (axis, facing) => {
|
|
1271
|
+
const n = vec34(
|
|
1272
|
+
axis.equal(0).select(facing, 0),
|
|
1273
|
+
axis.equal(1).select(facing, 0),
|
|
1274
|
+
axis.equal(2).select(facing, 0)
|
|
1275
|
+
).toVar();
|
|
1276
|
+
const right = axis.equal(0).select(
|
|
1277
|
+
vec34(0, 0, facing.negate()),
|
|
1278
|
+
axis.equal(1).select(vec34(1, 0, 0), vec34(facing, 0, 0))
|
|
1279
|
+
).toVar();
|
|
1280
|
+
const up = axis.equal(1).select(vec34(0, 0, facing.negate()), vec34(0, 1, 0)).toVar();
|
|
1281
|
+
return { n, right, up };
|
|
1282
|
+
};
|
|
1283
|
+
const ray = perspective.select(normalize2(raster.sub(eye)), viewAxis.negate());
|
|
1284
|
+
const raycast = Fn5((_, builder) => {
|
|
920
1285
|
const origin = perspective.select(
|
|
921
1286
|
eye,
|
|
922
1287
|
raster.sub(ray.mul(half.length().mul(4)))
|
|
923
1288
|
);
|
|
924
1289
|
const o = toBox(origin.sub(center)).div(half), r = toBox(ray).div(half);
|
|
925
|
-
const safe = sign3(r.add(1e-20)).mul(
|
|
926
|
-
const a =
|
|
927
|
-
const enter =
|
|
928
|
-
const exit =
|
|
1290
|
+
const safe = sign3(r.add(1e-20)).mul(max4(abs3(r), 1e-10));
|
|
1291
|
+
const a = vec34(-1).sub(o).div(safe), b = vec34(1).sub(o).div(safe);
|
|
1292
|
+
const enter = max4(max4(min2(a, b).x, min2(a, b).y), min2(a, b).z).max(0);
|
|
1293
|
+
const exit = min2(min2(max4(a, b).x, max4(a, b).y), max4(a, b).z);
|
|
1294
|
+
const rayBounds = viewBounds?.(builder, int4(attribute3("boxId", "float")), o, r, enter, exit);
|
|
929
1295
|
const occupiedStart = enter.toVar();
|
|
930
1296
|
if (occupancy) {
|
|
931
1297
|
const gridOrigin = o.add(1).mul(gridSize / 2);
|
|
932
1298
|
const gridRay = r.mul(gridSize / 2);
|
|
933
1299
|
const step = sign3(gridRay.add(1e-20));
|
|
934
|
-
const cell =
|
|
935
|
-
const inverse =
|
|
1300
|
+
const cell = floor2(gridOrigin.add(gridRay.mul(enter))).clamp(0, gridSize - 1).toVar();
|
|
1301
|
+
const inverse = vec34(1).div(safe.mul(gridSize / 2));
|
|
936
1302
|
const delta = abs3(inverse);
|
|
937
1303
|
const next = cell.add(step.add(1).mul(0.5)).sub(gridOrigin).mul(inverse).toVar();
|
|
938
1304
|
const t = enter.toVar();
|
|
939
1305
|
occupiedStart.assign(exit.add(1));
|
|
940
|
-
|
|
941
|
-
|
|
1306
|
+
Loop2(gridSize * 3 + 1, () => {
|
|
1307
|
+
If4(t.greaterThan(exit), () => {
|
|
942
1308
|
Break();
|
|
943
1309
|
});
|
|
944
|
-
const cellIndex =
|
|
1310
|
+
const cellIndex = uint2(
|
|
945
1311
|
cell.x.add(cell.y.mul(gridSize)).add(cell.z.mul(gridSize ** 2))
|
|
946
1312
|
).toVar();
|
|
947
|
-
const word =
|
|
1313
|
+
const word = textureLoad4(
|
|
948
1314
|
occupancy,
|
|
949
|
-
|
|
1315
|
+
ivec24(int4(cellIndex.shiftRight(5)), int4(attribute3("boxId", "float")))
|
|
950
1316
|
).r;
|
|
951
1317
|
const support = word.shiftRight(cellIndex.bitAnd(31)).bitAnd(1);
|
|
952
|
-
|
|
1318
|
+
If4(support.notEqual(uint2(0)), () => {
|
|
953
1319
|
occupiedStart.assign(t);
|
|
954
1320
|
Break();
|
|
955
1321
|
});
|
|
956
|
-
const edge =
|
|
957
|
-
const advance =
|
|
1322
|
+
const edge = min2(min2(next.x, next.y), next.z).toVar();
|
|
1323
|
+
const advance = vec34(
|
|
958
1324
|
next.x.lessThanEqual(edge).select(1, 0),
|
|
959
1325
|
next.y.lessThanEqual(edge).select(1, 0),
|
|
960
1326
|
next.z.lessThanEqual(edge).select(1, 0)
|
|
@@ -962,7 +1328,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
962
1328
|
cell.addAssign(advance.mul(step));
|
|
963
1329
|
next.addAssign(advance.mul(delta));
|
|
964
1330
|
t.assign(edge);
|
|
965
|
-
|
|
1331
|
+
If4(
|
|
966
1332
|
cell.x.lessThan(0).or(cell.x.greaterThan(gridSize - 1)).or(cell.y.lessThan(0)).or(cell.y.greaterThan(gridSize - 1)).or(cell.z.lessThan(0)).or(cell.z.greaterThan(gridSize - 1)),
|
|
967
1333
|
() => {
|
|
968
1334
|
Break();
|
|
@@ -971,19 +1337,19 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
971
1337
|
});
|
|
972
1338
|
}
|
|
973
1339
|
const look = toBox(ray).negate();
|
|
974
|
-
const bestT = exit.add(1).toVar(), bestUv =
|
|
1340
|
+
const bestT = exit.add(1).toVar(), bestUv = vec24(0).toVar(), bestFace = int4(0).toVar(), appearanceNear = preferFacingCapture ? float4(0).toVar() : null, valid = float4(0).toVar(), found = float4(0).toVar();
|
|
975
1341
|
const angles = abs3(look).toVar();
|
|
976
1342
|
const major = angles.x.greaterThanEqual(angles.y).and(angles.x.greaterThanEqual(angles.z)).select(
|
|
977
|
-
|
|
978
|
-
angles.y.greaterThanEqual(angles.z).select(
|
|
1343
|
+
int4(0),
|
|
1344
|
+
angles.y.greaterThanEqual(angles.z).select(int4(1), int4(2))
|
|
979
1345
|
).toVar();
|
|
980
1346
|
const nextAxis = major.add(1).mod(3), lastAxis = major.add(2).mod(3);
|
|
981
1347
|
const second = angles.element(nextAxis).greaterThanEqual(angles.element(lastAxis)).select(nextAxis, lastAxis).toVar();
|
|
982
|
-
const third =
|
|
983
|
-
|
|
1348
|
+
const third = int4(3).sub(major).sub(second).toVar();
|
|
1349
|
+
Loop2(
|
|
984
1350
|
{ start: 0, end: 6, type: "int", name: "viewIndex" },
|
|
985
1351
|
({ viewIndex: i }) => {
|
|
986
|
-
|
|
1352
|
+
If4(occupiedStart.greaterThan(exit), () => {
|
|
987
1353
|
Break();
|
|
988
1354
|
});
|
|
989
1355
|
valid.assign(0);
|
|
@@ -993,112 +1359,133 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
993
1359
|
look.element(axis).greaterThanEqual(0),
|
|
994
1360
|
look.element(axis).lessThan(0)
|
|
995
1361
|
);
|
|
996
|
-
const face = axis.mul(2).add(positive.select(
|
|
1362
|
+
const face = axis.mul(2).add(positive.select(int4(0), int4(1)));
|
|
1363
|
+
const bounds = rayBounds?.(face, enter, exit);
|
|
997
1364
|
const facing = positive.select(1, -1);
|
|
998
|
-
const n =
|
|
999
|
-
axis.equal(0).select(facing, 0),
|
|
1000
|
-
axis.equal(1).select(facing, 0),
|
|
1001
|
-
axis.equal(2).select(facing, 0)
|
|
1002
|
-
).toVar();
|
|
1003
|
-
const right = axis.equal(0).select(
|
|
1004
|
-
vec33(0, 0, facing.negate()),
|
|
1005
|
-
axis.equal(1).select(vec33(1, 0, 0), vec33(facing, 0, 0))
|
|
1006
|
-
).toVar();
|
|
1007
|
-
const up = axis.equal(1).select(vec33(0, 0, facing.negate()), vec33(0, 1, 0)).toVar();
|
|
1365
|
+
const { n, right, up } = faceBasis(axis, facing);
|
|
1008
1366
|
const direction = i.lessThan(3).select(1, -1);
|
|
1009
|
-
const rect =
|
|
1367
|
+
const rect = textureLoad4(
|
|
1010
1368
|
rects,
|
|
1011
|
-
|
|
1369
|
+
ivec24(face, int4(attribute3("boxId", "float")))
|
|
1012
1370
|
).toVar();
|
|
1013
1371
|
const sampleUv = (p) => rect.xy.add(
|
|
1014
|
-
|
|
1372
|
+
vec24(dot3(p, right), dot3(p, up)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
|
|
1015
1373
|
);
|
|
1016
1374
|
const decode = (d) => d.b.mul(65280).add(d.a.mul(255)).div(65535);
|
|
1017
1375
|
const depthAt = (uv2) => decode(texture3(maps.normalDepth, uv2).level(0));
|
|
1376
|
+
const projectedOrigin = vec24(dot3(o, right), dot3(o, up)).toVar();
|
|
1377
|
+
const projectedRay = vec24(dot3(r, right), dot3(r, up)).toVar();
|
|
1378
|
+
const axialOrigin = dot3(o, n).toVar();
|
|
1379
|
+
const axialRay = dot3(r, n).toVar();
|
|
1018
1380
|
const distance = (t) => {
|
|
1019
|
-
const
|
|
1020
|
-
|
|
1381
|
+
const uv2 = rect.xy.add(
|
|
1382
|
+
projectedOrigin.add(projectedRay.mul(t)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
|
|
1383
|
+
);
|
|
1384
|
+
return axialOrigin.add(axialRay.mul(t)).sub(float4(1).sub(depthAt(uv2).mul(2))).mul(direction);
|
|
1021
1385
|
};
|
|
1022
1386
|
const acceptHit = (hitT) => {
|
|
1023
1387
|
const point2 = o.add(r.mul(hitT)), uv2 = sampleUv(point2);
|
|
1024
|
-
const texel =
|
|
1025
|
-
const nearest =
|
|
1388
|
+
const texel = ivec24(uv2.mul(descriptor.atlasResolution));
|
|
1389
|
+
const nearest = textureLoad4(maps.normalDepth, texel);
|
|
1026
1390
|
const capturedBase = texture3(maps.baseColor, uv2).level(0);
|
|
1027
|
-
const occupied =
|
|
1391
|
+
const occupied = textureLoad4(maps.orm, texel).a.greaterThan(0.5);
|
|
1028
1392
|
const residual = abs3(
|
|
1029
|
-
dot3(point2, n).sub(
|
|
1393
|
+
dot3(point2, n).sub(float4(1).sub(decode(nearest).mul(2)))
|
|
1030
1394
|
);
|
|
1031
|
-
const tolerance =
|
|
1395
|
+
const tolerance = max4(
|
|
1032
1396
|
dot3(abs3(right), half).div(rect.z.mul(descriptor.atlasResolution)),
|
|
1033
1397
|
dot3(abs3(up), half).div(rect.w.mul(descriptor.atlasResolution))
|
|
1034
1398
|
).div(dot3(abs3(n), half)).mul(4).add(4 / 65535);
|
|
1035
|
-
|
|
1399
|
+
If4(
|
|
1036
1400
|
residual.lessThanEqual(tolerance).and(occupied).and(
|
|
1037
1401
|
capturedBase.a.greaterThanEqual(minimumAlpha)
|
|
1038
1402
|
),
|
|
1039
1403
|
() => {
|
|
1040
1404
|
valid.assign(1);
|
|
1041
|
-
|
|
1405
|
+
If4(hitT.lessThan(bestT), () => {
|
|
1042
1406
|
bestT.assign(hitT);
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1407
|
+
const useAppearance = () => {
|
|
1408
|
+
bestUv.assign(uv2);
|
|
1409
|
+
bestFace.assign(face);
|
|
1410
|
+
};
|
|
1411
|
+
if (preferFacingCapture) {
|
|
1412
|
+
const texel2 = max4(
|
|
1413
|
+
dot3(abs3(right), half).div(rect.z),
|
|
1414
|
+
dot3(abs3(up), half).div(rect.w)
|
|
1415
|
+
).div(descriptor.atlasResolution);
|
|
1416
|
+
const uncertainty = min2(
|
|
1417
|
+
texel2,
|
|
1418
|
+
residual.add(2 / 65535).div(abs3(axialRay).max(1e-10))
|
|
1054
1419
|
);
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
);
|
|
1061
|
-
bestNormal.mulAssign(
|
|
1062
|
-
dot3(bestNormal, ray).greaterThan(0).select(-1, 1)
|
|
1063
|
-
);
|
|
1420
|
+
If4(found.lessThan(0.5).or(hitT.add(uncertainty).lessThan(appearanceNear)), () => {
|
|
1421
|
+
appearanceNear.assign(hitT.sub(uncertainty));
|
|
1422
|
+
useAppearance();
|
|
1423
|
+
});
|
|
1424
|
+
} else useAppearance();
|
|
1425
|
+
found.assign(1);
|
|
1064
1426
|
});
|
|
1065
1427
|
}
|
|
1066
1428
|
);
|
|
1067
1429
|
};
|
|
1068
1430
|
const traceInterval = (start, end, steps) => {
|
|
1069
|
-
const footprint =
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1431
|
+
const footprint = vec24(dot3(r, right), dot3(r, up)).abs().mul(rect.zw).mul(descriptor.atlasResolution / 2).mul(end.sub(start));
|
|
1432
|
+
let density = steps / 8;
|
|
1433
|
+
if (descriptor.alphaMode === "blend" && transparency === "blended" && steps < 32) {
|
|
1434
|
+
density = i.equal(0).select(steps / 16, steps / 32);
|
|
1435
|
+
} else if (descriptor.alphaMode === "opaque" && steps === 16) {
|
|
1436
|
+
density = found.greaterThan(0.5).select(1, 2);
|
|
1437
|
+
}
|
|
1438
|
+
const samples = max4(float4(steps), max4(footprint.x, footprint.y).mul(density)).div(2).ceil().mul(2).toVar();
|
|
1439
|
+
const step = end.sub(start).div(samples).toVar();
|
|
1440
|
+
const pairs = int4(samples.div(2));
|
|
1441
|
+
const pairAt = (t) => t.sub(start).div(max4(step.mul(2), 1e-15));
|
|
1442
|
+
const firstPair = bounds ? int4(floor2(pairAt(bounds.start))).clamp(0, pairs).toVar() : int4(0);
|
|
1443
|
+
const lastPair = bounds ? int4(pairAt(bounds.end).ceil()).clamp(0, pairs).toVar() : pairs;
|
|
1444
|
+
const previous = start.toVar();
|
|
1445
|
+
if (bounds) If4(firstPair.greaterThan(0), () => {
|
|
1446
|
+
previous.assign(start.add(step.mul(float4(firstPair.mul(2)))));
|
|
1447
|
+
});
|
|
1448
|
+
const previousD = distance(previous).toVar();
|
|
1449
|
+
const atEntry = bounds ? firstPair.equal(0).and(start.greaterThanEqual(bounds.start)).and(start.lessThanEqual(bounds.end)) : null;
|
|
1450
|
+
If4(atEntry ? atEntry.and(previousD.lessThanEqual(0)) : previousD.lessThanEqual(0), () => {
|
|
1074
1451
|
acceptHit(start);
|
|
1075
1452
|
});
|
|
1076
|
-
|
|
1077
|
-
|
|
1453
|
+
Loop2({ start: firstPair, end: lastPair, type: "int" }, ({ i: i2 }) => {
|
|
1454
|
+
If4(valid.greaterThan(0.5).or(previous.greaterThan(bestT)), () => {
|
|
1078
1455
|
Break();
|
|
1079
1456
|
});
|
|
1080
|
-
const
|
|
1081
|
-
const t = start.add(step.mul(
|
|
1457
|
+
const [first, second2] = [1, 2].map((offset) => {
|
|
1458
|
+
const t = start.add(step.mul(float4(i2.mul(2).add(offset)))).toVar();
|
|
1082
1459
|
return { t, d: distance(t).toVar() };
|
|
1083
1460
|
});
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
})
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1461
|
+
const crossesFirst = first.d.mul(previousD).lessThanEqual(0);
|
|
1462
|
+
const crossesSecond = second2.d.mul(first.d).lessThanEqual(0);
|
|
1463
|
+
If4(crossesFirst.or(crossesSecond), () => {
|
|
1464
|
+
for (const [{ t, d }, crosses] of [[first, crossesFirst], [second2, crossesSecond]]) {
|
|
1465
|
+
If4(crosses.and(valid.lessThan(0.5)).and(previous.lessThanEqual(bestT)), () => {
|
|
1466
|
+
const crossing = refineSurfaceCrossing({ float: float4, If: If4, Loop: Loop2 }, {
|
|
1467
|
+
lo: previous,
|
|
1468
|
+
hi: t,
|
|
1469
|
+
loValue: previousD,
|
|
1470
|
+
hiValue: d,
|
|
1471
|
+
height: distance,
|
|
1472
|
+
steps: 3,
|
|
1473
|
+
secant: true
|
|
1474
|
+
});
|
|
1475
|
+
acceptHit(crossing);
|
|
1093
1476
|
});
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
}
|
|
1477
|
+
previous.assign(t);
|
|
1478
|
+
previousD.assign(d);
|
|
1479
|
+
}
|
|
1480
|
+
}).Else(() => {
|
|
1481
|
+
previous.assign(second2.t);
|
|
1482
|
+
previousD.assign(second2.d);
|
|
1483
|
+
});
|
|
1098
1484
|
});
|
|
1099
1485
|
};
|
|
1100
|
-
|
|
1101
|
-
|
|
1486
|
+
const supported = exit.greaterThanEqual(enter).and(abs3(look.element(axis)).greaterThan(1e-3));
|
|
1487
|
+
If4(
|
|
1488
|
+
bounds ? supported.and(bounds.start.lessThanEqual(bestT)).and(bounds.end.greaterThanEqual(occupiedStart)) : supported,
|
|
1102
1489
|
() => {
|
|
1103
1490
|
if (!accelerated) {
|
|
1104
1491
|
traceInterval(enter, exit, descriptor.raySteps);
|
|
@@ -1111,13 +1498,13 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
1111
1498
|
)
|
|
1112
1499
|
);
|
|
1113
1500
|
const interval = positive.select(depths.xy, depths.zw);
|
|
1114
|
-
const initialDepth =
|
|
1501
|
+
const initialDepth = float4(1).sub(dot3(o, n)).mul(0.5);
|
|
1115
1502
|
const depthSlope = dot3(r, n).negate().mul(0.5);
|
|
1116
1503
|
const da = interval.x.sub(2 / 65535).sub(initialDepth).div(depthSlope);
|
|
1117
1504
|
const db = interval.y.add(2 / 65535).sub(initialDepth).div(depthSlope);
|
|
1118
|
-
const start =
|
|
1119
|
-
const end =
|
|
1120
|
-
|
|
1505
|
+
const start = max4(occupiedStart, min2(da, db));
|
|
1506
|
+
const end = min2(min2(exit, bestT), max4(da, db));
|
|
1507
|
+
If4(
|
|
1121
1508
|
interval.x.lessThanEqual(interval.y).and(end.greaterThanEqual(start)),
|
|
1122
1509
|
() => {
|
|
1123
1510
|
const search = () => traceInterval(start, end, descriptor.raySteps);
|
|
@@ -1127,12 +1514,12 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
1127
1514
|
)
|
|
1128
1515
|
);
|
|
1129
1516
|
if (hasFlatView) {
|
|
1130
|
-
|
|
1517
|
+
If4(
|
|
1131
1518
|
interval.y.sub(interval.x).lessThanEqual(1 / 65535),
|
|
1132
1519
|
() => {
|
|
1133
1520
|
const t = interval.x.add(interval.y).mul(0.5).sub(initialDepth).div(depthSlope);
|
|
1134
|
-
|
|
1135
|
-
t.greaterThanEqual(enter).and(t.lessThanEqual(
|
|
1521
|
+
If4(
|
|
1522
|
+
t.greaterThanEqual(enter).and(t.lessThanEqual(min2(exit, bestT))),
|
|
1136
1523
|
() => {
|
|
1137
1524
|
acceptHit(t);
|
|
1138
1525
|
}
|
|
@@ -1152,57 +1539,65 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
1152
1539
|
return Result(
|
|
1153
1540
|
bestUv,
|
|
1154
1541
|
center.add(fromBox(point.mul(half))),
|
|
1155
|
-
|
|
1542
|
+
bestFace,
|
|
1156
1543
|
valid
|
|
1157
1544
|
);
|
|
1158
1545
|
});
|
|
1159
1546
|
const hit = raycast().toVar("boxHit");
|
|
1160
1547
|
hit.toStack();
|
|
1161
1548
|
const base = texture3(maps.baseColor, hit.get("uv")).level(0), orm = texture3(maps.orm, hit.get("uv")).level(0);
|
|
1162
|
-
const coverage =
|
|
1163
|
-
|
|
1549
|
+
const coverage = max4(surfaceCoverage(orm.a, base.a), 1e-10);
|
|
1550
|
+
const hitNormal = Fn5(() => {
|
|
1551
|
+
const face = hit.get("face");
|
|
1552
|
+
const { n, right, up } = faceBasis(face.div(2), face.mod(2).equal(0).select(1, -1));
|
|
1553
|
+
const sampled = texture3(maps.normalDepth, hit.get("uv")).level(0);
|
|
1554
|
+
const local = unpackNormal(sampled.rg.div(coverage));
|
|
1555
|
+
const direction = fromBox(right.mul(local.x).add(up.mul(local.y)).add(n.mul(local.z))).normalize().toVar();
|
|
1556
|
+
return direction.mul(dot3(direction, ray).greaterThan(0).select(-1, 1));
|
|
1557
|
+
})();
|
|
1558
|
+
let color = base.div(coverage), surfaceOrm = orm.rgb.div(coverage), emission = texture3(maps.emissive, hit.get("uv")).level(0).rgb.div(coverage), normal = hitNormal;
|
|
1164
1559
|
if (settings.interpolation !== "none") {
|
|
1165
1560
|
const fallbackColor = color, fallbackOrm = surfaceOrm, fallbackEmission = emission, fallbackNormal = normal;
|
|
1166
1561
|
const Surface = struct2(
|
|
1167
1562
|
{ color: "vec4", orm: "vec3", emission: "vec3", normal: "vec3" },
|
|
1168
1563
|
"BoxSurfaceBlend"
|
|
1169
1564
|
);
|
|
1170
|
-
const blended =
|
|
1565
|
+
const blended = Fn5(() => {
|
|
1171
1566
|
const point = toBox(hit.get("position").sub(center)).div(half);
|
|
1172
1567
|
const look = toBox(
|
|
1173
1568
|
perspective.select(normalize2(eye.sub(hit.get("position"))), viewAxis)
|
|
1174
1569
|
);
|
|
1175
|
-
const colors =
|
|
1570
|
+
const colors = vec45(0).toVar(), materials = vec34(0).toVar(), emissions = vec34(0).toVar(), normals = vec34(0).toVar(), weights = float4(0).toVar();
|
|
1176
1571
|
for (let axis = 0; axis < 3; axis++) {
|
|
1177
1572
|
const positive = look.element(axis).greaterThanEqual(0);
|
|
1178
1573
|
const front = BOX_FACE_AXES[axis * 2], back = BOX_FACE_AXES[axis * 2 + 1];
|
|
1179
|
-
const n = positive.select(
|
|
1574
|
+
const n = positive.select(vec34(...front.normal), vec34(...back.normal));
|
|
1180
1575
|
const right = positive.select(
|
|
1181
|
-
|
|
1182
|
-
|
|
1576
|
+
vec34(...front.right),
|
|
1577
|
+
vec34(...back.right)
|
|
1183
1578
|
);
|
|
1184
|
-
const up = positive.select(
|
|
1185
|
-
const face = positive.select(
|
|
1186
|
-
const rect =
|
|
1579
|
+
const up = positive.select(vec34(...front.up), vec34(...back.up));
|
|
1580
|
+
const face = positive.select(int4(axis * 2), int4(axis * 2 + 1));
|
|
1581
|
+
const rect = textureLoad4(
|
|
1187
1582
|
rects,
|
|
1188
|
-
|
|
1583
|
+
ivec24(face, int4(attribute3("boxId", "float")))
|
|
1189
1584
|
);
|
|
1190
1585
|
const uv2 = rect.xy.add(
|
|
1191
|
-
|
|
1586
|
+
vec24(dot3(point, right), dot3(point, up)).mul(0.5).add(0.5).clamp(0, 1).mul(rect.zw)
|
|
1192
1587
|
);
|
|
1193
|
-
const texel =
|
|
1194
|
-
const captured =
|
|
1588
|
+
const texel = ivec24(uv2.mul(descriptor.atlasResolution));
|
|
1589
|
+
const captured = textureLoad4(maps.normalDepth, texel);
|
|
1195
1590
|
const sampled = texture3(maps.baseColor, uv2).level(0);
|
|
1196
1591
|
const sampledOrm = texture3(maps.orm, uv2).level(0);
|
|
1197
|
-
const occupied =
|
|
1592
|
+
const occupied = textureLoad4(maps.orm, texel).a.greaterThan(0.5);
|
|
1198
1593
|
const depth = captured.b.mul(65280).add(captured.a.mul(255)).div(65535);
|
|
1199
|
-
const tolerance =
|
|
1594
|
+
const tolerance = max4(
|
|
1200
1595
|
dot3(abs3(right), half).div(rect.z.mul(descriptor.atlasResolution)),
|
|
1201
1596
|
dot3(abs3(up), half).div(rect.w.mul(descriptor.atlasResolution))
|
|
1202
1597
|
).div(dot3(abs3(n), half)).mul(4).add(4 / 65535);
|
|
1203
|
-
|
|
1598
|
+
If4(
|
|
1204
1599
|
occupied.and(sampled.a.greaterThanEqual(minimumAlpha)).and(
|
|
1205
|
-
abs3(dot3(point, n).sub(
|
|
1600
|
+
abs3(dot3(point, n).sub(float4(1).sub(depth.mul(2)))).lessThanEqual(
|
|
1206
1601
|
tolerance
|
|
1207
1602
|
)
|
|
1208
1603
|
),
|
|
@@ -1211,28 +1606,28 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
1211
1606
|
const filtered = filtering ? texture3(maps.normalDepth, uv2).level(0).toVar() : captured;
|
|
1212
1607
|
let weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
|
|
1213
1608
|
if (filtering) {
|
|
1214
|
-
const error = abs3(dot3(point, n).sub(
|
|
1609
|
+
const error = abs3(dot3(point, n).sub(float4(1).sub(
|
|
1215
1610
|
filtered.b.mul(65280).add(filtered.a.mul(255)).div(65535).mul(2)
|
|
1216
1611
|
)));
|
|
1217
|
-
weight = weight.mul(
|
|
1612
|
+
weight = weight.mul(float4(1).sub(error.div(tolerance)).clamp(0, 1));
|
|
1218
1613
|
}
|
|
1219
|
-
const coverage2 =
|
|
1614
|
+
const coverage2 = max4(surfaceCoverage(sampledOrm.a, sampled.a), 1e-10);
|
|
1220
1615
|
colors.addAssign(sampled.div(coverage2).mul(weight));
|
|
1221
1616
|
materials.addAssign(sampledOrm.rgb.div(coverage2).mul(weight));
|
|
1222
1617
|
emissions.addAssign(
|
|
1223
1618
|
texture3(maps.emissive, uv2).level(0).rgb.div(coverage2).mul(weight)
|
|
1224
1619
|
);
|
|
1225
1620
|
let shadingNormal;
|
|
1226
|
-
if (filtering) shadingNormal = filteredNormal(uv2,
|
|
1621
|
+
if (filtering) shadingNormal = filteredNormal(uv2, float4(1).sub(dot3(point, n)).mul(0.5), tolerance.mul(0.5), captured, sampled.a, filtered, sampledOrm.a);
|
|
1227
1622
|
else {
|
|
1228
1623
|
const packed = captured.rg.mul(2).sub(1);
|
|
1229
|
-
const decoded =
|
|
1624
|
+
const decoded = vec34(
|
|
1230
1625
|
packed,
|
|
1231
|
-
|
|
1626
|
+
float4(1).sub(abs3(packed.x)).sub(abs3(packed.y))
|
|
1232
1627
|
).toVar();
|
|
1233
|
-
|
|
1628
|
+
If4(decoded.z.lessThan(0), () => {
|
|
1234
1629
|
decoded.xy.assign(
|
|
1235
|
-
|
|
1630
|
+
vec24(1).sub(abs3(decoded.yx)).mul(sign3(decoded.xy.add(1e-20)))
|
|
1236
1631
|
);
|
|
1237
1632
|
});
|
|
1238
1633
|
shadingNormal = decoded;
|
|
@@ -1246,7 +1641,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
1246
1641
|
}
|
|
1247
1642
|
);
|
|
1248
1643
|
}
|
|
1249
|
-
const divisor =
|
|
1644
|
+
const divisor = max4(weights, 1e-10), valid = weights.greaterThan(1e-10);
|
|
1250
1645
|
return Surface(
|
|
1251
1646
|
valid.select(colors.div(divisor), fallbackColor),
|
|
1252
1647
|
valid.select(materials.div(divisor), fallbackOrm),
|
|
@@ -1278,7 +1673,7 @@ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false
|
|
|
1278
1673
|
material.maskShadowNode = material.maskNode;
|
|
1279
1674
|
material.depthNode = capturedSurfaceDepth(hit.get("position"));
|
|
1280
1675
|
material.receivedShadowPositionNode = modelWorldMatrix3.mul(
|
|
1281
|
-
|
|
1676
|
+
vec45(hit.get("position"), 1)
|
|
1282
1677
|
).xyz;
|
|
1283
1678
|
material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
|
|
1284
1679
|
return material;
|