Needle Engine

Changes between version 3.5.9-alpha.2 and 3.5.9-beta
Files changed (1) hide show
  1. src/engine/extensions/NEEDLE_progressive.ts +43 -24
src/engine/extensions/NEEDLE_progressive.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Material, Texture, TextureLoader } from "three";
1
+ import { Material, RawShaderMaterial, Texture, TextureLoader } from "three";
2
2
  import { GLTF, GLTFLoader, GLTFLoaderPlugin, GLTFParser } from "three/examples/jsm/loaders/GLTFLoader";
3
3
  import { SourceIdentifier } from "../engine_types";
4
4
  import { Context } from "../engine_setup";
@@ -36,42 +36,55 @@
36
36
  });
37
37
  }
38
38
 
39
-
40
39
  export class NEEDLE_progressive implements GLTFLoaderPlugin {
41
40
 
42
41
  static assignTextureLOD(context: Context, source: SourceIdentifier | undefined, material: Material, level: number = 0) {
43
42
  if (!material) return;
43
+
44
44
  for (let slot of Object.keys(material)) {
45
45
  const val = material[slot];
46
- if (val?.isTexture === true) {
46
+ if (val?.isTexture)
47
+ this.assignTextureLODForSlot(context, source, material, level, slot, val);
48
+ }
47
49
 
48
- if (debug) console.log("-----------\n", "FIND", material.name, slot, val?.name, val?.userData, val, material);
50
+ if (material instanceof RawShaderMaterial) {
51
+ // iterate uniforms
52
+ for (let slot of Object.keys(material.uniforms)) {
53
+ const val = material.uniforms[slot].value;
54
+ if (val?.isTexture)
55
+ this.assignTextureLODForSlot(context, source, material, level, slot, val);
56
+ }
57
+ }
58
+ }
49
59
 
50
- NEEDLE_progressive.getOrLoadTexture(context, source, material, slot, val, level).then(t => {
51
- if (t?.isTexture === true) {
60
+ private static assignTextureLODForSlot(context: Context, source: SourceIdentifier | undefined, material: Material, level: number, slot: string, val: any) {
61
+ if (val?.isTexture !== true) return;
52
62
 
53
- if (debug) console.log("Assign LOD", material.name, slot, t.name, t["guid"], material, "Prev:", val, "Now:", t, "\n--------------");
63
+ if (debug) console.log("-----------\n", "FIND", material.name, slot, val?.name, val?.userData, val, material);
54
64
 
55
- material[slot] = t;
56
- t.needsUpdate = true;
57
- material.needsUpdate = true;
65
+ NEEDLE_progressive.getOrLoadTexture(context, source, material, slot, val, level).then(t => {
66
+ if (t?.isTexture === true) {
58
67
 
59
- if (debug) {
60
- let debug_map = debug_toggle_maps.get(material);
61
- if (!debug_map) {
62
- debug_map = {};
63
- debug_toggle_maps.set(material, debug_map);
64
- }
65
- let entry = debug_map[slot];
66
- if (!entry) {
67
- entry = debug_map[slot] = { original: val, lod0: t };
68
- }
69
- entry.lod0 = t;
70
- }
68
+ if (debug) console.log("Assign LOD", material.name, slot, t.name, t["guid"], material, "Prev:", val, "Now:", t, "\n--------------");
69
+
70
+ material[slot] = t;
71
+ t.needsUpdate = true;
72
+ material.needsUpdate = true;
73
+
74
+ if (debug) {
75
+ let debug_map = debug_toggle_maps.get(material);
76
+ if (!debug_map) {
77
+ debug_map = {};
78
+ debug_toggle_maps.set(material, debug_map);
71
79
  }
72
- });
80
+ let entry = debug_map[slot];
81
+ if (!entry) {
82
+ entry = debug_map[slot] = { original: val, lod0: t };
83
+ }
84
+ entry.lod0 = t;
85
+ }
73
86
  }
74
- }
87
+ });
75
88
  }
76
89
 
77
90
  get name(): string {
@@ -176,6 +189,12 @@
176
189
  if (debug) console.log("Loading finished " + uri, material.name, slot, ext.guid);
177
190
  let index = -1;
178
191
  let found = false;
192
+
193
+ if (!gltf.parser.json?.textures) {
194
+ if (debug) console.warn("No textures in glTF " + uri + " - may be a bug", material.name, slot, ext.guid);
195
+ return resolve(null);
196
+ }
197
+
179
198
  for (const tex of gltf.parser.json.textures) {
180
199
  index++;
181
200
  if (tex?.extensions) {