@needle-tools/materialx 1.7.1 → 1.7.2

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,18 +4,24 @@ 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.7.2] - 2026-07-06
8
+
9
+ ### Fixed
10
+ - MaterialX materials now honor Three.js `InstancedMesh` per-instance transforms in generated vertex shaders, including position, normal, and tangent transforms.
11
+ - Raw `.mtlx` material creation can now resolve relative texture filenames through the public `path` option.
12
+
7
13
  ## [1.7.1] - 2026-07-01
8
14
 
9
15
  ### Fixed
10
16
  - Normalized MaterialX texture request paths when combining loader search paths, `fileprefix`, and relative filename values, avoiding duplicate slashes in hosted asset URLs.
11
17
  - Preserved absolute texture URLs and filename substitution tokens while resolving relative MaterialX texture paths.
12
- - Added MaterialX path fixtures with reference renders for included libraries, file prefixes, relative paths, and texture-load diagnostics.
18
+ - Improved texture path handling for included MaterialX libraries, file prefixes, relative paths, and texture-load diagnostics.
13
19
 
14
20
  ## [1.7.0] - 2026-06-30
15
21
 
16
22
  ### Added
17
23
  - Runtime environment radiance modes for fast Three.js PMREM sampling, MaterialX prefiltered latlong sampling, and MaterialX filtered importance sampling.
18
- - MaterialX viewer examples for material library inspection, PMREM comparison, shaderball previews, configurable environments, and graph-driven material loading.
24
+ - Browser examples for material library inspection, PMREM comparison, shaderball previews, configurable environments, and graph-driven material loading.
19
25
  - Missing tangents are now automatically generated for MaterialX materials that require them. Set `generateTangents: false` to disable this.
20
26
 
21
27
  ### Changed
@@ -27,8 +33,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
27
33
  - Added the JSON module import attribute for `package.json` so raw browser ESM imports can read the package version without MIME errors.
28
34
  - Made Node ESM imports browser-global safe and load the bundled MaterialX WASM/data files from the installed package when `ready()` runs under Node.
29
35
  - Switched bundled MaterialX Emscripten loaders to `node:module` builtin imports for Deno compatibility, and added runtime smoke scripts for Node, Deno, and Bun.
30
- - Corrected material parameter editing for color values, environment controls, and graph-loaded materials in the viewer examples.
31
- - Kept viewer debug panels and camera controls consistent across the material library, graph, shadows, and complex scenes.
36
+ - Corrected material parameter editing for color values, environment controls, and graph-loaded materials in browser examples.
37
+ - Improved consistency of example controls and camera behavior across the material library, graph, shadows, and complex scenes.
32
38
 
33
39
  ## [1.6.0] – 2026-04-01
34
40
 
@@ -77,7 +83,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
83
  - Fix: Improved error log formatting with package version
78
84
 
79
85
  ## [1.4.4] – 2026-03-17
80
- - Fix: Minor type fixes and improved debug logging
86
+ - Fix: Minor type fixes and improved logging
81
87
 
82
88
  ## [1.4.3] – 2026-02-20
83
89
  - Add: `globalThis.NEEDLE_MATERIALX_LOCATION` to override WASM location. Use `"package"` for package-local files, or a custom path for self-hosted/CDN.
