@needle-tools/materialx 1.2.0 → 1.2.1-next.343c31f

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/materialx",
3
- "version": "1.2.0",
3
+ "version": "1.2.1-next.343c31f",
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
+ }
package/src/materialx.js CHANGED
@@ -3,6 +3,7 @@ import { debug } from "./utils.js";
3
3
  import { renderPMREMToEquirect } from "./utils.texture.js";
4
4
  import { Light, Mesh, MeshBasicMaterial, Object3D, PlaneGeometry, PMREMGenerator, Scene, Texture, WebGLRenderer } from "three";
5
5
  import { registerLights, getLightData } from "./materialx.helper.js";
6
+ import { whiteTexture } from "./utils.texture.js";
6
7
 
7
8
  export const state = new class {
8
9
  /** @type {import("./materialx.types.js").MaterialX.MODULE | null} */
@@ -113,8 +114,9 @@ export async function ready() {
113
114
  */
114
115
 
115
116
  /**
116
- * MaterialXEnvironment manages the environment settings for MaterialX materials.
117
+ * MaterialXEnvironment manages the environment settings for MaterialX materials.
117
118
  */
119
+ // @dont-generate-component
118
120
  export class MaterialXEnvironment {
119
121
 
120
122
  /**
@@ -256,7 +258,14 @@ export class MaterialXEnvironment {
256
258
  // If the material has its own envMap, we don't use the irradiance texture
257
259
  return this._getTextures(material.envMap);
258
260
  }
259
- return this._getTextures(this._scene.environment);
261
+
262
+ // Use the scene background for lighting if no environment is available
263
+ // If we don't do this we don't see the correct lighting for scenes exported with 'Environment Lighting: Color' and 'Environment Reflections: Skybox'
264
+ const skybox = this._scene.environment || this._scene.background;
265
+ if (skybox instanceof Texture) {
266
+ return this._getTextures(skybox);
267
+ }
268
+ return this._getTextures(null);
260
269
  }
261
270
 
262
271
  /** @type {PMREMGenerator | null} */
@@ -284,6 +293,12 @@ export class MaterialXEnvironment {
284
293
  * @returns {{radianceTexture: Texture | null, irradianceTexture: Texture | null}}
285
294
  */
286
295
  _getTextures(texture) {
296
+
297
+ // Fallback to white texture if no texture is provided
298
+ if (!texture) {
299
+ texture = whiteTexture;
300
+ }
301
+
287
302
  /** @type {EnvironmentTextureSet | undefined} */
288
303
  let res = this._texturesCache.get(texture || null);
289
304
  if (res) {
@@ -322,7 +337,7 @@ export class MaterialXEnvironment {
322
337
  const lights = new Array();
323
338
  this._scene.traverse((/** @type {Object3D} */ object) => {
324
339
  if ((/** @type {Light} */ (object)).isLight && object.visible)
325
- lights.push(/** @type {Light} */ (object));
340
+ lights.push(/** @type {Light} */(object));
326
341
  });
327
342
  this._lights = lights;
328
343
  }
@@ -23,6 +23,7 @@ const worldViewPos = new Vector3();
23
23
  * @typedef {"highp" | "mediump" | "lowp"} Precision
24
24
  */
25
25
 
26
+ // @dont-generate-component
26
27
  export class MaterialXMaterial extends ShaderMaterial {
27
28
 
28
29
  /** The original name of the shader
@@ -258,21 +259,25 @@ export class MaterialXMaterial extends ShaderMaterial {
258
259
  // Update standard transformation matrices
259
260
  if (uniforms.u_worldMatrix) {
260
261
  uniforms.u_worldMatrix.value = object.matrixWorld;
262
+ // @ts-ignore
261
263
  uniforms.u_worldMatrix.needsUpdate = true;
262
264
  }
263
265
 
264
266
  if (uniforms.u_viewProjectionMatrix) {
265
267
  uniforms.u_viewProjectionMatrix.value.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
268
+ // @ts-ignore
266
269
  uniforms.u_viewProjectionMatrix.needsUpdate = true;
267
270
  }
268
271
 
269
272
  if (uniforms.u_worldInverseTransposeMatrix) {
270
273
  uniforms.u_worldInverseTransposeMatrix.value.setFromMatrix3(normalMat.getNormalMatrix(object.matrixWorld));
274
+ // @ts-ignore
271
275
  uniforms.u_worldInverseTransposeMatrix.needsUpdate = true;
272
276
  }
273
277
 
274
278
  if (uniforms.u_viewPosition) {
275
279
  uniforms.u_viewPosition.value.copy(camera.getWorldPosition(worldViewPos));
280
+ // @ts-ignore
276
281
  uniforms.u_viewPosition.needsUpdate = true;
277
282
  }
278
283
 
@@ -294,10 +299,10 @@ export class MaterialXMaterial extends ShaderMaterial {
294
299
  uniforms.u_frame.value = frame;
295
300
  }
296
301
 
302
+ this.uniformsNeedUpdate = true;
303
+
297
304
  // Update light uniforms
298
305
  this.updateEnvironmentUniforms(environment);
299
-
300
- this.uniformsNeedUpdate = true;
301
306
  }
302
307
 
303
308
  /**
@@ -331,6 +336,7 @@ export class MaterialXMaterial extends ShaderMaterial {
331
336
  if (uniforms.u_envRadiance) {
332
337
  const prev = uniforms.u_envRadiance.value;
333
338
  uniforms.u_envRadiance.value = textures.radianceTexture;
339
+ // @ts-ignore
334
340
  if (prev != textures.radianceTexture) uniforms.u_envRadiance.needsUpdate = true;
335
341
  }
336
342
  if (uniforms.u_envRadianceMips) {
@@ -339,6 +345,7 @@ export class MaterialXMaterial extends ShaderMaterial {
339
345
  if (uniforms.u_envIrradiance) {
340
346
  const prev = uniforms.u_envIrradiance.value;
341
347
  uniforms.u_envIrradiance.value = textures.irradianceTexture;
348
+ // @ts-ignore
342
349
  if (prev != textures.irradianceTexture) uniforms.u_envIrradiance.needsUpdate = true;
343
350
  }
344
351
 
@@ -3,6 +3,13 @@ import { getParam } from './utils.js';
3
3
 
4
4
  const debug = getParam("debugmaterialx");
5
5
 
6
+ export const whiteTexture = new Texture();
7
+ whiteTexture.needsUpdate = true;
8
+ whiteTexture.image = new Image();
9
+ whiteTexture.image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAANQTFRFr6+vGqg52AAAAAxJREFUeJxjZGBEgQAAWAAJLpjsTQAAAABJRU5ErkJggg=="
10
+
11
+
12
+
6
13
  /**
7
14
  * Renders a PMREM environment map to an equirectangular texture with specified roughness
8
15
  * @param {WebGLRenderer} renderer - Three.js WebGL renderer