@needle-tools/materialx 1.2.1-next.bc1fa29 → 1.2.2-next.4c698a4

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/CHANGELOG.md CHANGED
@@ -4,6 +4,9 @@ All notable changes to this package will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.2.2] - 2025-07-24
8
+ - Add: `preloadWasm` function with support to wait for network idle. This is automatically done for Needle Engine projects.
9
+
7
10
  ## [1.2.1] - 2025-07-23
8
11
  - Fix: error caused by scene.environment being null
9
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/materialx",
3
- "version": "1.2.1-next.bc1fa29",
3
+ "version": "1.2.2-next.4c698a4",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/src/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { ready, type MaterialXContext } from "./materialx.js";
1
+ export { ready, type MaterialXContext, preloadWasm } from "./materialx.js";
2
2
  export { MaterialXMaterial } from "./materialx.material.js";
3
3
  export { MaterialXLoader } from "./loader/loader.three.js";
4
4
 
@@ -10,5 +10,3 @@ declare const Experimental_API: {
10
10
 
11
11
  export { Experimental_API };
12
12
 
13
-
14
- export function preloadWasm(trigger: "immediately" | "network_idle"): Promise<void>;
package/src/index.js CHANGED
@@ -1,38 +1,16 @@
1
1
  import { createMaterialXMaterial } from "./loader/loader.three.js";
2
- import { ready } from "./materialx.js";
3
- import { waitForNetworkIdle } from "./utils.js";
4
2
 
5
-
6
- export { ready } from "./materialx.js";
3
+ export { ready, preloadWasm } from "./materialx.js";
7
4
  export { MaterialXMaterial } from "./materialx.material.js";
8
5
  export { MaterialXLoader } from "./loader/loader.three.js";
9
6
 
10
7
 
11
- /**
12
- * Preloads the MaterialX WebAssembly module.
13
- * @type {import("./index.js").preloadWasm}
14
- */
15
- export async function preloadWasm(trigger) {
16
- if (trigger === "immediately") {
17
- // Load the WASM module immediately
18
- await ready();
19
- } else if (trigger === "network_idle") {
20
- // Wait for network to be idle before loading
21
- await waitForNetworkIdle();
22
- await ready();
23
- }
24
- }
25
-
26
-
27
-
28
-
29
8
  /**
30
9
  * Experimental API for creating MaterialX materials.
31
10
  */
32
11
  const Experimental_API = {
33
12
  createMaterialXMaterial
34
- }
35
-
36
- export { Experimental_API }
13
+ };
37
14
 
15
+ export { Experimental_API };
38
16
 
@@ -1,6 +1,7 @@
1
1
  import { addCustomExtensionPlugin, Context } from "@needle-tools/engine";
2
2
  import { useNeedleMaterialX as _useNeedleMaterialX } from "./loader.three.js";
3
3
  import { debug } from "../utils.js";
4
+ import { preloadWasm } from "../materialx.js";
4
5
 
5
6
  /**
6
7
  * @typedef {import("@needle-tools/engine").INeedleGLTFExtensionPlugin} INeedleGLTFExtensionPlugin
@@ -58,5 +59,6 @@ export class MaterialXLoaderPlugin {
58
59
  * @returns {Promise<void>}
59
60
  */
60
61
  export async function useNeedleMaterialX() {
62
+ preloadWasm("network_idle");
61
63
  addCustomExtensionPlugin(new MaterialXLoaderPlugin());
62
64
  }
@@ -4,7 +4,7 @@ import { MaterialXContext } from "../materialx.js";
4
4
  import { MaterialXMaterial } from "../materialx.material.js";
5
5
  import { Callbacks } from "../materialx.helper.js";
6
6
 
