@threenative/three-native 0.3.12 → 0.3.13

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/src/gpu-stages.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  * a stage that drew no pass is absent. The order is the native stage id counting from 1
5
5
  * (`kStageScreenSpaceGi` in post_effects.h), so a new name goes last.
6
6
  */
7
+ import { WEB_FRAME_FIELD } from "./generated/web-frame-fields.js";
7
8
  export const GPU_STAGES = ["SSGI"] as const;
8
9
 
9
10
  export type GpuStage = (typeof GPU_STAGES)[number];
@@ -26,7 +27,7 @@ export function gpuStageId(name: unknown): number {
26
27
  export const SHADOW_STAGE = "Shadow";
27
28
 
28
29
  /** `tnw_web_frame` field of the shadow passes' segment (web_host.cpp). */
29
- export const SHADOW_SEGMENT_FIELD = 19;
30
+ export const SHADOW_SEGMENT_FIELD: number = WEB_FRAME_FIELD.gpuSegmentShadow;
30
31
 
31
32
  /**
32
33
  * The renderer's CPU stages by `tnw_web_frame` field (web_host.cpp 66 to 77): running ms of every render, which
@@ -39,21 +40,21 @@ export const SHADOW_SEGMENT_FIELD = 19;
39
40
  * and the batched queue submit that ends it (PRD-771), then the `tnw_web_poll` that opens it and its tail (surface texture release, frame gate, diagnostics) and the wall time of `Renderer::render` itself (`rendererMs`, which holds the prepare and encode halves) inside the whole `RenderDatabase::render` call (`hostDatabaseMs`), then the render targets' draws (`hostTargetMs`). The order of the names is the order the page reads them.
40
41
  */
41
42
  export const CPU_STAGE_FIELDS = {
42
- cpuVirtualPagesMs: 66,
43
- cpuPlanMs: 67,
44
- cpuShadowBuildMs: 68,
45
- cpuPrepareMs: 69,
46
- cpuEncodeMs: 70,
47
- cpuPostMs: 73,
48
- cpuSceneMs: 74,
49
- hostRenderMs: 75,
50
- hostComputeMs: 76,
51
- hostSubmitMs: 77,
52
- hostPollMs: 79,
53
- hostTailMs: 80,
54
- rendererMs: 81,
55
- hostDatabaseMs: 82,
56
- hostTargetMs: 83,
43
+ cpuVirtualPagesMs: WEB_FRAME_FIELD.cpuVirtualPagesMs,
44
+ cpuPlanMs: WEB_FRAME_FIELD.cpuPlanMs,
45
+ cpuShadowBuildMs: WEB_FRAME_FIELD.cpuShadowBuildMs,
46
+ cpuPrepareMs: WEB_FRAME_FIELD.cpuPrepareMs,
47
+ cpuEncodeMs: WEB_FRAME_FIELD.cpuEncodeMs,
48
+ cpuPostMs: WEB_FRAME_FIELD.cpuPostMs,
49
+ cpuSceneMs: WEB_FRAME_FIELD.sceneMs,
50
+ hostRenderMs: WEB_FRAME_FIELD.hostRenderMs,
51
+ hostComputeMs: WEB_FRAME_FIELD.hostComputeMs,
52
+ hostSubmitMs: WEB_FRAME_FIELD.hostSubmitMs,
53
+ hostPollMs: WEB_FRAME_FIELD.hostPollMs,
54
+ hostTailMs: WEB_FRAME_FIELD.hostTailMs,
55
+ rendererMs: WEB_FRAME_FIELD.rendererMs,
56
+ hostDatabaseMs: WEB_FRAME_FIELD.hostDatabaseMs,
57
+ hostTargetMs: WEB_FRAME_FIELD.hostTargetMs,
57
58
  } as const;
58
59
 
59
60
  export type CpuStageName = keyof typeof CPU_STAGE_FIELDS;
@@ -0,0 +1,49 @@
1
+ // three's GridHelper over the engine's LineSegments, ported from three r180 src/helpers/GridHelper.js (MIT).
2
+ // The engine binds LineSegments, BufferGeometry and LineBasicMaterial, so the helper is those three: no
3
+ // engine class of its own. `type` stays the engine's "LineSegments", which is how the renderer picks the
4
+ // line-list topology; `isGridHelper` marks it as three's does.
5
+
6
+ type Constructor = new (...args: unknown[]) => object;
7
+
8
+ type EngineClasses = Readonly<
9
+ Record<
10
+ "LineSegments" | "BufferGeometry" | "Float32BufferAttribute" | "LineBasicMaterial" | "Color",
11
+ Constructor
12
+ >
13
+ >;
14
+
15
+ export function defineGridHelper(classes: EngineClasses) {
16
+ const LineSegments = classes.LineSegments as new (
17
+ geometry: object,
18
+ material: object,
19
+ ) => { geometry: { dispose(): void }; material: { dispose(): void } };
20
+ return class GridHelper extends LineSegments {
21
+ readonly isGridHelper = true;
22
+
23
+ constructor(size = 10, divisions = 10, color1: unknown = 0x444444, color2: unknown = 0x888888) {
24
+ const center = new classes.Color(color1) as { r: number; g: number; b: number };
25
+ const line = new classes.Color(color2) as { r: number; g: number; b: number };
26
+ const middle = divisions / 2;
27
+ const step = size / divisions;
28
+ const halfSize = size / 2;
29
+ const vertices: number[] = [];
30
+ const colors: number[] = [];
31
+ for (let i = 0, k = -halfSize; i <= divisions; i++, k += step) {
32
+ vertices.push(-halfSize, 0, k, halfSize, 0, k, k, 0, -halfSize, k, 0, halfSize);
33
+ const color = i === middle ? center : line;
34
+ for (let vertex = 0; vertex < 4; vertex++) colors.push(color.r, color.g, color.b);
35
+ }
36
+ const geometry = new classes.BufferGeometry() as {
37
+ setAttribute(name: string, attribute: object): void;
38
+ };
39
+ geometry.setAttribute("position", new classes.Float32BufferAttribute(vertices, 3));
40
+ geometry.setAttribute("color", new classes.Float32BufferAttribute(colors, 3));
41
+ super(geometry, new classes.LineBasicMaterial({ vertexColors: true, toneMapped: false }));
42
+ }
43
+
44
+ dispose(): void {
45
+ this.geometry.dispose();
46
+ this.material.dispose();
47
+ }
48
+ };
49
+ }
@@ -196,15 +196,31 @@ export function defineObjectSurface(classes: ISurfaceClasses): void {
196
196
  }
197
197
 
198
198
  // three's scene classes and the `is<Class>` flag each one defines; every light is also `isLight`.
199
+ // type-flags.spec.ts fails when a bound class lacks a flag three's own instance reads true.
199
200
  const FLAGGED_CLASSES = [
200
201
  "NodeMaterial",
201
202
  "MeshBasicNodeMaterial",
202
203
  "MeshStandardNodeMaterial",
203
204
  "MeshPhysicalNodeMaterial",
204
205
  "SpriteNodeMaterial",
206
+ // Its parent is MeshStandardMaterial, so the material base (Material's direct children) misses it.
207
+ "MeshPhysicalMaterial",
205
208
  "Texture",
206
209
  "DataTexture",
207
210
  "CanvasTexture",
211
+ "CubeTexture",
212
+ "Data3DTexture",
213
+ "DataArrayTexture",
214
+ "DepthTexture",
215
+ "Source",
216
+ "RenderTarget",
217
+ "Matrix4",
218
+ "CatmullRomCurve3",
219
+ // A particle batch re-sends only the attributes it finds by this flag, as three's idiom has it.
220
+ "InstancedBufferAttribute",
221
+ "InstancedBufferGeometry",
222
+ "InterleavedBufferAttribute",
223
+ "CompressedTexture",
208
224
  "Object3D",
209
225
  "Scene",
210
226
  "Group",
@@ -244,6 +260,19 @@ export function defineReleaseNoops(classes: Readonly<Record<string, unknown>>):
244
260
  }
245
261
  }
