@needle-tools/materialx 1.0.4-next.6620f9d → 1.0.4-next.8713311

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.4-next.6620f9d",
3
+ "version": "1.0.4-next.8713311",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "exports": {
@@ -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
  }
@@ -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,4 +1,4 @@
1
- import { Camera, DoubleSide, FrontSide, GLSL3, MaterialParameters, Matrix3, Matrix4, Object3D, ShaderMaterial, Texture, Vector3 } from "three";
1
+ import { Camera, DoubleSide, FrontSide, GLSL3, IUniform, MaterialParameters, Matrix3, Matrix4, Object3D, ShaderMaterial, Texture, Vector3 } from "three";
2
2
  import { debug } from "./utils.js";
3
3
  import { MaterialXEnvironment } from "./materialx.js";
4
4
  import { getLightData, getUniformValues, Loaders } from "./materialx.helper.js";
@@ -19,6 +19,8 @@ declare type MaterialXMaterialInitParameters = {
19
19
  side?: MaterialParameters['side'],
20
20
  }
21
21
 
22
+ type Uniforms = Record<string, IUniform & { needsUpdate?: boolean }>;
23
+
22
24
  export class MaterialXMaterial extends ShaderMaterial {
23
25
 
24
26
  // copy(source: MaterialXMaterial): this {
@@ -114,6 +116,12 @@ export class MaterialXMaterial extends ShaderMaterial {
114
116
  Object.assign(this.uniforms, {
115
117
  ...getUniformValues(init.shader.getStage('vertex'), init.loaders, searchPath),
116
118
  ...getUniformValues(init.shader.getStage('pixel'), init.loaders, searchPath),
119
+
120
+ u_worldMatrix: { value: new Matrix4() },
121
+ u_viewProjectionMatrix: { value: new Matrix4() },
122
+ u_viewPosition: { value: new Vector3() },
123
+ u_worldInverseTransposeMatrix: { value: new Matrix4() },
124
+
117
125
  u_envMatrix: { value: new Matrix4() },
118
126
  u_envRadiance: { value: null, type: 't' },
119
127
  u_envRadianceMips: { value: 8, type: 'i' },
@@ -122,7 +130,7 @@ export class MaterialXMaterial extends ShaderMaterial {
122
130
  u_envIrradiance: { value: null, type: 't' },
123
131
  u_refractionEnv: { value: true },
124
132
  u_numActiveLightSources: { value: 0 },
125
- u_lightData: { value: [], needsUpdate: false }, // Array of light data
133
+ u_lightData: { value: [], needsUpdate: false }, // Array of light data. We need to set needsUpdate to false until we actually update it
126
134
  });
127
135
 
128
136
  if (debug) {
@@ -140,7 +148,7 @@ export class MaterialXMaterial extends ShaderMaterial {
140
148
  envMap: Texture | null = null; // Environment map texture, can be set externally
141
149
  updateUniforms = (context: Context, environment: MaterialXEnvironment, object: Object3D, camera: Camera) => {
142
150
 
143
- const uniforms = this.uniforms;
151
+ const uniforms = this.uniforms as Uniforms;
144
152
 
145
153
  // TODO remove. Not sure why this is needed, but without it
146
154
  // we currently get some "swimming" where matrices are not up to date.
@@ -148,23 +156,23 @@ export class MaterialXMaterial extends ShaderMaterial {
148
156
 
149
157
  // Update standard transformation matrices
150
158
  if (uniforms.u_worldMatrix) {
151
- if (!uniforms.u_worldMatrix.value?.isMatrix4) uniforms.u_worldMatrix.value = new Matrix4();
152
159
  uniforms.u_worldMatrix.value = object.matrixWorld;
160
+ uniforms.u_worldMatrix.needsUpdate = true;
153
161
  }
154
162
 
155
163
  if (uniforms.u_viewProjectionMatrix) {
156
- if (!uniforms.u_viewProjectionMatrix.value?.isMatrix4) uniforms.u_viewProjectionMatrix.value = new Matrix4();
157
164
  uniforms.u_viewProjectionMatrix.value.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
165
+ uniforms.u_viewProjectionMatrix.needsUpdate = true;
158
166
  }
159
167
 
160
168
  if (uniforms.u_viewPosition) {
161
- if (!uniforms.u_viewPosition.value?.isVector3) uniforms.u_viewPosition.value = new Vector3();
162
169
  uniforms.u_viewPosition.value.copy(camera.getWorldPosition(worldViewPos));
170
+ uniforms.u_viewPosition.needsUpdate = true;
163
171
  }
164
172
 
165
173
  if (uniforms.u_worldInverseTransposeMatrix) {
166
- if (!uniforms.u_worldInverseTransposeMatrix.value?.isMatrix4) uniforms.u_worldInverseTransposeMatrix.value = new Matrix4();
167
174
  uniforms.u_worldInverseTransposeMatrix.value.setFromMatrix3(normalMat.getNormalMatrix(object.matrixWorld));
175
+ uniforms.u_worldInverseTransposeMatrix.needsUpdate = true;
168
176
  }
169
177
 
170
178
  // Update time uniforms
@@ -183,37 +191,40 @@ export class MaterialXMaterial extends ShaderMaterial {
183
191
 
184
192
  private updateEnvironmentUniforms = (environment: MaterialXEnvironment) => {
185
193
 
194
+ const uniforms = this.uniforms as Uniforms;
195
+
186
196
  // Get lighting data from environment
187
197
  const lightData = environment.lightData || null;
188
198
  const lightCount = environment.lightCount || 0;
189
199
  const textures = environment.getTextures(this) || null;
190
200
 
191
201
  // Update light count
192
- if (this.uniforms.u_numActiveLightSources && lightCount >= 0) {
193
- this.uniforms.u_numActiveLightSources.value = lightCount;
202
+ if (uniforms.u_numActiveLightSources && lightCount >= 0) {
203
+ uniforms.u_numActiveLightSources.value = lightCount;
194
204
  }
195
205
 
196
206
  // Update light data
197
207
  if (lightData?.length) {
198
- this.uniforms.u_lightData.value = lightData;
199
- if ("needsUpdate" in this.uniforms.u_lightData && this.uniforms.u_lightData.needsUpdate === false) {
208
+ uniforms.u_lightData.value = lightData;
209
+ if ("needsUpdate" in uniforms.u_lightData && uniforms.u_lightData.needsUpdate === false) {
200
210
  if (debug) console.debug(`[MaterialX] LightData assigned (${this.name}, ${this.uuid})`, lightData);
201
- this.uniforms.u_lightData.needsUpdate = undefined;
211
+ uniforms.u_lightData.needsUpdate = undefined;
202
212
  }
203
213
  }
204
214
 
205
215
  // Update environment uniforms
206
- if (this.uniforms.u_envMatrix) {
207
- this.uniforms.u_envMatrix.value = identityMatrix;
208
- }
209
- if (this.uniforms.u_envRadiance) {
210
- this.uniforms.u_envRadiance.value = textures.radianceTexture;
216
+ if (uniforms.u_envRadiance) {
217
+ const prev = uniforms.u_envRadiance.value;
218
+ uniforms.u_envRadiance.value = textures.radianceTexture;
219
+ if (prev != textures.radianceTexture) uniforms.u_envRadiance.needsUpdate = true;
211
220
  }
212
- if (this.uniforms.u_envRadianceMips) {
213
- this.uniforms.u_envRadianceMips.value = Math.trunc(Math.log2(Math.max(textures.radianceTexture?.source.data.width ?? 0, textures.radianceTexture?.source.data.height ?? 0))) + 1;
221
+ if (uniforms.u_envRadianceMips) {
222
+ uniforms.u_envRadianceMips.value = Math.trunc(Math.log2(Math.max(textures.radianceTexture?.source.data.width ?? 0, textures.radianceTexture?.source.data.height ?? 0))) + 1;
214
223
  }
215
- if (this.uniforms.u_envIrradiance) {
216
- this.uniforms.u_envIrradiance.value = textures.irradianceTexture;
224
+ if (uniforms.u_envIrradiance) {
225
+ const prev = uniforms.u_envIrradiance.value;
226
+ uniforms.u_envIrradiance.value = textures.irradianceTexture;
227
+ if (prev != textures.irradianceTexture) uniforms.u_envIrradiance.needsUpdate = true;
217
228
  }
218
229
 
219
230
  this.uniformsNeedUpdate = true;