@needle-tools/materialx 1.0.3 → 1.0.4-next.29f5d7d

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.0.3",
3
+ "version": "1.0.4-next.29f5d7d",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "exports": {
@@ -14,10 +14,6 @@
14
14
  "require": "./codegen/register_types.js"
15
15
  }
16
16
  },
17
- "peerDependencies": {
18
- "@needle-tools/engine": "4.x",
19
- "three": "npm:@needle-tools/three@^0.169.5"
20
- },
21
17
  "devDependencies": {
22
18
  "@needle-tools/engine": "4.x",
23
19
  "@types/three": "0.169.0",
@@ -39,4 +35,4 @@
39
35
  "mtlx",
40
36
  "rendering"
41
37
  ]
42
- }
38
+ }
@@ -216,7 +216,7 @@ function toThreeUniform(uniforms: any, type: string, value: any, name: string, l
216
216
  const key = type + ':' + name;
217
217
  if (!valueTypeWarningMap.has(key)) {
218
218
  valueTypeWarningMap.set(key, true);
219
- console.warn('MaterialX: Unsupported uniform type: ' + type + ' for uniform: ' + name, value);
219
+ console.warn(`MaterialX: Unsupported uniform type: ${type} for uniform: ${name}`);
220
220
  }
221
221
  break;
222
222
  }
@@ -370,7 +370,7 @@ function threeLightTypeToMaterialXNodeName(threeLightType) {
370
370
  }
371
371
  };
372
372
 
373
- type LightData = {
373
+ export type LightData = {
374
374
  type: number, // Light type ID
375
375
  position: THREE.Vector3, // Position in world space
376
376
  direction: THREE.Vector3, // Direction in world space
@@ -469,7 +469,7 @@ export function getUniformValues(shaderStage: MaterialX.ShaderStage, loaders: Lo
469
469
  const value = variable.getValue()?.getData();
470
470
  const name = variable.getVariable();
471
471
  if (debug) console.log("Adding uniform", { path: variable.getPath(), name: name, value: value, type: variable.getType().getName() });
472
- threeUniforms[name] = toThreeUniform(uniforms, variable.getType().getName(), value, name, loaders, searchPath);;
472
+ threeUniforms[name] = toThreeUniform(uniforms, variable.getType().getName(), value, name, loaders, searchPath);
473
473
  }
474
474
  }
475
475
  }
@@ -1,7 +1,7 @@
1
1
  import { Camera, DoubleSide, FrontSide, GLSL3, MaterialParameters, Matrix3, Matrix4, Object3D, ShaderMaterial, Texture, Vector3 } from "three";
2
2
  import { debug } from "./utils.js";
3
3
  import { MaterialXEnvironment } from "./materialx.js";
4
- import { getUniformValues, Loaders } from "./materialx.helper.js";
4
+ import { getLightData, getUniformValues, Loaders } from "./materialx.helper.js";
5
5
  import { Context } from "@needle-tools/engine";
6
6
 
7
7
 