package/README.md CHANGED
@@ -6,6 +6,7 @@ Web runtime for [MaterialX](https://materialx.org/) materials in [Needle Engine]
6
6
  - Vertex displacement support (procedural noise, texture-based, animatable)
7
7
  - Material-level ambient occlusion (per glTF specification)
8
8
  - UV coordinate convention handling (glTF ↔ OpenGL)
9
+ - Three.js `InstancedMesh` support for generated MaterialX shaders
9
10
  - Automatic MikkTSpace tangent generation when MaterialX shaders need tangents
10
11
  - Three.js shadow support (directional, spot, point lights)
11
12
  - Alpha mask and blend transparency modes
@@ -138,7 +139,22 @@ IMPORTANT: The value must end with a trailing slash (`/`).
138
139
 
139
140
  ### Texture Path Resolution
140
141
 
141
- MaterialX texture filenames are resolved from the MaterialX-authored filename value, any active `fileprefix`, and the loader search path. Relative paths are normalized for web requests so hosted asset URLs do not gain duplicate slashes, while absolute URLs such as `https://`, `file://`, root-relative paths, data URIs, blob URLs, and MaterialX filename tokens such as `<UDIM>` are preserved.
142
+ MaterialX texture filenames are resolved from the MaterialX-authored filename value, any active `fileprefix`, and the loader path. Relative paths are normalized for web requests so hosted asset URLs do not gain duplicate slashes, while absolute URLs such as `https://`, `file://`, root-relative paths, data URIs, blob URLs, and MaterialX filename tokens such as `<UDIM>` are preserved.
143
+
144
+ For raw `.mtlx` XML loaded from a URL, pass the containing directory as `path`, matching the `parse(data, path, ...)` convention used by Three.js loaders:
145
+
146
+ ```ts
147
+ const material = await Experimental_API.createMaterialXMaterial(
148
+ mtlxSource,
149
+ 0,
150
+ {
151
+ getTexture: async (url) => textureLoader.loadAsync(url),
152
+ },
153
+ {
154
+ path: "https://example.com/materials/",
155
+ },
156
+ );
157
+ ```
142
158
 
143
159
 
144
160
  ## glTF Extension: NEEDLE_materials_mtlx
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@needle-tools/materialx",
3
3
  "description": "MaterialX material support for three.js and Needle Engine – render physically based MaterialX shaders in the browser via WebAssembly",
4
- "version": "1.7.1",
4
+ "version": "1.7.2",
5
5
  "type": "module",
6
6
  "license": "PolyForm-Noncommercial-1.0.0",
7
7
  "main": "index.js",
@@ -46,6 +46,8 @@ export interface MaterialXLoaderOptions {
46
46
  specularAntialiasing?: boolean;
47
47
  /** Generate missing MikkTSpace tangents for tangent-dependent shaders. Defaults to true. */
48
48
  generateTangents?: boolean;
49
+ /** Base path used to resolve relative MaterialX texture filenames for raw .mtlx material creation. */
50
+ path?: string;
49
51
  /** Flip texcoord V at the MaterialX texcoord node output. Defaults to false for standalone .mtlx creation and true for GLTFLoader integration. */
50
52
  hwTexcoordVerticalFlip?: boolean;
51
53
  /** Flip texcoord V inside MaterialX file texture sampling. Defaults to false for standalone .mtlx creation and true for GLTFLoader integration. */
@@ -359,6 +359,7 @@ export async function createMaterialXMaterial(mtlx, materialNodeNameOrIndex, loa
359
359
  environmentRadianceMode: options?.environmentRadianceMode,
360
360
  specularAntialiasing: options?.specularAntialiasing,
361
361
  generateTangents: options?.generateTangents,
362
+ path: options?.path,
362
363
  parameters: {
363
364
  // MASK uses discard; BLEND uses Three.js transparent sorting and blending.
364
365
  transparent: renderAsTransparent,
@@ -188,6 +188,10 @@ function resolveTexturePath(searchPath, value) {
188
188
  return new URL(value, withTrailingSlash(normalizedSearchPath)).href;
189
189
  }
190
190
 
191
+ if (THREE.LoaderUtils?.resolveURL) {
192
+ return normalizeRelativeTexturePath(THREE.LoaderUtils.resolveURL(value, withTrailingSlash(normalizedSearchPath)));
193
+ }
194
+
191
195
  return normalizeRelativeTexturePath(`${normalizedSearchPath}${IMAGE_PATH_SEPARATOR}${value}`);
192
196
  }
193
197
 
@@ -13,6 +13,7 @@ declare type MaterialXMaterialInitParameters = {
13
13
  environmentRadianceMode?: MaterialXEnvironmentRadianceMode;
14
14
  specularAntialiasing?: boolean;
15
15
  generateTangents?: boolean;
16
+ path?: string;
16
17
  debug?: boolean;
17
18
  }
18
19
 
@@ -354,6 +354,60 @@ function patchImageAddressModes(fragmentShader) {
354
354
  );
355
355
  }
356
356
 
357
+ function patchInstancingTransforms(vertexShader) {
358
+ if (!vertexShader.includes('u_worldMatrix') || vertexShader.includes('mtlxWorldMatrix')) return vertexShader;
359
+
360
+ let helper = `
361
+ mat4 mtlxWorldMatrix()
362
+ {
363
+ #ifdef USE_INSTANCING
364
+ return u_worldMatrix * instanceMatrix;
365
+ #else
366
+ return u_worldMatrix;
367
+ #endif
368
+ }
369
+ `;
370
+
371
+ if (vertexShader.includes('u_worldInverseTransposeMatrix')) {
372
+ helper += `
373
+ vec3 mtlxWorldNormal(vec3 objectNormal)
374
+ {
375
+ vec3 transformedNormal = objectNormal;
376
+ #ifdef USE_INSTANCING
377
+ mat3 instanceNormalMatrix = mat3(instanceMatrix);
378
+ transformedNormal /= vec3(
379
+ dot(instanceNormalMatrix[0], instanceNormalMatrix[0]),
380
+ dot(instanceNormalMatrix[1], instanceNormalMatrix[1]),
381
+ dot(instanceNormalMatrix[2], instanceNormalMatrix[2])
382
+ );
383
+ transformedNormal = instanceNormalMatrix * transformedNormal;
384
+ #endif
385
+ return normalize(mat3(u_worldInverseTransposeMatrix) * transformedNormal);
386
+ }
387
+ `;
388
+ }
389
+
390
+ vertexShader = vertexShader.replace(/void\s+main\s*\(\s*\)\s*\{/, `${helper}\nvoid main() {`);
391
+ vertexShader = vertexShader.replaceAll(
392
+ 'u_worldMatrix * vec4(position, 1.0)',
393
+ 'mtlxWorldMatrix() * vec4(position, 1.0)',
394
+ );
395
+ vertexShader = vertexShader.replaceAll(
396
+ 'mx_matrix_mul(u_worldMatrix, vec4(tangent, 0.0)).xyz',
397
+ '(mtlxWorldMatrix() * vec4(tangent, 0.0)).xyz',
398
+ );
399
+ vertexShader = vertexShader.replaceAll(
400
+ 'normalize(mx_matrix_mul(u_worldInverseTransposeMatrix, vec4(normal, 0.0)).xyz)',
401
+ 'mtlxWorldNormal(normal)',
402
+ );
403
+ vertexShader = vertexShader.replaceAll(
404
+ 'normalize(mat3(viewMatrix) * mat3(u_worldInverseTransposeMatrix) * normal)',
405
+ 'normalize(mat3(viewMatrix) * mtlxWorldNormal(normal))',
406
+ );
407
+
408
+ return vertexShader;
409
+ }
410
+
357
411
  /**
358
412
  * @typedef {Object} MaterialXMaterialInitParameters
359
413
  * @property {string} name
@@ -365,6 +419,7 @@ function patchImageAddressModes(fragmentShader) {
365
419
  * @property {"three-pmrem" | "materialx-prefiltered" | "materialx-fis"} [environmentRadianceMode]
366
420
  * @property {boolean} [specularAntialiasing] - Match Three.js glossy specular antialiasing. Defaults to true.
367
421
  * @property {boolean} [generateTangents] - Generate missing MikkTSpace tangents for tangent-dependent shaders. Defaults to true.
422
+ * @property {string} [path] - Base path used to resolve relative MaterialX texture filenames.
368
423
  * @property {boolean} [debug] - Debug flag
369
424
  */
370
425
 
@@ -733,6 +788,8 @@ $2`
733
788
  );
734
789
  } // end hasShadowUniforms
735
790
 
791
+ vertexShader = patchInstancingTransforms(vertexShader);
792
+
736
793
  const threeParameters = { ...init.parameters };
737
794
  materialParameters = {
738
795
  name: init.name,
@@ -754,7 +811,7 @@ $2`
754
811
  return;
755
812
  }
756
813
 
757
- const searchPath = ""; // Could be derived from the asset path if needed
814
+ const path = init.path || "";
758
815
  /** @type {Array<Promise<unknown>>} */
759
816
  const pendingTextureLoads = [];
760
817
  this.shaderName = init.shaderName || null;
@@ -771,8 +828,8 @@ $2`
771
828
  // uniforms below must keep their original object identity so async
772
829
  // texture resolution updates the same uniform object.
773
830
  mergeUniforms([createThreeLightUniforms()]),
774
- getUniformValues(init.shader.getStage('vertex'), init.loaders, searchPath, pendingTextureLoads),
775
- getUniformValues(init.shader.getStage('pixel'), init.loaders, searchPath, pendingTextureLoads),
831
+ getUniformValues(init.shader.getStage('vertex'), init.loaders, path, pendingTextureLoads),
832
+ getUniformValues(init.shader.getStage('pixel'), init.loaders, path, pendingTextureLoads),
776
833
  {
777
834
  u_worldMatrix: { value: new Matrix4() },
778
835
  u_worldInverseMatrix: { value: new Matrix4() },