@threenative/core 0.3.13 → 0.3.15

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 (50) hide show
  1. package/capabilities.json +299 -10
  2. package/dist/{chunk-4SRAQFTU.js → chunk-2GPHICV6.js} +1 -1
  3. package/dist/{chunk-DIPIJICH.js → chunk-GEMQJBY4.js} +70 -18
  4. package/dist/{chunk-EFWZWLPY.js → chunk-QAMSM3Z5.js} +354 -12
  5. package/dist/{chunk-SV4QVUTQ.js → chunk-QAQHYOWY.js} +1 -1
  6. package/dist/{chunk-EEABDLUC.js → chunk-VBBYWGAT.js} +2 -2
  7. package/dist/{chunk-GYUZJQ7U.js → chunk-VQRPEOKY.js} +4 -1
  8. package/dist/index.d.ts +605 -7
  9. package/dist/index.js +1973 -609
  10. package/dist/playtest.js +3 -3
  11. package/dist/{quality-BYqKGAwo.d.ts → quality-BADStsTn.d.ts} +2 -2
  12. package/dist/render-environment.d.ts +1 -1
  13. package/dist/ui-layer.js +2 -2
  14. package/dist/worker.js +2 -2
  15. package/dist/world.d.ts +13 -3
  16. package/dist/world.js +179 -36
  17. package/mcp/animation-server.mjs +1 -1
  18. package/mcp/engine-server.mjs +32 -6
  19. package/mcp/servers.mjs +3 -3
  20. package/package.json +5 -5
  21. package/scripts/ensure-mcp.mjs +6 -1
  22. package/src/clustered-batch.ts +146 -3
  23. package/src/clustered-mesh.ts +548 -20
  24. package/src/debug.ts +16 -0
  25. package/src/displaced-surface.ts +428 -0
  26. package/src/frame-budget.ts +16 -0
  27. package/src/generated/console-markers.ts +1 -0
  28. package/src/grass-field.ts +628 -0
  29. package/src/index.ts +112 -0
  30. package/src/render/material-key.ts +21 -1
  31. package/src/render/screen-probe-gather.ts +303 -0
  32. package/src/render/short-range-ao.ts +274 -0
  33. package/src/render/world-impostor-surface.ts +98 -3
  34. package/src/render-camera-cull.ts +9 -3
  35. package/src/renderer.ts +26 -4
  36. package/src/scatter-field.ts +496 -0
  37. package/src/startup-offscreen-bytes.ts +11 -5
  38. package/src/state.ts +16 -3
  39. package/src/static-transform.ts +75 -6
  40. package/src/tile-ring.ts +294 -0
  41. package/src/version.ts +1 -1
  42. package/src/warmup.ts +15 -2
  43. package/src/world-cell-batches.ts +4 -0
  44. package/src/world-cell-data.ts +7 -0
  45. package/src/world-cell-models.ts +36 -8
  46. package/src/world-cell-render-gpu-dress.ts +4 -2
  47. package/src/world-cell-render-util.ts +2 -2
  48. package/src/world-cells.ts +1 -1
  49. package/src/world-river-ribbon.ts +13 -22
  50. package/src/world-water-bodies.ts +113 -3
package/src/debug.ts CHANGED
@@ -139,6 +139,22 @@ export function debugFlag(name: string): boolean {
139
139
  return readLaunchFlag({ environment: envName(name), query: name });
140
140
  }
141
141
 