@@ -114,6 +114,12 @@ export class MaterialXMaterial extends ShaderMaterial {
114
114
  Object.assign(this.uniforms, {
115
115
  ...getUniformValues(init.shader.getStage('vertex'), init.loaders, searchPath),
116
116
  ...getUniformValues(init.shader.getStage('pixel'), init.loaders, searchPath),
117
+
118
+ u_worldMatrix: { value: new Matrix4() },
119
+ u_viewProjectionMatrix: { value: new Matrix4() },
120
+ u_viewPosition: { value: new Vector3() },
121
+ u_worldInverseTransposeMatrix: { value: new Matrix4() },
122
+
117
123
  u_envMatrix: { value: new Matrix4() },
118
124
  u_envRadiance: { value: null, type: 't' },
119
125
  u_envRadianceMips: { value: 8, type: 'i' },
@@ -122,7 +128,7 @@ export class MaterialXMaterial extends ShaderMaterial {
122
128
  u_envIrradiance: { value: null, type: 't' },
123
129
  u_refractionEnv: { value: true },
124
130
  u_numActiveLightSources: { value: 0 },
125
- u_lightData: { value: [] }, // Array of light data
131
+ u_lightData: { value: [], needsUpdate: false }, // Array of light data. We need to set needsUpdate to false until we actually update it
126
132
  });
127
133
 
128
134
  if (debug) {
@@ -148,22 +154,18 @@ export class MaterialXMaterial extends ShaderMaterial {
148
154
 
149
155
  // Update standard transformation matrices
150
156
  if (uniforms.u_worldMatrix) {
151
- if (!uniforms.u_worldMatrix.value?.isMatrix4) uniforms.u_worldMatrix.value = new Matrix4();
152
157
  uniforms.u_worldMatrix.value = object.matrixWorld;
153
158
  }
154
159
 
155
160
  if (uniforms.u_viewProjectionMatrix) {
156
- if (!uniforms.u_viewProjectionMatrix.value?.isMatrix4) uniforms.u_viewProjectionMatrix.value = new Matrix4();
157
161
  uniforms.u_viewProjectionMatrix.value.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
158
162
  }
159
163
 
160
164
  if (uniforms.u_viewPosition) {
161
- if (!uniforms.u_viewPosition.value?.isVector3) uniforms.u_viewPosition.value = new Vector3();
162
165
  uniforms.u_viewPosition.value.copy(camera.getWorldPosition(worldViewPos));
163
166
  }
164
167
 
165
168
  if (uniforms.u_worldInverseTransposeMatrix) {
166
- if (!uniforms.u_worldInverseTransposeMatrix.value?.isMatrix4) uniforms.u_worldInverseTransposeMatrix.value = new Matrix4();
167
169
  uniforms.u_worldInverseTransposeMatrix.value.setFromMatrix3(normalMat.getNormalMatrix(object.matrixWorld));
168
170
  }
169
171
 
@@ -194,14 +196,15 @@ export class MaterialXMaterial extends ShaderMaterial {
194
196
  }
195
197
 
196
198
  // Update light data
197
- if (lightData) {
199
+ if (lightData?.length) {
198
200
  this.uniforms.u_lightData.value = lightData;
201
+ if ("needsUpdate" in this.uniforms.u_lightData && this.uniforms.u_lightData.needsUpdate === false) {
202
+ if (debug) console.debug(`[MaterialX] LightData assigned (${this.name}, ${this.uuid})`, lightData);
203
+ this.uniforms.u_lightData.needsUpdate = undefined;
204
+ }
199
205
  }
200
206
 
201
207
  // Update environment uniforms
202
- if (this.uniforms.u_envMatrix) {
203
- this.uniforms.u_envMatrix.value = identityMatrix;
204
- }
205
208
  if (this.uniforms.u_envRadiance) {
206
209
  this.uniforms.u_envRadiance.value = textures.radianceTexture;
207
210
  }
package/src/materialx.ts CHANGED
@@ -4,7 +4,7 @@ import MaterialX from "../bin/JsMaterialXGenShader.js";
4
4
  import { debug } from "./utils.js";
5
5
  import { renderPMREMToEquirect } from "./textureHelper.js";
6
6
  import { Light, Material, MeshBasicMaterial, Object3D, PMREMGenerator, Texture } from "three";
7
- import { registerLights, getLightData } from "./materialx.helper.js";
7
+ import { registerLights, getLightData, LightData } from "./materialx.helper.js";
8
8
  import type { MaterialXMaterial } from "./materialx.material.js";
9
9
 
10
10
 
@@ -115,7 +115,7 @@ type EnvironmentTextureSet = {
115
115
  export class MaterialXEnvironment {
116
116
  private _context: Context | null = null;
117
117
  private _lights: Array<Light> = [];
118
- private _lightData: any = null;
118
+ private _lightData: null | LightData[] = null;
119
119
  private _lightCount: number = 0;
120
120
  private _initializePromise: Promise<boolean> | null = null;
121
121
 
@@ -133,7 +133,13 @@ export class MaterialXEnvironment {
133
133
  return this._initializePromise = this._initialize(context);
134
134
  }
135
135
 
136
- get lightData() { return this._lightData; }
136
+ get lightData() {
137
+ // if (this._lightData === null) {
138
+ // if(debug) console.warn("[MaterialX] Light data is not initialized, updating lighting");
139
+ // this.updateLighting(true);
140
+ // }
141
+ return this._lightData;
142
+ }
137
143
  get lightCount() { return this._lightCount || 0; }
138
144
  getTextures(material: MaterialXMaterial) {
139
145
  if (material.envMap) {
@@ -197,7 +203,7 @@ export class MaterialXEnvironment {
197
203
  this._lightCount = 0;
198
204
  this._pmremGenerator?.dispose();
199
205
  this._pmremGenerator = null;
200
- for(const textureSet of this._texturesCache.values()) {
206
+ for (const textureSet of this._texturesCache.values()) {
201
207
  textureSet.radianceTexture?.dispose();
202
208
  textureSet.irradianceTexture?.dispose();
203
209
  }