7
- export interface MaterialX_root_extension {
7
+ export interface MaterialX_root_document {
8
8
  /** e.g. 1.39 */
9
9
  version: string;
10
10
  /** e.g. "Material" */
@@ -21,9 +21,15 @@ export interface MaterialX_root_extension {
21
21
  }>;
22
22
  }
23
23
 
24
+ export interface MaterialX_root_extension {
25
+ documents: Array<MaterialX_root_document>;
26
+ }
27
+
24
28
  export interface MaterialX_material_extension {
25
29
  /** The MaterialX material name */
26
30
  name: string;
31
+ /** The index of the document in the documents array of the root extension. */
32
+ document?: number;
27
33
  /** The index of the shader in the shaders array of the root extension. */
28
34
  shader?: number;
29
35
  }
@@ -31,8 +31,17 @@ export class MaterialXLoader {
31
31
  /** @type {Promise<any> | null} */
32
32
  _documentReadyPromise = null;
33
33
 
34
+ /**
35
+ * @returns {MaterialX_root_extension | null}
36
+ */
34
37
  get materialX_root_data() {
35
- return /** @type {MaterialX_root_extension | null} */ (this.parser.json.extensions?.[this.name]) || null;
38
+ const ext = this.parser.json.extensions?.[this.name];
39
+ let result = null;
40
+ if ("documents" in ext && Array.isArray(ext.documents))
41
+ result = ext.documents;
42
+ else
43
+ result = [ext];
44
+ return result;
36
45
  }
37
46
 
38
47
  /** Generated materialX materials */
@@ -85,8 +94,9 @@ export class MaterialXLoader {
85
94
  // Handle different types of MaterialX data
86
95
  /** @type {MaterialX_material_extension} */
87
96
  const ext = materialDef.extensions?.[this.name];
88
-
89
- const mtlx = this.materialX_root_data?.mtlx;
97
+ const documentIndex = ext.document || 0;
98
+ const materialX_root_data = this.materialX_root_data?.[documentIndex];
99
+ const mtlx = materialX_root_data.mtlx || null;
90
100
 
91
101
  if (ext && mtlx) {
92
102
 
@@ -108,8 +118,8 @@ export class MaterialXLoader {
108
118
  const filenameWithoutExt = url.split('/').pop()?.split('.').shift() || '';
109
119
 
110
120
  // Resolve the texture from the MaterialX root extension
111
- if (this.materialX_root_data) {
112
- const textures = this.materialX_root_data.textures || [];
121
+ if (materialX_root_data) {
122
+ const textures = materialX_root_data.textures || [];
113
123
  let index = -1;
114
124
  for (const texture of textures) {
115
125
  // Find the texture by name and use the pointer string to get the index
@@ -300,12 +310,6 @@ export async function createMaterialXMaterial(mtlx, materialNodeName, loaders, o
300
310
 
301
311
  const shader = state.materialXGenerator.generate(elementName, renderableElement, state.materialXGenContext);
302
312
 
303
- // const rootExtension = this.materialX_root_data;
304
-
305
- // const shaderInfo = rootExtension && material_extension.shader !== undefined && material_extension.shader >= 0
306
- // ? rootExtension.shaders?.[material_extension.shader]
307
- // : null;
308
-
309
313
  const shaderMaterial = new MaterialXMaterial({
310
314
  name: materialNodeName,
311
315
  shaderName: null, //shaderInfo?.originalName || shaderInfo?.name || null,
@@ -1,6 +1,8 @@
1
1
  import { Light, Scene, Texture, WebGLRenderer } from "three";
2
2
  import type { MaterialX as MX } from "./materialx.types.js";
3
3
 
4
+ export function preloadWasm(trigger: "immediately" | "network_idle"): Promise<void>;
5
+
4
6
  export type MaterialXContext = {
5
7
  getTime?(): number;
6
8
  getFrame?(): number;
package/src/materialx.js CHANGED
@@ -1,10 +1,27 @@
1
1
  import MaterialX from "../bin/JsMaterialXGenShader.js";
2
- import { debug } from "./utils.js";
2
+ import { debug, waitForNetworkIdle } from "./utils.js";
3
3
  import { renderPMREMToEquirect } from "./utils.texture.js";
4
4
  import { Light, Mesh, MeshBasicMaterial, Object3D, PlaneGeometry, PMREMGenerator, Scene, Texture, WebGLRenderer } from "three";
5
5
  import { registerLights, getLightData } from "./materialx.helper.js";
6
6
  import { whiteTexture } from "./utils.texture.js";
7
7
 
8
+
9
+ /**
10
+ * Preloads the MaterialX WebAssembly module.
11
+ * @type {import("./materialx.js").preloadWasm}
12
+ */
13
+ export async function preloadWasm(trigger) {
14
+ if (trigger === "immediately") {
15
+ // Load the WASM module immediately
16
+ return ready();
17
+ } else if (trigger === "network_idle") {
18
+ // Wait for network to be idle before loading
19
+ return waitForNetworkIdle().then(ready);
20
+ }
21
+ }
22
+
23
+
24
+
8
25
  export const state = new class {
9
26
  /** @type {import("./materialx.types.js").MaterialX.MODULE | null} */
10
27
  materialXModule = null;
@@ -87,6 +87,11 @@ export class MaterialXMaterial extends ShaderMaterial {
87
87
  vertexShader = vertexShader.replace(/\bi_texcoord_3\b/g, 'uv3');
88
88
  vertexShader = vertexShader.replace(/\bi_tangent\b/g, 'tangent');
89
89
  vertexShader = vertexShader.replace(/\bi_color_0\b/g, 'color');
90
+ // TODO: do we need to add depthbuffer fragments? https://discourse.threejs.org/t/shadermaterial-render-order-with-logarithmicdepthbuffer-is-wrong/49221/4
91
+ // Add logdepthbuf_pars_vertex at the beginning of the vertex shader before main()
92
+ // vertexShader = vertexShader.replace(/void\s+main\s*\(\s*\)\s*{/, `#include <logdepthbuf_pars_vertex>\nvoid main() {`);
93
+ // // Add logdepthbuf_vertex to vertex shader if not present at end of main()
94
+ // vertexShader = vertexShader.replace(/void\s+main\s*\(\s*\)\s*{/, `void main() {\n #include <logdepthbuf_vertex>\n`);
90
95
 
91
96
  // Patch fragmentShader
92
97
  const precision = init.parameters?.precision || "highp";