246
262
 
263
+ /** The flags three's own instance of a listed class reads true. */
264
+ function flagsOf(name: string): string[] {
265
+ if (name.endsWith("Light")) return [`is${name}`, "isLight"];
266
+ // three renamed Source to TextureSource and keeps both flags.
267
+ if (name === "Source") return ["isSource", "isTextureSource"];
268
+ if (!name.endsWith("NodeMaterial")) return [`is${name}`];
269
+ // three's node material copies its classic material's defaults, flag included: a
270
+ // MeshStandardNodeMaterial reads isMeshStandardMaterial, as a physical one reads both.
271
+ const flags = [`is${name}`, "isNodeMaterial", `is${name.replace("Node", "")}`];
272
+ if (name === "MeshPhysicalNodeMaterial") flags.push("isMeshStandardMaterial");
273
+ return flags;
274
+ }
275
+
247
276
  /**
248
277
  * Puts three's type flags (`isMesh`, `isCamera`, `isLight`, ...) on a back end's scene classes. A
249
278
  * subclass inherits its parent's through the prototype chain, as in three; a missing class is skipped.
@@ -252,12 +281,7 @@ export function defineTypeFlags(classes: Readonly<Record<string, unknown>>): voi
252
281
  for (const name of FLAGGED_CLASSES) {
253
282
  const cls = classes[name] as Constructor | undefined;
254
283
  if (typeof cls !== "function") continue;
255
- const flags = name.endsWith("Light")
256
- ? [`is${name}`, "isLight"]
257
- : name.endsWith("NodeMaterial")
258
- ? [`is${name}`, "isNodeMaterial"]
259
- : [`is${name}`];
260
- for (const flag of flags)
284
+ for (const flag of flagsOf(name))
261
285
  Object.defineProperty(cls.prototype, flag, {
262
286
  configurable: true,
263
287
  writable: true,
package/src/pass-node.ts CHANGED
@@ -176,6 +176,8 @@ interface IPassTsl {
176
176
  texture(source: unknown, uv: unknown): unknown;
177
177
  uv(): unknown;
178
178
  readonly normalView: unknown;
179
+ /** three's diffuseColor node, when the engine's TSL table has one. */
180
+ readonly diffuseColor?: unknown;
179
181
  /** The TSL constructors the surface decode composes; a table that lacks them refuses by name. */
