@threenative/three-native 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threenative/three-native",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "license": "MIT",
5
5
  "description": "The native engine's machine-readable compatibility catalog and the generators that adapt it to TypeScript declarations and the C binding ABI.",
6
6
  "keywords": [
@@ -283,6 +283,10 @@ export class GLTFLoader {
283
283
  uris.map(async (uri) => {
284
284
  const response = await fetch(path + uri);
285
285
  if (!response.ok) throw new Error(`TN_WASM_GLTF_FETCH: ${response.status} ${path + uri}`);
286
+ // A dev server answers a missing file with its page and a 200 (Vite's SPA fallback); the
287
+ // engine would refuse those bytes only as "undecoded images", without the file's name.
288
+ if (response.headers.get("content-type")?.startsWith("text/html"))
289
+ throw new Error(`TN_WASM_GLTF_FETCH: text/html ${path + uri}`);
286
290
  // The engine loader asks for the uri with its percent escapes decoded.
287
291
  return [decodeURIComponent(uri), new Uint8Array(await response.arrayBuffer())] as const;
288
292
  }),
@@ -18,6 +18,8 @@ export const CENTER = 0;
18
18
  export const AVERAGE = 1;
19
19
  export const SAH = 2;
20
20
 
21
+ const generatedBounds = new WeakMap<object, object>();
22
+
21
23
  export class MeshBVH {
22
24
  readonly geometry: object;
23
25
 
@@ -35,6 +37,20 @@ export class MeshBVH {
35
37
  `TN_NATIVE_MESH_BVH_OPTIONS_UNSUPPORTED: ${unsupported.join(", ")}: the engine MeshBVH builds no tree, so it takes no other build option`,
36
38
  );
37
39
  this.geometry = geometry;
40
+ const bounded = geometry as {
41
+ readonly boundingBox?: object | null;
42
+ computeBoundingBox?: () => void;
43
+ };
44
+ const previous = generatedBounds.get(geometry);
45
+ if (
46
+ typeof bounded.computeBoundingBox === "function" &&
47
+ (bounded.boundingBox === null || (previous !== undefined && bounded.boundingBox === previous))
48
+ ) {
49
+ // Core rebuilds after position-version changes. Refresh only the box this adapter owns;
50
+ // callers override that ownership by assigning a different box, not editing it in place.
51
+ bounded.computeBoundingBox();
52
+ if (bounded.boundingBox != null) generatedBounds.set(geometry, bounded.boundingBox);
53
+ }
38
54
  }
39
55
 
40
56
  /** three-mesh-bvh's `raycastObject3D`: the object's hits (only the nearest with `firstHitOnly`). */
@@ -8,15 +8,23 @@
8
8
  import { __tnTsl } from "three/tsl";
9
9
  import { unsupportedExport } from "../refused.js";
10
10
  import { type IEffectTsl, definePostEffects } from "./post-effects.js";
11
+ import {
12
+ type IScreenSpaceTsl,
13
+ defineScreenSpaceGi,
14
+ defineScreenSpaceReflections,
15
+ } from "./screen-space.js";
11
16
 
12
17
  export const { ao, denoise, smaa, bloom, traa, film, lut3D } = definePostEffects(
13
18
  __tnTsl as IEffectTsl,
14
19
  );
15
20
 
21
+ // Screen-space reflections are authored over the engine TSL (screen-space.ts), not three's SSRNode.
22
+ export const ssr = defineScreenSpaceReflections(__tnTsl as IScreenSpaceTsl);
23
+ // Screen-space diffuse GI, the same way (three's SSGINode runs `Fn(...).setLayout(...)` at import).
24
+ export const ssgi = defineScreenSpaceGi(__tnTsl as IScreenSpaceTsl);
25
+
16
26
  // The other display nodes need live addon-node construction the engine lacks: importable, and they
17
27
  // refuse at use by name (as core-addons.mjs on the V8 player).
18
28
  export const fxaa = unsupportedExport("fxaa");
19
29
  export const godrays = unsupportedExport("godrays");
20
- export const ssgi = unsupportedExport("ssgi");
21
- export const ssr = unsupportedExport("ssr");
22
30
  export const sharpen = unsupportedExport("sharpen");
@@ -5,6 +5,8 @@
5
5
  * `__effect(name[, value])`, which the back end routes to tslEffectParameter.
6
6
  */
7
7
 
8
+ import { GPU_STAGES, gpuStageId } from "../gpu-stages.js";
9
+
8
10
  interface IEffectNode {
9
11
  __effect(name: string, value?: number): number;
10
12
  getTextureNode?: () => IEffectNode;
@@ -15,6 +17,7 @@ type Effect = (...args: unknown[]) => IEffectNode;
15
17
  /** The back end's TSL functions the effects are built by (the shared table's "ao", "bloom", ...). */
16
18
  export interface IEffectTsl {
17
19
  readonly ao: Effect;
20
+ readonly ssr: Effect;
18
21
  readonly denoise: Effect;
19
22
  readonly smaa: Effect;
20
23
  readonly bloom: Effect;
@@ -39,8 +42,116 @@ function uniforms(node: IEffectNode, names: readonly string[]): IEffectNode {
39
42
  return node;
40
43
  }
41
44
 
45
+ /** `node.gpuStage = "SSGI"` makes the effect's pass report its GPU time under that stage (gpu-stages.ts). */
46
+ function scaled(node: IEffectNode): IEffectNode {
47
+ return Object.defineProperty(node, "resolutionScale", {
48
+ configurable: true,
49
+ get: () => node.__effect("resolutionScale"),
50
+ set: (value: number) => {
51
+ node.__effect("resolutionScale", value);
52
+ },
53
+ });
54
+ }
55
+
56
+ function stageLabel(node: IEffectNode): IEffectNode {
57
+ return Object.defineProperty(node, "gpuStage", {
58
+ configurable: true,
59
+ get: () => GPU_STAGES[node.__effect("stage") - 1],
60
+ set: (name: string) => {
61
+ node.__effect("stage", gpuStageId(name));
62
+ },
63
+ });
64
+ }
65
+
66
+ const SSR_REFUSED_DEFAULTS: Readonly<Record<string, unknown>> = {
67
+ stochastic: false,
68
+ envImportanceSampling: false,
69
+ environmentNode: null,
70
+ diffuseNode: null,
71
+ };
72
+
73
+ function validateSsrOptions(options: Record<string, unknown>): void {
74
+ if (options === null || typeof options !== "object")
75
+ throw new TypeError("TN_NATIVE_SSR_OPTIONS: expected an options object");
76
+ for (const [name, value] of Object.entries(options)) {
77
+ if (["camera", "metalnessNode", "roughnessNode"].includes(name)) continue;
78
+ if (["reflectNonMetals", "binaryRefine"].includes(name)) {
79
+ if (typeof value !== "boolean")
80
+ throw new TypeError(`TN_NATIVE_SSR_OPTIONS: ${name} must be boolean`);
81
+ continue;
82
+ }
83
+ if (Object.hasOwn(SSR_REFUSED_DEFAULTS, name) && value === SSR_REFUSED_DEFAULTS[name]) continue;
84
+ throw new Error(`TN_NATIVE_SSR_UNSUPPORTED: ${name}`);
85
+ }
86
+ }
87
+
42
88
  export function definePostEffects(tsl: IEffectTsl) {
43
89
  return {
90
+ ssr(
91
+ color: unknown,
92
+ depth: unknown,
93
+ normal: unknown,
94
+ options: Record<string, unknown> = {},
95
+ ): IEffectNode {
96
+ validateSsrOptions(options);
97
+ const camera =
98
+ options.camera ?? (color as { passNode?: { camera?: unknown } } | null)?.passNode?.camera;
99
+ if (camera == null) throw new Error("TN_NATIVE_SSR_CAMERA: ssr() requires a camera");
100
+ const node = uniforms(
101
+ tsl.ssr(color, depth, normal, options.metalnessNode ?? null, options.roughnessNode ?? null),
102
+ [
103
+ "maxDistance",
104
+ "thickness",
105
+ "opacity",
106
+ "quality",
107
+ "maxSteps",
108
+ "intensity",
109
+ "maxLuminance",
110
+ "blurQuality",
111
+ ],
112
+ );
113
+ // r185 uses a number for blurQuality; also accept the uniform spelling used by native callers.
114
+ const blurQuality = Object.getOwnPropertyDescriptor(node, "blurQuality")?.value;
115
+ Object.defineProperty(node, "blurQuality", {
116
+ configurable: true,
117
+ get: () => blurQuality,
118
+ set: (value: number) => node.__effect("blurQuality", value),
119
+ });
120
+ for (const name of ["reflectNonMetals", "binaryRefine"]) {
121
+ node.__effect(name, Number(options[name] ?? false));
122
+ Object.defineProperty(node, name, {
123
+ configurable: true,
124
+ get: () => node.__effect(name) !== 0,
125
+ set: (value: boolean) => node.__effect(name, Number(value)),
126
+ });
127
+ }
128
+ Object.defineProperty(node, "resolutionScale", {
129
+ configurable: true,
130
+ get: () => node.__effect("resolutionScale"),
131
+ set: (value: number) => node.__effect("resolutionScale", value),
132
+ });
133
+ for (const [name, value] of Object.entries({
134
+ ...SSR_REFUSED_DEFAULTS,
135
+ historyTexture: null,
136
+ velocityTexture: null,
137
+ screenEdgeFadeBlack: false,
138
+ }))
139
+ Object.defineProperty(node, name, {
140
+ configurable: true,
141
+ get: () => value,
142
+ set: () => {
143
+ throw new Error(`TN_NATIVE_SSR_UNSUPPORTED: ${name}`);
144
+ },
145
+ });
146
+ for (const name of ["setHistory", "setEnvMap"])
147
+ Object.defineProperty(node, name, {
148
+ value: () => {
149
+ throw new Error(`TN_NATIVE_SSR_UNSUPPORTED: ${name}`);
150
+ },
151
+ });
152
+ node.getTextureNode = () => node;
153
+ return node;
154
+ },
44
155
  film: (input: unknown, intensity?: unknown, uv?: unknown): IEffectNode =>
45
156
  tsl.film(input, intensity, uv),
46
157
  lut3D: (input: unknown, lut: unknown, size: number, intensity: unknown): IEffectNode =>
@@ -130,37 +241,36 @@ export function definePostEffects(tsl: IEffectTsl) {
130
241
  ao(depthNode: unknown, normalNode: unknown, camera: unknown): IEffectNode {
131
242
  const node = uniforms(tsl.ao(depthNode, normalNode ?? null, camera), [
132
243
  "radius",
244
+ "reachPx",
133
245
  "thickness",
134
246
  "distanceExponent",
135
247
  "distanceFallOff",
136
248
  "scale",
137
249
  "samples",
138
250
  ]);
139
- Object.defineProperty(node, "resolutionScale", {
140
- configurable: true,
141
- get() {
142
- return node.__effect("resolutionScale");
143
- },
144
- set(value: number) {
145
- node.__effect("resolutionScale", value);
146
- },
147
- });
251
+ scaled(node);
148
252
  node.getTextureNode = () => node;
149
253
  return node;
150
254
  },
255
+ // The filter pass's target size, as on ao(): Unreal filters GTAO at the AO resolution
256
+ // (UE 5.8.3: Engine/Shaders/Private/PostProcessAmbientOcclusion.usf:1600-1602).
151
257
  denoise: (
152
258
  node: unknown,
153
259
  depthNode: unknown,
154
260
  normalNode: unknown,
155
261
  camera: unknown,
156
262
  ): IEffectNode =>
157
- uniforms(tsl.denoise(node, depthNode, normalNode ?? null, camera), [
158
- "lumaPhi",
159
- "depthPhi",
160
- "normalPhi",
161
- "radius",
162
- "index",
163
- ]),
263
+ scaled(
264
+ stageLabel(
265
+ uniforms(tsl.denoise(node, depthNode, normalNode ?? null, camera), [
266
+ "lumaPhi",
267
+ "depthPhi",
268
+ "normalPhi",
269
+ "radius",
270
+ "index",
271
+ ]),
272
+ ),
273
+ ),
164
274
  smaa: (node: unknown): IEffectNode => tsl.smaa(node),
165
275
  bloom: (node: unknown, strength?: number, radius?: number, threshold?: number): IEffectNode =>
166
276
  uniforms(tsl.bloom(node, strength, radius, threshold), [