142
+ /**
143
+ * The text this launch gave a debug switch, or `undefined` when no seam named it.
144
+ *
145
+ * `debugFlag`'s non-boolean sibling: the same naming rule — `?name` on the web, `TN_DEBUG_NAME` on
146
+ * a native launch — for a switch whose value is a word rather than a yes or no, such as a quality
147
+ * tier or a mode. An empty string means a seam named the switch and gave no value.
148
+ *
149
+ * @situation read a debug override whose value is not a boolean
150
+ * @situation override a quality tier or a mode for one run without changing the code
151
+ * @constraint a name in camelCase becomes UPPER_SNAKE: `debugValue("quality")` reads `?quality=` or `TN_DEBUG_QUALITY`
152
+ * @example const tier = debugValue("quality");
153
+ */
154
+ export function debugValue(name: string): string | undefined {
155
+ return readLaunchValue({ environment: envName(name), query: name });
156
+ }
157
+
142
158
  /**
143
159
  * Publish one game object for a capture script or the console, in development builds only.
144
160
  *
@@ -0,0 +1,428 @@
1
+ // PRD-668 Phase 1: the price of displaced, diced triangles, counted before a dice pass exists.
2
+ // Read-only. It walks what the scene already submits and counts the patches and micro-triangles
3
+ // Unreal's Nanite tessellation would dice them into. It draws nothing and decides no look.
4
+ //
5
+ // Clean-room port of the rules, not the code (UE 5.8.3):
6
+ // - dice rate 2 px: `Engine/Source/Runtime/Renderer/Private/Nanite/NaniteCullRaster.cpp:147-152`
7
+ // - edge factor = edge length / max(nearer depth, near) * view-to-pixels / dice rate, a quarter past
8
+ // the fade distance: `Engine/Shaders/Private/Nanite/NaniteTessellation.ush:62-101`, with
9
+ // LODScale * InvDiceRate = ViewToPixels / DicingRate (`NaniteShared.cpp:197-199`,
10
+ // `NaniteCullRaster.cpp:6993`), ViewToPixels = 0.5 * ViewToClip[1][1] * view height
11
+ // - fade distance: `NaniteTessellation.ush:49-59`; the fade-out size is min(start 4, end 1) px when
12
+ // the material enables the fade and 0 (no fade) when it does not
13
+ // (`Engine/Source/Runtime/Engine/Private/Rendering/NaniteResources.cpp:774-791`)
14
+ // - magnitude 4 cm about a 0.5 center, range (0 - c) * m to (1 - c) * m:
15
+ // `Engine/Source/Runtime/Engine/Classes/Engine/EngineTypes.h:3446-3458`, `NaniteResources.cpp:792-793`
16
+ // - table of factors 1 to 14 and a split past it at TF / 14, capped at 8, ceil and clamp, each child
17
+ // culled and priced from its own points:
18
+ // `Engine/Shaders/Shared/NaniteDefinitions.h:125-128`, `NaniteTessellation.ush:103-106,141-144`,
19
+ // `Engine/Shaders/Private/Nanite/NaniteSplit.usf:117-258`
20
+ // - a uniform pattern of factor n holds n * n triangles:
21
+ // `Engine/Source/Runtime/Renderer/Private/Nanite/TessellationTable.cpp:233-236`
22
+ import type { BufferAttribute, InstancedMesh, Mesh, Object3D, PerspectiveCamera } from "three";
23
+ import { Matrix4 } from "three";
24
+
25
+ /** `r.Nanite.DicingRate`: pixels per micro-triangle edge. */
26
+ export const DICE_RATE_PIXELS = 2;
27
+ /** `NANITE_TESSELLATION_TABLE_SIZE`: the largest edge factor one pattern holds. */
28
+ const TABLE_SIZE = 14;
29
+ /** The cap on one split level's factor (`min(GetSplitFactors(...), 8)`). */
30
+ const SPLIT_CAP = 8;
31
+ /** `NANITE_TESSELLATION_MAX_PATCH_SPLIT_LEVELS`. */
32
+ const MAX_SPLIT_LEVELS = 6;
33
+ /** The hard-coded edge-factor multiple past the fade distance. */
34
+ const LOW_TESS_MULTIPLE = 0.25;
35
+
36
+ export interface IDisplacementPriceOptions {
37
+ /** Pixels per micro-triangle edge. Default 2, Unreal's. */
38
+ readonly diceRate?: number;
39
+ /** Displacement magnitude in metres. Default 0.04, Unreal's 4 cm. */
40
+ readonly magnitude?: number;
41
+ /** The height that does not move the surface. Default 0.5, Unreal's. */
42
+ readonly center?: number;
43
+ /** Pixels the largest displacement spans where the fade ends. Default 0: fade off, Unreal's stock material. */
44
+ readonly fadeOutPixels?: number;
45
+ /** Culls each patch against this buffer, as Unreal's split pass tests its HZB. Default: none. */
46
+ readonly occlusion?: IOcclusionBuffer;
47
+ }
48
+
49
+ /**
50
+ * A conservative stand-in for Unreal's HZB: per texel, the far depth of the nearest triangle that
51
+ * covers its center. A patch is hidden only when every texel under it holds an occluder wholly in
52
+ * front of it, so no patch is ever hidden by itself.
53
+ */
54
+ export interface IOcclusionBuffer {
55
+ readonly width: number;
56
+ readonly height: number;
57
+ readonly depth: Float32Array;
58
+ }
59
+
60
+ export interface IDisplacementPrice {
61
+ /** Triangles the scene submits now: draw ranges times drawn copies. */
62
+ triangles: number;
63
+ /** Of those, the ones not wholly outside one frustum plane. */
64
+ visibleTriangles: number;
65
+ /** Visible triangles with an edge factor above 1, the ones a dice pass would split. */
66
+ dicedTriangles: number;
67
+ /** Leaf patches the diced triangles split into. */
68
+ patches: number;
69
+ /** Micro-triangles the diced triangles become. */
70
+ microTriangles: number;
71
+ /** `microTriangles - dicedTriangles`: what dicing adds to the submitted count. */
72
+ addedMicroTriangles: number;
73
+ /** Metres past which edge factors drop to a quarter, for unit scale; 0 when the fade is off. */
74
+ fadeDistance: number;
75
+ }
76
+
77
+ function clampFactor(factor: number): number {
78
+ return Math.min(TABLE_SIZE, Math.max(1, Math.ceil(factor)));
79
+ }
80
+
81
+ /**
82
+ * Micro-triangles in the pattern for three edge factors, after Unreal's ceil and 1-14 clamp.
83
+ *
84
+ * Exact for a uniform pattern (n * n). Unreal remeshes a non-uniform pattern; this estimates its
85
+ * interior vertices from the mean factor and closes with Euler's T = 2I + B - 2.
86
+ * ponytail: estimate for non-uniform patterns; read Unreal's baked table counts if a gate sits on the edge.
87
+ */
88
+ export function patternTriangles(a: number, b: number, c: number): number {
89
+ const boundary = clampFactor(a) + clampFactor(b) + clampFactor(c);
90
+ const mean = boundary / 3;
91
+ const interior = Math.max(0, Math.round(((mean - 1) * (mean - 2)) / 2));
92
+ return 2 * interior + boundary - 2;
93
+ }
94
+
95
+ /** One edge's tessellation factor, as `GetTessFactors` computes it for a perspective view. */
96
+ export function edgeFactor(
97
+ edgeLength: number,
98
+ nearerDepth: number,
99
+ viewToPixels: number,
100
+ diceRate: number,
101
+ fadeDistance: number,
102
+ near: number,
103
+ ): number {
104
+ const depth = Math.max(nearerDepth, near);
105
+ const distant = fadeDistance > 0 && depth > fadeDistance;
106
+ return ((edgeLength / depth) * viewToPixels * (distant ? LOW_TESS_MULTIPLE : 1)) / diceRate;
107
+ }
108
+
109
+ /** The view depth where the largest displacement spans `fadeOutPixels`; 0 when the fade is off. */
110
+ export function displacementFadeDistance(
111
+ magnitude: number,
112
+ center: number,
113
+ maxScale: number,
114
+ viewToPixels: number,
115
+ fadeOutPixels: number,
116
+ ): number {
117
+ if (fadeOutPixels <= 0) return 0;
118
+ const largest = Math.max(center, 1 - center) * magnitude;
119
+ return (largest * maxScale * viewToPixels) / fadeOutPixels;
120
+ }
121
+
122
+ /** Visits the n * n children of a uniform barycentric grid over a patch's three view-space points. */
123
+ function splitGrid(points: Float64Array, n: number, visit: (child: Float64Array) => void): void {
124
+ const at = (row: number, column: number, into: Float64Array, slot: number): void => {
125
+ const w0 = 1 - row / n;
126
+ const w1 = (row - column) / n;
127
+ const w2 = column / n;
128
+ for (let axis = 0; axis < 3; axis += 1)
129
+ into[slot * 3 + axis] =
130
+ w0 * (points[axis] as number) +
131
+ w1 * (points[3 + axis] as number) +
132
+ w2 * (points[6 + axis] as number);
133
+ };
134
+ for (let row = 0; row < n; row += 1)
135
+ for (let column = 0; column <= row; column += 1) {
136
+ const up = new Float64Array(9);
137
+ at(row, column, up, 0);
138
+ at(row + 1, column, up, 1);
139
+ at(row + 1, column + 1, up, 2);
140
+ visit(up);
141
+ if (column === row) continue;
142
+ const down = new Float64Array(9);
143
+ at(row, column, down, 0);
144
+ at(row + 1, column + 1, down, 1);
145
+ at(row, column + 1, down, 2);
146
+ visit(down);
147
+ }
148
+ }
149
+
150
+ const scratchInstance = new Matrix4();
151
+ const scratchView = new Matrix4();
152
+ const view = new Float64Array(9);
153
+
154
+ /** Writes vertex `vertex` of `position`, through `m`, into slot `k` of `view`. */
155
+ function toView(position: BufferAttribute, vertex: number, m: number[], k: number): void {
156
+ const x = position.getX(vertex);
157
+ const y = position.getY(vertex);
158
+ const z = position.getZ(vertex);
159
+ view[k * 3] =
160
+ (m[0] as number) * x + (m[4] as number) * y + (m[8] as number) * z + (m[12] as number);
161
+ view[k * 3 + 1] =
162
+ (m[1] as number) * x + (m[5] as number) * y + (m[9] as number) * z + (m[13] as number);
163
+ view[k * 3 + 2] =
164
+ (m[2] as number) * x + (m[6] as number) * y + (m[10] as number) * z + (m[14] as number);
165
+ }
166
+
167
+ /**
168
+ * Calls `visit` with each triangle `root` submits now, in `camera`'s view space: every visible mesh,
169
+ * its draw range only, once per drawn instance.
170
+ */
171
+ function forEachSubmittedTriangle(
172
+ root: Object3D,
173
+ camera: PerspectiveCamera,
174
+ visit: (points: Float64Array, modelView: Matrix4) => void,
175
+ ): void {
176
+ root.updateMatrixWorld(true);
177
+ camera.updateMatrixWorld();
178
+ const each = (mesh: Mesh, modelView: Matrix4): void => {
179
+ const geometry = mesh.geometry;
180
+ const position = geometry.getAttribute("position") as BufferAttribute;
181
+ const index = geometry.getIndex();
182
+ const total = index === null ? position.count : index.count;
183
+ const start = Math.max(0, geometry.drawRange.start);
184
+ const end = Math.min(total, start + geometry.drawRange.count);
185
+ const m = modelView.elements;
186
+ for (let corner = start; corner + 2 < end; corner += 3) {
187
+ for (let k = 0; k < 3; k += 1) toView(position, index?.getX(corner + k) ?? corner + k, m, k);
188
+ visit(view, modelView);
189
+ }
190
+ };
191
+ root.traverseVisible((object) => {
192
+ const mesh = object as Mesh;
193
+ if (mesh.isMesh !== true) return;
194
+ const instanced = object as InstancedMesh;
195
+ if (instanced.isInstancedMesh !== true) {
196
+ each(mesh, scratchView.multiplyMatrices(camera.matrixWorldInverse, mesh.matrixWorld));
197
+ return;
198
+ }
199
+ for (let copy = 0; copy < instanced.count; copy += 1) {
200
+ instanced.getMatrixAt(copy, scratchInstance);
201
+ scratchView
202
+ .multiplyMatrices(camera.matrixWorldInverse, mesh.matrixWorld)
203
+ .multiply(scratchInstance);
204
+ each(mesh, scratchView);
205
+ }
206
+ });
207
+ }
208
+
209
+ /** Writes point `k` of `points` into `into` as buffer texels; false in front of the near plane. */
210
+ function toTexel(
211
+ points: Float64Array,
212
+ k: number,
213
+ projection: number[],
214
+ near: number,
215
+ buffer: { readonly width: number; readonly height: number },
216
+ into: number[],
217
+ ): boolean {
218
+ const depth = -(points[k * 3 + 2] as number);
219
+ if (depth < near) return false;
220
+ const cx =
221
+ (projection[0] as number) * (points[k * 3] as number) - (projection[8] as number) * depth;
222
+ const cy =
223
+ (projection[5] as number) * (points[k * 3 + 1] as number) - (projection[9] as number) * depth;
224
+ into[k * 2] = (cx / depth + 1) * 0.5 * buffer.width;
225
+ into[k * 2 + 1] = (1 - cy / depth) * 0.5 * buffer.height;
226
+ return true;
227
+ }
228
+
229
+ /** The texel rectangle under three texel points, clamped to the buffer: left, right, top, bottom. */
230
+ function texelRect(
231
+ texel: number[],
232
+ width: number,
233
+ height: number,
234
+ ): [number, number, number, number] {
235
+ const xs = [texel[0] as number, texel[2] as number, texel[4] as number];
236
+ const ys = [texel[1] as number, texel[3] as number, texel[5] as number];
237
+ return [
238
+ Math.max(0, Math.floor(Math.min(...xs))),
239
+ Math.min(width - 1, Math.floor(Math.max(...xs))),
240
+ Math.max(0, Math.floor(Math.min(...ys))),
241
+ Math.min(height - 1, Math.floor(Math.max(...ys))),
242
+ ];
243
+ }
244
+
245
+ /** Keeps `far` at every texel center the triangle covers, where it is nearer than the buffer. */
246
+ function rasterize(buffer: IOcclusionBuffer, texel: number[], far: number): void {
247
+ const [x0, y0, x1, y1, x2, y2] = texel as [number, number, number, number, number, number];
248
+ const area = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
249
+ if (area === 0) return;
250
+ const [left, right, top, bottom] = texelRect(texel, buffer.width, buffer.height);
251
+ for (let y = top; y <= bottom; y += 1)
252
+ for (let x = left; x <= right; x += 1) {
253
+ const px = x + 0.5;
254
+ const py = y + 0.5;
255
+ // Signed by the area, so either winding covers the same texels.
256
+ const w0 = ((x2 - x1) * (py - y1) - (y2 - y1) * (px - x1)) * area;
257
+ const w1 = ((x0 - x2) * (py - y2) - (y0 - y2) * (px - x2)) * area;
258
+ const w2 = ((x1 - x0) * (py - y0) - (y1 - y0) * (px - x0)) * area;
259
+ const slot = y * buffer.width + x;
260
+ if (w0 >= 0 && w1 >= 0 && w2 >= 0 && far < (buffer.depth[slot] as number))
261
+ buffer.depth[slot] = far;
262
+ }
263
+ }
264
+
265
+ /**
266
+ * Rasterizes every triangle `root` submits into a `width` x `height` occlusion buffer.
267
+ *
268
+ * Each covered texel center keeps the smallest far depth over the triangles that cover it, so an
269
+ * occluder never claims to hide more than it does. Triangles that cross the near plane are skipped.
270
+ * ponytail: a CPU raster for a measurement; a real dice pass reads the GPU's own HZB.
271
+ */
272
+ export function occlusionBuffer(
273
+ root: Object3D,
274
+ camera: PerspectiveCamera,
275
+ width: number,
276
+ height: number,
277
+ ): IOcclusionBuffer {
278
+ const buffer: IOcclusionBuffer = {
279
+ depth: new Float32Array(width * height).fill(Number.POSITIVE_INFINITY),
280
+ height,
281
+ width,
282
+ };
283
+ const projection = camera.projectionMatrix.elements;
284
+ const texel = [0, 0, 0, 0, 0, 0];
285
+ forEachSubmittedTriangle(root, camera, (points) => {
286
+ for (let k = 0; k < 3; k += 1)
287
+ if (!toTexel(points, k, projection, camera.near, buffer, texel)) return;
288
+ rasterize(
289
+ buffer,
290
+ texel,
291
+ -Math.min(points[2] as number, points[5] as number, points[8] as number),
292
+ );
293
+ });
294
+ return buffer;
295
+ }
296
+
297
+ /** True when some texel under the patch holds no occluder wholly in front of `nearest`. */
298
+ function unoccluded(buffer: IOcclusionBuffer, texel: number[], nearest: number): boolean {
299
+ const [left, right, top, bottom] = texelRect(texel, buffer.width, buffer.height);
300
+ if (left > right || top > bottom) return true;
301
+ for (let y = top; y <= bottom; y += 1)
302
+ for (let x = left; x <= right; x += 1)
303
+ if ((buffer.depth[y * buffer.width + x] as number) >= nearest) return true;
304
+ return false;
305
+ }
306
+
307
+ /**
308
+ * Prices dicing every triangle `root` submits now, seen by `camera` at `viewportHeight` pixels.
309
+ *
310
+ * Frustum culling per patch, as Unreal's split pass does, and occlusion when `options.occlusion`
311
+ * is given (`NaniteSplit.usf:228`, `Cull.FrustumHZB`). Back faces are diced too: Unreal's patch
312
+ * back-face cull is compiled out (`NaniteSplit.usf:196-202`). Without an occlusion buffer the price
313
+ * is an upper bound.
314
+ */
315
+ export function priceDisplacedSurface(
316
+ root: Object3D,
317
+ camera: PerspectiveCamera,
318
+ viewportHeight: number,
319
+ options: IDisplacementPriceOptions = {},
320
+ ): IDisplacementPrice {
321
+ const diceRate = options.diceRate ?? DICE_RATE_PIXELS;
322
+ const magnitude = options.magnitude ?? 0.04;
323
+ const center = options.center ?? 0.5;
324
+ const fadeOutPixels = options.fadeOutPixels ?? 0;
325
+ const occlusion = options.occlusion;
326
+ if (!(diceRate > 0) || !(viewportHeight > 0))
327
+ throw new Error("TN_DISPLACED_SURFACE: dice rate and viewport height must be positive.");
328
+ const projection = camera.projectionMatrix.elements;
329
+ const viewToPixels = 0.5 * (projection[5] as number) * viewportHeight;
330
+ const { near, far } = camera;
331
+ const price: IDisplacementPrice = {
332
+ addedMicroTriangles: 0,
333
+ dicedTriangles: 0,
334
+ fadeDistance: displacementFadeDistance(magnitude, center, 1, viewToPixels, fadeOutPixels),
335
+ microTriangles: 0,
336
+ patches: 0,
337
+ triangles: 0,
338
+ visibleTriangles: 0,
339
+ };
340
+ /** Which frustum planes view-space point `k` of `points` lies outside, one bit each. */
341
+ const outcode = (points: Float64Array, k: number): number => {
342
+ const vx = points[k * 3] as number;
343
+ const vy = points[k * 3 + 1] as number;
344
+ const vz = points[k * 3 + 2] as number;
345
+ const cx = (projection[0] as number) * vx + (projection[8] as number) * vz;
346
+ const cy = (projection[5] as number) * vy + (projection[9] as number) * vz;
347
+ const depth = -vz;
348
+ return (
349
+ (cx < -depth ? 1 : 0) |
350
+ (cx > depth ? 2 : 0) |
351
+ (cy < -depth ? 4 : 0) |
352
+ (cy > depth ? 8 : 0) |
353
+ (depth < near ? 16 : 0) |
354
+ (depth > far ? 32 : 0)
355
+ );
356
+ };
357
+ /** True when all three points lie outside one frustum plane. */
358
+ const outside = (points: Float64Array): boolean =>
359
+ (outcode(points, 0) & outcode(points, 1) & outcode(points, 2)) !== 0;
360
+ const texel = [0, 0, 0, 0, 0, 0];
361
+ /** False when the occlusion buffer hides the patch, its nearest point pulled in by the displacement. */
362
+ const seen = (points: Float64Array, reach: number): boolean => {
363
+ if (occlusion === undefined) return true;
364
+ for (let k = 0; k < 3; k += 1)
365
+ if (!toTexel(points, k, projection, near, occlusion, texel)) return true;
366
+ const nearest =
367
+ -Math.max(points[2] as number, points[5] as number, points[8] as number) - reach;
368
+ return unoccluded(occlusion, texel, nearest);
369
+ };
370
+ const factorOf = (points: Float64Array, i: number, j: number, fade: number): number =>
371
+ edgeFactor(
372
+ Math.hypot(
373
+ (points[i * 3] as number) - (points[j * 3] as number),
374
+ (points[i * 3 + 1] as number) - (points[j * 3 + 1] as number),
375
+ (points[i * 3 + 2] as number) - (points[j * 3 + 2] as number),
376
+ ),
377
+ Math.min(-(points[i * 3 + 2] as number), -(points[j * 3 + 2] as number)),
378
+ viewToPixels,
379
+ diceRate,
380
+ fade,
381
+ near,
382
+ );
383
+
384
+ /**
385
+ * Dices one patch, given as nine view-space numbers, into `price`; false when it needs no dice.
386
+ *
387
+ * Past a factor of 14 it splits, as `NaniteSplit.usf:239-258` does, by TF / 14 capped at 8, and
388
+ * every child culls and prices itself from its own points (`NaniteSplit.usf:117-235`).
389
+ * ponytail: children are a uniform grid at the largest split factor where Unreal's split pattern
390
+ * follows each edge; the leaf count shifts, the micro-triangle density does not.
391
+ */
392
+ const dice = (
393
+ points: Float64Array,
394
+ fade: number,
395
+ reach: number,
396
+ level: number,
397
+ root: boolean,
398
+ ): boolean => {
399
+ if (!root && outside(points)) return true;
400
+ const a = factorOf(points, 0, 1, fade);
401
+ const b = factorOf(points, 1, 2, fade);
402
+ const c = factorOf(points, 2, 0, fade);
403
+ const largest = Math.max(a, b, c);
404
+ if (root && largest <= 1) return false;
405
+ // An occluded root is not diced at all; an occluded child is a culled part of a diced one.
406
+ if (!seen(points, reach)) return !root;
407
+ if (largest <= TABLE_SIZE || level >= MAX_SPLIT_LEVELS) {
408
+ price.patches += 1;
409
+ price.microTriangles += patternTriangles(a, b, c);
410
+ return true;
411
+ }
412
+ const n = clampFactor(Math.min(largest / TABLE_SIZE, SPLIT_CAP));
413
+ splitGrid(points, n, (child) => dice(child, fade, reach, level + 1, false));
414
+ return true;
415
+ };
416
+
417
+ forEachSubmittedTriangle(root, camera, (points, modelView) => {
418
+ price.triangles += 1;
419
+ if (outside(points)) return;
420
+ price.visibleTriangles += 1;
421
+ const scale = modelView.getMaxScaleOnAxis();
422
+ const fade = displacementFadeDistance(magnitude, center, scale, viewToPixels, fadeOutPixels);
423
+ const reach = Math.max(center, 1 - center) * magnitude * scale;
424
+ if (dice(points, fade, reach, 0, true)) price.dicedTriangles += 1;
425
+ });
426
+ price.addedMicroTriangles = price.microTriangles - price.dicedTriangles;
427
+ return price;
428
+ }
@@ -64,6 +64,8 @@ function requireTarget(target: ITargetFps): ITargetFps {
64
64
 
65
65
  /** Marker printed once per report window. */
66
66
  export const FRAME_BUDGET_MARKER = CONSOLE_MARKER.frameBudget;
67
+ /** Marker printed once, at the end of the first frame: the game drew, and with which threads. */
68
+ export const FIRST_FRAME_MARKER = CONSOLE_MARKER.firstFrame;
67
69
  /** Marker printed the moment a gap between presented frames exceeds `hitchMs`. */
68
70
  export const FRAME_HITCH_MARKER = CONSOLE_MARKER.frameHitch;
69
71
 
@@ -782,6 +784,7 @@ export class FrameBudget {
782
784
  #lastPresentCount: number | undefined;
783
785
  #presentsInWindow = 0;
784
786
  #firstFrameStart: number | undefined;
787
+ #firstFrameReported = false;
785
788
  #firstTaskFrameStart: number | undefined;
786
789
 
787
790
  constructor(options: IFrameBudgetOptions = {}) {
@@ -1147,6 +1150,19 @@ export class FrameBudget {
1147
1150
  const simulationEnd = this.#simulationEnd ?? this.#frameStart;
1148
1151
  const frameMs = Math.max(0, nowMs - this.#frameStart);
1149
1152
  this.#lastFrameEnd = nowMs;
1153
+ // The first drawn frame after the startup cover lifts: the world the player sees. A software
1154
+ // adapter needs minutes to fill a window, and the thread verdict is known now. A boot line like
1155
+ // TN_QUALITY_TIER, so it goes to the console and the window sink stays per window.
1156
+ if (!this.#firstFrameReported && !this.#covered && !this.#renderSkippedThisFrame) {
1157
+ this.#firstFrameReported = true;
1158
+ const threads = (globalThis as { __tnWebThreads?: unknown }).__tnWebThreads;
1159
+ console.info(
1160
+ `${FIRST_FRAME_MARKER}:${JSON.stringify({
1161
+ frameMs: round(frameMs),
1162
+ ...(typeof threads === "string" ? { threads } : {}),
1163
+ })}`,
1164
+ );
1165
+ }
1150
1166
 
1151
1167
  this.#countPresents();
1152
1168
 
@@ -3,6 +3,7 @@
3
3
  // The `TN_*` console markers the perf tooling parses. Add the colon where a line is matched.
4
4
  export const CONSOLE_MARKER = {
5
5
  bridgeCensus: "TN_BRIDGE_CENSUS",
6
+ firstFrame: "TN_FIRST_FRAME",
6
7
  frameBudget: "TN_FRAME_BUDGET",
7
8
  frameHitch: "TN_FRAME_HITCH",
8
9
  hostGap: "TN_HOST_GAP",