Needle Engine

Changes between version 3.11.1-beta and 3.11.2-beta
Files changed (3) hide show
  1. src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +21 -7
  2. src/engine/engine_patcher.ts +2 -1
  3. src/engine/extensions/NEEDLE_progressive.ts +2 -2
src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  import { RegisteredAnimationInfo, UsdzAnimation } from "../Animation.js";
7
7
  import { getWorldPosition, getWorldQuaternion, getWorldScale, setWorldPosition, setWorldQuaternion, setWorldScale } from "../../../../../engine/engine_three_utils.js";
8
8
 
9
- import { Object3D, Material, Vector3, Quaternion } from "three";
9
+ import { Object3D, Material, Vector3, Quaternion, Mesh, Group } from "three";
10
10
  import { USDDocument, USDObject } from "../../ThreeUSDZExporter.js";
11
11
 
12
12
  import { BehaviorExtension, UsdzBehaviour } from "./Behaviour.js";
@@ -154,14 +154,27 @@
154
154
  @serializable()
155
155
  fadeDuration : number = 0;
156
156
 
157
- private _objectsWithThisMaterial: Renderer[] = [];
157
+ private _objectsWithThisMaterial: Mesh[] = [];
158
158
 
159
159
  awake() {
160
160
  if (this.variantMaterial && this.materialToSwitch) {
161
161
  const renderer = GameObject.findObjectsOfType(Renderer);
162
162
  for (const rend of renderer) {
163
- if (rend.sharedMaterial === this.materialToSwitch) {
164
- this._objectsWithThisMaterial.push(rend);
163
+ for (let i = 0; i < rend.sharedMaterials.length; i++) {
164
+ const mat = rend.sharedMaterials[i];
165
+ if (mat === this.materialToSwitch) {
166
+ if (rend.gameObject instanceof Mesh) {
167
+ this._objectsWithThisMaterial.push(rend.gameObject);
168
+ }
169
+ else if (rend.gameObject instanceof Group) {
170
+ for (const child of rend.gameObject.children) {
171
+ if (child instanceof Mesh && child.material === mat) {
172
+ this._objectsWithThisMaterial.push(child);
173
+ }
174
+ }
175
+ }
176
+ break;
177
+ }
165
178
  }
166
179
  }
167
180
  }
@@ -169,8 +182,9 @@
169
182
 
170
183
  onPointerClick() {
171
184
  if (!this.variantMaterial) return;
172
- for (const rend of this._objectsWithThisMaterial) {
173
- rend.sharedMaterial = this.variantMaterial;
185
+ for (let i = 0; i < this._objectsWithThisMaterial.length; i++) {
186
+ const obj = this._objectsWithThisMaterial[i];
187
+ obj.material = this.variantMaterial;
174
188
  }
175
189
  }
176
190
 
@@ -196,7 +210,7 @@
196
210
 
197
211
  createBehaviours(_ext: BehaviorExtension, model: USDObject, _context) {
198
212
 
199
- const shouldExport = this._objectsWithThisMaterial.find(o => o.gameObject.uuid === model.uuid);
213
+ const shouldExport = this._objectsWithThisMaterial.find(o => o.uuid === model.uuid);
200
214
  if (shouldExport) {
201
215
  this.targetModels.push(model);
202
216
  }
src/engine/engine_patcher.ts CHANGED
@@ -58,7 +58,8 @@
58
58
 
59
59
  // TODO: we probably want to turn this into a symbol to prevent anyone from overriding it
60
60
  // But when we need to store the symbol per prototype to allow e.g. material disposing to iterate those and dispose all
61
- const backingField = Symbol(fieldName + "__needle");// Symbol(fieldName);// + " (patched)";
61
+ // TODO: making symbols here breaks e.g. progressive textures because they iterate on the property keys
62
+ const backingField = fieldName + "___needle";// Symbol(fieldName);// + " (patched)";
62
63
 
63
64
  internalAddPatch(prototype, fieldName, beforeCallback, afterCallback);
64
65
 
src/engine/extensions/NEEDLE_progressive.ts CHANGED
@@ -45,7 +45,7 @@
45
45
 
46
46
  for (const slot of Object.keys(material)) {
47
47
  const val = material[slot];
48
- if (val?.isTexture) {
48
+ if (val instanceof Texture) {
49
49
  const task = this.assignTextureLODForSlot(context, source, material, level, slot, val);
50
50
  promises.push(task);
51
51
  }
@@ -55,7 +55,7 @@
55
55
  // iterate uniforms
56
56
  for (const slot of Object.keys(material.uniforms)) {
57
57
  const val = material.uniforms[slot].value;
58
- if (val?.isTexture) {
58
+ if (val instanceof Texture) {
59
59
  const task = this.assignTextureLODForSlot(context, source, material, level, slot, val);
60
60
  promises.push(task);
61
61
  }