180
182
  readonly floor?: (value: unknown) => unknown;
181
183
  readonly vec4?: (...values: unknown[]) => unknown;
@@ -214,14 +216,19 @@ const RESERVED_SLOTS: ReadonlySet<string> = new Set([
214
216
  "emissive",
215
217
  ]);
216
218
 
219
+ /** The pass's own `normalView` spelling of its normal target, which the scene pass draws itself. */
220
+ const passNormals = new WeakSet<object>();
221
+
217
222
  /**
218
- * Whether a material's `mrtNode` carries this output. The engine's scene pass draws its own colour,
219
- * depth, velocity, normal and surface targets every frame, so a material writes only the names the pass
220
- * does not own; a marker value (the pass's own slot) says the same for any other name it reached.
223
+ * Whether a material's `mrtNode` writes this output. The engine's scene pass draws its own colour, depth, velocity,
224
+ * normal and surface targets every frame, so the pass's own spellings of them (a slot marker, `normal: normalView`)
225
+ * stay with the pass. Any other node, under any name, is the material's: a render target's texture of that name
226
+ * takes it (an impostor bake writes `output` and `normal`, PRD-858).
221
227
  */
222
228
  export function isCustomMrtOutput(name: string, value: unknown): boolean {
223
- if (RESERVED_SLOTS.has(name)) return false;
224
- return (value as { isMRTSlot?: boolean } | null)?.isMRTSlot !== true;
229
+ if (value === null || typeof value !== "object") return false;
230
+ if ((value as { isMRTSlot?: boolean }).isMRTSlot === true) return false;
231
+ return !(name === "normal" && (passNormals.has(value) || "__tnNormalView" in value));
225
232
  }
226
233
 
227
234
  /** WebGPU's guaranteed colour-attachment count, and what a device's limit reports here when it answers. */
@@ -286,9 +293,19 @@ export function definePass(
286
293
  const slot = (name: string): IMRTSlot => Object.freeze({ isMRTSlot: true, name });
287
294
  const output = slot("output");
288
295
  const normalView = tsl.normalView;
296
+ if (normalView !== null && typeof normalView === "object") passNormals.add(normalView);
289
297
  const metalness = slot("metalness");
290
298
  const roughness = slot("roughness");
291
- const diffuseColor = slot("diffuseColor");
299
+ // three's diffuseColor is a node a material reads (`diffuseColor.a`) and the pass's slot in mrt(); the engine's node
300
+ // carries the slot marker, so both spellings hold.
301
+ const engineDiffuse = tsl.diffuseColor;
302
+ const diffuseColor: IMRTSlot =
303
+ engineDiffuse !== null && typeof engineDiffuse === "object"
304
+ ? (Object.defineProperties(engineDiffuse, {
305
+ isMRTSlot: { value: true },
306
+ name: { value: "diffuseColor" },
307
+ }) as IMRTSlot)
308
+ : slot("diffuseColor");
292
309
  const emissive = slot("emissive");
293
310
 
294
311
  /** A scene pass output: one of the slots, or `normal` as normalView (or vec4(normalView, 0)). */
@@ -606,6 +623,9 @@ export function definePass(
606
623
  // reads this name (Renderer::planAux), so the read is the request and no MRT output names it: the occlusion and
607
624
  // the gather belong to the opaque surface, and the composite restores what a translucent sheet drew over it.
608
625
  if (name === "translucent") return tsl.texture({ name: "translucent" }, tsl.uv());
626
+ // Each lit draw's indirect diffuse light (hemisphere, ambient, environment, irradiance node; no direct light),
627
+ // drawn while the post graph reads the name (Renderer::planAux), so an occlusion can darken that share alone.
628
+ if (name === "indirectDiffuse") return tsl.texture({ name: "indirectDiffuse" }, tsl.uv());
609
629
  if (
610
630
  (name === "metalness" || name === "roughness") &&
611
631
  this.#mrt?.outputs.normal === normalView &&