@needle-tools/engine 4.13.0-next.2d429c0 → 4.13.0-next.db5e54e

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.
@@ -137,7 +137,7 @@ export class ReflectionProbe extends Behaviour {
137
137
  // and some need reflection probe and some don't
138
138
  // we need to make sure we don't override the material but use a copy
139
139
 
140
- private static _rendererMaterialsCache: Map<IRenderer, Array<{ material: Material, copy: Material }>> = new Map();
140
+ private static _rendererMaterialsCache: Map<IRenderer, Array<{ material: Material, copy: Material, originalVersion: number }>> = new Map();
141
141
 
142
142
  onSet(_rend: IRenderer) {
143
143
  if (disable) return;
@@ -171,15 +171,16 @@ export class ReflectionProbe extends Behaviour {
171
171
  let cached = rendererCache[i];
172
172
 
173
173
  // make sure we have the currently assigned material cached (and an up to date clone of that)
174
- // TODO: this is causing problems with progressive textures sometimes (depending on the order) when the version changes and we re-create materials over and over. We might want to just set the material envmap instead of making a clone
174
+ // Compare against the stored original version, not the clone's version (which gets modified by needsUpdate)
175
+ // This prevents cloning materials every frame when onUnset restores the original material
175
176
  const isCachedInstance = material === cached?.copy;
176
- const hasChanged = !cached || cached.material.uuid !== material.uuid || cached.copy.version !== material.version;
177
+ const hasChanged = !cached || cached.material.uuid !== material.uuid || cached.originalVersion !== material.version;
177
178
  if (!isCachedInstance && hasChanged) {
178
179
  if (debug) {
179
180
  let reason = "";
180
181
  if (!cached) reason = "not cached";
181
182
  else if (cached.material !== material) reason = "reference changed; cached instance?: " + isCachedInstance;
182
- else if (cached.copy.version !== material.version) reason = "version changed";
183
+ else if (cached.originalVersion !== material.version) reason = "version changed";
183
184
  console.warn("Cloning material", material.name, material.version, "Reason:", reason, "\n", material.uuid, "\n", cached?.copy.uuid, "\n", _rend.name);
184
185
  }
185
186
 
@@ -189,11 +190,13 @@ export class ReflectionProbe extends Behaviour {
189
190
  if (cached) {
190
191
  cached.copy = clone;
191
192
  cached.material = material;
193
+ cached.originalVersion = material.version;
192
194
  }
193
195
  else {
194
196
  cached = {
195
197
  material: material,
196
- copy: clone
198
+ copy: clone,
199
+ originalVersion: material.version
197
200
  };
198
201
  rendererCache.push(cached);
199
202
  }
@@ -213,9 +216,10 @@ export class ReflectionProbe extends Behaviour {
213
216
  const copy = cached?.copy;
214
217
 
215
218
  if ("envMap" in copy) {
216
- // make sure the reflection probe is assigned
217
- copy.envMap = this.texture;
218
- copy.needsUpdate = true;
219
+ if (copy.envMap !== this.texture) { // Only update if changed
220
+ copy.envMap = this.texture;
221
+ copy.needsUpdate = true;
222
+ }
219
223
  }
220
224
 
221
225
  _rend.sharedMaterials[i] = copy;