@threenative/core 0.3.3 → 0.3.5
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/capabilities.json +2019 -59
- package/dist/{assets-CYKk2WTu.d.ts → assets-CqvE429w.d.ts} +45 -3
- package/dist/{audio-7i3Xl0l3.d.ts → audio-DIJE5HTr.d.ts} +43 -3
- package/dist/{canvas-layer-C1SnMoJ-.d.ts → canvas-layer-C1-5CviZ.d.ts} +5 -1
- package/dist/{game-D_6r-k4Y.d.ts → game-Bj8G1wmD.d.ts} +242 -17
- package/dist/hot.d.ts +5 -5
- package/dist/index.d.ts +1131 -105
- package/dist/index.js +15201 -4782
- package/dist/model-lod-BDAUYgsS.d.ts +197 -0
- package/dist/playtest.d.ts +6 -4
- package/dist/playtest.js +84 -11
- package/dist/react-css.d.ts +62 -0
- package/dist/react-css.js +591 -0
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-Cy4qeBOA.d.ts → renderer-9yEo9hPa.d.ts} +676 -21
- package/dist/terrain-jobs-worker.d.ts +2 -0
- package/dist/terrain-jobs-worker.js +130 -0
- package/dist/ui-layer.d.ts +5 -3
- package/dist/ui-layer.js +59 -8
- package/dist/world.d.ts +1596 -17
- package/dist/world.js +17035 -521
- package/gpl/fixtures/make_world_fixture.py +184 -0
- package/gpl/recipes/_common.py +11 -0
- package/gpl/recipes/decimate.py +11 -5
- package/gpl/recipes/export_world.py +699 -0
- package/mcp/blender-server.mjs +61 -4
- package/mcp/blender.mjs +3 -4
- package/mcp/engine-server.mjs +55 -21
- package/mcp/servers.mjs +4 -4
- package/package.json +15 -9
- package/patches/three@0.185.1-prd269-upgrade.patch +22418 -0
- package/patches/three@0.185.1.patch +8246 -389
- package/scripts/apply-three-patch.mjs +123 -41
- package/src/animation-blend.ts +264 -0
- package/src/animation-composition.ts +362 -0
- package/src/animation-layers.ts +260 -0
- package/src/animation-root-motion.ts +283 -0
- package/src/animation.ts +896 -0
- package/src/assets.ts +1303 -0
- package/src/atmosphere/index.ts +335 -0
- package/src/atmosphere/luts.ts +298 -0
- package/src/atmosphere/params.ts +323 -0
- package/src/audio.ts +912 -0
- package/src/billboard.ts +124 -0
- package/src/bone-lengths.ts +157 -0
- package/src/camera-shake.ts +116 -0
- package/src/canvas-layer.ts +47 -0
- package/src/clip-audit.ts +401 -0
- package/src/clustered-batch.ts +492 -0
- package/src/clustered-mesh.ts +638 -0
- package/src/compute-driven.ts +176 -0
- package/src/compute-timing.ts +318 -0
- package/src/config.ts +593 -0
- package/src/debug.ts +82 -0
- package/src/engine-render-hook.ts +25 -0
- package/src/entities.ts +79 -0
- package/src/entity-snapshot.ts +53 -0
- package/src/flight.ts +683 -0
- package/src/fluid-field.ts +748 -0
- package/src/fluid-particles.ts +1253 -0
- package/src/frame-budget.ts +1368 -0
- package/src/game.ts +2390 -0
- package/src/geometry-capture.ts +1098 -0
- package/src/gpu-class.ts +160 -0
- package/src/gpu-frame-observation.ts +424 -0
- package/src/gpu-readback.ts +143 -0
- package/src/gpu-scene-bvh.ts +464 -0
- package/src/grounding.ts +100 -0
- package/src/hot.ts +161 -0
- package/src/import-meta.d.ts +13 -0
- package/src/index.ts +1536 -0
- package/src/input.ts +690 -0
- package/src/instanced-batch-lod.ts +283 -0
- package/src/instanced-batch.ts +274 -0
- package/src/interpolation.ts +129 -0
- package/src/launch-diagnostics.ts +161 -0
- package/src/loop.ts +564 -0
- package/src/matrix-world.ts +245 -0
- package/src/merge-parts.ts +812 -0
- package/src/model-lod.ts +1315 -0
- package/src/net-protocol.ts +333 -0
- package/src/net-session.ts +477 -0
- package/src/net.ts +80 -0
- package/src/ocean/fft.ts +72 -0
- package/src/ocean/spectral.ts +728 -0
- package/src/particle-significance.ts +102 -0
- package/src/particles.ts +648 -0
- package/src/path-follow.ts +175 -0
- package/src/picking.ts +250 -0
- package/src/pipeline-census.ts +1566 -0
- package/src/platform.ts +210 -0
- package/src/playtest.ts +839 -0
- package/src/pointer-events.ts +411 -0
- package/src/pose-measure.ts +435 -0
- package/src/profiling/FrameCounters.ts +231 -0
- package/src/profiling/Spans.ts +548 -0
- package/src/profiling/opacity-convention.ts +21 -0
- package/src/profiling/render-list-validate.ts +175 -0
- package/src/profiling/render-trap-audit.ts +84 -0
- package/src/profiling/scene-warning.ts +190 -0
- package/src/profiling/span-probes.ts +191 -0
- package/src/profiling/unlit-foliage-warning.ts +149 -0
- package/src/projection-apply.ts +1529 -0
- package/src/projection-marker.ts +142 -0
- package/src/projection-plan.ts +1258 -0
- package/src/projection-skinned.ts +419 -0
- package/src/projection-stability.ts +409 -0
- package/src/projection-uniform.ts +328 -0
- package/src/random.ts +41 -0
- package/src/react-css.ts +604 -0
- package/src/react-glyphs.ts +114 -0
- package/src/react-host.ts +576 -0
- package/src/react-layout.ts +265 -0
- package/src/react.ts +60 -0
- package/src/render/alpha-antialiasing.ts +159 -0
- package/src/render/batched-velocity.ts +126 -0
- package/src/render/chain.ts +1021 -0
- package/src/render/daylight.ts +189 -0
- package/src/render/depth-pyramid.ts +596 -0
- package/src/render/foliage-alpha.ts +166 -0
- package/src/render/material-key.ts +60 -0
- package/src/render/mesh-pool.ts +144 -0
- package/src/render/probe-volume.ts +1668 -0
- package/src/render/resolution-subrect.ts +253 -0
- package/src/render/velocity.ts +442 -0
- package/src/render/virtual-shadow-pages.ts +827 -0
- package/src/render/virtual-shadow-partial.ts +174 -0
- package/src/render/virtual-shadow.ts +3330 -0
- package/src/render/world-impostor-surface.ts +722 -0
- package/src/render/world-impostor.ts +558 -0
- package/src/render-camera-cull.ts +441 -0
- package/src/render-pass-budget.ts +501 -0
- package/src/renderProjection.ts +758 -0
- package/src/renderer-config.ts +127 -0
- package/src/renderer.ts +1847 -0
- package/src/replay-protocol.ts +85 -0
- package/src/replay.ts +205 -0
- package/src/resolution-scaler.ts +819 -0
- package/src/rig-preparation.ts +271 -0
- package/src/ripple-field.ts +432 -0
- package/src/scale.ts +109 -0
- package/src/scene.ts +239 -0
- package/src/schedule.ts +155 -0
- package/src/skeletal-mesh.ts +61 -0
- package/src/skeleton.ts +100 -0
- package/src/snow-field.ts +744 -0
- package/src/softbody-topology.ts +269 -0
- package/src/softbody.ts +516 -0
- package/src/sprite-animator.ts +222 -0
- package/src/startup-readiness.ts +386 -0
- package/src/state.ts +66 -0
- package/src/static-transform.ts +274 -0
- package/src/storage-buffer.ts +253 -0
- package/src/streaming.ts +308 -0
- package/src/target-fps.ts +92 -0
- package/src/terrain-jobs-worker.ts +17 -0
- package/src/terrain-jobs.ts +353 -0
- package/src/textures.ts +39 -0
- package/src/tracers.ts +237 -0
- package/src/ui-bridge.ts +368 -0
- package/src/ui-hit-regions.ts +380 -0
- package/src/ui-layer.ts +83 -0
- package/src/ui-state.ts +220 -0
- package/src/version.ts +2 -0
- package/src/viewport.ts +253 -0
- package/src/warmup.ts +1357 -0
- package/src/water-surface.ts +384 -0
- package/src/wave-field.ts +524 -0
- package/src/world-capabilities.ts +131 -0
- package/src/world-cell-proxy.ts +392 -0
- package/src/world-cells.ts +10564 -0
- package/src/world-gpu-scene.ts +3354 -0
- package/src/world-heightmap.ts +82 -0
- package/src/world-package.ts +469 -0
- package/src/world-passes.ts +582 -0
- package/src/world-region.ts +17 -0
- package/src/world-terrain-splat.ts +564 -0
- package/src/world-tiles.ts +3826 -0
- package/src/world-topology.ts +370 -0
- package/src/world-validate.ts +48 -0
- package/src/world.ts +805 -0
- package/dist/gpu-readback-CMklJs6r.d.ts +0 -112
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Box3,
|
|
3
|
+
BufferGeometry,
|
|
4
|
+
DoubleSide,
|
|
5
|
+
DynamicDrawUsage,
|
|
6
|
+
Float32BufferAttribute,
|
|
7
|
+
type InstancedBufferAttribute,
|
|
8
|
+
InstancedInterleavedBuffer,
|
|
9
|
+
type Material,
|
|
10
|
+
type Object3D,
|
|
11
|
+
Sphere,
|
|
12
|
+
Vector3,
|
|
13
|
+
} from "three";
|
|
14
|
+
import {
|
|
15
|
+
Break,
|
|
16
|
+
Fn,
|
|
17
|
+
If,
|
|
18
|
+
Loop,
|
|
19
|
+
abs,
|
|
20
|
+
attribute,
|
|
21
|
+
cameraPosition,
|
|
22
|
+
cameraProjectionMatrix,
|
|
23
|
+
cameraViewMatrix,
|
|
24
|
+
clamp,
|
|
25
|
+
cross,
|
|
26
|
+
dFdx,
|
|
27
|
+
dFdy,
|
|
28
|
+
dot,
|
|
29
|
+
element,
|
|
30
|
+
float,
|
|
31
|
+
floor,
|
|
32
|
+
instanceIndex,
|
|
33
|
+
instancedBufferAttribute,
|
|
34
|
+
instancedDynamicBufferAttribute,
|
|
35
|
+
length,
|
|
36
|
+
log2,
|
|
37
|
+
mat3,
|
|
38
|
+
mat4,
|
|
39
|
+
max,
|
|
40
|
+
modelWorldMatrix,
|
|
41
|
+
normalize,
|
|
42
|
+
positionGeometry,
|
|
43
|
+
select,
|
|
44
|
+
storage,
|
|
45
|
+
texture,
|
|
46
|
+
transformNormal,
|
|
47
|
+
uniform,
|
|
48
|
+
varyingProperty,
|
|
49
|
+
vec2,
|
|
50
|
+
vec3,
|
|
51
|
+
vec4,
|
|
52
|
+
} from "three/tsl";
|
|
53
|
+
import {
|
|
54
|
+
EventNode,
|
|
55
|
+
MeshBasicNodeMaterial,
|
|
56
|
+
MeshPhysicalNodeMaterial,
|
|
57
|
+
MeshStandardNodeMaterial,
|
|
58
|
+
type Node,
|
|
59
|
+
type NodeMaterial,
|
|
60
|
+
} from "three/webgpu";
|
|
61
|
+
import { IMPOSTOR_VIEW_GRID, type WorldImpostorAtlas } from "./world-impostor.js";
|
|
62
|
+
|
|
63
|
+
/** The surface reconstructs alpha-covered ray/depth crossings in the three neighboring views. */
|
|
64
|
+
export const IMPOSTOR_SURFACE_PARALLAX = true;
|
|
65
|
+
|
|
66
|
+
/** Mip compensation for the impostor cutout, the scattered-foliage constant: 0.25 drops needles. */
|
|
67
|
+
export const IMPOSTOR_MIP_ALPHA_SCALE = 0.75;
|
|
68
|
+
|
|
69
|
+
/** The alpha cutoff a source part gets when it authored none; the same 0.5 the scatter path uses. */
|
|
70
|
+
const DEFAULT_ALPHA_TEST = 0.5;
|
|
71
|
+
/** The positive alpha number the shadow pass copies; see the material setup in `WorldImpostorSurface`. */
|
|
72
|
+
const IMPOSTOR_SHADOW_ALPHA_FLOOR = 1e-4;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The per-instance view-distance attribute a far aggregate writes: one float per placement, the
|
|
76
|
+
* asset's own `cullDistance(maxDistance)` or `Infinity` when it authored none. One shared surface
|
|
77
|
+
* per atlas reads it so two canonical ids over one atlas key keep their own cutoffs instead of the
|
|
78
|
+
* first member's, and the value is static, so the attribute is uploaded once and never per frame.
|
|
79
|
+
*/
|
|
80
|
+
export const IMPOSTOR_FAR_CULL_ATTRIBUTE = "tnFarCull";
|
|
81
|
+
|
|
82
|
+
/** The node classes a GLB surface converts to, exactly as foliage-alpha.ts. */
|
|
83
|
+
const NODE_CLASSES: ReadonlyMap<string, new () => NodeMaterial> = new Map<
|
|
84
|
+
string,
|
|
85
|
+
new () => NodeMaterial
|
|
86
|
+
>([
|
|
87
|
+
["MeshBasicMaterial", MeshBasicNodeMaterial],
|
|
88
|
+
["MeshPhysicalMaterial", MeshPhysicalNodeMaterial],
|
|
89
|
+
["MeshStandardMaterial", MeshStandardNodeMaterial],
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
/** Never carried from the source onto its node twin, exactly as foliage-alpha.ts. */
|
|
93
|
+
const UNSYNCED = new Set(["_listeners", "id", "uuid", "version"]);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Maps a source material authored against the source geometry's UVs. Every one of them refers to a
|
|
97
|
+
* UV layout the two-triangle quad does not have, so each is cleared and albedo/normals come only
|
|
98
|
+
* from the baked atlas.
|
|
99
|
+
*
|
|
100
|
+
* The whole family is here, not the classic set: a physical source's `transmissionMap`, `thicknessMap`
|
|
101
|
+
* and specular/iridescence pair are sampled the same way three samples `map` — through
|
|
102
|
+
* `attribute("uv")` — and a quad with no `uv` at all makes every one of them a
|
|
103
|
+
* `Vertex attribute "uv" not found on geometry` warning on every build of that material, for a sample
|
|
104
|
+
* the sampler was already returning as zero.
|
|
105
|
+
*/
|
|
106
|
+
const GEOMETRY_MAPS = [
|
|
107
|
+
"map",
|
|
108
|
+
"normalMap",
|
|
109
|
+
"roughnessMap",
|
|
110
|
+
"metalnessMap",
|
|
111
|
+
"alphaMap",
|
|
112
|
+
"aoMap",
|
|
113
|
+
"emissiveMap",
|
|
114
|
+
"bumpMap",
|
|
115
|
+
"displacementMap",
|
|
116
|
+
"lightMap",
|
|
117
|
+
"specularMap",
|
|
118
|
+
"clearcoatMap",
|
|
119
|
+
"clearcoatNormalMap",
|
|
120
|
+
"clearcoatRoughnessMap",
|
|
121
|
+
"sheenColorMap",
|
|
122
|
+
"sheenRoughnessMap",
|
|
123
|
+
"iridescenceMap",
|
|
124
|
+
"anisotropyMap",
|
|
125
|
+
"iridescenceThicknessMap",
|
|
126
|
+
"specularColorMap",
|
|
127
|
+
"specularIntensityMap",
|
|
128
|
+
"thicknessMap",
|
|
129
|
+
"transmissionMap",
|
|
130
|
+
] as const;
|
|
131
|
+
|
|
132
|
+
/** One `mat4` node per CPU instance-matrix attribute, derived once and kept live with its source. */
|
|
133
|
+
const INSTANCE_COLUMNS = new WeakMap<InstancedBufferAttribute, InstancedInterleavedBuffer>();
|
|
134
|
+
|
|
135
|
+
/** The options one impostor surface is built with. Colour and normals come from the atlas. */
|
|
136
|
+
export interface IImpostorSurfaceOptions {
|
|
137
|
+
/** The completed bake. Borrowed: this surface never disposes it. */
|
|
138
|
+
readonly atlas: WorldImpostorAtlas;
|
|
139
|
+
/** The baked bounds' centre in asset-local space; see `impostorBounds`. */
|
|
140
|
+
readonly center: Vector3;
|
|
141
|
+
/** The baked bounds' radius in asset-local space. */
|
|
142
|
+
readonly radius: number;
|
|
143
|
+
/** The dominant source material; its class, roughness and metalness set the lit surface. */
|
|
144
|
+
readonly source: Material;
|
|
145
|
+
/** The source's own cutoff, or the scatter default. Kept positive for the shadow alpha caster. */
|
|
146
|
+
readonly alphaTest?: number;
|
|
147
|
+
readonly mipScale?: number;
|
|
148
|
+
/**
|
|
149
|
+
* When `true`, this surface is a whole-map far aggregate: the view-distance gate is read per
|
|
150
|
+
* instance from {@link IMPOSTOR_FAR_CULL_ATTRIBUTE} instead of a uniform, so two canonical assets
|
|
151
|
+
* over the same atlas key each honour their own authored `cullDistance(maxDistance)` rather than
|
|
152
|
+
* borrowing the first member's value. It is a main-pass gate only — an orthographic (shadow)
|
|
153
|
+
* camera never culls through it — so a far caster still reaches the virtual-shadow levels.
|
|
154
|
+
*/
|
|
155
|
+
readonly cull?: boolean;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** A signed one for a component, with zero read as positive so the fold is deterministic. */
|
|
159
|
+
function signNotZeroNode(value: Node<"float">): Node<"float"> {
|
|
160
|
+
return select(value.greaterThanEqual(0), float(1), float(-1));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The baker's stable-up rule as a node: world-up, or `+Z` at the poles so the cross never dies. */
|
|
164
|
+
function frameBasisNode(direction: Node<"vec3">): {
|
|
165
|
+
readonly right: Node<"vec3">;
|
|
166
|
+
readonly up: Node<"vec3">;
|
|
167
|
+
} {
|
|
168
|
+
const hint = select(abs(direction.y).greaterThan(0.999), vec3(0, 0, 1), vec3(0, 1, 0));
|
|
169
|
+
const right = normalize(cross(hint, direction));
|
|
170
|
+
const up = normalize(cross(direction, right));
|
|
171
|
+
return { right, up };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** `octahedralViewOf` as a node: endpoints, then the last octant folded behind the sphere. */
|
|
175
|
+
function octahedralViewNode(
|
|
176
|
+
ix: Node<"float">,
|
|
177
|
+
iy: Node<"float">,
|
|
178
|
+
grid = IMPOSTOR_VIEW_GRID,
|
|
179
|
+
): Node<"vec3"> {
|
|
180
|
+
const n1 = float(grid - 1);
|
|
181
|
+
const u = ix.mul(2).div(n1).sub(1);
|
|
182
|
+
const v = iy.mul(2).div(n1).sub(1);
|
|
183
|
+
const z = float(1).sub(abs(u)).sub(abs(v));
|
|
184
|
+
const folded = vec3(
|
|
185
|
+
float(1).sub(abs(v)).mul(signNotZeroNode(u)),
|
|
186
|
+
float(1).sub(abs(u)).mul(signNotZeroNode(v)),
|
|
187
|
+
z,
|
|
188
|
+
);
|
|
189
|
+
return normalize(select(z.lessThan(0), folded, vec3(u, v, z)));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** `encodeOctahedral` as a node: project a unit direction onto the folded octahedral square. */
|
|
193
|
+
function encodeOctahedralNode(direction: Node<"vec3">): Node<"vec2"> {
|
|
194
|
+
const d = normalize(direction);
|
|
195
|
+
const sum = abs(d.x).add(abs(d.y)).add(abs(d.z));
|
|
196
|
+
const px = d.x.div(sum);
|
|
197
|
+
const py = d.y.div(sum);
|
|
198
|
+
const foldedX = float(1).sub(abs(py)).mul(signNotZeroNode(px));
|
|
199
|
+
const foldedY = float(1).sub(abs(px)).mul(signNotZeroNode(py));
|
|
200
|
+
const front = d.z.greaterThanEqual(0);
|
|
201
|
+
return vec2(select(front, px, foldedX), select(front, py, foldedY));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Golus's mip-aware cutoff, read from the actual atlas UV derivatives of the sampled frames. */
|
|
205
|
+
function mipThresholdNode(
|
|
206
|
+
uvs: readonly Node<"vec2">[],
|
|
207
|
+
pixels: number,
|
|
208
|
+
cutoff: number,
|
|
209
|
+
mipScale: number,
|
|
210
|
+
): Node<"float"> {
|
|
211
|
+
const size = vec2(pixels, pixels);
|
|
212
|
+
let footprint: Node<"float"> | undefined;
|
|
213
|
+
for (const uv of uvs) {
|
|
214
|
+
const frame = max(length(dFdx(uv).mul(size)), length(dFdy(uv).mul(size)));
|
|
215
|
+
footprint = footprint === undefined ? frame : max(footprint, frame);
|
|
216
|
+
}
|
|
217
|
+
const scale = float(1).add(max(log2(footprint as Node<"float">), float(0)).mul(mipScale));
|
|
218
|
+
return float(cutoff).div(scale);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** The projection's `[3][3]`: zero for perspective, one for orthographic (a shadow camera too). */
|
|
222
|
+
function orthographicNode(): Node<"bool"> {
|
|
223
|
+
const column = element(cameraProjectionMatrix as never, 3) as Node<"vec4">;
|
|
224
|
+
return column.w.greaterThan(0.5) as Node<"bool">;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** `mat3` of a `mat4` node; the 0.185 typings only accept the conversion overload directly. */
|
|
228
|
+
function asMat3(matrix: Node<"mat4">): Node<"mat3"> {
|
|
229
|
+
// quality-allow: three types `mat4` and `mat3` nodes as unrelated, so both casts cross via `unknown`.
|
|
230
|
+
return mat3(matrix as unknown as Node<"mat3">) as unknown as Node<"mat3">;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Copy a source instance attribute's ranges and version onto the interleaved buffer a node path
|
|
235
|
+
* derived from it. Without this the derived buffer keeps its first upload and a matrix updated after
|
|
236
|
+
* the first frame never reaches the GPU — `Instance.js` performs the same copy per frame.
|
|
237
|
+
*/
|
|
238
|
+
export function syncInstanceRanges(
|
|
239
|
+
source: InstancedBufferAttribute,
|
|
240
|
+
derived: InstancedInterleavedBuffer,
|
|
241
|
+
): void {
|
|
242
|
+
derived.clearUpdateRanges();
|
|
243
|
+
derived.updateRanges.push(...source.updateRanges);
|
|
244
|
+
if (source.version !== derived.version) derived.version = source.version;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The event that runs {@link syncInstanceRanges} for one instance attribute: once per frame, the
|
|
249
|
+
* type three's own `Instance.js` sync uses (`OnFrameUpdate`, not re-exported, so built directly).
|
|
250
|
+
* Never OBJECT: world batches are `static`, and three skips a settled static object's OBJECT
|
|
251
|
+
* updates, so an OBJECT sync never ran and the GPU kept the batch's first, zeroed upload.
|
|
252
|
+
*/
|
|
253
|
+
export function instanceSyncEvent(
|
|
254
|
+
source: InstancedBufferAttribute,
|
|
255
|
+
derived: InstancedInterleavedBuffer,
|
|
256
|
+
): EventNode {
|
|
257
|
+
return new EventNode(EventNode.FRAME as ConstructorParameters<typeof EventNode>[0], () =>
|
|
258
|
+
syncInstanceRanges(source, derived),
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* The per-instance matrix as a `mat4` node, choosing the path by the attribute three installed.
|
|
264
|
+
*
|
|
265
|
+
* A `StorageInstancedBufferAttribute` is read through `storage(...).element(instanceIndex)`, which
|
|
266
|
+
* is the same seam three's own `Instance.js` uses and which includes the indirect draw's
|
|
267
|
+
* `firstInstance`. A plain CPU `InstancedBufferAttribute` cannot require storage support, so it is
|
|
268
|
+
* read as the same four `instancedBufferAttribute` columns of an `InstancedInterleavedBuffer` over
|
|
269
|
+
* its own array, and a per-frame update copies the attribute's ranges and version onto that
|
|
270
|
+
* derived buffer exactly as `Instance.js` does — a derived buffer nobody re-uploads draws a stale
|
|
271
|
+
* pose. See {@link instanceSyncEvent}.
|
|
272
|
+
*/
|
|
273
|
+
function instanceMatrixNode(attribute: InstancedBufferAttribute): Node<"mat4"> {
|
|
274
|
+
const count = Math.max(attribute.count, 1);
|
|
275
|
+
if (
|
|
276
|
+
(attribute as { isStorageInstancedBufferAttribute?: boolean })
|
|
277
|
+
.isStorageInstancedBufferAttribute === true
|
|
278
|
+
)
|
|
279
|
+
return storage(attribute as never, "mat4", count).element(instanceIndex) as Node<"mat4">;
|
|
280
|
+
let interleaved = INSTANCE_COLUMNS.get(attribute);
|
|
281
|
+
if (interleaved === undefined) {
|
|
282
|
+
interleaved = new InstancedInterleavedBuffer(attribute.array, 16, 1);
|
|
283
|
+
INSTANCE_COLUMNS.set(attribute, interleaved);
|
|
284
|
+
}
|
|
285
|
+
const bufferFn =
|
|
286
|
+
attribute.usage === DynamicDrawUsage
|
|
287
|
+
? instancedDynamicBufferAttribute
|
|
288
|
+
: instancedBufferAttribute;
|
|
289
|
+
const columns = [
|
|
290
|
+
bufferFn(interleaved, "vec4", 16, 0),
|
|
291
|
+
bufferFn(interleaved, "vec4", 16, 4),
|
|
292
|
+
bufferFn(interleaved, "vec4", 16, 8),
|
|
293
|
+
bufferFn(interleaved, "vec4", 16, 12),
|
|
294
|
+
];
|
|
295
|
+
instanceSyncEvent(attribute, interleaved).toStack();
|
|
296
|
+
return mat4(
|
|
297
|
+
columns[0] as Node<"vec4">,
|
|
298
|
+
columns[1] as Node<"vec4">,
|
|
299
|
+
columns[2] as Node<"vec4">,
|
|
300
|
+
columns[3] as Node<"vec4">,
|
|
301
|
+
) as Node<"mat4">;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** `cameraViewMatrix * modelWorldMatrix * instanceMatrix`: asset-space to view-space per instance. */
|
|
305
|
+
function instanceViewMatrixNode(object: Object3D): Node<"mat4"> {
|
|
306
|
+
const attribute = (object as { instanceMatrix?: InstancedBufferAttribute }).instanceMatrix;
|
|
307
|
+
const instance = attribute === undefined ? identityMat4() : instanceMatrixNode(attribute);
|
|
308
|
+
return cameraViewMatrix.mul(modelWorldMatrix).mul(instance) as Node<"mat4">;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** A `mat4` identity node for a plain, non-instanced mesh. */
|
|
312
|
+
function identityMat4(): Node<"mat4"> {
|
|
313
|
+
return mat4(
|
|
314
|
+
vec4(1, 0, 0, 0),
|
|
315
|
+
vec4(0, 1, 0, 0),
|
|
316
|
+
vec4(0, 0, 1, 0),
|
|
317
|
+
vec4(0, 0, 0, 1),
|
|
318
|
+
) as Node<"mat4">;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** A node twin of a source material: its class and every value it authored, by reference. */
|
|
322
|
+
function cloneLitMaterial(source: Material): NodeMaterial {
|
|
323
|
+
if (Reflect.get(source, "isNodeMaterial") === true) return (source as NodeMaterial).clone();
|
|
324
|
+
const nodeClass = NODE_CLASSES.get(source.type);
|
|
325
|
+
if (nodeClass === undefined)
|
|
326
|
+
throw new Error(`WorldImpostorSurface cannot convert material type '${source.type}'.`);
|
|
327
|
+
const twin = new nodeClass();
|
|
328
|
+
for (const key in source)
|
|
329
|
+
if (!UNSYNCED.has(key)) Reflect.set(twin, key, Reflect.get(source, key));
|
|
330
|
+
return twin;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* The final LOD of one baked asset: a two-triangle, camera-facing quad that samples and blends the
|
|
335
|
+
* octahedral atlas the baker produced.
|
|
336
|
+
*
|
|
337
|
+
* The quad stays lit and carries the source's own scalar surface values. Albedo and normals are the
|
|
338
|
+
* baked ones, unpremultiplied by their coverage before shading; the source's UV maps are cleared
|
|
339
|
+
* because none of them describe the quad's UVs. The cutout is a mip-aware threshold so the
|
|
340
|
+
* blended coverage survives distance, while `alphaTest` stays a positive number for the
|
|
341
|
+
* virtual-shadow caster contract.
|
|
342
|
+
*/
|
|
343
|
+
export class WorldImpostorSurface {
|
|
344
|
+
readonly geometry: BufferGeometry;
|
|
345
|
+
readonly material: NodeMaterial;
|
|
346
|
+
/** Two triangles, always: the shape is the point of the final LOD. */
|
|
347
|
+
readonly quads = 1;
|
|
348
|
+
/** Whether this surface reads the per-instance far cull attribute; see the option. */
|
|
349
|
+
readonly cull: boolean;
|
|
350
|
+
#disposed = false;
|
|
351
|
+
|
|
352
|
+
constructor(options: IImpostorSurfaceOptions) {
|
|
353
|
+
const atlas = options.atlas;
|
|
354
|
+
const radius = Math.max(options.radius, 1e-4);
|
|
355
|
+
const cutoff = Math.max(
|
|
356
|
+
options.alphaTest ??
|
|
357
|
+
(options.source.alphaTest > 0 ? options.source.alphaTest : DEFAULT_ALPHA_TEST),
|
|
358
|
+
1e-4,
|
|
359
|
+
);
|
|
360
|
+
const mipScale = options.mipScale ?? IMPOSTOR_MIP_ALPHA_SCALE;
|
|
361
|
+
const center = options.center.clone();
|
|
362
|
+
if (atlas.color.image === null && atlas.normal.image === null)
|
|
363
|
+
throw new Error("WorldImpostorSurface requires a baked atlas.");
|
|
364
|
+
const material = cloneLitMaterial(options.source);
|
|
365
|
+
for (const key of GEOMETRY_MAPS) Reflect.set(material, key, null);
|
|
366
|
+
// Albedo and coverage are baked into the atlas; a vertex-colour attribute the quad does not
|
|
367
|
+
// have, an opacity multiplier or an opacity node would apply the source's own colour and
|
|
368
|
+
// coverage a second time on top of the baked one.
|
|
369
|
+
material.vertexColors = false;
|
|
370
|
+
material.opacity = 1;
|
|
371
|
+
material.opacityNode = null;
|
|
372
|
+
material.transparent = false;
|
|
373
|
+
material.depthWrite = true;
|
|
374
|
+
material.depthTest = true;
|
|
375
|
+
material.side = DoubleSide;
|
|
376
|
+
// A positive number, never the cutoff: three's shadow pass copies this number as its own cutoff
|
|
377
|
+
// and ignores `alphaTestNode`, and a shadow-map texel is metres wide, so the blended coverage it
|
|
378
|
+
// reads is far below the authored 0.5 everywhere and every fragment would be discarded. The
|
|
379
|
+
// discard that matters runs through `maskShadowNode` below, with the same mip-aware threshold the
|
|
380
|
+
// main pass uses; this floor only drops empty space.
|
|
381
|
+
material.alphaTest = IMPOSTOR_SHADOW_ALPHA_FLOOR;
|
|
382
|
+
material.userData = { ...material.userData, tnWholeAssetImpostor: true };
|
|
383
|
+
material.needsUpdate = true;
|
|
384
|
+
|
|
385
|
+
const centerUniform = uniform(center);
|
|
386
|
+
const radiusUniform = uniform(radius);
|
|
387
|
+
const cull = options.cull === true;
|
|
388
|
+
const name = `WorldImpostorSurface${surfaceId}`;
|
|
389
|
+
surfaceId += 1;
|
|
390
|
+
const relVarying = varyingProperty("vec3", `${name}Rel`);
|
|
391
|
+
const dirVarying = varyingProperty("vec3", `${name}Dir`);
|
|
392
|
+
|
|
393
|
+
const positionNode = Fn((_inputs: unknown, builder: { readonly object: Object3D }) => {
|
|
394
|
+
const object = builder.object;
|
|
395
|
+
const instance = userInstanceMatrix(object);
|
|
396
|
+
const view = cameraViewMatrix.mul(modelWorldMatrix).mul(instance);
|
|
397
|
+
const centerView = view.mul(vec4(centerUniform, 1)).xyz;
|
|
398
|
+
// The authored cull contract in `world-gpu-scene.ts` `cullAndSelect` measures horizontal
|
|
399
|
+
// world-XZ distance from the camera, not Euclidean view distance: an elevated camera would
|
|
400
|
+
// otherwise cull trees still in range horizontally. The centre is the same
|
|
401
|
+
// `modelWorldMatrix * instance * center` the contract stores as `placement.centre`.
|
|
402
|
+
const centerWorld = modelWorldMatrix.mul(instance).mul(vec4(centerUniform, 1)).xyz;
|
|
403
|
+
const farDistance = length(
|
|
404
|
+
vec2(centerWorld.x.sub(cameraPosition.x), centerWorld.z.sub(cameraPosition.z)),
|
|
405
|
+
);
|
|
406
|
+
// Perspective looks from the instance centre toward the eye; orthographic (and therefore a
|
|
407
|
+
// shadow camera) is a fixed forward, so one surface serves the main and shadow passes.
|
|
408
|
+
const eyeView = select(orthographicNode(), vec3(0, 0, 1), normalize(centerView.negate()));
|
|
409
|
+
const dAsset = normalize(asMat3(view).inverse().mul(eyeView));
|
|
410
|
+
const basis = frameBasisNode(dAsset);
|
|
411
|
+
const corner = positionGeometry.xy;
|
|
412
|
+
const rel = basis.right.mul(corner.x).add(basis.up.mul(corner.y)).mul(radiusUniform);
|
|
413
|
+
// A main-pass horizontal-distance gate: when the instance centre is past the asset's own cull
|
|
414
|
+
// the quad collapses to a point, which the rasteriser drops. The cull is a per-instance
|
|
415
|
+
// attribute, so one surface serves every canonical asset over this atlas each at its own
|
|
416
|
+
// authored distance. An orthographic camera (a shadow level) is exempt, so the far half still
|
|
417
|
+
// casts.
|
|
418
|
+
const gated = cull
|
|
419
|
+
? rel.mul(
|
|
420
|
+
select(
|
|
421
|
+
orthographicNode(),
|
|
422
|
+
float(1),
|
|
423
|
+
select(
|
|
424
|
+
farDistance.lessThanEqual(
|
|
425
|
+
attribute(IMPOSTOR_FAR_CULL_ATTRIBUTE, "float") as Node<"float">,
|
|
426
|
+
),
|
|
427
|
+
float(1),
|
|
428
|
+
float(0),
|
|
429
|
+
),
|
|
430
|
+
),
|
|
431
|
+
)
|
|
432
|
+
: rel;
|
|
433
|
+
relVarying.assign(gated);
|
|
434
|
+
dirVarying.assign(
|
|
435
|
+
select(orthographicNode(), dAsset, asMat3(view).inverse().mul(centerView.negate())),
|
|
436
|
+
);
|
|
437
|
+
return instance.mul(vec4(centerUniform.add(gated), 1)).xyz;
|
|
438
|
+
})();
|
|
439
|
+
|
|
440
|
+
const fragment = buildFragmentNodes(
|
|
441
|
+
atlas,
|
|
442
|
+
centerUniform,
|
|
443
|
+
radiusUniform,
|
|
444
|
+
cutoff,
|
|
445
|
+
mipScale,
|
|
446
|
+
relVarying,
|
|
447
|
+
dirVarying,
|
|
448
|
+
);
|
|
449
|
+
material.colorNode = vec4(fragment.albedo, fragment.coverage);
|
|
450
|
+
material.alphaTestNode = fragment.threshold;
|
|
451
|
+
material.maskShadowNode = fragment.coverage.greaterThan(fragment.threshold);
|
|
452
|
+
material.normalNode = Fn((_inputs: unknown, builder: { readonly object: Object3D }) =>
|
|
453
|
+
transformNormal(fragment.normalAsset, instanceViewMatrixNode(builder.object)),
|
|
454
|
+
)();
|
|
455
|
+
material.positionNode = positionNode;
|
|
456
|
+
|
|
457
|
+
const geometry = new BufferGeometry();
|
|
458
|
+
geometry.setAttribute(
|
|
459
|
+
"position",
|
|
460
|
+
new Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0], 3),
|
|
461
|
+
);
|
|
462
|
+
geometry.setIndex([0, 1, 2, 0, 2, 3]);
|
|
463
|
+
// Authored, not measured: the vertex shader relocates every corner per instance, so the literal
|
|
464
|
+
// quad bounds cannot bound the draw. The baked sphere is the conservative shape to cull against.
|
|
465
|
+
geometry.boundingBox = new Box3(
|
|
466
|
+
center.clone().subScalar(radius),
|
|
467
|
+
center.clone().addScalar(radius),
|
|
468
|
+
);
|
|
469
|
+
geometry.boundingSphere = new Sphere(center.clone(), radius);
|
|
470
|
+
this.geometry = geometry;
|
|
471
|
+
this.material = material;
|
|
472
|
+
this.cull = cull;
|
|
473
|
+
// Readable metadata for diagnostics and tests: a far surface reads the per-instance cull
|
|
474
|
+
// attribute, so a shared-atlas cohort cannot silently borrow a member's maximum.
|
|
475
|
+
if (cull) material.userData.tnFarCull = IMPOSTOR_FAR_CULL_ATTRIBUTE;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** Dispose the owned geometry and the material twin; the borrowed atlas is never touched. */
|
|
479
|
+
dispose(): void {
|
|
480
|
+
if (this.#disposed) return;
|
|
481
|
+
this.#disposed = true;
|
|
482
|
+
this.geometry.dispose();
|
|
483
|
+
this.material.dispose();
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
let surfaceId = 0;
|
|
488
|
+
|
|
489
|
+
/** The attribute node for the position pass; a plain `Mesh` falls back to the identity. */
|
|
490
|
+
function userInstanceMatrix(object: Object3D): Node<"mat4"> {
|
|
491
|
+
const attribute = (object as { instanceMatrix?: InstancedBufferAttribute }).instanceMatrix;
|
|
492
|
+
return attribute === undefined ? identityMat4() : instanceMatrixNode(attribute);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
interface IFragmentNodes {
|
|
496
|
+
readonly albedo: Node<"vec3">;
|
|
497
|
+
readonly coverage: Node<"float">;
|
|
498
|
+
readonly normalAsset: Node<"vec3">;
|
|
499
|
+
readonly threshold: Node<"float">;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/** Build the fragment graph once and hand the same node objects to colour, normal and alpha test. */
|
|
503
|
+
function buildFragmentNodes(
|
|
504
|
+
atlas: WorldImpostorAtlas,
|
|
505
|
+
centerUniform: Node<"vec3">,
|
|
506
|
+
radiusUniform: Node<"float">,
|
|
507
|
+
cutoff: number,
|
|
508
|
+
mipScale: number,
|
|
509
|
+
relVarying: Node<"vec3">,
|
|
510
|
+
dirVarying: Node<"vec3">,
|
|
511
|
+
): IFragmentNodes {
|
|
512
|
+
void centerUniform;
|
|
513
|
+
const grid = IMPOSTOR_VIEW_GRID;
|
|
514
|
+
const n2 = float(grid - 2);
|
|
515
|
+
const p = encodeOctahedralNode(dirVarying);
|
|
516
|
+
const gx = p.x
|
|
517
|
+
.mul(0.5)
|
|
518
|
+
.add(0.5)
|
|
519
|
+
.mul(float(grid - 1));
|
|
520
|
+
const gy = p.y
|
|
521
|
+
.mul(0.5)
|
|
522
|
+
.add(0.5)
|
|
523
|
+
.mul(float(grid - 1));
|
|
524
|
+
const cx = clamp(floor(gx), float(0), n2);
|
|
525
|
+
const cy = clamp(floor(gy), float(0), n2);
|
|
526
|
+
const fx = gx.sub(cx);
|
|
527
|
+
const fy = gy.sub(cy);
|
|
528
|
+
const useLower = fx.add(fy).lessThanEqual(1);
|
|
529
|
+
|
|
530
|
+
const cellX = [
|
|
531
|
+
select(useLower, cx, cx.add(1)),
|
|
532
|
+
select(useLower, cx.add(1), cx),
|
|
533
|
+
select(useLower, cx, cx.add(1)),
|
|
534
|
+
];
|
|
535
|
+
const cellY = [
|
|
536
|
+
select(useLower, cy, cy.add(1)),
|
|
537
|
+
select(useLower, cy, cy.add(1)),
|
|
538
|
+
select(useLower, cy.add(1), cy),
|
|
539
|
+
];
|
|
540
|
+
const weights = [
|
|
541
|
+
select(useLower, float(1).sub(fx).sub(fy), fx.add(fy).sub(1)),
|
|
542
|
+
select(useLower, fx, float(1).sub(fx)),
|
|
543
|
+
select(useLower, fy, float(1).sub(fy)),
|
|
544
|
+
];
|
|
545
|
+
|
|
546
|
+
const colorMap = texture(atlas.color);
|
|
547
|
+
const normalMap = texture(atlas.normal);
|
|
548
|
+
const uvs: Node<"vec2">[] = [];
|
|
549
|
+
const colors: Node<"vec4">[] = [];
|
|
550
|
+
const normals: Node<"vec4">[] = [];
|
|
551
|
+
for (let index = 0; index < 3; index += 1) {
|
|
552
|
+
const direction = octahedralViewNode(
|
|
553
|
+
cellX[index] as Node<"float">,
|
|
554
|
+
cellY[index] as Node<"float">,
|
|
555
|
+
grid,
|
|
556
|
+
);
|
|
557
|
+
const basis = frameBasisNode(direction);
|
|
558
|
+
// Row 0 of a three render target is the top of the frame the bake drew, so `up` runs toward v=0;
|
|
559
|
+
// see `impostorFrameUv`.
|
|
560
|
+
const ray = normalize(select(orthographicNode(), dirVarying, dirVarying.sub(relVarying)));
|
|
561
|
+
const layer = (cellY[index] as Node<"float">)
|
|
562
|
+
.mul(grid)
|
|
563
|
+
.add(cellX[index] as Node<"float">)
|
|
564
|
+
.toInt();
|
|
565
|
+
// A crossing must remain covered throughout refinement; alpha holes and depth jumps
|
|
566
|
+
// cannot create an interior surface. One march step per baked texel, eight refinements.
|
|
567
|
+
const hit = Fn(() => {
|
|
568
|
+
const found = float(0).toVar();
|
|
569
|
+
const hitUv = vec2(2).toVar();
|
|
570
|
+
const previousGap = float(0).toVar();
|
|
571
|
+
const previousAlpha = float(0).toVar();
|
|
572
|
+
const previousT = radiusUniform.toVar();
|
|
573
|
+
Loop({ start: 0, end: atlas.pixels + 1, type: "int" }, ({ i }) => {
|
|
574
|
+
const t = float(1).sub(float(i).div(atlas.pixels).mul(2)).mul(radiusUniform);
|
|
575
|
+
const point = relVarying.add(ray.mul(t));
|
|
576
|
+
const sampleUv = vec2(dot(point, basis.right), dot(point, basis.up).negate())
|
|
577
|
+
.div(radiusUniform)
|
|
578
|
+
.mul(0.5)
|
|
579
|
+
.add(0.5);
|
|
580
|
+
const c = colorMap.depth(layer).sample(sampleUv).level(float(0)).toVar();
|
|
581
|
+
const n = normalMap.depth(layer).sample(sampleUv).level(float(0)).toVar();
|
|
582
|
+
const depth = float(1)
|
|
583
|
+
.sub(
|
|
584
|
+
float(1)
|
|
585
|
+
.sub(n.a)
|
|
586
|
+
.div(max(c.a, float(0.001)))
|
|
587
|
+
.mul(2),
|
|
588
|
+
)
|
|
589
|
+
.mul(radiusUniform);
|
|
590
|
+
const gap = dot(point, direction).sub(depth).toVar();
|
|
591
|
+
If(
|
|
592
|
+
found
|
|
593
|
+
.equal(0)
|
|
594
|
+
.and(previousAlpha.greaterThan(cutoff))
|
|
595
|
+
.and(c.a.greaterThan(cutoff))
|
|
596
|
+
.and(previousGap.greaterThan(0))
|
|
597
|
+
.and(gap.lessThanEqual(0)),
|
|
598
|
+
() => {
|
|
599
|
+
const front = previousT.toVar();
|
|
600
|
+
const back = t.toVar();
|
|
601
|
+
const valid = float(1).toVar();
|
|
602
|
+
Loop({ start: 0, end: 8, type: "int" }, () => {
|
|
603
|
+
const mid = front.add(back).mul(0.5).toVar();
|
|
604
|
+
const p = relVarying.add(ray.mul(mid));
|
|
605
|
+
const u = vec2(dot(p, basis.right), dot(p, basis.up).negate())
|
|
606
|
+
.div(radiusUniform)
|
|
607
|
+
.mul(0.5)
|
|
608
|
+
.add(0.5);
|
|
609
|
+
const mc = colorMap.depth(layer).sample(u).level(float(0)).toVar();
|
|
610
|
+
const mn = normalMap.depth(layer).sample(u).level(float(0)).toVar();
|
|
611
|
+
const md = float(1)
|
|
612
|
+
.sub(
|
|
613
|
+
float(1)
|
|
614
|
+
.sub(mn.a)
|
|
615
|
+
.div(max(mc.a, float(0.001)))
|
|
616
|
+
.mul(2),
|
|
617
|
+
)
|
|
618
|
+
.mul(radiusUniform);
|
|
619
|
+
const mg = dot(p, direction).sub(md);
|
|
620
|
+
If(mc.a.lessThanEqual(cutoff), () => {
|
|
621
|
+
valid.assign(0);
|
|
622
|
+
});
|
|
623
|
+
If(mg.greaterThan(0), () => {
|
|
624
|
+
front.assign(mid);
|
|
625
|
+
}).Else(() => {
|
|
626
|
+
back.assign(mid);
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
const p = relVarying.add(ray.mul(front.add(back).mul(0.5)));
|
|
630
|
+
const u = vec2(dot(p, basis.right), dot(p, basis.up).negate())
|
|
631
|
+
.div(radiusUniform)
|
|
632
|
+
.mul(0.5)
|
|
633
|
+
.add(0.5);
|
|
634
|
+
const hc = colorMap.depth(layer).sample(u).level(float(0)).toVar();
|
|
635
|
+
const hn = normalMap.depth(layer).sample(u).level(float(0)).toVar();
|
|
636
|
+
const hd = float(1)
|
|
637
|
+
.sub(
|
|
638
|
+
float(1)
|
|
639
|
+
.sub(hn.a)
|
|
640
|
+
.div(max(hc.a, float(0.001)))
|
|
641
|
+
.mul(2),
|
|
642
|
+
)
|
|
643
|
+
.mul(radiusUniform);
|
|
644
|
+
If(
|
|
645
|
+
valid
|
|
646
|
+
.equal(1)
|
|
647
|
+
.and(hc.a.greaterThan(cutoff))
|
|
648
|
+
.and(
|
|
649
|
+
abs(dot(p, direction).sub(hd)).lessThanEqual(
|
|
650
|
+
radiusUniform.mul(2 / 255).div(max(hc.a, float(0.001))),
|
|
651
|
+
),
|
|
652
|
+
),
|
|
653
|
+
() => {
|
|
654
|
+
hitUv.assign(u);
|
|
655
|
+
found.assign(1);
|
|
656
|
+
},
|
|
657
|
+
);
|
|
658
|
+
},
|
|
659
|
+
);
|
|
660
|
+
previousGap.assign(gap);
|
|
661
|
+
previousAlpha.assign(c.a);
|
|
662
|
+
previousT.assign(t);
|
|
663
|
+
// The first valid crossing owns the result; later samples cannot change it.
|
|
664
|
+
If(found.equal(1), () => {
|
|
665
|
+
Break();
|
|
666
|
+
});
|
|
667
|
+
});
|
|
668
|
+
return vec3(hitUv, found);
|
|
669
|
+
})();
|
|
670
|
+
uvs.push(hit.xy);
|
|
671
|
+
colors.push(colorMap.depth(layer).sample(hit.xy).mul(hit.z).toVar() as Node<"vec4">);
|
|
672
|
+
normals.push(normalMap.depth(layer).sample(hit.xy).mul(hit.z).toVar() as Node<"vec4">);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
const [w0, w1, w2] = weights as [Node<"float">, Node<"float">, Node<"float">];
|
|
676
|
+
const [c0, c1, c2] = colors as [Node<"vec4">, Node<"vec4">, Node<"vec4">];
|
|
677
|
+
const [n0, n1, n2v] = normals as [Node<"vec4">, Node<"vec4">, Node<"vec4">];
|
|
678
|
+
const coverage = c0.a.mul(w0).add(c1.a.mul(w1)).add(c2.a.mul(w2));
|
|
679
|
+
const safe = max(coverage, float(1e-4));
|
|
680
|
+
const albedo = c0.rgb.mul(w0).add(c1.rgb.mul(w1)).add(c2.rgb.mul(w2)).div(safe);
|
|
681
|
+
const encodedNormal = n0.rgb.mul(w0).add(n1.rgb.mul(w1)).add(n2v.rgb.mul(w2)).div(safe);
|
|
682
|
+
const normalAsset = normalize(encodedNormal.mul(2).sub(1));
|
|
683
|
+
return {
|
|
684
|
+
albedo,
|
|
685
|
+
coverage,
|
|
686
|
+
normalAsset,
|
|
687
|
+
threshold: mipThresholdNode(uvs, atlas.pixels, cutoff, mipScale),
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* The frame basis the baker's orthographic camera actually built for `direction`: the three
|
|
693
|
+
* endpoints of a view's own `lookAt`, so the sampler's projection and the bake share one basis.
|
|
694
|
+
*/
|
|
695
|
+
export function impostorFrameBasis(direction: Vector3): {
|
|
696
|
+
readonly right: Vector3;
|
|
697
|
+
readonly up: Vector3;
|
|
698
|
+
} {
|
|
699
|
+
const d = direction.clone().normalize();
|
|
700
|
+
const hint = Math.abs(d.y) > 0.999 ? new Vector3(0, 0, 1) : new Vector3(0, 1, 0);
|
|
701
|
+
const right = new Vector3().crossVectors(hint, d).normalize();
|
|
702
|
+
const up = new Vector3().crossVectors(d, right).normalize();
|
|
703
|
+
return { right, up };
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* The UV a baked frame gives one asset-local offset, as the surface shader projects it.
|
|
708
|
+
*
|
|
709
|
+
* `v` runs downward: a three render target's row 0 is the top of the frame its camera drew (WebGPU
|
|
710
|
+
* samples it unflipped, and WebGL's render-target flip lands the same place), so the GL-style
|
|
711
|
+
* `0.5 + y/2` reads the frame upside down.
|
|
712
|
+
*/
|
|
713
|
+
export function impostorFrameUv(
|
|
714
|
+
relative: Vector3,
|
|
715
|
+
basis: { readonly right: Vector3; readonly up: Vector3 },
|
|
716
|
+
radius: number,
|
|
717
|
+
): { readonly u: number; readonly v: number } {
|
|
718
|
+
return {
|
|
719
|
+
u: (relative.dot(basis.right) / radius) * 0.5 + 0.5,
|
|
720
|
+
v: 0.5 - (relative.dot(basis.up) / radius) * 0.5,
|
|
721
|
+
};
|
|
722
|
+
}
|