@threenative/core 0.3.4 → 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 +1505 -79
- package/dist/{audio-7i3Xl0l3.d.ts → audio-DIJE5HTr.d.ts} +43 -3
- package/dist/{canvas-layer-DDmC_VVF.d.ts → canvas-layer-C1-5CviZ.d.ts} +5 -1
- package/dist/{game-CljaDv4D.d.ts → game-Bj8G1wmD.d.ts} +102 -11
- package/dist/hot.d.ts +4 -4
- package/dist/index.d.ts +712 -118
- package/dist/index.js +12423 -4955
- package/dist/{gpu-readback-CqJEfWNQ.d.ts → model-lod-BDAUYgsS.d.ts} +80 -5
- package/dist/playtest.d.ts +6 -10
- package/dist/playtest.js +47 -24
- 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-CfsS2hxi.d.ts → renderer-9yEo9hPa.d.ts} +489 -22
- package/dist/terrain-jobs-worker.d.ts +2 -0
- package/dist/terrain-jobs-worker.js +130 -0
- package/dist/ui-layer.js +59 -8
- package/dist/world.d.ts +766 -73
- package/dist/world.js +9307 -3543
- package/gpl/recipes/export_world.py +18 -1
- package/mcp/blender-server.mjs +7 -4
- package/mcp/blender.mjs +3 -4
- package/mcp/engine-server.mjs +2 -2
- 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 +6594 -700
- package/scripts/apply-three-patch.mjs +62 -4
- 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
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bitmap glyph set the native React overlay draws text with.
|
|
3
|
+
*
|
|
4
|
+
* This is the same mechanism the template geometry HUDs already use
|
|
5
|
+
* (`templates/minimal/src/render/hud.ts`): a 5x7 pixel grid per character, one instanced quad per
|
|
6
|
+
* lit pixel. It needs no font file, no texture, no `document`, and no rasteriser, which is why it
|
|
7
|
+
* is the only text path that works identically in a browser and inside the native host.
|
|
8
|
+
*
|
|
9
|
+
* The set is deliberately a *default*, not a decision: a `text` element may carry its own `font`
|
|
10
|
+
* prop, so a game that wants different letterforms never edits this package. What is fixed here is
|
|
11
|
+
* the grid, because the layout pass measures against it.
|
|
12
|
+
*
|
|
13
|
+
* Unmapped characters are not silently dropped — {@link glyphPixels} returns `undefined` and the host
|
|
14
|
+
* reports `TN_REACT_UNKNOWN_GLYPH` by name, per the PRD's negative control.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Pixel columns in one glyph cell. */
|
|
18
|
+
export const GLYPH_WIDTH = 5;
|
|
19
|
+
/** Pixel rows in one glyph cell. */
|
|
20
|
+
export const GLYPH_HEIGHT = 7;
|
|
21
|
+
/** Columns from one glyph's left edge to the next, before `letterSpacing`. */
|
|
22
|
+
export const GLYPH_ADVANCE = 6;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Rows are written as art so a reader can see the letter: `#` is a lit pixel, `.` is empty, and each
|
|
26
|
+
* entry is exactly {@link GLYPH_HEIGHT} rows of {@link GLYPH_WIDTH} characters.
|
|
27
|
+
*
|
|
28
|
+
* Lowercase input is upper-cased before lookup. There is no lowercase form in a 5x7 cell that stays
|
|
29
|
+
* legible at HUD sizes, and pretending otherwise would draw blanks.
|
|
30
|
+
*/
|
|
31
|
+
const GLYPHS: Readonly<Record<string, string>> = {
|
|
32
|
+
" ": ".".repeat(GLYPH_WIDTH * GLYPH_HEIGHT),
|
|
33
|
+
"0": ".###." + "#...#" + "#..##" + "#.#.#" + "##..#" + "#...#" + ".###.",
|
|
34
|
+
"1": "..#.." + ".##.." + "..#.." + "..#.." + "..#.." + "..#.." + ".###.",
|
|
35
|
+
"2": ".###." + "#...#" + "....#" + "...#." + "..#.." + ".#..." + "#####",
|
|
36
|
+
"3": "#####" + "...#." + "..#.." + "...#." + "....#" + "#...#" + ".###.",
|
|
37
|
+
"4": "...#." + "..##." + ".#.#." + "#..#." + "#####" + "...#." + "...#.",
|
|
38
|
+
"5": "#####" + "#...." + "####." + "....#" + "....#" + "#...#" + ".###.",
|
|
39
|
+
"6": "..##." + ".#..." + "#...." + "####." + "#...#" + "#...#" + ".###.",
|
|
40
|
+
"7": "#####" + "....#" + "...#." + "..#.." + ".#..." + ".#..." + ".#...",
|
|
41
|
+
"8": ".###." + "#...#" + "#...#" + ".###." + "#...#" + "#...#" + ".###.",
|
|
42
|
+
"9": ".###." + "#...#" + "#...#" + ".####" + "....#" + "...#." + ".##..",
|
|
43
|
+
A: "..#.." + ".#.#." + "#...#" + "#...#" + "#####" + "#...#" + "#...#",
|
|
44
|
+
B: "####." + "#...#" + "#...#" + "####." + "#...#" + "#...#" + "####.",
|
|
45
|
+
C: ".####" + "#...." + "#...." + "#...." + "#...." + "#...." + ".####",
|
|
46
|
+
D: "###.." + "#..#." + "#...#" + "#...#" + "#...#" + "#..#." + "###..",
|
|
47
|
+
E: "#####" + "#...." + "#...." + "####." + "#...." + "#...." + "#####",
|
|
48
|
+
F: "#####" + "#...." + "#...." + "####." + "#...." + "#...." + "#....",
|
|
49
|
+
G: ".###." + "#...#" + "#...." + "#..##" + "#...#" + "#...#" + ".###.",
|
|
50
|
+
H: "#...#" + "#...#" + "#...#" + "#####" + "#...#" + "#...#" + "#...#",
|
|
51
|
+
I: ".###." + "..#.." + "..#.." + "..#.." + "..#.." + "..#.." + ".###.",
|
|
52
|
+
J: "..###" + "...#." + "...#." + "...#." + "...#." + "#..#." + ".##..",
|
|
53
|
+
K: "#...#" + "#..#." + "#.#.." + "##..." + "#.#.." + "#..#." + "#...#",
|
|
54
|
+
L: "#...." + "#...." + "#...." + "#...." + "#...." + "#...." + "#####",
|
|
55
|
+
M: "#...#" + "##.##" + "#.#.#" + "#.#.#" + "#...#" + "#...#" + "#...#",
|
|
56
|
+
N: "#...#" + "##..#" + "##..#" + "#.#.#" + "#..##" + "#..##" + "#...#",
|
|
57
|
+
O: ".###." + "#...#" + "#...#" + "#...#" + "#...#" + "#...#" + ".###.",
|
|
58
|
+
P: "####." + "#...#" + "#...#" + "####." + "#...." + "#...." + "#....",
|
|
59
|
+
Q: ".###." + "#...#" + "#...#" + "#...#" + "#.#.#" + "#..#." + ".##.#",
|
|
60
|
+
R: "####." + "#...#" + "#...#" + "####." + "#.#.." + "#..#." + "#...#",
|
|
61
|
+
S: ".####" + "#...." + "#...." + ".###." + "....#" + "....#" + "####.",
|
|
62
|
+
T: "#####" + "..#.." + "..#.." + "..#.." + "..#.." + "..#.." + "..#..",
|
|
63
|
+
U: "#...#" + "#...#" + "#...#" + "#...#" + "#...#" + "#...#" + ".###.",
|
|
64
|
+
V: "#...#" + "#...#" + "#...#" + "#...#" + "#...#" + ".#.#." + "..#..",
|
|
65
|
+
W: "#...#" + "#...#" + "#...#" + "#.#.#" + "#.#.#" + "##.##" + "#...#",
|
|
66
|
+
X: "#...#" + "#...#" + ".#.#." + "..#.." + ".#.#." + "#...#" + "#...#",
|
|
67
|
+
Y: "#...#" + "#...#" + ".#.#." + "..#.." + "..#.." + "..#.." + "..#..",
|
|
68
|
+
Z: "#####" + "....#" + "...#." + "..#.." + ".#..." + "#...." + "#####",
|
|
69
|
+
":": "....." + "..#.." + "..#.." + "....." + "..#.." + "..#.." + ".....",
|
|
70
|
+
".": "....." + "....." + "....." + "....." + "....." + "..#.." + "..#..",
|
|
71
|
+
",": "....." + "....." + "....." + "....." + "..#.." + "..#.." + ".#...",
|
|
72
|
+
"-": "....." + "....." + "....." + "#####" + "....." + "....." + ".....",
|
|
73
|
+
"+": "....." + "..#.." + "..#.." + "#####" + "..#.." + "..#.." + ".....",
|
|
74
|
+
"=": "....." + "....." + "#####" + "....." + "#####" + "....." + ".....",
|
|
75
|
+
"/": "....#" + "....#" + "...#." + "..#.." + ".#..." + "#...." + "#....",
|
|
76
|
+
"|": "..#.." + "..#.." + "..#.." + "..#.." + "..#.." + "..#.." + "..#..",
|
|
77
|
+
"!": "..#.." + "..#.." + "..#.." + "..#.." + "..#.." + "....." + "..#..",
|
|
78
|
+
"?": ".###." + "#...#" + "....#" + "...#." + "..#.." + "....." + "..#..",
|
|
79
|
+
"'": "..#.." + "..#.." + "....." + "....." + "....." + "....." + ".....",
|
|
80
|
+
"(": "...#." + "..#.." + ".#..." + ".#..." + ".#..." + "..#.." + "...#.",
|
|
81
|
+
")": ".#..." + "..#.." + "...#." + "...#." + "...#." + "..#.." + ".#...",
|
|
82
|
+
"[": ".###." + ".#..." + ".#..." + ".#..." + ".#..." + ".#..." + ".###.",
|
|
83
|
+
"]": ".###." + "...#." + "...#." + "...#." + "...#." + "...#." + ".###.",
|
|
84
|
+
"#": ".#.#." + ".#.#." + "#####" + ".#.#." + "#####" + ".#.#." + ".#.#.",
|
|
85
|
+
$: "..#.." + ".####" + "#.#.." + ".###." + "..#.#" + "####." + "..#..",
|
|
86
|
+
"%": "##..#" + "##.#." + "...#." + "..#.." + ".#..." + ".#.##" + "#..##",
|
|
87
|
+
"*": "....." + "#.#.#" + ".###." + "#####" + ".###." + "#.#.#" + ".....",
|
|
88
|
+
"·": "....." + "....." + "....." + "..#.." + "....." + "....." + ".....",
|
|
89
|
+
"°": ".##.." + "#..#." + ".##.." + "....." + "....." + "....." + ".....",
|
|
90
|
+
"<": "...#." + "..#.." + ".#..." + "#...." + ".#..." + "..#.." + "...#.",
|
|
91
|
+
">": ".#..." + "..#.." + "...#." + "....#" + "...#." + "..#.." + ".#...",
|
|
92
|
+
_: "....." + "....." + "....." + "....." + "....." + "....." + "#####",
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** A glyph's lit pixels, as `[x, y]` offsets inside its 5x7 cell. `undefined` when unmapped. */
|
|
96
|
+
export function glyphPixels(character: string): readonly (readonly [number, number])[] | undefined {
|
|
97
|
+
const art = GLYPHS[character] ?? GLYPHS[character.toUpperCase()];
|
|
98
|
+
if (art === undefined) return undefined;
|
|
99
|
+
const pixels: [number, number][] = [];
|
|
100
|
+
for (let index = 0; index < GLYPH_WIDTH * GLYPH_HEIGHT; index += 1) {
|
|
101
|
+
if (art[index] !== "#") continue;
|
|
102
|
+
pixels.push([index % GLYPH_WIDTH, Math.floor(index / GLYPH_WIDTH)]);
|
|
103
|
+
}
|
|
104
|
+
return pixels;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Every character this glyph set can draw, for error messages and for the templates' AGENTS.md.
|
|
109
|
+
* @situation discover which characters a native React HUD can draw
|
|
110
|
+
* @example supportedGlyphs().includes("A")
|
|
111
|
+
*/
|
|
112
|
+
export function supportedGlyphs(): string {
|
|
113
|
+
return Object.keys(GLYPHS).join("");
|
|
114
|
+
}
|
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import ReactReconciler from "react-reconciler";
|
|
3
|
+
import { DiscreteEventPriority, NoEventPriority } from "react-reconciler/constants.js";
|
|
4
|
+
import {
|
|
5
|
+
Color,
|
|
6
|
+
Group,
|
|
7
|
+
InstancedMesh,
|
|
8
|
+
Matrix4,
|
|
9
|
+
Mesh,
|
|
10
|
+
MeshBasicMaterial,
|
|
11
|
+
type Object3D,
|
|
12
|
+
PlaneGeometry,
|
|
13
|
+
} from "three";
|
|
14
|
+
import type { CanvasLayer } from "./canvas-layer.js";
|
|
15
|
+
import { GLYPH_HEIGHT, glyphPixels, supportedGlyphs } from "./react-glyphs.js";
|
|
16
|
+
import {
|
|
17
|
+
type ILayoutNode,
|
|
18
|
+
type IOverlayStyle,
|
|
19
|
+
type OverlayColor,
|
|
20
|
+
assertKnownStyle,
|
|
21
|
+
layoutTree,
|
|
22
|
+
textOffset,
|
|
23
|
+
} from "./react-layout.js";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A React renderer that commits to `CanvasLayer` instead of the DOM.
|
|
27
|
+
*
|
|
28
|
+
* `react` is the component model and has no DOM in it; `react-dom` is one renderer among several,
|
|
29
|
+
* and `react-reconciler` is the supported way to write another. The native host has no DOM to
|
|
30
|
+
* render into and no rasteriser to paint one with, so this maps React elements straight onto
|
|
31
|
+
* Three.js objects inside the orthographic, screen-pixel `CanvasLayer` that `renderOverlay` already
|
|
32
|
+
* draws on every platform. Nothing here imports `react-dom`, which is what keeps
|
|
33
|
+
* `TN_NATIVE_WEB_ONLY_UI` satisfied on a native bundle.
|
|
34
|
+
*
|
|
35
|
+
* There are exactly two element types, and that is the whole vocabulary:
|
|
36
|
+
*
|
|
37
|
+
* - `<view>` — a rectangle. Draws when it has a `background`; otherwise it only positions children.
|
|
38
|
+
* - `<text>` — a run of bitmap glyphs. Its children must be strings or numbers.
|
|
39
|
+
*
|
|
40
|
+
* Anything else throws `TN_REACT_UNKNOWN_ELEMENT` naming the tag. Tailwind class names cannot cross
|
|
41
|
+
* — they are CSS — so styling is the `style` prop, whose supported keys are named exhaustively on
|
|
42
|
+
* `IOverlayStyle` and enforced by `assertKnownStyle`.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/** The two host element types, as strings React sees. Namespaced so no DOM or SVG tag can collide. */
|
|
46
|
+
export const VIEW_ELEMENT = "tn-view";
|
|
47
|
+
/** @see VIEW_ELEMENT */
|
|
48
|
+
export const TEXT_ELEMENT = "tn-text";
|
|
49
|
+
|
|
50
|
+
type HostKind = "view" | "text";
|
|
51
|
+
|
|
52
|
+
interface IHostNode extends ILayoutNode {
|
|
53
|
+
readonly kind: HostKind;
|
|
54
|
+
props: Record<string, unknown>;
|
|
55
|
+
/**
|
|
56
|
+
* Every child in commit order, host elements and raw strings alike. React interleaves them
|
|
57
|
+
* (`<text>HP {health}</text>` is two raw nodes), so the order has to survive here even though
|
|
58
|
+
* only the host ones get a box.
|
|
59
|
+
*/
|
|
60
|
+
nodes: AnyNode[];
|
|
61
|
+
/** The subset the layout pass walks. Derived from {@link IHostNode.nodes} after each mutation. */
|
|
62
|
+
children: IHostNode[];
|
|
63
|
+
object?: Object3D;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface ITextNode {
|
|
67
|
+
readonly kind: "raw";
|
|
68
|
+
text: string;
|
|
69
|
+
/**
|
|
70
|
+
* React hands `commitTextUpdate` the text instance and nothing else, so a raw node has to know
|
|
71
|
+
* which `<text>` owns it. Without this back-pointer the parent keeps the string it was mounted
|
|
72
|
+
* with, and a HUD counter freezes at its first value while every other signal says it committed.
|
|
73
|
+
*/
|
|
74
|
+
parent?: IHostNode;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type AnyNode = IHostNode | ITextNode;
|
|
78
|
+
|
|
79
|
+
interface IContainer {
|
|
80
|
+
readonly root: IHostNode;
|
|
81
|
+
invalidate(): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const UNIT_PLANE = new PlaneGeometry(1, 1);
|
|
85
|
+
const HOST_CONTEXT = {};
|
|
86
|
+
|
|
87
|
+
function isHost(node: AnyNode): node is IHostNode {
|
|
88
|
+
return node.kind !== "raw";
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function styleOf(props: Record<string, unknown>, type: string): IOverlayStyle {
|
|
92
|
+
const style = (props.style ?? {}) as IOverlayStyle;
|
|
93
|
+
if (typeof style !== "object")
|
|
94
|
+
throw new Error(`TN_REACT_BAD_STYLE: <${type}> style must be an object.`);
|
|
95
|
+
assertKnownStyle(style, type);
|
|
96
|
+
return style;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function makeNode(type: string, props: Record<string, unknown>): IHostNode {
|
|
100
|
+
if (type !== VIEW_ELEMENT && type !== TEXT_ELEMENT) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`TN_REACT_UNKNOWN_ELEMENT: <${type}> has no native mapping. The native overlay renders <View> and <Text> only; a DOM tag such as <div> or <svg> cannot cross, because there is no DOM and no rasteriser on the native host.`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
kind: type === VIEW_ELEMENT ? "view" : "text",
|
|
107
|
+
props,
|
|
108
|
+
style: styleOf(props, type),
|
|
109
|
+
text: "",
|
|
110
|
+
nodes: [],
|
|
111
|
+
children: [],
|
|
112
|
+
box: { x: 0, y: 0, width: 0, height: 0 },
|
|
113
|
+
resolvedFontSize: 14,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Re-derive a node's drawable children and its own glyph run after its child list changed. */
|
|
118
|
+
function reconcileChildren(node: IHostNode): void {
|
|
119
|
+
const children: IHostNode[] = [];
|
|
120
|
+
let text = "";
|
|
121
|
+
for (const child of node.nodes) {
|
|
122
|
+
if (isHost(child)) children.push(child);
|
|
123
|
+
else text += child.text;
|
|
124
|
+
}
|
|
125
|
+
node.children = children;
|
|
126
|
+
node.text = text;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function toColor(value: OverlayColor | undefined, fallback: number): Color {
|
|
130
|
+
if (value === undefined) return new Color(fallback);
|
|
131
|
+
return new Color(value as never);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A `view` paints one quad; a `text` paints one instanced quad per lit glyph pixel. */
|
|
135
|
+
function syncObject(node: IHostNode, parent: Group, order: { value: number }): void {
|
|
136
|
+
order.value += 1;
|
|
137
|
+
const renderOrder = 10_000 + order.value;
|
|
138
|
+
if (node.kind === "view") {
|
|
139
|
+
const background = node.style.background;
|
|
140
|
+
if (background === undefined) {
|
|
141
|
+
disposeObject(node);
|
|
142
|
+
} else {
|
|
143
|
+
const mesh = (node.object as Mesh | undefined) ?? newQuad();
|
|
144
|
+
node.object = mesh;
|
|
145
|
+
const material = mesh.material as MeshBasicMaterial;
|
|
146
|
+
material.color.copy(toColor(background, 0xffffff));
|
|
147
|
+
material.opacity = node.style.opacity ?? 1;
|
|
148
|
+
mesh.renderOrder = renderOrder;
|
|
149
|
+
mesh.scale.set(Math.max(node.box.width, 0), Math.max(node.box.height, 0), 1);
|
|
150
|
+
mesh.position.set(node.box.x + node.box.width / 2, -(node.box.y + node.box.height / 2), 0);
|
|
151
|
+
// Object3D.add removes and re-appends an existing child, which keeps Three's draw order in
|
|
152
|
+
// step with React's keyed sibling order after a move.
|
|
153
|
+
parent.add(mesh);
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
syncText(node, parent, renderOrder);
|
|
157
|
+
}
|
|
158
|
+
syncChildren(node.children, parent, order);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Paint z-indexed siblings as stable subtrees, with every parent background before its children. */
|
|
162
|
+
function syncChildren(
|
|
163
|
+
children: readonly IHostNode[],
|
|
164
|
+
parent: Group,
|
|
165
|
+
order: { value: number },
|
|
166
|
+
): void {
|
|
167
|
+
const sorted = children
|
|
168
|
+
.map((child, index) => ({ child, index }))
|
|
169
|
+
.sort(
|
|
170
|
+
(left, right) =>
|
|
171
|
+
(left.child.style.zIndex ?? 0) - (right.child.style.zIndex ?? 0) ||
|
|
172
|
+
left.index - right.index,
|
|
173
|
+
);
|
|
174
|
+
for (const { child } of sorted) syncObject(child, parent, order);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function newQuad(): Mesh {
|
|
178
|
+
const mesh = new Mesh(
|
|
179
|
+
UNIT_PLANE,
|
|
180
|
+
new MeshBasicMaterial({ depthTest: false, depthWrite: false, transparent: true }),
|
|
181
|
+
);
|
|
182
|
+
mesh.frustumCulled = false;
|
|
183
|
+
return mesh;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function syncText(node: IHostNode, parent: Group, renderOrder: number): void {
|
|
187
|
+
const cell = node.resolvedFontSize / GLYPH_HEIGHT;
|
|
188
|
+
const advance = 6 * cell + (node.style.letterSpacing ?? 0);
|
|
189
|
+
const runs: [number, number][] = [];
|
|
190
|
+
const originX = node.box.x + textOffset(node);
|
|
191
|
+
for (const [index, character] of [...node.text].entries()) {
|
|
192
|
+
const pixels = glyphPixels(character);
|
|
193
|
+
if (pixels === undefined) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
`TN_REACT_UNKNOWN_GLYPH: '${character}' is not in the overlay glyph set, so "${node.text}" would draw with a hole in it. Supported: ${supportedGlyphs()}`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
for (const [px, py] of pixels) {
|
|
199
|
+
runs.push([originX + index * advance + px * cell, node.box.y + py * cell]);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const needed = Math.max(runs.length, 1);
|
|
203
|
+
let mesh = node.object as InstancedMesh | undefined;
|
|
204
|
+
if (mesh === undefined || mesh.instanceMatrix.count < needed) {
|
|
205
|
+
disposeObject(node);
|
|
206
|
+
mesh = new InstancedMesh(
|
|
207
|
+
UNIT_PLANE,
|
|
208
|
+
new MeshBasicMaterial({ depthTest: false, depthWrite: false, transparent: true }),
|
|
209
|
+
// Headroom on purpose: a HUD number goes 30 -> 29 -> 28 all day, and reallocating whenever
|
|
210
|
+
// the pixel count ticks up by one would churn a buffer every second. Grow, never shrink.
|
|
211
|
+
Math.max(needed * 2, 256),
|
|
212
|
+
);
|
|
213
|
+
mesh.frustumCulled = false;
|
|
214
|
+
node.object = mesh;
|
|
215
|
+
}
|
|
216
|
+
const material = mesh.material as MeshBasicMaterial;
|
|
217
|
+
material.color.copy(toColor(node.style.color, 0xffffff));
|
|
218
|
+
material.opacity = node.style.opacity ?? 1;
|
|
219
|
+
mesh.renderOrder = renderOrder;
|
|
220
|
+
const matrix = new Matrix4();
|
|
221
|
+
for (const [index, [x, y]] of runs.entries()) {
|
|
222
|
+
matrix.makeScale(cell, cell, 1);
|
|
223
|
+
matrix.setPosition(x + cell / 2, -(y + cell / 2), 0);
|
|
224
|
+
mesh.setMatrixAt(index, matrix);
|
|
225
|
+
}
|
|
226
|
+
mesh.count = runs.length;
|
|
227
|
+
mesh.instanceMatrix.needsUpdate = true;
|
|
228
|
+
parent.add(mesh);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function disposeObject(node: IHostNode): void {
|
|
232
|
+
const object = node.object;
|
|
233
|
+
if (object === undefined) return;
|
|
234
|
+
object.removeFromParent();
|
|
235
|
+
const mesh = object as Mesh;
|
|
236
|
+
(mesh.material as MeshBasicMaterial | undefined)?.dispose();
|
|
237
|
+
node.object = undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function disposeTree(node: IHostNode): void {
|
|
241
|
+
disposeObject(node);
|
|
242
|
+
for (const child of node.children) disposeTree(child);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
let currentPriority: number = NoEventPriority;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The host config. Shape and required members come from PRD-216's Phase 0 spike, which paid for
|
|
249
|
+
* the React 19 surprises: `react-reconciler@0.33` has no `prepareUpdate`, and React 19 throws at
|
|
250
|
+
* mount unless `resolveUpdatePriority`, `maySuspendCommit`, `NotPendingTransition` and a
|
|
251
|
+
* `HostTransitionContext` object are present.
|
|
252
|
+
*/
|
|
253
|
+
const config = {
|
|
254
|
+
supportsMutation: true,
|
|
255
|
+
supportsPersistence: false,
|
|
256
|
+
supportsHydration: false,
|
|
257
|
+
isPrimaryRenderer: false,
|
|
258
|
+
noTimeout: -1 as const,
|
|
259
|
+
scheduleTimeout: setTimeout,
|
|
260
|
+
cancelTimeout: clearTimeout,
|
|
261
|
+
|
|
262
|
+
createInstance: (type: string, props: Record<string, unknown>) => makeNode(type, props),
|
|
263
|
+
createTextInstance: (text: string): ITextNode => ({ kind: "raw", text }),
|
|
264
|
+
shouldSetTextContent: () => false,
|
|
265
|
+
getPublicInstance: (instance: AnyNode) => instance,
|
|
266
|
+
getRootHostContext: () => HOST_CONTEXT,
|
|
267
|
+
getChildHostContext: () => HOST_CONTEXT,
|
|
268
|
+
|
|
269
|
+
appendInitialChild: (parent: IHostNode, child: AnyNode) => attach(parent, child),
|
|
270
|
+
appendChild: (parent: IHostNode, child: AnyNode) => attach(parent, child),
|
|
271
|
+
appendChildToContainer: (container: IContainer, child: AnyNode) => attach(container.root, child),
|
|
272
|
+
insertBefore: (parent: IHostNode, child: AnyNode, before: AnyNode) =>
|
|
273
|
+
insert(parent, child, before),
|
|
274
|
+
insertInContainerBefore: (container: IContainer, child: AnyNode, before: AnyNode) =>
|
|
275
|
+
insert(container.root, child, before),
|
|
276
|
+
removeChild: (parent: IHostNode, child: AnyNode) => detach(parent, child),
|
|
277
|
+
removeChildFromContainer: (container: IContainer, child: AnyNode) =>
|
|
278
|
+
detach(container.root, child),
|
|
279
|
+
clearContainer: (container: IContainer) => {
|
|
280
|
+
for (const child of [...container.root.nodes]) detach(container.root, child);
|
|
281
|
+
},
|
|
282
|
+
finalizeInitialChildren: () => false,
|
|
283
|
+
commitUpdate: (
|
|
284
|
+
instance: IHostNode,
|
|
285
|
+
type: string,
|
|
286
|
+
_prev: Record<string, unknown>,
|
|
287
|
+
next: Record<string, unknown>,
|
|
288
|
+
) => {
|
|
289
|
+
instance.props = next;
|
|
290
|
+
instance.style = styleOf(next, type);
|
|
291
|
+
},
|
|
292
|
+
commitTextUpdate: (instance: ITextNode, _old: string, next: string) => {
|
|
293
|
+
instance.text = next;
|
|
294
|
+
if (instance.parent !== undefined) reconcileChildren(instance.parent);
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
prepareForCommit: () => null,
|
|
298
|
+
resetAfterCommit: (container: IContainer) => container.invalidate(),
|
|
299
|
+
preparePortalMount: () => undefined,
|
|
300
|
+
detachDeletedInstance: () => undefined,
|
|
301
|
+
beforeActiveInstanceBlur: () => undefined,
|
|
302
|
+
afterActiveInstanceBlur: () => undefined,
|
|
303
|
+
prepareScopeUpdate: () => undefined,
|
|
304
|
+
getInstanceFromNode: () => null,
|
|
305
|
+
getInstanceFromScope: () => null,
|
|
306
|
+
|
|
307
|
+
setCurrentUpdatePriority: (priority: number) => {
|
|
308
|
+
currentPriority = priority;
|
|
309
|
+
},
|
|
310
|
+
getCurrentUpdatePriority: () => currentPriority,
|
|
311
|
+
// Discrete, not default, and deliberately so. A default-lane update is handed to React's own
|
|
312
|
+
// Scheduler, which reaches for `MessageChannel` and only falls back to `setTimeout`; on the
|
|
313
|
+
// native host that is one more browser global to depend on for a HUD that has to repaint inside
|
|
314
|
+
// a fixed-step frame anyway. Discrete puts the update on the sync lane, so `flushSyncWork()`
|
|
315
|
+
// from the game loop is all that is ever needed and the overlay never waits on a scheduler.
|
|
316
|
+
resolveUpdatePriority: () =>
|
|
317
|
+
currentPriority !== NoEventPriority ? currentPriority : DiscreteEventPriority,
|
|
318
|
+
getCurrentEventPriority: () => DiscreteEventPriority,
|
|
319
|
+
|
|
320
|
+
shouldAttemptEagerTransition: () => false,
|
|
321
|
+
requestPostPaintCallback: () => undefined,
|
|
322
|
+
maySuspendCommit: () => false,
|
|
323
|
+
preloadInstance: () => true,
|
|
324
|
+
startSuspendingCommit: () => undefined,
|
|
325
|
+
suspendInstance: () => undefined,
|
|
326
|
+
waitForCommitToBeReady: () => null,
|
|
327
|
+
resetFormInstance: () => undefined,
|
|
328
|
+
trackSchedulerEvent: () => undefined,
|
|
329
|
+
resolveEventType: () => null,
|
|
330
|
+
resolveEventTimeStamp: () => -1.1,
|
|
331
|
+
// biome-ignore lint/style/useNamingConvention: exact React reconciler host API name.
|
|
332
|
+
NotPendingTransition: null,
|
|
333
|
+
// biome-ignore lint/style/useNamingConvention: exact React reconciler host API name.
|
|
334
|
+
HostTransitionContext: {
|
|
335
|
+
$$typeof: Symbol.for("react.context"),
|
|
336
|
+
// biome-ignore lint/style/useNamingConvention: exact React context API name.
|
|
337
|
+
Provider: null,
|
|
338
|
+
// biome-ignore lint/style/useNamingConvention: exact React context API name.
|
|
339
|
+
Consumer: null,
|
|
340
|
+
_currentValue: null,
|
|
341
|
+
_currentValue2: null,
|
|
342
|
+
_threadCount: 0,
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
function attach(parent: IHostNode, child: AnyNode): void {
|
|
347
|
+
if (!isHost(child)) child.parent = parent;
|
|
348
|
+
const existing = parent.nodes.indexOf(child);
|
|
349
|
+
if (existing >= 0) parent.nodes.splice(existing, 1);
|
|
350
|
+
parent.nodes.push(child);
|
|
351
|
+
reconcileChildren(parent);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function insert(parent: IHostNode, child: AnyNode, before: AnyNode): void {
|
|
355
|
+
if (child === before) return;
|
|
356
|
+
if (!isHost(child)) child.parent = parent;
|
|
357
|
+
const existing = parent.nodes.indexOf(child);
|
|
358
|
+
if (existing >= 0) parent.nodes.splice(existing, 1);
|
|
359
|
+
const at = parent.nodes.indexOf(before);
|
|
360
|
+
parent.nodes.splice(at < 0 ? parent.nodes.length : at, 0, child);
|
|
361
|
+
reconcileChildren(parent);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function detach(parent: IHostNode, child: AnyNode): void {
|
|
365
|
+
const at = parent.nodes.indexOf(child);
|
|
366
|
+
if (at >= 0) parent.nodes.splice(at, 1);
|
|
367
|
+
if (isHost(child)) disposeTree(child);
|
|
368
|
+
reconcileChildren(parent);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// biome-ignore lint/suspicious/noExplicitAny: react-reconciler's generics are 20 positional slots.
|
|
372
|
+
const reconciler = (ReactReconciler as unknown as (config: unknown) => any)(config);
|
|
373
|
+
|
|
374
|
+
export interface IReactOverlayOptions {
|
|
375
|
+
/** Where the tree is drawn. `ctx.canvasLayer` in a game. */
|
|
376
|
+
canvasLayer: Pick<CanvasLayer, "scene" | "camera" | "onResize">;
|
|
377
|
+
/**
|
|
378
|
+
* Called with any error React could not recover from, before the overlay draws its own named
|
|
379
|
+
* failure banner. The default logs it; nothing swallows it, because a blank HUD and a broken HUD
|
|
380
|
+
* must never look the same.
|
|
381
|
+
*/
|
|
382
|
+
onError?: (error: Error) => void;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export interface IReactOverlay {
|
|
386
|
+
/** Mount or update the tree. Synchronous, so a caller can assert on the result immediately. */
|
|
387
|
+
render(element: ReactNode): void;
|
|
388
|
+
/** Re-run layout — call after a resize. Cheap and idempotent; it no-ops when nothing moved. */
|
|
389
|
+
refresh(): void;
|
|
390
|
+
/** Unmount the tree and release every Three.js object it created. */
|
|
391
|
+
dispose(): void;
|
|
392
|
+
/** How many Three.js objects the last commit produced. For budgets and tests. */
|
|
393
|
+
readonly objectCount: number;
|
|
394
|
+
/** Number of hook/store updates that changed host nodes during `refresh()`. */
|
|
395
|
+
readonly commitCount: number;
|
|
396
|
+
/** Flush plus draw cost of the latest state-changing `refresh()`, in milliseconds. */
|
|
397
|
+
readonly lastCommitMs: number | undefined;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Mount React into a `CanvasLayer`.
|
|
402
|
+
*
|
|
403
|
+
* @situation show a React HUD on Android, iOS or desktop native
|
|
404
|
+
* @situation render the same React component on web and on a phone without a WebView
|
|
405
|
+
* @constraint import `react`, never `react-dom`, from a native entry
|
|
406
|
+
* @example const overlay = createReactOverlay({ canvasLayer: ctx.canvasLayer });
|
|
407
|
+
*/
|
|
408
|
+
export function createReactOverlay(options: IReactOverlayOptions): IReactOverlay {
|
|
409
|
+
const { canvasLayer } = options;
|
|
410
|
+
const group = new Group();
|
|
411
|
+
group.name = "ThreeNativeReactOverlay";
|
|
412
|
+
canvasLayer.scene.add(group);
|
|
413
|
+
const root: IHostNode = {
|
|
414
|
+
kind: "view",
|
|
415
|
+
props: {},
|
|
416
|
+
style: {},
|
|
417
|
+
text: "",
|
|
418
|
+
nodes: [],
|
|
419
|
+
children: [],
|
|
420
|
+
box: { x: 0, y: 0, width: 0, height: 0 },
|
|
421
|
+
resolvedFontSize: 14,
|
|
422
|
+
};
|
|
423
|
+
let dirty = true;
|
|
424
|
+
let lastWidth = 0;
|
|
425
|
+
let lastHeight = 0;
|
|
426
|
+
let objectCount = 0;
|
|
427
|
+
let commitCount = 0;
|
|
428
|
+
let lastCommitMs: number | undefined;
|
|
429
|
+
let failure: { message: string; source: "draw" | "react" } | undefined;
|
|
430
|
+
let failureObject: Object3D | undefined;
|
|
431
|
+
const container: IContainer = {
|
|
432
|
+
root,
|
|
433
|
+
invalidate: () => {
|
|
434
|
+
dirty = true;
|
|
435
|
+
},
|
|
436
|
+
};
|
|
437
|
+
const report = (error: unknown): void => {
|
|
438
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
439
|
+
failure = { message, source: "react" };
|
|
440
|
+
dirty = true;
|
|
441
|
+
const named = new Error(`TN_REACT_OVERLAY_FAILED: ${message}`);
|
|
442
|
+
if (options.onError !== undefined) options.onError(named);
|
|
443
|
+
else console.error(named.message);
|
|
444
|
+
};
|
|
445
|
+
const fiberRoot = reconciler.createContainer(
|
|
446
|
+
container,
|
|
447
|
+
0,
|
|
448
|
+
null,
|
|
449
|
+
false,
|
|
450
|
+
null,
|
|
451
|
+
"tn",
|
|
452
|
+
report,
|
|
453
|
+
report,
|
|
454
|
+
report,
|
|
455
|
+
null,
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
const draw = (): void => {
|
|
459
|
+
const { camera } = canvasLayer;
|
|
460
|
+
const width = camera.right - camera.left;
|
|
461
|
+
const height = camera.top - camera.bottom;
|
|
462
|
+
if (!dirty && width === lastWidth && height === lastHeight) return;
|
|
463
|
+
lastWidth = width;
|
|
464
|
+
lastHeight = height;
|
|
465
|
+
dirty = false;
|
|
466
|
+
// Nodes are laid out in screen pixels with the origin top-left and y growing downwards, which
|
|
467
|
+
// is how a HUD is described. The whole group is parked on the camera's top-left corner so that
|
|
468
|
+
// `(x, -y)` on a node is already the right place; nothing below needs to know the viewport.
|
|
469
|
+
group.position.set(-width / 2, height / 2, 0);
|
|
470
|
+
try {
|
|
471
|
+
if (failure?.source === "react") {
|
|
472
|
+
showFailure(failure.message, width);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
layoutTree(root, width, height);
|
|
476
|
+
const order = { value: 0 };
|
|
477
|
+
syncChildren(root.children, group, order);
|
|
478
|
+
objectCount = order.value;
|
|
479
|
+
failure = undefined;
|
|
480
|
+
clearFailure();
|
|
481
|
+
} catch (error) {
|
|
482
|
+
// A layout or glyph refusal must show as itself, not as an empty screen.
|
|
483
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
484
|
+
failure = { message, source: "draw" };
|
|
485
|
+
if (options.onError !== undefined) options.onError(new Error(message));
|
|
486
|
+
else console.error(message);
|
|
487
|
+
showFailure(message, width);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
const clearFailure = (): void => {
|
|
492
|
+
if (failureObject !== undefined) {
|
|
493
|
+
failureObject.removeFromParent();
|
|
494
|
+
((failureObject as Mesh).material as MeshBasicMaterial | undefined)?.dispose();
|
|
495
|
+
failureObject = undefined;
|
|
496
|
+
}
|
|
497
|
+
for (const child of group.children) child.visible = true;
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
const showFailure = (message: string, width: number): void => {
|
|
501
|
+
clearFailure();
|
|
502
|
+
for (const child of group.children) child.visible = false;
|
|
503
|
+
failureObject = drawFailure(group, message, width);
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
const stopResize = canvasLayer.onResize(() => {
|
|
507
|
+
dirty = true;
|
|
508
|
+
draw();
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
return {
|
|
512
|
+
render(element: ReactNode) {
|
|
513
|
+
failure = undefined;
|
|
514
|
+
reconciler.updateContainerSync(element, fiberRoot, null, null);
|
|
515
|
+
reconciler.flushSyncWork();
|
|
516
|
+
draw();
|
|
517
|
+
},
|
|
518
|
+
refresh() {
|
|
519
|
+
// A hook that fired outside `render` (a store subscription, a timer) has queued work that
|
|
520
|
+
// nobody has flushed yet. Flushing here is what makes `refresh()` per-frame-safe: with no
|
|
521
|
+
// pending work it is the ~0.4 microseconds Phase 0 measured, and with work it commits it.
|
|
522
|
+
const startedAt = globalThis.performance?.now() ?? Date.now();
|
|
523
|
+
const wasDirty = dirty;
|
|
524
|
+
reconciler.flushSyncWork();
|
|
525
|
+
const committed = dirty && !wasDirty;
|
|
526
|
+
draw();
|
|
527
|
+
if (committed) {
|
|
528
|
+
commitCount += 1;
|
|
529
|
+
lastCommitMs = (globalThis.performance?.now() ?? Date.now()) - startedAt;
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
dispose() {
|
|
533
|
+
stopResize();
|
|
534
|
+
reconciler.updateContainerSync(null, fiberRoot, null, null);
|
|
535
|
+
reconciler.flushSyncWork();
|
|
536
|
+
clearFailure();
|
|
537
|
+
disposeTree(root);
|
|
538
|
+
group.removeFromParent();
|
|
539
|
+
},
|
|
540
|
+
get objectCount() {
|
|
541
|
+
return objectCount;
|
|
542
|
+
},
|
|
543
|
+
get commitCount() {
|
|
544
|
+
return commitCount;
|
|
545
|
+
},
|
|
546
|
+
get lastCommitMs() {
|
|
547
|
+
return lastCommitMs;
|
|
548
|
+
},
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* The negative control, made visible. When a component throws, the screen shows the error's own
|
|
554
|
+
* first line in red rather than going blank — a blank overlay is indistinguishable from "the UI was
|
|
555
|
+
* never mounted", which is the exact failure this whole PRD exists to end.
|
|
556
|
+
*/
|
|
557
|
+
function drawFailure(group: Group, message: string, width: number): Object3D | undefined {
|
|
558
|
+
const line = message.split(":")[0] ?? "TN_REACT_OVERLAY_FAILED";
|
|
559
|
+
const node: IHostNode = {
|
|
560
|
+
kind: "text",
|
|
561
|
+
props: {},
|
|
562
|
+
style: { color: "#ff5f4d", fontSize: 18 },
|
|
563
|
+
text: line.slice(0, 48).toUpperCase(),
|
|
564
|
+
nodes: [],
|
|
565
|
+
children: [],
|
|
566
|
+
box: { x: 12, y: 12, width: Math.max(width - 24, 1), height: 18 },
|
|
567
|
+
resolvedFontSize: 18,
|
|
568
|
+
};
|
|
569
|
+
try {
|
|
570
|
+
syncText(node, group, 20_000);
|
|
571
|
+
return node.object;
|
|
572
|
+
} catch {
|
|
573
|
+
// The failure text itself uses a character the font lacks. Nothing further to draw.
|
|
574
|
+
return undefined;
|
|
575
|
+
}
|
|
576
|
+
}
|