@needle-tools/materialx 1.3.3 → 1.3.4
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/README.md +4 -2
- package/package.json +1 -1
- package/src/loader/loader.three.js +1 -1
- package/src/materialx.helper.js +4 -3
- package/src/materialx.js +1 -1
- package/src/materialx.material.js +13 -14
- package/src/utils.js +6 -0
package/README.md
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
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
4
|
|
|
5
|
-
> [!TIP]
|
|
6
5
|
> 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).
|
|
6
|
+
> [Learn more in the Needle Engine documentation](https://engine.needle.tools/docs/how-to-guides/export/materialx.html).
|
|
7
|
+
|
|
8
|
+
[](https://engine.needle.tools/samples/material-x)
|
|
9
|
+
|
|
8
10
|
|
|
9
11
|
## Installation
|
|
10
12
|
`npm i @needle-tools/materialx`
|
package/package.json
CHANGED
|
@@ -92,7 +92,7 @@ export class MaterialXLoader {
|
|
|
92
92
|
|
|
93
93
|
/** @type {MaterialDefinition} */
|
|
94
94
|
const materialDef = this.parser.json.materials?.[materialIndex];
|
|
95
|
-
if (debug) console.
|
|
95
|
+
if (debug) console.debug("[MaterialX] extension found in material:", materialDef.extensions?.[this.name]);
|
|
96
96
|
|
|
97
97
|
// Handle different types of MaterialX data
|
|
98
98
|
/** @type {MaterialX_material_extension} */
|
package/src/materialx.helper.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
//
|
|
5
5
|
import * as THREE from 'three';
|
|
6
6
|
import { debug, debugUpdate } from './utils.js';
|
|
7
|
+
import { isDevEnvironment } from './utils.js';
|
|
7
8
|
|
|
8
9
|
const IMAGE_PROPERTY_SEPARATOR = "_";
|
|
9
10
|
const UADDRESS_MODE_SUFFIX = IMAGE_PROPERTY_SEPARATOR + "uaddressmode";
|
|
@@ -223,7 +224,7 @@ function toThreeUniform(uniforms, type, value, name, loaders, searchPath) {
|
|
|
223
224
|
const key = type + ':' + name;
|
|
224
225
|
if (!valueTypeWarningMap.has(key)) {
|
|
225
226
|
valueTypeWarningMap.set(key, true);
|
|
226
|
-
console.warn(`MaterialX: Unsupported uniform
|
|
227
|
+
if (debug || isDevEnvironment()) console.warn(`MaterialX: Unsupported uniform '${name}': ${type}`);
|
|
227
228
|
}
|
|
228
229
|
break;
|
|
229
230
|
}
|
|
@@ -425,11 +426,11 @@ export function getLightData(lights, genContext) {
|
|
|
425
426
|
|
|
426
427
|
const lightDefinitionName = threeLightTypeToMaterialXNodeName(light.type);
|
|
427
428
|
|
|
428
|
-
if(!lightDefinitionName){
|
|
429
|
+
if (!lightDefinitionName) {
|
|
429
430
|
continue; // Unsupported light type
|
|
430
431
|
}
|
|
431
432
|
if (!lightTypesBound[lightDefinitionName]) {
|
|
432
|
-
if(debug) console.error("MaterialX: Light type not registered in context. Make sure to register light types before using them.", lightDefinitionName);
|
|
433
|
+
if (debug) console.error("MaterialX: Light type not registered in context. Make sure to register light types before using them.", lightDefinitionName);
|
|
433
434
|
}
|
|
434
435
|
|
|
435
436
|
const wp = light.getWorldPosition(new THREE.Vector3());
|
package/src/materialx.js
CHANGED
|
@@ -214,9 +214,9 @@ export class MaterialXEnvironment {
|
|
|
214
214
|
}
|
|
215
215
|
this._lastUpdateFrame = frame;
|
|
216
216
|
this.updateLighting(false);
|
|
217
|
-
const textures = this._getTextures(scene.environment);
|
|
218
217
|
|
|
219
218
|
if (debug && !this["_debug"]) {
|
|
219
|
+
const textures = this._getTextures(scene.environment);
|
|
220
220
|
this["_debug"] = true;
|
|
221
221
|
// Show both of them on cubes in the scene
|
|
222
222
|
const unlitMat = new MeshBasicMaterial();
|
|
@@ -6,7 +6,6 @@ import { cloneUniforms, cloneUniformsGroups } from "three/src/renderers/shaders/
|
|
|
6
6
|
|
|
7
7
|
// Add helper matrices for uniform updates (similar to MaterialX example)
|
|
8
8
|
const normalMat = new Matrix3();
|
|
9
|
-
const worldViewPos = new Vector3();
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* @typedef {Object} MaterialXMaterialInitParameters
|
|
@@ -241,8 +240,9 @@ export class MaterialXMaterial extends ShaderMaterial {
|
|
|
241
240
|
const env = MaterialXEnvironment.get(_scene);
|
|
242
241
|
if (env) {
|
|
243
242
|
env.update(frame, _scene, renderer);
|
|
244
|
-
this.
|
|
243
|
+
this.updateEnvironmentUniforms(env);
|
|
245
244
|
}
|
|
245
|
+
this.updateUniforms(renderer, object, camera, time, frame);
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
/** @type {number} */
|
|
@@ -251,24 +251,32 @@ export class MaterialXMaterial extends ShaderMaterial {
|
|
|
251
251
|
envMap = null; // Environment map texture, can be set externally
|
|
252
252
|
|
|
253
253
|
/**
|
|
254
|
-
* @param {MaterialXEnvironment} environment
|
|
255
254
|
* @param {WebGLRenderer} _renderer
|
|
256
255
|
* @param {Object3D} object
|
|
257
256
|
* @param {Camera} camera
|
|
258
257
|
* @param {number} [time]
|
|
259
258
|
* @param {number} [frame]
|
|
260
259
|
*/
|
|
261
|
-
updateUniforms = (
|
|
260
|
+
updateUniforms = (_renderer, object, camera, time, frame) => {
|
|
261
|
+
|
|
262
|
+
// window.showBalloonMessage(`Updating MaterialX uniforms for ${object.name} at frame ${frame}`);
|
|
262
263
|
|
|
263
264
|
const uniforms = this.uniforms;
|
|
264
265
|
|
|
265
266
|
// Update standard transformation matrices
|
|
266
267
|
if (uniforms.u_worldMatrix) {
|
|
267
|
-
uniforms.u_worldMatrix.value
|
|
268
|
+
uniforms.u_worldMatrix.value.copy(object.matrixWorld);
|
|
268
269
|
// @ts-ignore
|
|
269
270
|
uniforms.u_worldMatrix.needsUpdate = true;
|
|
270
271
|
}
|
|
271
272
|
|
|
273
|
+
// Update view position
|
|
274
|
+
if (uniforms.u_viewPosition) {
|
|
275
|
+
uniforms.u_viewPosition.value.setFromMatrixPosition(camera.matrixWorld);
|
|
276
|
+
// @ts-ignore
|
|
277
|
+
uniforms.u_viewPosition.needsUpdate = true;
|
|
278
|
+
}
|
|
279
|
+
|
|
272
280
|
if (uniforms.u_viewProjectionMatrix) {
|
|
273
281
|
uniforms.u_viewProjectionMatrix.value.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
|
|
274
282
|
// @ts-ignore
|
|
@@ -281,12 +289,6 @@ export class MaterialXMaterial extends ShaderMaterial {
|
|
|
281
289
|
uniforms.u_worldInverseTransposeMatrix.needsUpdate = true;
|
|
282
290
|
}
|
|
283
291
|
|
|
284
|
-
if (uniforms.u_viewPosition) {
|
|
285
|
-
uniforms.u_viewPosition.value.copy(camera.getWorldPosition(worldViewPos));
|
|
286
|
-
// @ts-ignore
|
|
287
|
-
uniforms.u_viewPosition.needsUpdate = true;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
292
|
// if (uniforms.u_shadowMap) {
|
|
291
293
|
// const light = environment.lights?.[2] || null;
|
|
292
294
|
// uniforms.u_shadowMatrix.value = light?.shadow?.matrix.clone().premultiply(object.matrixWorld.clone()).invert();
|
|
@@ -306,9 +308,6 @@ export class MaterialXMaterial extends ShaderMaterial {
|
|
|
306
308
|
}
|
|
307
309
|
|
|
308
310
|
this.uniformsNeedUpdate = true;
|
|
309
|
-
|
|
310
|
-
// Update light uniforms
|
|
311
|
-
this.updateEnvironmentUniforms(environment);
|
|
312
311
|
}
|
|
313
312
|
|
|
314
313
|
/**
|
package/src/utils.js
CHANGED
|
@@ -56,4 +56,10 @@ export async function waitForNetworkIdle() {
|
|
|
56
56
|
console.debug("[MaterialX] Can not wait for network idle, using fallback");
|
|
57
57
|
return new Promise(res => setTimeout(res, 100)); // Fallback to a short delay
|
|
58
58
|
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
export function isDevEnvironment() {
|
|
63
|
+
// check if we're in localhost or using an ip address
|
|
64
|
+
return window.location.hostname === "localhost" || /^\d{1,3}(\.\d{1,3}){3}$/.test(window.location.hostname);
|
|
59
65
|
}
|