@needle-tools/materialx 1.2.2 → 1.3.1-next.403a134

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.3.0] - 2025-08-12
8
+ - Change: Refactor extension to use a documents array instead of a single document, backwards compatibility is maintained
9
+
7
10
  ## [1.2.2] - 2025-07-24
8
11
  - Add: `preloadWasm` function with support to wait for network idle. This is automatically done for Needle Engine projects.
9
12
 
package/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Needle MaterialX
2
2
 
3
- Web runtime support to load and display MaterialX materials in Needle Engine and three.js
3
+ Web runtime support to load and display MaterialX materials in Needle Engine and three.js via the MaterialX WebAssembly library. glTF files containing the `NEEDLE_materials_mtlx` extension can be loaded with this package. There is also experimental support for loading raw MaterialX XML files.
4
+
5
+ > [!TIP]
6
+ > When using Needle Engine for Unity, this package can be added to your project via the Needle Engine component. Add it to the "NpmDef Dependencies" field. MaterialX files will then be automatically generated from compatible Shader Graphs with the "MaterialX" export type.
7
+ > [Learn more in the Needle Engine documentation](https://engine.needle.tools/docs/materialx.html).
4
8
 
5
9
  ## Installation
6
10
  `npm i @needle-tools/materialx`
@@ -26,6 +30,59 @@ import { useNeedleMaterialX } from "@needle-tools/materialx";
26
30
  useNeedleMaterialX(<yourGltfLoaderInstance>);
27
31
  ```
28
32
 
33
+ ## glTF Extension: NEEDLE_materials_mtlx
34
+
35
+ There is a root level glTF extension, `NEEDLE_materials_mtlx`, which contains an array of documents. Each document contains a MaterialX xml string, and information about textures and materials used in the document, so they can be resolved from inside the glTF buffers.
36
+
37
+ The `textures` array is used to resolve texture paths from inside the MaterialX document to texture pointers in the glTF file.
38
+
39
+ Materials can also contain the `NEEDLE_materials_mtlx` extension, which references a document and shader. This allows you to use the same MaterialX document for multiple materials in the glTF file.
40
+
41
+ ### Root-level extension
42
+ ```json
43
+ {
44
+ "extensions": {
45
+ "NEEDLE_materials_mtlx": {
46
+ "documents": [
47
+ {
48
+ "name": "Perforated_Metal",
49
+ "version": "1.38",
50
+ "xml": "<?xml version='1.0' encoding='UTF-8'?><materialx version=\"1.38\">...</materialx>",
51
+ "textures": [
52
+ {
53
+ "pointer": "/textures/0",
54
+ "name": "Perforated_Metal_diffuse"
55
+ },
56
+ {
57
+ "pointer": "/textures/1",
58
+ "name": "Perforated_Metal_normal"
59
+ }
60
+ ],
61
+ "shaders": [ // optional ]
62
+ }
63
+ ]
64
+ }
65
+ }
66
+ }
67
+ ```
68
+
69
+ ### Material-level extension
70
+ ```json
71
+ {
72
+ "materials": [
73
+ {
74
+ "name": "Perforated Metal",
75
+ "extensions": {
76
+ "NEEDLE_materials_mtlx": {
77
+ "name": "Perforated_Metal",
78
+ "document": 0,
79
+ "shader": 0
80
+ }
81
+ }
82
+ }
83
+ ]
84
+ }
85
+ ```
29
86
 
30
87
  <br />
31
88
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/materialx",
3
- "version": "1.2.2",
3
+ "version": "1.3.1-next.403a134",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -56,4 +56,4 @@
56
56
  "mtlx",
57
57
  "rendering"
58
58
  ]
59
- }
59
+ }
@@ -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,20 @@ 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
+ if (!ext) {
40
+ return null;
41
+ }
42
+ let result = null;
43
+ if ("documents" in ext && Array.isArray(ext.documents))
44
+ result = ext.documents;
45
+ else
46
+ result = [ext];
47
+ return result;
36
48
  }
37
49
 
38
50
  /** Generated materialX materials */
@@ -85,8 +97,9 @@ export class MaterialXLoader {
85
97
  // Handle different types of MaterialX data
86
98
  /** @type {MaterialX_material_extension} */
87
99
  const ext = materialDef.extensions?.[this.name];
88
-
89
- const mtlx = this.materialX_root_data?.mtlx;
100
+ const documentIndex = ext.document || 0;
101
+ const materialX_root_data = this.materialX_root_data?.[documentIndex];
102
+ const mtlx = materialX_root_data.mtlx || null;
90
103
 
91
104
  if (ext && mtlx) {
92
105
 
@@ -108,8 +121,8 @@ export class MaterialXLoader {
108
121
  const filenameWithoutExt = url.split('/').pop()?.split('.').shift() || '';
109
122
 
110
123
  // Resolve the texture from the MaterialX root extension
111
- if (this.materialX_root_data) {
112
- const textures = this.materialX_root_data.textures || [];
124
+ if (materialX_root_data) {
125
+ const textures = materialX_root_data.textures || [];
113
126
  let index = -1;
114
127
  for (const texture of textures) {
115
128
  // Find the texture by name and use the pointer string to get the index
@@ -300,12 +313,6 @@ export async function createMaterialXMaterial(mtlx, materialNodeName, loaders, o
300
313
 
301
314
  const shader = state.materialXGenerator.generate(elementName, renderableElement, state.materialXGenContext);
302
315
 
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
316
  const shaderMaterial = new MaterialXMaterial({
310
317
  name: materialNodeName,
311
318
  shaderName: null, //shaderInfo?.originalName || shaderInfo?.name || 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";