@needle-tools/materialx 1.7.1 → 1.7.3
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 +16 -5
- package/README.md +17 -1
- package/package.json +1 -1
- package/src/loader/loader.three.d.ts +2 -0
- package/src/loader/loader.three.js +1 -0
- package/src/materialx.helper.js +4 -0
- package/src/materialx.material.d.ts +1 -0
- package/src/materialx.material.js +64 -4
package/CHANGELOG.md
CHANGED
|
@@ -4,18 +4,29 @@ 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.3] - 2026-07-09
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
- Three PMREM environment sampling now reads the encoded CubeUV atlas from the base texture level, keeping glossy MaterialX reflections stable for prefiltered KTX2 environments that include hardware mip levels.
|
|
11
|
+
|
|
12
|
+
## [1.7.2] - 2026-07-06
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
- MaterialX materials now honor Three.js `InstancedMesh` per-instance transforms in generated vertex shaders, including position, normal, and tangent transforms.
|
|
16
|
+
- Raw `.mtlx` material creation can now resolve relative texture filenames through the public `path` option.
|
|
17
|
+
|
|
7
18
|
## [1.7.1] - 2026-07-01
|
|
8
19
|
|
|
9
20
|
### Fixed
|
|
10
21
|
- Normalized MaterialX texture request paths when combining loader search paths, `fileprefix`, and relative filename values, avoiding duplicate slashes in hosted asset URLs.
|
|
11
22
|
- Preserved absolute texture URLs and filename substitution tokens while resolving relative MaterialX texture paths.
|
|
12
|
-
-
|
|
23
|
+
- Improved texture path handling for included MaterialX libraries, file prefixes, relative paths, and texture-load diagnostics.
|
|
13
24
|
|
|
14
25
|
## [1.7.0] - 2026-06-30
|
|
15
26
|
|
|
16
27
|
### Added
|
|
17
28
|
- Runtime environment radiance modes for fast Three.js PMREM sampling, MaterialX prefiltered latlong sampling, and MaterialX filtered importance sampling.
|
|
18
|
-
-
|
|
29
|
+
- Browser examples for material library inspection, PMREM comparison, shaderball previews, configurable environments, and graph-driven material loading.
|
|
19
30
|
- Missing tangents are now automatically generated for MaterialX materials that require them. Set `generateTangents: false` to disable this.
|
|
20
31
|
|
|
21
32
|
### Changed
|
|
@@ -27,8 +38,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
|
27
38
|
- Added the JSON module import attribute for `package.json` so raw browser ESM imports can read the package version without MIME errors.
|
|
28
39
|
- 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
40
|
- 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
|
|
31
|
-
-
|
|
41
|
+
- Corrected material parameter editing for color values, environment controls, and graph-loaded materials in browser examples.
|
|
42
|
+
- Improved consistency of example controls and camera behavior across the material library, graph, shadows, and complex scenes.
|
|
32
43
|
|
|
33
44
|
## [1.6.0] – 2026-04-01
|
|
34
45
|
|
|
@@ -77,7 +88,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
|
77
88
|
- Fix: Improved error log formatting with package version
|
|
78
89
|
|
|
79
90
|
## [1.4.4] – 2026-03-17
|
|
80
|
-
- Fix: Minor type fixes and improved
|
|
91
|
+
- Fix: Minor type fixes and improved logging
|
|
81
92
|
|
|
82
93
|
## [1.4.3] – 2026-02-20
|
|
83
94
|
- 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
|
|
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.
|
|
4
|
+
"version": "1.7.3",
|
|
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,
|
package/src/materialx.helper.js
CHANGED
|
@@ -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
|
|
|
@@ -203,7 +203,10 @@ vec3 mx_cubeuv_bilinear(sampler2D envMap, vec3 direction, float mipInt) {
|
|
|
203
203
|
uv.y += 4.0 * (exp2(u_envRadianceCubeUVMaxMip) - faceSize);
|
|
204
204
|
uv.x *= u_envRadianceCubeUVTexelWidth;
|
|
205
205
|
uv.y *= u_envRadianceCubeUVTexelHeight;
|
|
206
|
-
|
|
206
|
+
// PMREM encodes roughness in the CubeUV atlas itself. If the source KTX2
|
|
207
|
+
// carries hardware mip levels, implicit sampling may pick those mips and
|
|
208
|
+
// corrupt the atlas lookup.
|
|
209
|
+
return textureLod(envMap, uv, 0.0).rgb;
|
|
207
210
|
}
|
|
208
211
|
|
|
209
212
|
float mx_cubeuv_roughnessToMip(float roughness) {
|
|
@@ -354,6 +357,60 @@ function patchImageAddressModes(fragmentShader) {
|
|
|
354
357
|
);
|
|
355
358
|
}
|
|
356
359
|
|
|
360
|
+
function patchInstancingTransforms(vertexShader) {
|
|
361
|
+
if (!vertexShader.includes('u_worldMatrix') || vertexShader.includes('mtlxWorldMatrix')) return vertexShader;
|
|
362
|
+
|
|
363
|
+
let helper = `
|
|
364
|
+
mat4 mtlxWorldMatrix()
|
|
365
|
+
{
|
|
366
|
+
#ifdef USE_INSTANCING
|
|
367
|
+
return u_worldMatrix * instanceMatrix;
|
|
368
|
+
#else
|
|
369
|
+
return u_worldMatrix;
|
|
370
|
+
#endif
|
|
371
|
+
}
|
|
372
|
+
`;
|
|
373
|
+
|
|
374
|
+
if (vertexShader.includes('u_worldInverseTransposeMatrix')) {
|
|
375
|
+
helper += `
|
|
376
|
+
vec3 mtlxWorldNormal(vec3 objectNormal)
|
|
377
|
+
{
|
|
378
|
+
vec3 transformedNormal = objectNormal;
|
|
379
|
+
#ifdef USE_INSTANCING
|
|
380
|
+
mat3 instanceNormalMatrix = mat3(instanceMatrix);
|
|
381
|
+
transformedNormal /= vec3(
|
|
382
|
+
dot(instanceNormalMatrix[0], instanceNormalMatrix[0]),
|
|
383
|
+
dot(instanceNormalMatrix[1], instanceNormalMatrix[1]),
|
|
384
|
+
dot(instanceNormalMatrix[2], instanceNormalMatrix[2])
|
|
385
|
+
);
|
|
386
|
+
transformedNormal = instanceNormalMatrix * transformedNormal;
|
|
387
|
+
#endif
|
|
388
|
+
return normalize(mat3(u_worldInverseTransposeMatrix) * transformedNormal);
|
|
389
|
+
}
|
|
390
|
+
`;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
vertexShader = vertexShader.replace(/void\s+main\s*\(\s*\)\s*\{/, `${helper}\nvoid main() {`);
|
|
394
|
+
vertexShader = vertexShader.replaceAll(
|
|
395
|
+
'u_worldMatrix * vec4(position, 1.0)',
|
|
396
|
+
'mtlxWorldMatrix() * vec4(position, 1.0)',
|
|
397
|
+
);
|
|
398
|
+
vertexShader = vertexShader.replaceAll(
|
|
399
|
+
'mx_matrix_mul(u_worldMatrix, vec4(tangent, 0.0)).xyz',
|
|
400
|
+
'(mtlxWorldMatrix() * vec4(tangent, 0.0)).xyz',
|
|
401
|
+
);
|
|
402
|
+
vertexShader = vertexShader.replaceAll(
|
|
403
|
+
'normalize(mx_matrix_mul(u_worldInverseTransposeMatrix, vec4(normal, 0.0)).xyz)',
|
|
404
|
+
'mtlxWorldNormal(normal)',
|
|
405
|
+
);
|
|
406
|
+
vertexShader = vertexShader.replaceAll(
|
|
407
|
+
'normalize(mat3(viewMatrix) * mat3(u_worldInverseTransposeMatrix) * normal)',
|
|
408
|
+
'normalize(mat3(viewMatrix) * mtlxWorldNormal(normal))',
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
return vertexShader;
|
|
412
|
+
}
|
|
413
|
+
|
|
357
414
|
/**
|
|
358
415
|
* @typedef {Object} MaterialXMaterialInitParameters
|
|
359
416
|
* @property {string} name
|
|
@@ -365,6 +422,7 @@ function patchImageAddressModes(fragmentShader) {
|
|
|
365
422
|
* @property {"three-pmrem" | "materialx-prefiltered" | "materialx-fis"} [environmentRadianceMode]
|
|
366
423
|
* @property {boolean} [specularAntialiasing] - Match Three.js glossy specular antialiasing. Defaults to true.
|
|
367
424
|
* @property {boolean} [generateTangents] - Generate missing MikkTSpace tangents for tangent-dependent shaders. Defaults to true.
|
|
425
|
+
* @property {string} [path] - Base path used to resolve relative MaterialX texture filenames.
|
|
368
426
|
* @property {boolean} [debug] - Debug flag
|
|
369
427
|
*/
|
|
370
428
|
|
|
@@ -733,6 +791,8 @@ $2`
|
|
|
733
791
|
);
|
|
734
792
|
} // end hasShadowUniforms
|
|
735
793
|
|
|
794
|
+
vertexShader = patchInstancingTransforms(vertexShader);
|
|
795
|
+
|
|
736
796
|
const threeParameters = { ...init.parameters };
|
|
737
797
|
materialParameters = {
|
|
738
798
|
name: init.name,
|
|
@@ -754,7 +814,7 @@ $2`
|
|
|
754
814
|
return;
|
|
755
815
|
}
|
|
756
816
|
|
|
757
|
-
const
|
|
817
|
+
const path = init.path || "";
|
|
758
818
|
/** @type {Array<Promise<unknown>>} */
|
|
759
819
|
const pendingTextureLoads = [];
|
|
760
820
|
this.shaderName = init.shaderName || null;
|
|
@@ -771,8 +831,8 @@ $2`
|
|
|
771
831
|
// uniforms below must keep their original object identity so async
|
|
772
832
|
// texture resolution updates the same uniform object.
|
|
773
833
|
mergeUniforms([createThreeLightUniforms()]),
|
|
774
|
-
getUniformValues(init.shader.getStage('vertex'), init.loaders,
|
|
775
|
-
getUniformValues(init.shader.getStage('pixel'), init.loaders,
|
|
834
|
+
getUniformValues(init.shader.getStage('vertex'), init.loaders, path, pendingTextureLoads),
|
|
835
|
+
getUniformValues(init.shader.getStage('pixel'), init.loaders, path, pendingTextureLoads),
|
|
776
836
|
{
|
|
777
837
|
u_worldMatrix: { value: new Matrix4() },
|
|
778
838
|
u_worldInverseMatrix: { value: new Matrix4() },
|