@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.
Files changed (137) hide show
  1. package/README.md +3 -5
  2. package/capabilities.json +739 -50
  3. package/dist/{assets-CqvE429w.d.ts → assets-YUbXa3-Y.d.ts} +101 -2
  4. package/dist/{canvas-layer-C1-5CviZ.d.ts → canvas-layer-vCtanIeB.d.ts} +1 -1
  5. package/dist/{game-Jz4Fdoeb.d.ts → game-cb99J-YE.d.ts} +100 -31
  6. package/dist/hot.d.ts +4 -4
  7. package/dist/hot.js +23 -5
  8. package/dist/index.d.ts +1742 -29
  9. package/dist/index.js +22443 -12423
  10. package/dist/{model-lod-BDAUYgsS.d.ts → model-lod-CTpZ6imz.d.ts} +1 -1
  11. package/dist/net.js +39 -12
  12. package/dist/playtest.d.ts +4 -4
  13. package/dist/playtest.js +48 -9
  14. package/dist/react-css.js +23 -0
  15. package/dist/react.d.ts +2 -2
  16. package/dist/{renderer-9yEo9hPa.d.ts → renderer-rZm0hVqq.d.ts} +178 -5
  17. package/dist/ui-layer.d.ts +11 -2
  18. package/dist/ui-layer.js +76 -4
  19. package/dist/worker.d.ts +36 -0
  20. package/dist/worker.js +1088 -0
  21. package/dist/world.d.ts +11 -3
  22. package/dist/world.js +1117 -197
  23. package/mcp/animation-server.mjs +265 -0
  24. package/mcp/animation.mjs +22 -0
  25. package/mcp/call.mjs +105 -0
  26. package/mcp/devtools.mjs +5 -0
  27. package/mcp/engine-server.mjs +713 -37
  28. package/mcp/launch.mjs +3 -2
  29. package/mcp/servers.d.mts +2 -0
  30. package/mcp/servers.mjs +33 -8
  31. package/package.json +12 -8
  32. package/patches/three@0.185.1-prd269-upgrade.patch +308 -0
  33. package/patches/three@0.185.1.patch +481 -272
  34. package/src/animation-packet-state.ts +88 -0
  35. package/src/animation-pose-packet.ts +537 -0
  36. package/src/animation.ts +779 -31
  37. package/src/asset-cache.ts +719 -0
  38. package/src/assets.ts +521 -152
  39. package/src/audio.ts +29 -3
  40. package/src/auto-batch.ts +792 -0
  41. package/src/config.ts +2 -0
  42. package/src/frame-budget.ts +268 -2
  43. package/src/game.ts +525 -105
  44. package/src/grounding.ts +7 -6
  45. package/src/hot.ts +19 -5
  46. package/src/index.ts +381 -6
  47. package/src/input.ts +21 -2
  48. package/src/interpolation.ts +19 -1
  49. package/src/launch-diagnostics.ts +7 -2
  50. package/src/look/ao-multibounce.ts +29 -0
  51. package/src/look/ao-reach.ts +39 -0
  52. package/src/look/auto-exposure.ts +264 -0
  53. package/src/look/default-tone-mapping.ts +44 -0
  54. package/src/look/exposure-graph.ts +208 -0
  55. package/src/look/exposure-readback.ts +97 -0
  56. package/src/look/exposure-settings.ts +132 -0
  57. package/src/look/filmic-tone-mapping.ts +852 -0
  58. package/src/look/gi-registry.ts +55 -0
  59. package/src/look/local-exposure.ts +507 -0
  60. package/src/look/look-diagnostics.ts +24 -0
  61. package/src/look/motion-blur.ts +42 -0
  62. package/src/look/quality.ts +309 -0
  63. package/src/look/reflection-trace.ts +344 -0
  64. package/src/look/unreal-bloom.ts +295 -0
  65. package/src/look/vignette.ts +14 -0
  66. package/src/look/world-environment.ts +2033 -0
  67. package/src/loop.ts +25 -1
  68. package/src/merge-parts.ts +25 -10
  69. package/src/model-lod.ts +14 -0
  70. package/src/net-session.ts +58 -14
  71. package/src/ocean/spectral.ts +41 -7
  72. package/src/picking.ts +72 -5
  73. package/src/platform.ts +29 -1
  74. package/src/playtest.ts +54 -8
  75. package/src/profiling/FrameCounters.ts +166 -5
  76. package/src/profiling/FrameStages.ts +289 -0
  77. package/src/profiling/Spans.ts +39 -25
  78. package/src/profiling/debug-telemetry.ts +312 -0
  79. package/src/profiling/scene-warning.ts +42 -6
  80. package/src/projection-apply.ts +11 -3
  81. package/src/projection-content.ts +189 -0
  82. package/src/projection-marker.ts +6 -2
  83. package/src/projection-plan.ts +279 -28
  84. package/src/projection-skinned.ts +620 -5
  85. package/src/projection-stability.ts +16 -0
  86. package/src/projection-uniform.ts +5 -1
  87. package/src/quiet-markers.ts +56 -0
  88. package/src/render/budget-ladder.ts +256 -0
  89. package/src/render/chain.ts +89 -7
  90. package/src/render/exposure-units.ts +59 -0
  91. package/src/render/frame-time-budget.ts +162 -0
  92. package/src/render/gi-budget.ts +231 -0
  93. package/src/render/gi-visualize.ts +49 -0
  94. package/src/render/material-key.ts +1 -0
  95. package/src/render/probe-volume.ts +196 -36
  96. package/src/render/rough-diffuse.ts +177 -0
  97. package/src/render/scene-trace.ts +532 -0
  98. package/src/render/shadow-filter-budget.ts +237 -0
  99. package/src/render/sky-capture.ts +194 -0
  100. package/src/render/soft-shadow.ts +607 -0
  101. package/src/render/surface-cache.ts +418 -0
  102. package/src/render/traced-reflections.ts +408 -0
  103. package/src/render/two-sided-foliage.ts +120 -0
  104. package/src/render/virtual-shadow-pages.ts +112 -12
  105. package/src/render/virtual-shadow.ts +386 -38
  106. package/src/render-camera-cull.ts +536 -31
  107. package/src/render-pass-budget.ts +140 -22
  108. package/src/renderProjection.ts +65 -9
  109. package/src/renderer-config.ts +3 -2
  110. package/src/renderer.ts +289 -14
  111. package/src/resolution-scaler.ts +65 -7
  112. package/src/skeletal-mesh.ts +4 -0
  113. package/src/snow-field.ts +46 -0
  114. package/src/softbody.ts +6 -5
  115. package/src/startup-offscreen-bytes.ts +168 -0
  116. package/src/startup-readiness.ts +137 -2
  117. package/src/ui-bridge.ts +38 -0
  118. package/src/ui-hit-regions.ts +6 -2
  119. package/src/ui-layer.ts +1 -0
  120. package/src/ui-state.ts +32 -3
  121. package/src/version.ts +1 -1
  122. package/src/viewport.ts +8 -5
  123. package/src/warmup.ts +76 -10
  124. package/src/worker-audio.ts +579 -0
  125. package/src/worker.ts +842 -0
  126. package/src/world-cells.ts +4 -1
  127. package/gpl/LICENSE.GPL +0 -117
  128. package/gpl/convert.py +0 -192
  129. package/gpl/fixtures/make_world_fixture.py +0 -184
  130. package/gpl/recipes/_common.py +0 -180
  131. package/gpl/recipes/bake_ao.py +0 -111
  132. package/gpl/recipes/decimate.py +0 -70
  133. package/gpl/recipes/export_world.py +0 -699
  134. package/gpl/recipes/retarget.py +0 -131
  135. package/gpl/recipes/unwrap.py +0 -71
  136. package/mcp/blender-server.mjs +0 -689
  137. package/mcp/blender.mjs +0 -26
