@threenative/core 0.3.11 → 0.3.12
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 +1096 -26
- package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
- package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-CWuFRT7G.d.ts} +1 -1
- package/dist/chunk-2Z44XSI2.js +35 -0
- package/dist/chunk-5O73CDMR.js +1849 -0
- package/dist/chunk-6KLNISRJ.js +153 -0
- package/dist/chunk-7UUT3TY5.js +133 -0
- package/dist/chunk-CFGTPFUH.js +231 -0
- package/dist/chunk-DBELTTTM.js +615 -0
- package/dist/chunk-GMHATRPW.js +7701 -0
- package/dist/chunk-GXY7B3JG.js +46 -0
- package/dist/chunk-KIRUKTBQ.js +187 -0
- package/dist/chunk-N5LRP4BR.js +220 -0
- package/dist/chunk-OKXUCASC.js +2238 -0
- package/dist/chunk-QWJEMUUT.js +94 -0
- package/dist/chunk-VAZM36TV.js +497 -0
- package/dist/{game-DzMcitDO.d.ts → game-D7pAzZUY.d.ts} +15 -3
- package/dist/hot.d.ts +3 -3
- package/dist/hot.js +2 -46
- package/dist/index.d.ts +329 -302
- package/dist/index.js +10566 -22552
- package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-B6BMqS1r.d.ts} +33 -3
- package/dist/playtest.d.ts +3 -3
- package/dist/playtest.js +24 -75
- package/dist/react-css.js +1 -215
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-rZm0hVqq.d.ts → renderer-DvFVoAgj.d.ts} +13 -4
- package/dist/spells/index.d.ts +643 -0
- package/dist/spells/index.js +1558 -0
- package/dist/spells/ultra/index.d.ts +1600 -0
- package/dist/spells/ultra/index.js +6804 -0
- package/dist/terrain-jobs-worker.js +1 -123
- package/dist/two-sided-foliage-W523J3UM.js +1 -0
- package/dist/ui-layer.js +5 -384
- package/dist/worker.js +4 -41
- package/dist/world.d.ts +725 -259
- package/dist/world.js +16714 -18284
- package/mcp/animation-server.mjs +1 -1
- package/mcp/animation.mjs +2 -3
- package/mcp/engine-server.mjs +142 -47
- package/mcp/servers.mjs +9 -2
- package/mcp/terrain.mjs +8 -0
- package/package.json +14 -6
- package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
- package/patches/three@0.187.0.patch +2004 -407
- package/src/assets.ts +9 -2
- package/src/card-lod.ts +152 -0
- package/src/compressed-texture-preparation.ts +1008 -0
- package/src/frame-budget.ts +31 -20
- package/src/game-dev-tools.ts +93 -0
- package/src/game-frame-loop.ts +995 -0
- package/src/game-renderer-metrics.ts +77 -0
- package/src/game-startup.ts +1001 -0
- package/src/game-validate.ts +70 -0
- package/src/game.ts +253 -1935
- package/src/index.ts +27 -2
- package/src/launch-diagnostics.ts +22 -12
- package/src/look/ao-admission.ts +24 -0
- package/src/look/filmic-tone-mapping.ts +15 -13
- package/src/look/look-diagnostics.ts +3 -3
- package/src/look/quality.ts +10 -5
- package/src/look/world-environment.ts +76 -9
- package/src/loop.ts +29 -0
- package/src/model-lod.ts +42 -3
- package/src/particle-kinds.ts +515 -0
- package/src/picking.ts +17 -3
- package/src/playtest.ts +27 -1
- package/src/projection-apply.ts +4 -1
- package/src/projection-plan.ts +9 -6
- package/src/render/mesh-pool.ts +1 -1
- package/src/render/resolution-subrect.ts +2 -0
- package/src/render/rough-diffuse.ts +46 -12
- package/src/render/soft-shadow.ts +27 -12
- package/src/render/virtual-shadow-adaptive.ts +55 -0
- package/src/render/virtual-shadow-casters.ts +175 -0
- package/src/render/virtual-shadow-layers.ts +76 -0
- package/src/render/virtual-shadow-level.ts +224 -0
- package/src/render/virtual-shadow-markers.ts +193 -0
- package/src/render/virtual-shadow-options.ts +370 -0
- package/src/render/virtual-shadow-pages.ts +5 -0
- package/src/render/virtual-shadow.ts +213 -992
- package/src/render/world-impostor-surface.ts +293 -34
- package/src/render-pass-budget.ts +34 -7
- package/src/renderProjection.ts +22 -0
- package/src/renderer.ts +92 -37
- package/src/snow-field.ts +1 -1
- package/src/spells/cast-pool.ts +361 -0
- package/src/spells/index.ts +177 -0
- package/src/spells/mechanisms.ts +295 -0
- package/src/spells/reaction-field.ts +415 -0
- package/src/spells/spell-lights.ts +139 -0
- package/src/spells/spell-world.ts +187 -0
- package/src/spells/spell.ts +397 -0
- package/src/spells/spells.ts +283 -0
- package/src/spells/ultra/engine/camera.ts +184 -0
- package/src/spells/ultra/engine/quality.ts +96 -0
- package/src/spells/ultra/index.ts +52 -0
- package/src/spells/ultra/main.ts +305 -0
- package/src/spells/ultra/scene/arenaMaterial.ts +325 -0
- package/src/spells/ultra/spells/cast.ts +233 -0
- package/src/spells/ultra/spells/fire.ts +1267 -0
- package/src/spells/ultra/vfx/FluidVolume.ts +816 -0
- package/src/spells/ultra/vfx/FluidVolume.tsl.ts +855 -0
- package/src/spells/ultra/vfx/FxTarget.ts +61 -0
- package/src/spells/ultra/vfx/GroundField.ts +1399 -0
- package/src/spells/ultra/vfx/Particles.ts +1475 -0
- package/src/spells/ultra/vfx/Ribbons.ts +645 -0
- package/src/spells/ultra/vfx/Shockwaves.ts +286 -0
- package/src/spells/ultra/vfx/Volumes.ts +489 -0
- package/src/spells/ultra/vfx/common.ts +1195 -0
- package/src/spells/ultra/vfx/noise.ts +441 -0
- package/src/startup-readiness.ts +128 -34
- package/src/version.ts +1 -1
- package/src/warmup.ts +56 -8
- package/src/water-surface.ts +9 -3
- package/src/world-bundle-safety.ts +401 -0
- package/src/world-cell-batches.ts +1216 -0
- package/src/world-cell-chunks.ts +1132 -0
- package/src/world-cell-data.ts +995 -0
- package/src/world-cell-impostors.ts +1449 -0
- package/src/world-cell-lod-bias.ts +151 -0
- package/src/world-cell-models.ts +1074 -0
- package/src/world-cell-options.ts +189 -0
- package/src/world-cell-placements.ts +694 -0
- package/src/world-cell-prewarm.ts +557 -0
- package/src/world-cell-readiness.ts +357 -0
- package/src/world-cell-render-bundles.ts +459 -0
- package/src/world-cell-render-gpu-dress.ts +714 -0
- package/src/world-cell-render-main-cull.ts +484 -0
- package/src/world-cell-render-shadow-claims.ts +198 -0
- package/src/world-cell-render-util.ts +246 -0
- package/src/world-cell-render.ts +1493 -0
- package/src/world-cell-residency.ts +745 -0
- package/src/world-cells.ts +1318 -10418
- package/src/world-chunk-shadow.ts +308 -0
- package/src/world-collar.ts +315 -0
- package/src/world-gpu-flags.ts +163 -0
- package/src/world-gpu-readback.ts +881 -0
- package/src/world-gpu-reference.ts +510 -0
- package/src/world-gpu-scene.ts +214 -2264
- package/src/world-gpu-shadow.ts +190 -0
- package/src/world-gpu-types.ts +381 -0
- package/src/world-gpu-validation.ts +518 -0
- package/src/world-heightfield.ts +802 -0
- package/src/world-load-limiter.ts +106 -0
- package/src/world-noise.ts +241 -0
- package/src/world-package.ts +42 -0
- package/src/world-region.ts +1 -1
- package/src/world-terrain-splat.ts +365 -149
- package/src/world-tile-budget.ts +256 -0
- package/src/world-tile-geometry.ts +469 -0
- package/src/world-tile-lod.ts +408 -0
- package/src/world-tile-observation.ts +566 -0
- package/src/world-tile-stitch.ts +166 -0
- package/src/world-tiles.ts +170 -1745
- package/src/world-water-bodies.ts +359 -0
- package/src/world.ts +35 -741
|
@@ -157,6 +157,8 @@ export class ResolutionSubrect implements IResolutionSubrect {
|
|
|
157
157
|
capacityChanged = widthCapacity !== binding.width || heightCapacity !== binding.height;
|
|
158
158
|
binding.width = widthCapacity;
|
|
159
159
|
binding.height = heightCapacity;
|
|
160
|
+
binding.viewWidth = Math.max(1, Math.round(width));
|
|
161
|
+
binding.viewHeight = Math.max(1, Math.round(height));
|
|
160
162
|
} else {
|
|
161
163
|
if (binding.width === 0 || binding.height === 0)
|
|
162
164
|
throw new Error("TN_RESOLUTION_SUBRECT: target capacity not reserved");
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import type { Light, Material, Mesh, Object3D } from "three";
|
|
2
2
|
import {
|
|
3
|
+
F_Schlick,
|
|
3
4
|
clamp,
|
|
4
5
|
diffuseColor,
|
|
5
6
|
metalness,
|
|
6
7
|
normalView,
|
|
7
8
|
positionViewDirection,
|
|
8
9
|
roughness,
|
|
10
|
+
specularColor,
|
|
11
|
+
specularF90,
|
|
9
12
|
uniform,
|
|
10
13
|
vec3,
|
|
11
14
|
} from "three/tsl";
|
|
@@ -16,9 +19,10 @@ declare module "three" {
|
|
|
16
19
|
// biome-ignore lint/style/useNamingConvention: a module augmentation keeps three's own class name.
|
|
17
20
|
interface Material {
|
|
18
21
|
/**
|
|
19
|
-
* Unreal's Substrate
|
|
20
|
-
* standard or physical material with EON where three shades Lambert
|
|
21
|
-
*
|
|
22
|
+
* Unreal's Substrate slab as a fresh project compiles it (PRD-653, PRD-632): direct light shades the diffuse lobe
|
|
23
|
+
* of a standard or physical material with EON where three shades Lambert, with no energy conservation (the diffuse
|
|
24
|
+
* keeps the light three's Fresnel takes, the specular has no multi-scattering weight). The default is off.
|
|
25
|
+
* Indirect light stays three's, and a material changed after its first draw needs `needsUpdate`.
|
|
22
26
|
*/
|
|
23
27
|
roughDiffuse?: boolean;
|
|
24
28
|
}
|
|
@@ -52,10 +56,15 @@ type Lamp = Light & { readonly isDirectionalLight?: boolean; readonly sourceRadi
|
|
|
52
56
|
interface IDirectInput {
|
|
53
57
|
readonly lightDirection: Node<"vec3">;
|
|
54
58
|
readonly lightNode?: { readonly light?: Lamp };
|
|
55
|
-
readonly reflectedLight: {
|
|
59
|
+
readonly reflectedLight: {
|
|
60
|
+
readonly directDiffuse: Node<"vec3">;
|
|
61
|
+
readonly directSpecular: Node<"vec3">;
|
|
62
|
+
};
|
|
56
63
|
}
|
|
57
64
|
interface ILightingModel {
|
|
58
65
|
direct(input: IDirectInput, builder: unknown): void;
|
|
66
|
+
/** three's multi-scattering weight on the direct specular lobe, set in `start`. */
|
|
67
|
+
readonly multiScatteringCompensation?: Node<"vec3">;
|
|
59
68
|
}
|
|
60
69
|
interface IPatchable {
|
|
61
70
|
setupLightingModel?: (this: Material, builder: unknown) => ILightingModel;
|
|
@@ -97,9 +106,12 @@ function roughDiffuseFactor(
|
|
|
97
106
|
}
|
|
98
107
|
|
|
99
108
|
/**
|
|
100
|
-
* Turns a stock lighting model's
|
|
101
|
-
*
|
|
102
|
-
*
|
|
109
|
+
* Turns a stock lighting model's direct light into a fresh Unreal project's Substrate slab: EON in place of Lambert, and
|
|
110
|
+
* no energy conservation (GBuffer format 0 compiles it off), so the diffuse keeps the light three's `1 - F_Schlick(VoH)`
|
|
111
|
+
* takes away and the specular lobe loses three's multi-scattering weight. UE 5.8.3:
|
|
112
|
+
* Engine/Shaders/Private/ShadingEnergyConservation.ush:7-31, ShadingEnergyConservationTemplate.ush:73-78 and :134-135,
|
|
113
|
+
* used at Substrate/SubstrateEvaluation.ush:585-589 and :836. The model's two terms go into scratch variables and return
|
|
114
|
+
* rescaled, so whatever else it multiplies them by (sheen's energy compensation) stays in; clearcoat is untouched.
|
|
103
115
|
*/
|
|
104
116
|
function roughen(model: ILightingModel): ILightingModel {
|
|
105
117
|
const lambert = model.direct;
|
|
@@ -109,9 +121,17 @@ function roughen(model: ILightingModel): ILightingModel {
|
|
|
109
121
|
builder: unknown,
|
|
110
122
|
): void {
|
|
111
123
|
const scratch = vec3(0).toVar() as Node<"vec3">;
|
|
124
|
+
const specular = vec3(0).toVar() as Node<"vec3">;
|
|
112
125
|
lambert.call(
|
|
113
126
|
this,
|
|
114
|
-
{
|
|
127
|
+
{
|
|
128
|
+
...input,
|
|
129
|
+
reflectedLight: {
|
|
130
|
+
...input.reflectedLight,
|
|
131
|
+
directDiffuse: scratch,
|
|
132
|
+
directSpecular: specular,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
115
135
|
builder,
|
|
116
136
|
);
|
|
117
137
|
const nov = clamp(normalView.dot(positionViewDirection)) as Node<"float">;
|
|
@@ -122,10 +142,24 @@ function roughen(model: ILightingModel): ILightingModel {
|
|
|
122
142
|
if (weight !== undefined) r = r.mul(weight) as Node<"float">;
|
|
123
143
|
// three's diffuseContribution, which three/tsl does not export: the colour less its metal share.
|
|
124
144
|
const albedo = diffuseColor.rgb.mul(metalness.oneMinus()) as Node<"vec3">;
|
|
125
|
-
|
|
126
|
-
(
|
|
127
|
-
|
|
128
|
-
|
|
145
|
+
// three's diffuse lost 1 - F at the half vector; that never reaches 0 where N.L and N.V are both positive.
|
|
146
|
+
const half = input.lightDirection.add(positionViewDirection).normalize();
|
|
147
|
+
const schlick = F_Schlick({
|
|
148
|
+
f0: specularColor,
|
|
149
|
+
f90: specularF90,
|
|
150
|
+
dotVH: clamp(positionViewDirection.dot(half)),
|
|
151
|
+
});
|
|
152
|
+
// quality-allow: three's types give F_Schlick an OperatorNode, which carries no vec3 component type; the node is a vec3 at run time
|
|
153
|
+
const fresnel = schlick as unknown as Node<"vec3">;
|
|
154
|
+
const factor = roughDiffuseFactor(albedo, r, nov, nol, vol).div(
|
|
155
|
+
fresnel.oneMinus().max(EPSILON),
|
|
156
|
+
);
|
|
157
|
+
type Sum = Node<"vec3"> & { addAssign(value: Node): void };
|
|
158
|
+
(input.reflectedLight.directDiffuse as Sum).addAssign(scratch.mul(factor));
|
|
159
|
+
const weight3 = this.multiScatteringCompensation;
|
|
160
|
+
(input.reflectedLight.directSpecular as Sum).addAssign(
|
|
161
|
+
weight3 === undefined ? specular : specular.div(weight3),
|
|
162
|
+
);
|
|
129
163
|
};
|
|
130
164
|
return model;
|
|
131
165
|
}
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
min,
|
|
22
22
|
positionWorld,
|
|
23
23
|
reference,
|
|
24
|
+
renderGroup,
|
|
24
25
|
sqrt,
|
|
25
26
|
step,
|
|
26
27
|
textureLoad,
|
|
@@ -30,6 +31,16 @@ import {
|
|
|
30
31
|
} from "three/tsl";
|
|
31
32
|
import type { Node, TextureNode } from "three/webgpu";
|
|
32
33
|
|
|
34
|
+
/**
|
|
35
|
+
* A shadow input in the render group. The patched renderer skips object-group uploads for a settled static
|
|
36
|
+
* receiver, so a shadow camera's near, far, frustum or map size there froze at the value it settled with
|
|
37
|
+
* (`virtual-shadow.ts` `rendered` keeps its own receiver inputs here for the same reason).
|
|
38
|
+
*/
|
|
39
|
+
function shadowInput<T>(node: T): T {
|
|
40
|
+
// quality-allow: three's ReferenceNode has setGroup at runtime; its declaration omits it
|
|
41
|
+
return (node as unknown as { setGroup(group: typeof renderGroup): T }).setGroup(renderGroup);
|
|
42
|
+
}
|
|
43
|
+
|
|
33
44
|
/** tan(5 degrees): Unreal's `r.Shadow.Virtual.SubsurfaceShadowMinSourceAngle`, the sun's least source on a leaf. */
|
|
34
45
|
export const LEAF_MIN_SOURCE_TANGENT = Math.tan((5 * Math.PI) / 180);
|
|
35
46
|
|
|
@@ -205,13 +216,17 @@ export function sourceAngleShadowFilter({
|
|
|
205
216
|
)
|
|
206
217
|
throw new RangeError("source-angle shadows require a comparison depth texture, not VSM");
|
|
207
218
|
const reversed = depthTexture.compareFunction === GreaterEqualCompare;
|
|
208
|
-
const size = reference("mapSize", "vec2", shadow);
|
|
209
|
-
const near = reference("near", "float", camera);
|
|
210
|
-
const range = reference("far", "float", camera).sub(near);
|
|
219
|
+
const size = shadowInput(reference("mapSize", "vec2", shadow));
|
|
220
|
+
const near = shadowInput(reference("near", "float", camera));
|
|
221
|
+
const range = shadowInput(reference("far", "float", camera)).sub(near);
|
|
211
222
|
const span = vec2(
|
|
212
|
-
reference("right", "float", camera).sub(
|
|
213
|
-
|
|
214
|
-
|
|
223
|
+
shadowInput(reference("right", "float", camera)).sub(
|
|
224
|
+
shadowInput(reference("left", "float", camera)),
|
|
225
|
+
),
|
|
226
|
+
shadowInput(reference("top", "float", camera)).sub(
|
|
227
|
+
shadowInput(reference("bottom", "float", camera)),
|
|
228
|
+
),
|
|
229
|
+
).div(shadowInput(reference("zoom", "float", camera)));
|
|
215
230
|
const texel = vec2(1).div(size);
|
|
216
231
|
const isArray = "isArrayTexture" in depthTexture && depthTexture.isArrayTexture === true;
|
|
217
232
|
const withLayer = (sample: TextureNode) => (isArray ? sample.depth(float(depthLayer)) : sample);
|
|
@@ -396,9 +411,9 @@ export function sourceRadiusShadowFilter({
|
|
|
396
411
|
);
|
|
397
412
|
if (depthTexture.compareFunction !== LessEqualCompare)
|
|
398
413
|
throw new RangeError("source-radius shadows require a forward-depth comparison texture");
|
|
399
|
-
const near = reference("near", "float", camera);
|
|
400
|
-
const far = reference("far", "float", camera);
|
|
401
|
-
const size = reference("mapSize", "vec2", shadow).x;
|
|
414
|
+
const near = shadowInput(reference("near", "float", camera));
|
|
415
|
+
const far = shadowInput(reference("far", "float", camera));
|
|
416
|
+
const size = shadowInput(reference("mapSize", "vec2", shadow)).x;
|
|
402
417
|
// A [0, 1] perspective depth and its view depth.
|
|
403
418
|
const distanceOf = (depth: Node<"float">) =>
|
|
404
419
|
near.mul(far).div(far.sub(depth.mul(far.sub(near))));
|
|
@@ -528,9 +543,9 @@ export function sourceRadiusShadowFilter({
|
|
|
528
543
|
return visibility;
|
|
529
544
|
})();
|
|
530
545
|
}
|
|
531
|
-
const tanHalf = uniform(1)
|
|
532
|
-
|
|
533
|
-
|
|
546
|
+
const tanHalf = uniform(1)
|
|
547
|
+
.setGroup(renderGroup)
|
|
548
|
+
.onRenderUpdate(() => Math.tan((camera.fov * Math.PI) / 360) / camera.zoom);
|
|
534
549
|
const uvPerTangent = float(0.5).div(tanHalf);
|
|
535
550
|
const sizes = vec2(size);
|
|
536
551
|
const load = (uv: Node<"vec2">) =>
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a level's own measured render cost buys, as a wider refresh trail and a raised caster gate.
|
|
3
|
+
*
|
|
4
|
+
* A level render is a whole scene draw, so the engine cannot know what one is worth until it has
|
|
5
|
+
* paid for it. These are the constants of that trade and the two decisions taken from it; the node
|
|
6
|
+
* owns the measurement, the per-level state and the marker lines.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** How much a level's texel gate steps up per over-budget render. */
|
|
10
|
+
const GATE_SCALE_RISE = 1.5;
|
|
11
|
+
/** The most a level's texel gate may scale up, so a big building is never in reach. */
|
|
12
|
+
const GATE_SCALE_CAP = 8;
|
|
13
|
+
/** How much the gate steps back toward 1 once the render is comfortably under budget. */
|
|
14
|
+
const GATE_SCALE_DECAY = 0.5;
|
|
15
|
+
/** Below this share of the affordable budget a level's gate decays; between, it holds. */
|
|
16
|
+
const GATE_DECAY_SHARE = 0.5;
|
|
17
|
+
/**
|
|
18
|
+
* How much of a level's last two render costs the smoothed cost keeps, `(0, 1]`. Half and half, so a
|
|
19
|
+
* first draw with a cold pipeline is not the level's price for the rest of the session.
|
|
20
|
+
*/
|
|
21
|
+
export const COST_SMOOTHING = 0.5;
|
|
22
|
+
/**
|
|
23
|
+
* A level's first measured renders are thrown away before its cost is trusted. The first draw of a
|
|
24
|
+
* map compiles its shaders and uploads its textures, so it reads tens of milliseconds however cheap
|
|
25
|
+
* the level is; seeding the smoothed cost with that warm-up would widen the trail or raise the gate
|
|
26
|
+
* for a level that is not actually expensive. Two renders is enough for the pipeline to be warm.
|
|
27
|
+
*/
|
|
28
|
+
export const COST_WARMUP_RENDERS = 2;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The trail a level may step by, from what its own last render cost: the configured step, widened in
|
|
32
|
+
* proportion to how far the cost overshoots the affordable share, and capped by the margin the
|
|
33
|
+
* window's guard leaves, so the camera stays inside the region this level serves and everything it
|
|
34
|
+
* serves stays inside the map.
|
|
35
|
+
*/
|
|
36
|
+
export function trailStep(
|
|
37
|
+
base: number,
|
|
38
|
+
costMs: number,
|
|
39
|
+
affordableMs: number,
|
|
40
|
+
margin: number,
|
|
41
|
+
): number {
|
|
42
|
+
return Math.min(base * Math.max(1, costMs / affordableMs), margin);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The scale a level's texel gate takes after one over-budget render: up past the cap on a render
|
|
47
|
+
* that costs more than the frame can afford, back toward 1 once it is comfortably under half of it,
|
|
48
|
+
* and unchanged between the two — that band is the hysteresis, so a level on the budget does not
|
|
49
|
+
* oscillate.
|
|
50
|
+
*/
|
|
51
|
+
export function nextGateScale(scale: number, costMs: number, affordableMs: number): number {
|
|
52
|
+
if (costMs > affordableMs) return Math.min(GATE_SCALE_CAP, scale * GATE_SCALE_RISE);
|
|
53
|
+
if (costMs < affordableMs * GATE_DECAY_SHARE) return Math.max(1, scale * GATE_SCALE_DECAY);
|
|
54
|
+
return scale;
|
|
55
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import type { Box3, BufferGeometry, Mesh, Sphere } from "three";
|
|
2
|
+
import { lodChainOf } from "../model-lod.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a caster is, what a level measures it by, and how it is sorted into static and moving.
|
|
6
|
+
*
|
|
7
|
+
* One layer of the pipeline: the world sphere and box a level derives its depth from, the texel and
|
|
8
|
+
* LOD gates that decide whether a caster draws into a level at all, and the motion record that keeps
|
|
9
|
+
* a walking mesh out of the cached maps until it holds still.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* World spheres one level render collects: centre, radius, shadow flags, the box's own height
|
|
14
|
+
* range, seven numbers each.
|
|
15
|
+
*/
|
|
16
|
+
export const POOL_STRIDE = 7;
|
|
17
|
+
/**
|
|
18
|
+
* How many window widths a caster may be before the window is cut out of it instead. One: an
|
|
19
|
+
* object wider than the window it is in is a mass, and only its height reaches the frustum.
|
|
20
|
+
*/
|
|
21
|
+
export const MAX_MASS_WINDOW_WIDTHS = 1;
|
|
22
|
+
export const POOL_CASTERS = 1 << 0;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A mesh in the caster table, with the three channels the probe reads off the object rather than off
|
|
26
|
+
* the geometry: the bounds three's own cull would use, which `Mesh` does not declare, the
|
|
27
|
+
* `casterPrewarmOwed` flag `WorldCells` writes (see `SharedBatch.awaitPrewarmDraw`), and the
|
|
28
|
+
* `mainAdmitted` answer it publishes for a caster half (see `SharedBatch.setMainAdmitted`).
|
|
29
|
+
*/
|
|
30
|
+
export interface ICasterMesh extends Mesh {
|
|
31
|
+
boundingBox?: Box3 | null;
|
|
32
|
+
boundingSphere?: Sphere | null;
|
|
33
|
+
casterPrewarmOwed?: boolean;
|
|
34
|
+
mainAdmitted?: boolean;
|
|
35
|
+
chunkShadowProxy?: boolean;
|
|
36
|
+
casterMinDiameter?: number;
|
|
37
|
+
/**
|
|
38
|
+
* How many per-level buffers a chunk shadow proxy may keep, set by the level probe from the
|
|
39
|
+
* ladder's own length; see `selectChunkShadowParts`.
|
|
40
|
+
*/
|
|
41
|
+
casterLevelSlots?: number;
|
|
42
|
+
computeBoundingBox(): void;
|
|
43
|
+
computeBoundingSphere(): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* One caster's memoised world values, and the inputs they were derived from.
|
|
48
|
+
*
|
|
49
|
+
* The key is the world matrix plus the local sphere and the local box's two Y values — everything
|
|
50
|
+
* `Sphere.applyMatrix4` and `Box3.applyMatrix4` read, and nothing else. Twenty-two doubles compare
|
|
51
|
+
* against the pair of matrix applies they replace, and a key that matches cannot have a different
|
|
52
|
+
* answer, so this is a memo on the inputs rather than a guess about whether the mesh moved. A
|
|
53
|
+
* caster under a frozen subtree, a streamed cell nobody touched and a walking stag are therefore
|
|
54
|
+
* the same case: whichever of them changed its world matrix is recomputed, and the rest are not.
|
|
55
|
+
*
|
|
56
|
+
* A fresh slot is all zeros, which no world matrix can be — its last element is 1 — so the first
|
|
57
|
+
* render of a caster always misses and always computes.
|
|
58
|
+
*/
|
|
59
|
+
const CASTER_KEY_STRIDE = 22;
|
|
60
|
+
/** World centre and radius, then the box's world height range: what the pool row is written from. */
|
|
61
|
+
const CASTER_WORLD_STRIDE = 6;
|
|
62
|
+
export const CASTER_STRIDE = CASTER_KEY_STRIDE + CASTER_WORLD_STRIDE;
|
|
63
|
+
|
|
64
|
+
export const MOTION_STATIC = 0;
|
|
65
|
+
/** Drawn through the mover maps and kept out of the cached levels. */
|
|
66
|
+
export const MOTION_MOVER = 1;
|
|
67
|
+
/**
|
|
68
|
+
* Back in the cached levels, still drawn through the mover maps until every level the redraw asked
|
|
69
|
+
* has rendered it, so the shadow never drops out between the two.
|
|
70
|
+
*/
|
|
71
|
+
export const MOTION_SETTLING = 2;
|
|
72
|
+
|
|
73
|
+
/** What the motion scan remembers of one caster mesh between frames. */
|
|
74
|
+
export interface ICasterMotion {
|
|
75
|
+
/** The 16 world-matrix elements at the last scan: where the cached levels last saw it. */
|
|
76
|
+
readonly world: Float64Array;
|
|
77
|
+
/** `instanceMatrix.version` at the last scan; unused on a plain mesh. */
|
|
78
|
+
instanceVersion: number;
|
|
79
|
+
phase: typeof MOTION_STATIC | typeof MOTION_MOVER | typeof MOTION_SETTLING;
|
|
80
|
+
/** `pinStatic` named this mesh or an ancestor. */
|
|
81
|
+
pinned: boolean;
|
|
82
|
+
/** Consecutive scans without movement while a mover. */
|
|
83
|
+
quiet: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* What a world publishes on its root for a shadow level to draw a map from GPU-scene keys:
|
|
88
|
+
* `(renderer, level) => void`, the level's own three numbers and the scene that selects against them.
|
|
89
|
+
*
|
|
90
|
+
* Engine-internal and duck-typed. `WorldCells` publishes it as `tnShadowGpuKeys`; nothing in a
|
|
91
|
+
* template or a manifest names it, and this module cannot import the world package to name the type,
|
|
92
|
+
* so the contract is the call and the flag behind it.
|
|
93
|
+
*/
|
|
94
|
+
export type IShadowKeyDispatch = (renderer: unknown, level: IShadowLevelNumbers) => void;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* One shadow map's own three numbers, duck-typed from `world-gpu-scene`'s `IShadowLevel`: the six
|
|
98
|
+
* planes of this map's own shadow camera, its texel gate in world metres, and the chain level it
|
|
99
|
+
* draws at. Typed here rather than imported for the
|
|
100
|
+
* same reason as the dispatch above.
|
|
101
|
+
*/
|
|
102
|
+
interface IShadowLevelNumbers {
|
|
103
|
+
readonly planes: Float32Array;
|
|
104
|
+
readonly gate: number;
|
|
105
|
+
readonly base: number;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The state cached maps depend on: visible, casting, and admitted by the main pass. */
|
|
109
|
+
/** A skinned mesh, or one with a morph target weighted above zero: its pixels change with no matrix change. */
|
|
110
|
+
export function deforms(mesh: Mesh): boolean {
|
|
111
|
+
if ((mesh as { isSkinnedMesh?: boolean }).isSkinnedMesh === true) return true;
|
|
112
|
+
const influences = mesh.morphTargetInfluences;
|
|
113
|
+
if (influences === undefined) return false;
|
|
114
|
+
for (const weight of influences) if (weight !== 0) return true;
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function casterFlag(mesh: {
|
|
119
|
+
castShadow?: boolean;
|
|
120
|
+
mainAdmitted?: boolean;
|
|
121
|
+
userData?: { tnShadowSwap?: unknown };
|
|
122
|
+
visible?: boolean;
|
|
123
|
+
}): number {
|
|
124
|
+
// A mesh marked tnShadowSwap trades visibility with a twin over the same ground — terrain's
|
|
125
|
+
// tile and merged block, or one LOD level for the next. That flip is not a change the cached map
|
|
126
|
+
// must redraw for (it measured ~80 % of a walk's flips); castShadow and mainAdmitted still are,
|
|
127
|
+
// and the next window move redraws whatever resolution difference a LOD step left behind.
|
|
128
|
+
return (
|
|
129
|
+
(mesh.visible === true || mesh.userData?.tnShadowSwap === true ? 1 : 0) |
|
|
130
|
+
(mesh.castShadow === true ? 2 : 0) |
|
|
131
|
+
(mesh.mainAdmitted === false ? 4 : 0)
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A `casterFlag` whose mesh draws into a level map: shown, casting and admitted. */
|
|
136
|
+
export function flagCasts(flag: number): boolean {
|
|
137
|
+
return (flag & 7) === 3;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The diameter a shadow level's texel gate judges one caster by: the part's own geometry radius
|
|
142
|
+
* times the largest instance scale `WorldCells` placed into this batch, or the mesh's own sphere
|
|
143
|
+
* when it publishes no scale (anything the world does not own).
|
|
144
|
+
*/
|
|
145
|
+
export function instanceDiameter(mesh: Mesh, sphereRadius: number): number {
|
|
146
|
+
const scale = (mesh as Mesh & { casterInstanceScale?: number }).casterInstanceScale;
|
|
147
|
+
if (scale === undefined || scale <= 0) return sphereRadius * 2;
|
|
148
|
+
const geometry = mesh.geometry;
|
|
149
|
+
if (geometry.boundingSphere === null) geometry.computeBoundingSphere();
|
|
150
|
+
const radius = geometry.boundingSphere?.radius;
|
|
151
|
+
if (radius === undefined || radius <= 0) return sphereRadius * 2;
|
|
152
|
+
return radius * 2 * scale;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The geometry a caster draws into a level past the finest: the coarsest rung of its AutoLOD chain
|
|
157
|
+
* whose error stays within `budget` (world units: texels of the level times `shadowLodTexels`), and
|
|
158
|
+
* never finer than the rung the main pass shows, which the viewer already cannot tell apart. Where
|
|
159
|
+
* no rung is that accurate the mesh keeps what it has. A rung whose error is more than a texel
|
|
160
|
+
* draws as a facet on the shadow's edge: the coarsest rung of a sphere, 60 mm out, put straight
|
|
161
|
+
* segments on a 7 mm texel. `undefined` leaves the geometry alone.
|
|
162
|
+
*/
|
|
163
|
+
export function shadowLod(mesh: Mesh, budget: number): BufferGeometry | undefined {
|
|
164
|
+
const chain = lodChainOf(mesh.geometry);
|
|
165
|
+
if (chain === undefined) return undefined;
|
|
166
|
+
// The largest instance scale `WorldCells` placed into this batch is on top of the mesh's own.
|
|
167
|
+
const placed = (mesh as Mesh & { casterInstanceScale?: number }).casterInstanceScale;
|
|
168
|
+
const scale =
|
|
169
|
+
mesh.matrixWorld.getMaxScaleOnAxis() * (placed !== undefined && placed > 0 ? placed : 1);
|
|
170
|
+
const shown = chain.levels.indexOf(mesh.geometry);
|
|
171
|
+
for (let rung = chain.levels.length - 1; rung > shown; rung -= 1) {
|
|
172
|
+
if ((chain.errors[rung] as number) * scale <= budget) return chain.levels[rung];
|
|
173
|
+
}
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The object layers a virtual shadow draws on, and the clip ladder its levels sit at.
|
|
3
|
+
*
|
|
4
|
+
* One owner for the numbers: the node renders the maps, `WorldCells` mints the casters on these
|
|
5
|
+
* bits, and the chunk proxies stand in for merged geometry. A copy that drifts puts the world's
|
|
6
|
+
* casters on a layer no level camera reads, and no error says so — the shadow is simply missing.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The object layer tracked casters are enabled on, so each level's mover camera sees only them.
|
|
11
|
+
* Keep it free of other uses; the main camera never needs it (tracked objects keep layer 0).
|
|
12
|
+
*/
|
|
13
|
+
export const VIRTUAL_SHADOW_MOVER_LAYER = 29;
|
|
14
|
+
/**
|
|
15
|
+
* The object layer a shadow-only caster is put on, so each level's own shadow camera renders it and
|
|
16
|
+
* the main camera never sees it. The level cameras have it enabled already; an object that exists
|
|
17
|
+
* only as a shadow caster calls `object.layers.set(VIRTUAL_SHADOW_CASTER_LAYER)`.
|
|
18
|
+
*/
|
|
19
|
+
// The layer a shadow-only object is put on: a level's own shadow camera renders it, the main
|
|
20
|
+
// camera never sees it. Three skips an object whose layers do not meet the camera's, and it
|
|
21
|
+
// overwrites a shadow camera's mask with the main camera's whenever that mask is layer 0 and
|
|
22
|
+
// nothing else — so enabling this bit on the level cameras is also what pins their mask, and a
|
|
23
|
+
// caster that lives only on it is reachable from the cascade and from nothing else. 28 is below the
|
|
24
|
+
// mover layer and off the main camera, which renders layer 0.
|
|
25
|
+
export const VIRTUAL_SHADOW_CASTER_LAYER = 28;
|
|
26
|
+
/**
|
|
27
|
+
* The wide counterpart of {@link VIRTUAL_SHADOW_CASTER_LAYER}: one caster mesh per
|
|
28
|
+
* `asset:level:part` holding every resident cell's records, for the levels whose window covers the
|
|
29
|
+
* whole resident ring. A level renders exactly one of the two caster layers — whichever submits
|
|
30
|
+
* fewer draws, counted off the meshes themselves, because a cluster per square is a draw per square
|
|
31
|
+
* for the same pixels once the window holds the ring. `WorldCells` writes both halves of every key,
|
|
32
|
+
* so whichever a level picks is there; the main camera renders neither.
|
|
33
|
+
*/
|
|
34
|
+
export const VIRTUAL_SHADOW_WIDE_CASTER_LAYER = 27;
|
|
35
|
+
/**
|
|
36
|
+
* The wide half of short assets: ferns, grass and bushes. `WorldCells` classifies assets by
|
|
37
|
+
* `shadows.smallCasterMetres`; every level then applies its measured texel gate, so a resolved
|
|
38
|
+
* shadow survives the fine window's edge. The main camera never draws this internal layer.
|
|
39
|
+
*/
|
|
40
|
+
export const VIRTUAL_SHADOW_SMALL_CASTER_LAYER = 26;
|
|
41
|
+
/**
|
|
42
|
+
* The counterpart of both caster layers for a GPU-driven map: one mesh per `asset:level:part`
|
|
43
|
+
* drawing that key's shadow twin, so a level submits one indirect draw per key instead of one per
|
|
44
|
+
* grid square. `WorldCells` mints these instead of the two caster halves when a shadow level draws
|
|
45
|
+
* from the GPU scene (`?tnShadowGpuKeys=1`), and every level camera carries this bit, which is why
|
|
46
|
+
* it is not one half of a choice: a key is the whole of what a keyed map draws of the world.
|
|
47
|
+
*/
|
|
48
|
+
export const VIRTUAL_SHADOW_KEY_LAYER = 25;
|
|
49
|
+
export const DEFAULT_CLIP_EXTENTS: readonly number[] = [16, 48, 144];
|
|
50
|
+
export const DEFAULT_MAX_LEVEL_RATIO = 2;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Levels with no step wider than `maxRatio`, and for each the authored level it came from (an
|
|
54
|
+
* inserted level names the authored level it leads into).
|
|
55
|
+
*/
|
|
56
|
+
export function clipLadder(
|
|
57
|
+
authored: readonly number[],
|
|
58
|
+
maxRatio: number,
|
|
59
|
+
): { readonly extents: number[]; readonly source: number[] } {
|
|
60
|
+
const extents: number[] = [];
|
|
61
|
+
const source: number[] = [];
|
|
62
|
+
authored.forEach((extent, index) => {
|
|
63
|
+
const previous = authored[index - 1];
|
|
64
|
+
if (previous !== undefined && previous > 0 && extent / previous > maxRatio) {
|
|
65
|
+
const steps = Math.ceil(Math.log(extent / previous) / Math.log(maxRatio));
|
|
66
|
+
const ratio = (extent / previous) ** (1 / steps);
|
|
67
|
+
for (let step = 1; step < steps; step += 1) {
|
|
68
|
+
extents.push(previous * ratio ** step);
|
|
69
|
+
source.push(index);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
extents.push(extent);
|
|
73
|
+
source.push(index);
|
|
74
|
+
});
|
|
75
|
+
return { extents, source };
|
|
76
|
+
}
|