@threenative/core 0.3.6 → 0.3.8
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/README.md +3 -5
- package/capabilities.json +739 -50
- package/dist/{assets-CqvE429w.d.ts → assets-YUbXa3-Y.d.ts} +101 -2
- package/dist/{canvas-layer-C1-5CviZ.d.ts → canvas-layer-vCtanIeB.d.ts} +1 -1
- package/dist/{game-Jz4Fdoeb.d.ts → game-cb99J-YE.d.ts} +100 -31
- package/dist/hot.d.ts +4 -4
- package/dist/hot.js +23 -5
- package/dist/index.d.ts +1742 -29
- package/dist/index.js +22443 -12423
- package/dist/{model-lod-BDAUYgsS.d.ts → model-lod-CTpZ6imz.d.ts} +1 -1
- package/dist/net.js +39 -12
- package/dist/playtest.d.ts +4 -4
- package/dist/playtest.js +48 -9
- package/dist/react-css.js +23 -0
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-9yEo9hPa.d.ts → renderer-rZm0hVqq.d.ts} +178 -5
- package/dist/ui-layer.d.ts +11 -2
- package/dist/ui-layer.js +76 -4
- package/dist/worker.d.ts +36 -0
- package/dist/worker.js +1088 -0
- package/dist/world.d.ts +11 -3
- package/dist/world.js +1117 -197
- package/mcp/animation-server.mjs +265 -0
- package/mcp/animation.mjs +22 -0
- package/mcp/call.mjs +105 -0
- package/mcp/devtools.mjs +5 -0
- package/mcp/engine-server.mjs +713 -37
- package/mcp/launch.mjs +3 -2
- package/mcp/servers.d.mts +2 -0
- package/mcp/servers.mjs +33 -8
- package/package.json +12 -8
- package/patches/three@0.185.1-prd269-upgrade.patch +308 -0
- package/patches/three@0.185.1.patch +481 -272
- package/src/animation-packet-state.ts +88 -0
- package/src/animation-pose-packet.ts +537 -0
- package/src/animation.ts +779 -31
- package/src/asset-cache.ts +719 -0
- package/src/assets.ts +521 -152
- package/src/audio.ts +29 -3
- package/src/auto-batch.ts +792 -0
- package/src/config.ts +2 -0
- package/src/frame-budget.ts +268 -2
- package/src/game.ts +525 -105
- package/src/grounding.ts +7 -6
- package/src/hot.ts +19 -5
- package/src/index.ts +381 -6
- package/src/input.ts +21 -2
- package/src/interpolation.ts +19 -1
- package/src/launch-diagnostics.ts +7 -2
- package/src/look/ao-multibounce.ts +29 -0
- package/src/look/ao-reach.ts +39 -0
- package/src/look/auto-exposure.ts +264 -0
- package/src/look/default-tone-mapping.ts +44 -0
- package/src/look/exposure-graph.ts +208 -0
- package/src/look/exposure-readback.ts +97 -0
- package/src/look/exposure-settings.ts +132 -0
- package/src/look/filmic-tone-mapping.ts +852 -0
- package/src/look/gi-registry.ts +55 -0
- package/src/look/local-exposure.ts +507 -0
- package/src/look/look-diagnostics.ts +24 -0
- package/src/look/motion-blur.ts +42 -0
- package/src/look/quality.ts +309 -0
- package/src/look/reflection-trace.ts +344 -0
- package/src/look/unreal-bloom.ts +295 -0
- package/src/look/vignette.ts +14 -0
- package/src/look/world-environment.ts +2033 -0
- package/src/loop.ts +25 -1
- package/src/merge-parts.ts +25 -10
- package/src/model-lod.ts +14 -0
- package/src/net-session.ts +58 -14
- package/src/ocean/spectral.ts +41 -7
- package/src/picking.ts +72 -5
- package/src/platform.ts +29 -1
- package/src/playtest.ts +54 -8
- package/src/profiling/FrameCounters.ts +166 -5
- package/src/profiling/FrameStages.ts +289 -0
- package/src/profiling/Spans.ts +39 -25
- package/src/profiling/debug-telemetry.ts +312 -0
- package/src/profiling/scene-warning.ts +42 -6
- package/src/projection-apply.ts +11 -3
- package/src/projection-content.ts +189 -0
- package/src/projection-marker.ts +6 -2
- package/src/projection-plan.ts +279 -28
- package/src/projection-skinned.ts +620 -5
- package/src/projection-stability.ts +16 -0
- package/src/projection-uniform.ts +5 -1
- package/src/quiet-markers.ts +56 -0
- package/src/render/budget-ladder.ts +256 -0
- package/src/render/chain.ts +89 -7
- package/src/render/exposure-units.ts +59 -0
- package/src/render/frame-time-budget.ts +162 -0
- package/src/render/gi-budget.ts +231 -0
- package/src/render/gi-visualize.ts +49 -0
- package/src/render/material-key.ts +1 -0
- package/src/render/probe-volume.ts +196 -36
- package/src/render/rough-diffuse.ts +177 -0
- package/src/render/scene-trace.ts +532 -0
- package/src/render/shadow-filter-budget.ts +237 -0
- package/src/render/sky-capture.ts +194 -0
- package/src/render/soft-shadow.ts +607 -0
- package/src/render/surface-cache.ts +418 -0
- package/src/render/traced-reflections.ts +408 -0
- package/src/render/two-sided-foliage.ts +120 -0
- package/src/render/virtual-shadow-pages.ts +112 -12
- package/src/render/virtual-shadow.ts +386 -38
- package/src/render-camera-cull.ts +536 -31
- package/src/render-pass-budget.ts +140 -22
- package/src/renderProjection.ts +65 -9
- package/src/renderer-config.ts +3 -2
- package/src/renderer.ts +289 -14
- package/src/resolution-scaler.ts +65 -7
- package/src/skeletal-mesh.ts +4 -0
- package/src/snow-field.ts +46 -0
- package/src/softbody.ts +6 -5
- package/src/startup-offscreen-bytes.ts +168 -0
- package/src/startup-readiness.ts +137 -2
- package/src/ui-bridge.ts +38 -0
- package/src/ui-hit-regions.ts +6 -2
- package/src/ui-layer.ts +1 -0
- package/src/ui-state.ts +32 -3
- package/src/version.ts +1 -1
- package/src/viewport.ts +8 -5
- package/src/warmup.ts +76 -10
- package/src/worker-audio.ts +579 -0
- package/src/worker.ts +842 -0
- package/src/world-cells.ts +4 -1
- package/gpl/LICENSE.GPL +0 -117
- package/gpl/convert.py +0 -192
- package/gpl/fixtures/make_world_fixture.py +0 -184
- package/gpl/recipes/_common.py +0 -180
- package/gpl/recipes/bake_ao.py +0 -111
- package/gpl/recipes/decimate.py +0 -70
- package/gpl/recipes/export_world.py +0 -699
- package/gpl/recipes/retarget.py +0 -131
- package/gpl/recipes/unwrap.py +0 -71
- package/mcp/blender-server.mjs +0 -689
- package/mcp/blender.mjs +0 -26
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Surface Cache: what a traced global-illumination ray reads at the surface it hits (PRD-639).
|
|
3
|
+
*
|
|
4
|
+
* Unreal keeps a lit proxy of every mesh, the Surface Cache, and its rays read that, never the material
|
|
5
|
+
* (UE 5.8.3: `Engine/Shaders/Private/Lumen/LumenSoftwareRayTracing.ush`, `SurfaceCache/LumenSurfaceCache.usf`).
|
|
6
|
+
* This is the same job in texture space: the meshes already carry UVs, so a material's base colour and
|
|
7
|
+
* emissive maps are reduced to one small tile each and packed into an atlas, and a hit reads the tile at its
|
|
8
|
+
* UV. A material without a map keeps its flat colour. `SurfaceLights` adds the local lights at the hit.
|
|
9
|
+
*
|
|
10
|
+
* Mechanism only. Every colour, texture and light comes from the game's materials and lights; the cache owns none.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** A colour channel triple in linear light, as three's `Color` holds it. */
|
|
14
|
+
interface IRgb {
|
|
15
|
+
readonly r: number;
|
|
16
|
+
readonly g: number;
|
|
17
|
+
readonly b: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Pixels a map can be read from: `ImageData` or a typed array with a size. */
|
|
21
|
+
interface IPixels {
|
|
22
|
+
readonly data: ArrayLike<number>;
|
|
23
|
+
readonly width: number;
|
|
24
|
+
readonly height: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The part of a three `Texture` the cache reads. `image` is an `ImageData`, an `ImageBitmap` or a canvas. */
|
|
28
|
+
export interface ISurfaceCacheTexture {
|
|
29
|
+
readonly image?: unknown;
|
|
30
|
+
/** "srgb" (three's `SRGBColorSpace`) is decoded to linear; anything else is taken as linear. */
|
|
31
|
+
readonly colorSpace?: string;
|
|
32
|
+
/** three's default is true; glTF textures load with false (v = 0 is the top row). */
|
|
33
|
+
readonly flipY?: boolean;
|
|
34
|
+
/** Wrap mode as three's constants: 1000 repeat, 1001 clamp, 1002 mirrored repeat. Default repeat. */
|
|
35
|
+
readonly wrapS?: number;
|
|
36
|
+
readonly wrapT?: number;
|
|
37
|
+
/** three's UV transform; `updateMatrix` is called when present. */
|
|
38
|
+
readonly matrix?: { readonly elements: ArrayLike<number> };
|
|
39
|
+
updateMatrix?(): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The part of a three `MeshStandardMaterial` the cache reads. */
|
|
43
|
+
export interface ISurfaceCacheMaterial {
|
|
44
|
+
readonly name?: string;
|
|
45
|
+
readonly color?: IRgb;
|
|
46
|
+
readonly map?: ISurfaceCacheTexture | null;
|
|
47
|
+
readonly metalness?: number;
|
|
48
|
+
readonly emissive?: IRgb;
|
|
49
|
+
readonly emissiveIntensity?: number;
|
|
50
|
+
readonly emissiveMap?: ISurfaceCacheTexture | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ISurfaceCacheOptions {
|
|
54
|
+
/** Atlas edge in texels, a power of two. Default 2048 (Unreal: 2048 at Medium, 4096 at Epic). */
|
|
55
|
+
readonly atlasSize?: number;
|
|
56
|
+
/** Scales the 64-texel tile a material starts from. Default 1. The atlas budget still shrinks it. */
|
|
57
|
+
readonly cardTexelDensityScale?: number;
|
|
58
|
+
/**
|
|
59
|
+
* The pixels of a map whose own `image` carries none. The owned engine decodes a glTF's maps itself, so JS sees
|
|
60
|
+
* their size and nothing else: the game hands over the image it read from the same file. Returning `undefined`
|
|
61
|
+
* keeps the texture's own `image`.
|
|
62
|
+
*/
|
|
63
|
+
readonly images?: (material: ISurfaceCacheMaterial, slot: "map" | "emissiveMap") => unknown;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ISurfaceCacheReport {
|
|
67
|
+
readonly atlasSize: number;
|
|
68
|
+
/** Bytes of the albedo and emissive atlases as 8-bit RGBA, the form a GPU upload takes. */
|
|
69
|
+
readonly atlasBytes: number;
|
|
70
|
+
readonly tileEdge: number;
|
|
71
|
+
readonly tilesInUse: number;
|
|
72
|
+
readonly mappedMaterials: number;
|
|
73
|
+
readonly flatMaterials: number;
|
|
74
|
+
/** Materials that have a map the cache could not use, so their hits return the flat colour. */
|
|
75
|
+
readonly fallbacks: readonly {
|
|
76
|
+
readonly material: string;
|
|
77
|
+
readonly reason: "unreadable-image";
|
|
78
|
+
/** What the image was and why it could not be read. */
|
|
79
|
+
readonly detail: string;
|
|
80
|
+
}[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const START_TILE = 64;
|
|
84
|
+
const MIN_TILE = 2;
|
|
85
|
+
const MAX_TILE = 512;
|
|
86
|
+
const REPEAT = 1000;
|
|
87
|
+
const MIRRORED = 1002;
|
|
88
|
+
/** Largest edge read from a source image. Past it the browser downsamples the draw. */
|
|
89
|
+
const MAX_READ = 1024;
|
|
90
|
+
|
|
91
|
+
const isPowerOfTwo = (n: number): boolean => Number.isInteger(n) && n > 0 && (n & (n - 1)) === 0;
|
|
92
|
+
|
|
93
|
+
function srgbToLinear(v: number): number {
|
|
94
|
+
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The pixels of a map, or a sentence saying what the image was and why it cannot be read. */
|
|
98
|
+
function pixelsOf(image: unknown): IPixels | string {
|
|
99
|
+
const candidate = image as
|
|
100
|
+
| { data?: unknown; width?: number; height?: number; constructor?: { name?: string } }
|
|
101
|
+
| null
|
|
102
|
+
| undefined;
|
|
103
|
+
const what = `${candidate?.constructor?.name ?? typeof image} ${candidate?.width ?? "?"}x${candidate?.height ?? "?"}`;
|
|
104
|
+
if (typeof candidate?.width !== "number" || typeof candidate.height !== "number")
|
|
105
|
+
return `${what}, no size`;
|
|
106
|
+
const data =
|
|
107
|
+
candidate.data instanceof ArrayBuffer ? new Uint8Array(candidate.data) : candidate.data;
|
|
108
|
+
if (
|
|
109
|
+
ArrayBuffer.isView(data) &&
|
|
110
|
+
!(data instanceof DataView) &&
|
|
111
|
+
candidate.width > 0 &&
|
|
112
|
+
candidate.height > 0
|
|
113
|
+
)
|
|
114
|
+
return {
|
|
115
|
+
data: data as ArrayBufferView & ArrayLike<number>,
|
|
116
|
+
width: candidate.width,
|
|
117
|
+
height: candidate.height,
|
|
118
|
+
};
|
|
119
|
+
if (typeof OffscreenCanvas === "undefined") return `${what}, no OffscreenCanvas`;
|
|
120
|
+
if (!candidate.width || !candidate.height) return `${what}, empty (a closed bitmap?)`;
|
|
121
|
+
const scale = Math.min(1, MAX_READ / Math.max(candidate.width, candidate.height));
|
|
122
|
+
const [w, h] = [
|
|
123
|
+
Math.max(1, Math.round(candidate.width * scale)),
|
|
124
|
+
Math.max(1, Math.round(candidate.height * scale)),
|
|
125
|
+
];
|
|
126
|
+
try {
|
|
127
|
+
const context = new OffscreenCanvas(w, h).getContext("2d");
|
|
128
|
+
context?.drawImage(image as CanvasImageSource, 0, 0, w, h);
|
|
129
|
+
return context?.getImageData(0, 0, w, h) ?? `${what}, no 2d context`;
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return `${what}, ${String(error).slice(0, 120)}`;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* One map as a `tile` x `tile` block of linear RGB, row 0 at the top of the image. Reducing averages every source
|
|
137
|
+
* texel a tile texel covers (a bounce carries the mean albedo); enlarging repeats texels, so a tiny map reads exact.
|
|
138
|
+
*/
|
|
139
|
+
function reduce(pixels: IPixels, tile: number, srgb: boolean, out: Float32Array): void {
|
|
140
|
+
const { data, width, height } = pixels;
|
|
141
|
+
const unit = data instanceof Float32Array || data instanceof Float64Array ? 1 : 255;
|
|
142
|
+
const decode = srgb && unit === 255;
|
|
143
|
+
const channels = Math.max(3, Math.round(data.length / (width * height)));
|
|
144
|
+
for (let ty = 0; ty < tile; ty += 1) {
|
|
145
|
+
const [y0, y1] = [
|
|
146
|
+
Math.floor((ty * height) / tile),
|
|
147
|
+
Math.max(Math.floor((ty * height) / tile) + 1, Math.ceil(((ty + 1) * height) / tile)),
|
|
148
|
+
];
|
|
149
|
+
for (let tx = 0; tx < tile; tx += 1) {
|
|
150
|
+
const [x0, x1] = [
|
|
151
|
+
Math.floor((tx * width) / tile),
|
|
152
|
+
Math.max(Math.floor((tx * width) / tile) + 1, Math.ceil(((tx + 1) * width) / tile)),
|
|
153
|
+
];
|
|
154
|
+
const sum = [0, 0, 0];
|
|
155
|
+
for (let y = y0; y < y1; y += 1)
|
|
156
|
+
for (let x = x0; x < x1; x += 1)
|
|
157
|
+
for (let c = 0; c < 3; c += 1) {
|
|
158
|
+
const v = (data[(y * width + x) * channels + c] ?? 0) / unit;
|
|
159
|
+
sum[c] = (sum[c] ?? 0) + (decode ? srgbToLinear(v) : v);
|
|
160
|
+
}
|
|
161
|
+
const n = (y1 - y0) * (x1 - x0);
|
|
162
|
+
for (let c = 0; c < 3; c += 1) out[(ty * tile + tx) * 3 + c] = (sum[c] ?? 0) / n;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function wrap(v: number, mode: number | undefined): number {
|
|
168
|
+
if (mode === REPEAT || mode === undefined) return v - Math.floor(v);
|
|
169
|
+
if (mode === MIRRORED) {
|
|
170
|
+
const m = v - 2 * Math.floor(v / 2);
|
|
171
|
+
return m > 1 ? 2 - m : m;
|
|
172
|
+
}
|
|
173
|
+
return Math.min(1, Math.max(0, v));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
interface ITile {
|
|
177
|
+
/** Linear RGB, `tile * tile * 3`. */
|
|
178
|
+
readonly texels: Float32Array;
|
|
179
|
+
readonly texture: ISurfaceCacheTexture;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface ISlot {
|
|
183
|
+
readonly albedo: IRgb;
|
|
184
|
+
readonly albedoTile: ITile | undefined;
|
|
185
|
+
readonly emissive: IRgb;
|
|
186
|
+
readonly emissiveTile: ITile | undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* A material atlas over a fixed list of materials. A hit names the material by its index in that list (the
|
|
191
|
+
* trace's material table) and the UV from the triangle's barycentrics. Static, like the scene's BVH: build
|
|
192
|
+
* a new one when the materials change.
|
|
193
|
+
*/
|
|
194
|
+
export class SurfaceCache {
|
|
195
|
+
readonly report: ISurfaceCacheReport;
|
|
196
|
+
readonly #slots: readonly ISlot[];
|
|
197
|
+
readonly #tile: number;
|
|
198
|
+
|
|
199
|
+
private constructor(slots: readonly ISlot[], tile: number, report: ISurfaceCacheReport) {
|
|
200
|
+
this.#slots = slots;
|
|
201
|
+
this.#tile = tile;
|
|
202
|
+
this.report = report;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
static from(
|
|
206
|
+
materials: readonly ISurfaceCacheMaterial[],
|
|
207
|
+
options: ISurfaceCacheOptions = {},
|
|
208
|
+
): SurfaceCache {
|
|
209
|
+
const atlasSize = options.atlasSize ?? 2048;
|
|
210
|
+
const scale = options.cardTexelDensityScale ?? 1;
|
|
211
|
+
if (materials.length === 0) throw new RangeError("SurfaceCache needs at least one material");
|
|
212
|
+
if (!isPowerOfTwo(atlasSize))
|
|
213
|
+
throw new RangeError(`atlasSize ${atlasSize} must be a power of two`);
|
|
214
|
+
if (!(scale > 0) || !Number.isFinite(scale))
|
|
215
|
+
throw new RangeError(`cardTexelDensityScale ${scale} must be positive`);
|
|
216
|
+
// One albedo tile and one emissive tile per material; the atlas holds `(atlasSize / tile)^2` of each.
|
|
217
|
+
let tile = Math.min(
|
|
218
|
+
MAX_TILE,
|
|
219
|
+
Math.max(MIN_TILE, 2 ** Math.round(Math.log2(START_TILE * scale))),
|
|
220
|
+
);
|
|
221
|
+
while (tile > MIN_TILE && Math.floor(atlasSize / tile) ** 2 < materials.length) tile /= 2;
|
|
222
|
+
if (Math.floor(atlasSize / tile) ** 2 < materials.length)
|
|
223
|
+
throw new RangeError(`atlasSize ${atlasSize} holds fewer than ${materials.length} materials`);
|
|
224
|
+
|
|
225
|
+
const fallbacks: { material: string; reason: "unreadable-image"; detail: string }[] = [];
|
|
226
|
+
let mapped = 0;
|
|
227
|
+
const tileOf = (
|
|
228
|
+
material: ISurfaceCacheMaterial,
|
|
229
|
+
index: number,
|
|
230
|
+
slot: "map" | "emissiveMap",
|
|
231
|
+
): ITile | undefined => {
|
|
232
|
+
const texture = material[slot];
|
|
233
|
+
if (texture === null || texture === undefined) return undefined;
|
|
234
|
+
const pixels = pixelsOf(options.images?.(material, slot) ?? texture.image);
|
|
235
|
+
if (typeof pixels === "string") {
|
|
236
|
+
fallbacks.push({
|
|
237
|
+
material: material.name || `material ${index}`,
|
|
238
|
+
reason: "unreadable-image",
|
|
239
|
+
detail: pixels,
|
|
240
|
+
});
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
const texels = new Float32Array(tile * tile * 3);
|
|
244
|
+
reduce(pixels, tile, texture.colorSpace === "srgb", texels);
|
|
245
|
+
texture.updateMatrix?.();
|
|
246
|
+
return { texels, texture };
|
|
247
|
+
};
|
|
248
|
+
const slots = materials.map((material, index): ISlot => {
|
|
249
|
+
const k = 1 - (material.metalness ?? 0);
|
|
250
|
+
const color = material.color ?? { r: 1, g: 1, b: 1 };
|
|
251
|
+
const e = material.emissive ?? { r: 0, g: 0, b: 0 };
|
|
252
|
+
const intensity = material.emissiveIntensity ?? 1;
|
|
253
|
+
const albedoTile = tileOf(material, index, "map");
|
|
254
|
+
const emissiveTile = tileOf(material, index, "emissiveMap");
|
|
255
|
+
if (albedoTile !== undefined || emissiveTile !== undefined) mapped += 1;
|
|
256
|
+
return {
|
|
257
|
+
albedo: { r: color.r * k, g: color.g * k, b: color.b * k },
|
|
258
|
+
albedoTile,
|
|
259
|
+
emissive: { r: e.r * intensity, g: e.g * intensity, b: e.b * intensity },
|
|
260
|
+
emissiveTile,
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
return new SurfaceCache(slots, tile, {
|
|
264
|
+
atlasSize,
|
|
265
|
+
atlasBytes: atlasSize * atlasSize * 4 * 2,
|
|
266
|
+
tileEdge: tile,
|
|
267
|
+
tilesInUse: materials.length,
|
|
268
|
+
mappedMaterials: mapped,
|
|
269
|
+
flatMaterials: materials.length - mapped,
|
|
270
|
+
fallbacks,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Writes the diffuse albedo (linear RGB) at `u`, `v` of material `slot`: the map times the colour factor. */
|
|
275
|
+
albedo(slot: number, u: number, v: number, out: number[]): number[] {
|
|
276
|
+
const s = this.#slot(slot);
|
|
277
|
+
return this.#read(s.albedoTile, s.albedo, u, v, out);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Writes the emitted radiance at `u`, `v`: the emissive map times the colour times the intensity. */
|
|
281
|
+
emissive(slot: number, u: number, v: number, out: number[]): number[] {
|
|
282
|
+
const s = this.#slot(slot);
|
|
283
|
+
return this.#read(s.emissiveTile, s.emissive, u, v, out);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
#slot(slot: number): ISlot {
|
|
287
|
+
const s = this.#slots[slot];
|
|
288
|
+
if (s === undefined) throw new RangeError(`SurfaceCache has no material ${slot}`);
|
|
289
|
+
return s;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
#read(tile: ITile | undefined, factor: IRgb, u: number, v: number, out: number[]): number[] {
|
|
293
|
+
if (tile === undefined) {
|
|
294
|
+
out[0] = factor.r;
|
|
295
|
+
out[1] = factor.g;
|
|
296
|
+
out[2] = factor.b;
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
const m = tile.texture.matrix?.elements;
|
|
300
|
+
// three's UV transform, column-major 3x3 acting on (u, v, 1).
|
|
301
|
+
const [tu, tv] =
|
|
302
|
+
m === undefined || m.length < 9
|
|
303
|
+
? [u, v]
|
|
304
|
+
: [
|
|
305
|
+
(m[0] ?? 1) * u + (m[3] ?? 0) * v + (m[6] ?? 0),
|
|
306
|
+
(m[1] ?? 0) * u + (m[4] ?? 1) * v + (m[7] ?? 0),
|
|
307
|
+
];
|
|
308
|
+
const x = wrap(tu, tile.texture.wrapS);
|
|
309
|
+
// flipY (three's default) puts v = 0 at the bottom row; glTF textures load with it false.
|
|
310
|
+
const y = wrap(tile.texture.flipY === false ? tv : 1 - tv, tile.texture.wrapT);
|
|
311
|
+
const n = this.#tile;
|
|
312
|
+
const [px, py] = [Math.min(n - 1, Math.floor(x * n)), Math.min(n - 1, Math.floor(y * n))];
|
|
313
|
+
const at = (py * n + px) * 3;
|
|
314
|
+
out[0] = (tile.texels[at] ?? 0) * factor.r;
|
|
315
|
+
out[1] = (tile.texels[at + 1] ?? 0) * factor.g;
|
|
316
|
+
out[2] = (tile.texels[at + 2] ?? 0) * factor.b;
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** A local light, in three's units: `intensity` is candela and `distance` the cutoff (0 = none). */
|
|
322
|
+
export interface ISurfaceLight {
|
|
323
|
+
readonly kind: "point" | "spot";
|
|
324
|
+
readonly position: readonly [number, number, number];
|
|
325
|
+
readonly color: readonly [number, number, number];
|
|
326
|
+
readonly intensity: number;
|
|
327
|
+
readonly distance: number;
|
|
328
|
+
/** Spot only: unit direction the cone points along, half angle in radians, penumbra 0..1. */
|
|
329
|
+
readonly direction?: readonly [number, number, number];
|
|
330
|
+
readonly angle?: number;
|
|
331
|
+
readonly penumbra?: number;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export interface ISurfaceLightsOptions {
|
|
335
|
+
/** The most lights evaluated at one hit, the brightest first. Default 8. */
|
|
336
|
+
readonly maxLights?: number;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* The local lights at a hit: culled to the ones whose range reaches the point, the brightest first, capped, with
|
|
341
|
+
* one shadow ray each, supplied by the caller (the bake's BVH ray, or the kernel's). Lights skipped by the cap are
|
|
342
|
+
* counted in `skipped`.
|
|
343
|
+
*/
|
|
344
|
+
export class SurfaceLights {
|
|
345
|
+
/** Lights left out by the cap, summed over every `shade` call. */
|
|
346
|
+
skipped = 0;
|
|
347
|
+
readonly #lights: readonly ISurfaceLight[];
|
|
348
|
+
readonly #max: number;
|
|
349
|
+
readonly #scores: number[];
|
|
350
|
+
readonly #order: number[] = [];
|
|
351
|
+
|
|
352
|
+
constructor(lights: readonly ISurfaceLight[], options: ISurfaceLightsOptions = {}) {
|
|
353
|
+
const max = options.maxLights ?? 8;
|
|
354
|
+
if (!Number.isInteger(max) || max < 1)
|
|
355
|
+
throw new RangeError(`maxLights ${max} must be an integer of 1 or more`);
|
|
356
|
+
this.#lights = lights;
|
|
357
|
+
this.#max = max;
|
|
358
|
+
this.#scores = new Array<number>(lights.length).fill(0);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Adds the light the hit receives to `out` (RGB, linear): albedo x colour x (intensity / pi) / d^2 x window x cos
|
|
363
|
+
* (x cone for a spot). `visible(i)` answers the shadow ray to light `i`; it is not asked for a light behind the
|
|
364
|
+
* surface, out of range or outside its cone.
|
|
365
|
+
*/
|
|
366
|
+
shade(
|
|
367
|
+
point: readonly [number, number, number],
|
|
368
|
+
normal: readonly [number, number, number],
|
|
369
|
+
albedo: readonly [number, number, number],
|
|
370
|
+
out: number[],
|
|
371
|
+
visible: (light: number) => boolean,
|
|
372
|
+
): number[] {
|
|
373
|
+
const order = this.#order;
|
|
374
|
+
order.length = 0;
|
|
375
|
+
for (let i = 0; i < this.#lights.length; i += 1) {
|
|
376
|
+
const score = this.#score(this.#lights[i] as ISurfaceLight, point, normal);
|
|
377
|
+
this.#scores[i] = score;
|
|
378
|
+
if (score > 0) order.push(i);
|
|
379
|
+
}
|
|
380
|
+
order.sort((a, b) => (this.#scores[b] ?? 0) - (this.#scores[a] ?? 0));
|
|
381
|
+
this.skipped += Math.max(0, order.length - this.#max);
|
|
382
|
+
for (let k = 0; k < Math.min(order.length, this.#max); k += 1) {
|
|
383
|
+
const i = order[k] as number;
|
|
384
|
+
if (!visible(i)) continue;
|
|
385
|
+
const light = this.#lights[i] as ISurfaceLight;
|
|
386
|
+
for (let c = 0; c < 3; c += 1)
|
|
387
|
+
out[c] = (out[c] ?? 0) + (albedo[c] ?? 0) * (light.color[c] ?? 0) * (this.#scores[i] ?? 0);
|
|
388
|
+
}
|
|
389
|
+
return out;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** The light's contribution per unit albedo and colour at `point`, 0 when it cannot reach it. */
|
|
393
|
+
#score(
|
|
394
|
+
light: ISurfaceLight,
|
|
395
|
+
[px, py, pz]: readonly [number, number, number],
|
|
396
|
+
[nx, ny, nz]: readonly [number, number, number],
|
|
397
|
+
): number {
|
|
398
|
+
const [dx, dy, dz] = [light.position[0] - px, light.position[1] - py, light.position[2] - pz];
|
|
399
|
+
const d = Math.hypot(dx, dy, dz);
|
|
400
|
+
if (d < 0.02 || (light.distance > 0 && d >= light.distance)) return 0;
|
|
401
|
+
const cos = (nx * dx + ny * dy + nz * dz) / d;
|
|
402
|
+
if (cos <= 0) return 0;
|
|
403
|
+
// three's getDistanceAttenuation: inverse square times a window that closes at the cutoff distance.
|
|
404
|
+
const window =
|
|
405
|
+
light.distance > 0 ? Math.min(1, Math.max(0, 1 - (d / light.distance) ** 4)) ** 2 : 1;
|
|
406
|
+
let cone = 1;
|
|
407
|
+
if (light.kind === "spot") {
|
|
408
|
+
const dir = light.direction ?? [0, -1, 0];
|
|
409
|
+
const toHit = (-dx * dir[0] - dy * dir[1] - dz * dir[2]) / d;
|
|
410
|
+
const outer = Math.cos(light.angle ?? Math.PI / 3);
|
|
411
|
+
const inner = Math.cos((light.angle ?? Math.PI / 3) * (1 - (light.penumbra ?? 0)));
|
|
412
|
+
// three's getSpotAttenuation: smoothstep between the outer and the inner cone.
|
|
413
|
+
const t = Math.min(1, Math.max(0, (toHit - outer) / Math.max(inner - outer, 1e-6)));
|
|
414
|
+
cone = t * t * (3 - 2 * t);
|
|
415
|
+
}
|
|
416
|
+
return (light.intensity / Math.PI / (d * d)) * window * cos * cone;
|
|
417
|
+
}
|
|
418
|
+
}
|