@needle-tools/materialx 1.0.4-next.29f5d7d → 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.29f5d7d",
3
+ "version": "1.0.4-next.8713311",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "exports": {
@@ -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 {
@@ -146,7 +148,7 @@ export class MaterialXMaterial extends ShaderMaterial {
146
148
  envMap: Texture | null = null; // Environment map texture, can be set externally
147
149
  updateUniforms = (context: Context, environment: MaterialXEnvironment, object: Object3D, camera: Camera) => {
148
150
 
149
- const uniforms = this.uniforms;
151
+ const uniforms = this.uniforms as Uniforms;
150
152
 
151
153
  // TODO remove. Not sure why this is needed, but without it
152
154
  // we currently get some "swimming" where matrices are not up to date.
@@ -155,18 +157,22 @@ export class MaterialXMaterial extends ShaderMaterial {
155
157
  // Update standard transformation matrices
156
158
  if (uniforms.u_worldMatrix) {
157
159
  uniforms.u_worldMatrix.value = object.matrixWorld;
160
+ uniforms.u_worldMatrix.needsUpdate = true;
158
161
  }
159
162
 
160
163
  if (uniforms.u_viewProjectionMatrix) {
161
164
  uniforms.u_viewProjectionMatrix.value.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
165
+ uniforms.u_viewProjectionMatrix.needsUpdate = true;
162
166
  }
163
167
 
164
168
  if (uniforms.u_viewPosition) {
165
169
  uniforms.u_viewPosition.value.copy(camera.getWorldPosition(worldViewPos));
170
+ uniforms.u_viewPosition.needsUpdate = true;
166
171
  }
167
172
 
168
173
  if (uniforms.u_worldInverseTransposeMatrix) {
169
174
  uniforms.u_worldInverseTransposeMatrix.value.setFromMatrix3(normalMat.getNormalMatrix(object.matrixWorld));
175
+ uniforms.u_worldInverseTransposeMatrix.needsUpdate = true;
170
176
  }
171
177
 
172
178
  // Update time uniforms
@@ -185,34 +191,40 @@ export class MaterialXMaterial extends ShaderMaterial {
185
191
 
186
192
  private updateEnvironmentUniforms = (environment: MaterialXEnvironment) => {
187
193
 
194
+ const uniforms = this.uniforms as Uniforms;
195
+
188
196
  // Get lighting data from environment
189
197
  const lightData = environment.lightData || null;
190
198
  const lightCount = environment.lightCount || 0;
191
199
  const textures = environment.getTextures(this) || null;
192
200
 
193
201
  // Update light count
194
- if (this.uniforms.u_numActiveLightSources && lightCount >= 0) {
195
- this.uniforms.u_numActiveLightSources.value = lightCount;
202
+ if (uniforms.u_numActiveLightSources && lightCount >= 0) {
203
+ uniforms.u_numActiveLightSources.value = lightCount;
196
204
  }
197
205
 
198
206
  // Update light data
199
207
  if (lightData?.length) {
200
- this.uniforms.u_lightData.value = lightData;
201
- 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) {
202
210
  if (debug) console.debug(`[MaterialX] LightData assigned (${this.name}, ${this.uuid})`, lightData);
203
- this.uniforms.u_lightData.needsUpdate = undefined;
211
+ uniforms.u_lightData.needsUpdate = undefined;
204
212
  }
205
213
  }
206
214
 
207
215
  // Update environment uniforms
208
- if (this.uniforms.u_envRadiance) {
209
- 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;
210
220
  }
211
- if (this.uniforms.u_envRadianceMips) {
212
- 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;
213
223
  }
214
- if (this.uniforms.u_envIrradiance) {
215
- 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;
216
228
  }
217
229
 
218
230
  this.uniformsNeedUpdate = true;