@@ -0,0 +1,208 @@
1
+ import { FloatType, NearestFilter, RenderTarget } from "three";
2
+ import {
3
+ Fn,
4
+ If,
5
+ Loop,
6
+ float,
7
+ int,
8
+ ivec2,
9
+ mix,
10
+ screenCoordinate,
11
+ smoothstep,
12
+ type texture,
13
+ textureLoad,
14
+ textureSize,
15
+ vec2,
16
+ vec4,
17
+ } from "three/tsl";
18
+ import type { Node } from "three/webgpu";
19
+ import type { IExposureSettings } from "./exposure-settings.js";
20
+
21
+ export type ColourTexture = ReturnType<typeof texture>;
22
+ export type Meter = (colour: Node<"vec4">, uv: Node<"vec2">) => Node<"vec2">;
23
+ export type Decode = (mean: Node<"float">) => Node<"float">;
24
+
25
+ export function exposureTarget(): RenderTarget {
26
+ return new RenderTarget(1, 1, {
27
+ type: FloatType,
28
+ minFilter: NearestFilter,
29
+ magFilter: NearestFilter,
30
+ depthBuffer: false,
31
+ });
32
+ }
33
+
34
+ /** Sum/16 at each level keeps values bounded. Mask partial blocks; never replicate edge texels. */
35
+ export function reduceExposure(
36
+ input: ColourTexture,
37
+ size: Node<"vec2">,
38
+ meter?: Meter,
39
+ ): Node<"vec4"> {
40
+ return Fn(() => {
41
+ // TextureSizeNode omits typed TSL extensions; retain its checked Node base before narrowing.
42
+ const measuredSize: Node = meter === undefined ? size : textureSize(input);
43
+ const sourceSize = meter === undefined ? size : vec2(measuredSize as Node<"uvec2">);
44
+ const sum = vec2(0).toVar();
45
+ const origin = screenCoordinate.xy.floor().mul(4);
46
+ for (let y = 0; y < 4; y++)
47
+ for (let x = 0; x < 4; x++) {
48
+ const pixel = origin.add(vec2(x, y));
49
+ If(pixel.x.lessThan(sourceSize.x).and(pixel.y.lessThan(sourceSize.y)), () => {
50
+ const colour = textureLoad(input, ivec2(pixel));
51
+ sum.addAssign(
52
+ meter === undefined ? colour.rg : meter(colour, pixel.add(0.5).div(sourceSize)),
53
+ );
54
+ });
55
+ }
56
+ return vec4(sum.div(16), 0, 1);
57
+ })();
58
+ }
59
+
60
+ /** The histogram has 64 bins. Each bin spans (maxStops - minStops) / 64 stops of scene luminance. */
61
+ export const exposureBins = 64;
62
+ /** The gather splits the blocks over 64 tiles, so a bin is summed by 64 short loops, not one long. */
63
+ export const exposureTiles = 64;
64
+
65
+ /** Unreal's histogram floor and bucket width in log2 luminance: EV100 -10 to 20 in 64 buckets (Scene.cpp:498-509). */
66
+ const BLACK_FLOOR = -10;
67
+ const BLACK_BUCKET = 30 / 63;
68
+
69
+ /**
70
+ * The share of a block's weight Unreal keeps at this log2 luminance. Unreal splits a sample between
71
+ * the two of its 64 buckets around its position and drops the share in bucket 0
72
+ * (r.EyeAdaptation.BlackHistogramBucketInfluence 0, UE 5.8.3:
73
+ * Engine/Source/Runtime/Renderer/Private/PostProcess/PostProcessEyeAdaptation.cpp:110-115 and
74
+ * Engine/Shaders/Private/PostProcessHistogram.usf:150-179), so a black void does not open the eye.
75
+ * The floor is Unreal's, not this histogram's: a game's narrow `minStops`..`maxStops` keeps its dark blocks.
76
+ * ponytail: judged per 16x16 block mean, not per pixel; a block that mixes void and lit pixels counts whole.
77
+ */
78
+ export function blackBucketWeight(log2Luminance: number): number {
79
+ return Math.min(1, Math.max(0, (log2Luminance - BLACK_FLOOR) / BLACK_BUCKET));
80
+ }
81
+
82
+ /**
83
+ * Stage one of the histogram, drawn as a 64 (bins) x 64 (tiles) target. Texel (bin, tile) gathers
84
+ * its tile of the block means of `blocks` (the second reduction level, about 8 000 texels at
85
+ * 1080p, so about 130 per tile) that fall in its bin.
86
+ * r = summed weight, g = summed weight * log2 luminance, so a bin keeps its exact mean.
87
+ */
88
+ export function histogramTiles(
89
+ blocks: ColourTexture,
90
+ size: Node<"vec2">,
91
+ policy: Readonly<IExposureSettings>,
92
+ ): Node<"vec4"> {
93
+ // Range follows the authored stops: goal = log2(key) - log2(L), clamped to [min, max].
94
+ const low = Math.log2(policy.key) - policy.maxStops;
95
+ const span = policy.maxStops - policy.minStops;
96
+ return Fn(() => {
97
+ const bin = screenCoordinate.x.floor();
98
+ const weight = float(0).toVar();
99
+ const logSum = float(0).toVar();
100
+ const width = int(size.x);
101
+ const total = int(size.x.mul(size.y));
102
+ const perTile = total.add(exposureTiles - 1).div(exposureTiles);
103
+ const first = int(screenCoordinate.y.floor()).mul(perTile);
104
+ Loop({ start: 0, end: perTile }, ({ i: offset }) => {
105
+ const i = first.add(offset);
106
+ If(i.lessThan(total), () => {
107
+ const block = textureLoad(blocks, ivec2(i.mod(width), i.div(width)));
108
+ const raw = block.r.div(block.g.max(1e-20)).max(1e-20).log2();
109
+ const log = raw.clamp(low, low + span);
110
+ const index = log
111
+ .sub(low)
112
+ .mul(exposureBins / span)
113
+ .floor()
114
+ .min(exposureBins - 1);
115
+ const kept = block.g.mul(raw.sub(BLACK_FLOOR).div(BLACK_BUCKET).clamp(0, 1));
116
+ If(kept.greaterThan(0).and(index.equal(bin)), () => {
117
+ weight.addAssign(kept);
118
+ logSum.addAssign(kept.mul(log));
119
+ });
120
+ });
121
+ });
122
+ return vec4(weight, logSum, 0, 1);
123
+ })();
124
+ }
125
+
126
+ /** Stage two, drawn as a 64x1 target: one texel per bin, summing that bin over every tile. */
127
+ export function histogramExposure(tiles: ColourTexture): Node<"vec4"> {
128
+ return Fn(() => {
129
+ const bin = int(screenCoordinate.x.floor());
130
+ const sum = vec2(0).toVar();
131
+ Loop(exposureTiles, ({ i }) => {
132
+ sum.addAssign(textureLoad(tiles, ivec2(bin, i)).rg);
133
+ });
134
+ return vec4(sum, 0, 1);
135
+ })();
136
+ }
137
+
138
+ /**
139
+ * UE's average without outliers: drop the darkest lowPercent of the weight, stop at
140
+ * highPercent, take the weighted log2 mean of what is left. Returns (log2 mean, kept weight).
141
+ */
142
+ function clippedLog2Mean(
143
+ histogram: ColourTexture,
144
+ policy: Readonly<IExposureSettings>,
145
+ ): Node<"vec2"> {
146
+ const total = float(0).toVar();
147
+ Loop(exposureBins, ({ i }) => {
148
+ total.addAssign(textureLoad(histogram, ivec2(i, 0)).r);
149
+ });
150
+ const dropLow = total.mul(policy.lowPercent / 100).toVar();
151
+ const keepUpTo = total.mul(policy.highPercent / 100).toVar();
152
+ const kept = float(0).toVar();
153
+ const logSum = float(0).toVar();
154
+ Loop(exposureBins, ({ i }) => {
155
+ const bin = textureLoad(histogram, ivec2(i, 0));
156
+ // toVar: each is read after a variable it depends on was assigned, so it must hold the value it had
157
+ // when it was computed. A bare expression node is evaluated again at every use, after the assignments.
158
+ const removed = bin.r.min(dropLow).toVar();
159
+ dropLow.subAssign(removed);
160
+ keepUpTo.subAssign(removed);
161
+ const keep = bin.r.sub(removed).min(keepUpTo).toVar();
162
+ keepUpTo.subAssign(keep);
163
+ kept.addAssign(keep);
164
+ logSum.addAssign(bin.g.mul(keep.div(bin.r.max(1e-20))));
165
+ });
166
+ return vec2(logSum.div(kept.max(1e-20)), kept);
167
+ }
168
+
169
+ export function adaptExposure(
170
+ histogram: ColourTexture,
171
+ previous: ColourTexture,
172
+ resetMode: Node<"float">,
173
+ seed: Node<"float">,
174
+ delta: Node<"float">,
175
+ policy: Readonly<IExposureSettings>,
176
+ decode: Decode,
177
+ bias: Node<"float"> = float(0),
178
+ ): Node<"vec4"> {
179
+ return Fn(() => {
180
+ const measure = clippedLog2Mean(histogram, policy);
181
+ const luminance = decode(measure.x.exp2());
182
+ const valid = measure.y
183
+ .greaterThan(0)
184
+ .and(luminance.greaterThan(0))
185
+ .and(luminance.lessThan(3.4e38));
186
+ const goal = float(Math.log2(policy.key))
187
+ .add(bias)
188
+ .sub(luminance.max(1e-20).log2())
189
+ .clamp(policy.minStops, policy.maxStops);
190
+ const history = textureLoad(previous, ivec2(0));
191
+ const old = resetMode.equal(1).select(seed, history.r);
192
+ // History with no measured luminance (a target never drawn reads 0, an empty histogram wrote -1)
193
+ // is no history: adopt the target, as Unreal forces it on a view's first frame. On the Wasm
194
+ // engine the first adapt draws are skipped while pipelines compile, so a first-draw-only snap
195
+ // was lost and Lantern Vale opened 1.8 stops dark, then brightened for a second after its cover.
196
+ const fresh = resetMode.equal(2).or(resetMode.equal(0).and(history.g.lessThanEqual(0)));
197
+ const error = goal.sub(old);
198
+ const rate = error.greaterThan(0).select(float(policy.rateUp), float(policy.rateDown));
199
+ const normal = float(1).sub(delta.mul(rate).negate().exp());
200
+ const cut = smoothstep(policy.snapLo, policy.snapHi, error.abs()).mul(policy.snapGain);
201
+ const adapted = valid.select(
202
+ fresh.select(goal, mix(old, goal, mix(normal, float(1), cut))),
203
+ old,
204
+ );
205
+ const settled = goal.sub(adapted).abs().lessThanEqual(policy.settleStops).select(1, 0);
206
+ return vec4(adapted, valid.select(luminance, -1), goal, settled);
207
+ })();
208
+ }
@@ -0,0 +1,97 @@
1
+ import type { RenderTarget } from "three";
2
+ import type { Renderer } from "three/webgpu";
3
+
4
+ /**
5
+ * A cold start reads a target the adapt pass has not drawn yet: pipelines compile asynchronously,
6
+ * so the first draws are skipped and the 1x1 target is still all zero. The adapt pass can also draw
7
+ * before the histogram pass has: then its histogram is empty and it writes -1 into luminance. This
8
+ * happens at a cold start and after every reset. Both are "not measured yet": retry on the next
9
+ * frame, and fail only when no measurement arrives. Any other bad value fails at once.
10
+ */
11
+ const COLD_READS_BEFORE_FAILURE = 120;
12
+
13
+ export class ExposureReadback {
14
+ #cold = 0;
15
+ #pending = false;
16
+ #generation = 0;
17
+ #lastReport = Number.NEGATIVE_INFINITY;
18
+ #disposed = false;
19
+ #value: Record<string, unknown>;
20
+ constructor(
21
+ readonly constant: number,
22
+ readonly enabled: () => boolean,
23
+ ) {
24
+ this.#value = { measured: false, applied: enabled() };
25
+ }
26
+ invalidate(): void {
27
+ this.#generation++;
28
+ this.#value = { measured: false, applied: this.enabled() };
29
+ this.#lastReport = Number.NEGATIVE_INFINITY;
30
+ }
31
+ get(): Record<string, unknown> {
32
+ return { ...this.#value };
33
+ }
34
+ dispose(): void {
35
+ this.#disposed = true;
36
+ this.#generation++;
37
+ }
38
+ report(renderer: Renderer, result: RenderTarget, time: number, interval: number): void {
39
+ if (this.#pending || time - this.#lastReport < interval) return;
40
+ this.#pending = true;
41
+ this.#lastReport = time;
42
+ const generation = this.#generation;
43
+ const current = () => !this.#disposed && generation === this.#generation;
44
+ const enabled = this.enabled;
45
+ const constant = this.constant;
46
+ const publish = (observation: Record<string, unknown>) => {
47
+ this.#value = observation;
48
+ };
49
+ const finished = () => {
50
+ this.#pending = false;
51
+ };
52
+ void renderer
53
+ .readRenderTargetPixelsAsync(result, 0, 0, 1, 1)
54
+ .then((values) => {
55
+ if (!current()) return;
56
+ if (
57
+ values instanceof Float32Array &&
58
+ (values[1] === 0 || values[1] === -1) &&
59
+ ++this.#cold < COLD_READS_BEFORE_FAILURE
60
+ ) {
61
+ this.#lastReport = Number.NEGATIVE_INFINITY;
62
+ return;
63
+ }
64
+ this.#cold = 0;
65
+ if (
66
+ !(values instanceof Float32Array) ||
67
+ values.length < 4 ||
68
+ !Array.from(values).every(Number.isFinite) ||
69
+ (values[1] ?? 0) <= 0
70
+ )
71
+ throw new Error("Exposure readback is invalid; measurement is unavailable.");
72
+ const observation = {
73
+ measured: true,
74
+ applied: enabled(),
75
+ luminance: values[1],
76
+ exposureStops: enabled() ? values[0] : Math.log2(constant),
77
+ targetStops: values[2],
78
+ settled: values[3] === 1,
79
+ };
80
+ publish(observation);
81
+ console.info(`TN_AUTO_EXPOSURE:${JSON.stringify(observation)}`);
82
+ })
83
+ .catch((error: unknown) => {
84
+ if (!current()) return;
85
+ const observation = {
86
+ measured: false,
87
+ applied: enabled(),
88
+ reason: String(error),
89
+ };
90
+ publish(observation);
91
+ console.error(`TN_AUTO_EXPOSURE:${JSON.stringify(observation)}`);
92
+ })
93
+ .finally(() => {
94
+ finished();
95
+ });
96
+ }
97
+ }
@@ -0,0 +1,132 @@
1
+ import { dot, float, vec2, vec3 } from "three/tsl";
2
+ import type { Node } from "three/webgpu";
3
+
4
+ export interface IExposureSettings {
5
+ enabled: boolean;
6
+ /** Middle-grey target; increase to brighten the metered subject. */
7
+ key: number;
8
+ /** Exposure limits in stops. Expand only if the scene needs more range. */
9
+ minStops: number;
10
+ maxStops: number;
11
+ /**
12
+ * Percent of metered weight, dark to bright, ignored at each end of the histogram.
13
+ * Raise highPercent's gap (lower it) when a large bright backdrop still sets the exposure.
14
+ */
15
+ lowPercent: number;
16
+ highPercent: number;
17
+ /** Exponential response per second in log2 space. Up opens the eye; down squints. */
18
+ rateUp: number;
19
+ rateDown: number;
20
+ /** Stop error where cut acceleration begins / reaches its authored maximum. */
21
+ snapLo: number;
22
+ snapHi: number;
23
+ snapGain: number;
24
+ /**
25
+ * Seed for reset(value). The first frame and reset() without a value adopt the measured target.
26
+ */
27
+ initialExposure: number;
28
+ settleStops: number;
29
+ /**
30
+ * Limit catch-up after a suspended frame, avoiding an unrequested cut. A frame the renderer's
31
+ * gate declined is not a suspension: the drawn frame already spans that time, so it is spent.
32
+ */
33
+ maxDelta: number;
34
+ /** Readback/marker cadence in seconds. Metering still runs on every drawn frame. */
35
+ reportInterval: number;
36
+ }
37
+
38
+ // Unreal 5.8.3 project defaults (PRD-632; Engine/Source/Runtime/Engine/Private/Scene.cpp:494,
39
+ // :520 and SceneView.cpp:183-190): histogram metering that ignores the darkest and brightest
40
+ // 10%, mid grey 0.18 lifted by the +1 EV exposure bias (key 0.18 x 2^1), 3 stops/s brightening,
41
+ // 1 stop/s darkening. The meter averages log2 luminance (Engine/Shaders/Private/
42
+ // PostProcessHistogramCommon.ush, ComputeAverageLuminanceWithoutOutlier) and sets
43
+ // exposure = key / average (EyeAdaptationCommon in PostProcessEyeAdaptation.usf).
44
+ //
45
+ // The -10..+20 EV100 range clamps the average luminance to 2^-10..2^20 (the lens attenuation of
46
+ // 0.78 makes the luminance scale 1: PostProcessEyeAdaptation.cpp LuminanceMaxFromLensAttenuation),
47
+ // so the exposure, in stops, runs from log2(key) - 20 to log2(key) + 10.
48
+ const KEY = 0.36;
49
+ export const exposureSettings: Readonly<IExposureSettings> = {
50
+ enabled: true,
51
+ key: KEY,
52
+ minStops: Math.log2(KEY) - 20,
53
+ maxStops: Math.log2(KEY) + 10,
54
+ lowPercent: 10,
55
+ highPercent: 90,
56
+ rateUp: 3,
57
+ rateDown: 1,
58
+ snapLo: 3,
59
+ snapHi: 8,
60
+ snapGain: 1,
61
+ initialExposure: 1,
62
+ settleStops: 0.25,
63
+ maxDelta: 0.1,
64
+ reportInterval: 0.5,
65
+ };
66
+
67
+ /**
68
+ * Return weighted luminance and weight. The histogram's percent clip, not a clamp here, rejects
69
+ * small bright sources. The upper bound only keeps a half-float overflow from poisoning a block.
70
+ * Unreal weights red, green and blue equally by default (r.AutoExposure.LuminanceMethod 0, the
71
+ * uniform weights in Engine/Source/Runtime/Renderer/Private/PostProcess/PostProcessEyeAdaptation.cpp
72
+ * and CalculateEyeAdaptationLuminance in Engine/Shaders/Private/PostProcessHistogramCommon.ush),
73
+ * not Rec. 709, so a blue sky meters brighter than it does with the Rec. 709 weights.
74
+ */
75
+ export function exposureMeter(colour: Node<"vec4">, uv: Node<"vec2">): Node<"vec2"> {
76
+ const luminance = dot(colour.rgb, vec3(1 / 3, 1 / 3, 1 / 3)).clamp(0.0001, 65504);
77
+ // Unreal meters the whole view with weight 1 unless a meter mask texture is set
78
+ // (EyeAdaptation_MeterMaskTexture, white by default); there is no bottom-of-frame preference.
79
+ void uv;
80
+ const weight = float(1);
81
+ return vec2(luminance, weight);
82
+ }
83
+
84
+ /** Receives the clipped histogram's linear mean luminance. Change it if the game encodes RMS. */
85
+ export function exposureLuminance(mean: Node<"float">): Node<"float"> {
86
+ return mean.max(0.0001);
87
+ }
88
+
89
+ export function validateExposureSettings(value: IExposureSettings): void {
90
+ for (const key of Object.keys(exposureSettings) as (keyof IExposureSettings)[]) {
91
+ if (key === "enabled") continue;
92
+ const number = value[key];
93
+ if (typeof number !== "number" || !Number.isFinite(Math.fround(number)))
94
+ throw new Error(`Invalid exposure ${key}: expected a finite float32 number.`);
95
+ }
96
+ if (
97
+ typeof value.enabled !== "boolean" ||
98
+ Math.fround(value.key) <= 0 ||
99
+ Math.fround(value.initialExposure) <= 0 ||
100
+ value.rateUp < 0 ||
101
+ value.rateDown < 0 ||
102
+ value.snapLo < 0 ||
103
+ value.snapHi <= value.snapLo ||
104
+ value.snapGain < 0 ||
105
+ value.snapGain > 1 ||
106
+ value.lowPercent < 0 ||
107
+ value.highPercent > 100 ||
108
+ value.lowPercent >= value.highPercent ||
109
+ value.minStops >= value.maxStops ||
110
+ value.minStops < -126 ||
111
+ value.maxStops > 126 ||
112
+ value.settleStops < 0 ||
113
+ value.maxDelta <= 0 ||
114
+ value.reportInterval <= 0
115
+ )
116
+ throw new Error("Invalid exposure settings: check limits, rates and thresholds.");
117
+ }
118
+
119
+ /** Resize only the meter. The separate history targets always remain 1x1. */
120
+ export function exposureReductionSizes(width: number, height: number): [number, number][] {
121
+ if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width < 1 || height < 1)
122
+ throw new Error("Exposure drawing buffer dimensions must be positive integers.");
123
+ const sizes: [number, number][] = [];
124
+ let w = width;
125
+ let h = height;
126
+ do {
127
+ w = Math.ceil(w / 4);
128
+ h = Math.ceil(h / 4);
129
+ sizes.push([w, h]);
130
+ } while (w > 1 || h > 1);
131
+ return sizes;
132
+ }