@needle-tools/materialx 1.7.0 → 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 +17 -4
- package/README.md +20 -0
- 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 +78 -5
- package/src/materialx.material.d.ts +1 -0
- package/src/materialx.material.js +60 -3
package/CHANGELOG.md
CHANGED
|
@@ -4,11 +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
|
+
|
|
13
|
+
## [1.7.1] - 2026-07-01
|
|
14
|
+
|
|
15
|
+
### Fixed
|
|
16
|
+
- Normalized MaterialX texture request paths when combining loader search paths, `fileprefix`, and relative filename values, avoiding duplicate slashes in hosted asset URLs.
|
|
17
|
+
- Preserved absolute texture URLs and filename substitution tokens while resolving relative MaterialX texture paths.
|
|
18
|
+
- Improved texture path handling for included MaterialX libraries, file prefixes, relative paths, and texture-load diagnostics.
|
|
19
|
+
|
|
7
20
|
## [1.7.0] - 2026-06-30
|
|
8
21
|
|
|
9
22
|
### Added
|
|
10
23
|
- Runtime environment radiance modes for fast Three.js PMREM sampling, MaterialX prefiltered latlong sampling, and MaterialX filtered importance sampling.
|
|
11
|
-
-
|
|
24
|
+
- Browser examples for material library inspection, PMREM comparison, shaderball previews, configurable environments, and graph-driven material loading.
|
|
12
25
|
- Missing tangents are now automatically generated for MaterialX materials that require them. Set `generateTangents: false` to disable this.
|
|
13
26
|
|
|
14
27
|
### Changed
|
|
@@ -20,8 +33,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
|
20
33
|
- Added the JSON module import attribute for `package.json` so raw browser ESM imports can read the package version without MIME errors.
|
|
21
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.
|
|
22
35
|
- Switched bundled MaterialX Emscripten loaders to `node:module` builtin imports for Deno compatibility, and added runtime smoke scripts for Node, Deno, and Bun.
|
|
23
|
-
- Corrected material parameter editing for color values, environment controls, and graph-loaded materials in
|
|
24
|
-
-
|
|
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.
|
|
25
38
|
|
|
26
39
|
## [1.6.0] – 2026-04-01
|
|
27
40
|
|
|
@@ -70,7 +83,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
|
70
83
|
- Fix: Improved error log formatting with package version
|
|
71
84
|
|
|
72
85
|
## [1.4.4] – 2026-03-17
|
|
73
|
-
- Fix: Minor type fixes and improved
|
|
86
|
+
- Fix: Minor type fixes and improved logging
|
|
74
87
|
|
|
75
88
|
## [1.4.3] – 2026-02-20
|
|
76
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
|
|
@@ -136,6 +137,25 @@ globalThis.NEEDLE_MATERIALX_LOCATION = "/assets/materialx/";
|
|
|
136
137
|
|
|
137
138
|
IMPORTANT: The value must end with a trailing slash (`/`).
|
|
138
139
|
|
|
140
|
+
### Texture Path Resolution
|
|
141
|
+
|
|
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
|
+
```
|
|
158
|
+
|
|
139
159
|
|
|
140
160
|
## glTF Extension: NEEDLE_materials_mtlx
|
|
141
161
|
|
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.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,
|
package/src/materialx.helper.js
CHANGED
|
@@ -122,6 +122,79 @@ function addToCache(key, value) {
|
|
|
122
122
|
if (debug) console.log('[MaterialX] Added to cache:', key, value);
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
/**
|
|
126
|
+
* @param {string} value
|
|
127
|
+
* @returns {boolean}
|
|
128
|
+
*/
|
|
129
|
+
function isAbsoluteTexturePath(value) {
|
|
130
|
+
return /^[a-z][a-z0-9+.-]*:/i.test(value) || value.startsWith("//") || value.startsWith("/");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @param {string} value
|
|
135
|
+
* @returns {boolean}
|
|
136
|
+
*/
|
|
137
|
+
function isAbsoluteUrl(value) {
|
|
138
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(value);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @param {string} value
|
|
143
|
+
* @returns {string}
|
|
144
|
+
*/
|
|
145
|
+
function withTrailingSlash(value) {
|
|
146
|
+
return value.endsWith(IMAGE_PATH_SEPARATOR) ? value : `${value}${IMAGE_PATH_SEPARATOR}`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @param {string} value
|
|
151
|
+
* @returns {string}
|
|
152
|
+
*/
|
|
153
|
+
function normalizeRelativeTexturePath(value) {
|
|
154
|
+
const suffixStart = value.search(/[?#]/);
|
|
155
|
+
const path = suffixStart >= 0 ? value.slice(0, suffixStart) : value;
|
|
156
|
+
const suffix = suffixStart >= 0 ? value.slice(suffixStart) : "";
|
|
157
|
+
const hasLeadingSlash = path.startsWith(IMAGE_PATH_SEPARATOR);
|
|
158
|
+
const parts = [];
|
|
159
|
+
|
|
160
|
+
for (const part of path.split(IMAGE_PATH_SEPARATOR)) {
|
|
161
|
+
if (!part || part === ".") continue;
|
|
162
|
+
if (part === "..") {
|
|
163
|
+
const previous = parts[parts.length - 1];
|
|
164
|
+
if (previous && previous !== "..") parts.pop();
|
|
165
|
+
else if (!hasLeadingSlash) parts.push(part);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
parts.push(part);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const normalizedPath = `${hasLeadingSlash ? IMAGE_PATH_SEPARATOR : ""}${parts.join(IMAGE_PATH_SEPARATOR)}`;
|
|
172
|
+
return `${normalizedPath}${suffix}`;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {string} searchPath
|
|
177
|
+
* @param {string} value
|
|
178
|
+
* @returns {string}
|
|
179
|
+
*/
|
|
180
|
+
function resolveTexturePath(searchPath, value) {
|
|
181
|
+
if (isAbsoluteTexturePath(value)) return value;
|
|
182
|
+
if (!searchPath) return value;
|
|
183
|
+
|
|
184
|
+
const normalizedSearchPath = searchPath.replace(/\/+$/, "");
|
|
185
|
+
if (!normalizedSearchPath) return value;
|
|
186
|
+
|
|
187
|
+
if (isAbsoluteUrl(normalizedSearchPath)) {
|
|
188
|
+
return new URL(value, withTrailingSlash(normalizedSearchPath)).href;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (THREE.LoaderUtils?.resolveURL) {
|
|
192
|
+
return normalizeRelativeTexturePath(THREE.LoaderUtils.resolveURL(value, withTrailingSlash(normalizedSearchPath)));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return normalizeRelativeTexturePath(`${normalizedSearchPath}${IMAGE_PATH_SEPARATOR}${value}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
125
198
|
/**
|
|
126
199
|
* Get Three uniform from MaterialX value
|
|
127
200
|
* @param {any} uniforms
|
|
@@ -165,19 +238,19 @@ function toThreeUniform(threeUniforms, uniforms, type, value, name, loaders, sea
|
|
|
165
238
|
// Cache / reuse texture to avoid reload overhead.
|
|
166
239
|
// Note: that data blobs and embedded data textures are not cached as they are transient data.
|
|
167
240
|
let checkCache = true;
|
|
168
|
-
let texturePath =
|
|
241
|
+
let texturePath = value;
|
|
169
242
|
if (value.startsWith('blob:')) {
|
|
170
|
-
texturePath = value;
|
|
171
243
|
checkCache = false;
|
|
172
244
|
}
|
|
173
245
|
else if (value.startsWith('data:')) {
|
|
174
|
-
texturePath = value;
|
|
175
246
|
checkCache = false;
|
|
176
247
|
}
|
|
177
248
|
else if (value.startsWith('http')) {
|
|
178
|
-
texturePath = value;
|
|
179
249
|
checkCache = true;
|
|
180
250
|
}
|
|
251
|
+
else {
|
|
252
|
+
texturePath = resolveTexturePath(searchPath, value);
|
|
253
|
+
}
|
|
181
254
|
|
|
182
255
|
const cacheKey = loaders.cacheKey?.length ? `${loaders.cacheKey}-${texturePath}` : texturePath;
|
|
183
256
|
const cacheValue = checkCache ? tryGetFromCache(cacheKey) : null;
|
|
@@ -247,7 +320,7 @@ function toThreeUniform(threeUniforms, uniforms, type, value, name, loaders, sea
|
|
|
247
320
|
case 'displacementshader':
|
|
248
321
|
case 'volumeshader':
|
|
249
322
|
case 'lightshader':
|
|
250
|
-
// MaterialX closure/shader types — not
|
|
323
|
+
// MaterialX closure/shader types — not emitted uniforms, skip silently
|
|
251
324
|
break;
|
|
252
325
|
default:
|
|
253
326
|
const key = type + ':' + name;
|
|
@@ -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
|
|
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,
|
|
775
|
-
getUniformValues(init.shader.getStage('pixel'), init.loaders,
|
|
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() },
|