@@ -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
|
46
|
+
if (val?.isTexture)
|
47
|
+
this.assignTextureLODForSlot(context, source, material, level, slot, val);
|
48
|
+
}
|
47
49
|
|
48
|
-
|
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
|
-
|
51
|
-
|
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
|
-
|
63
|
+
if (debug) console.log("-----------\n", "FIND", material.name, slot, val?.name, val?.userData, val, material);
|
54
64
|
|
55
|
-
|
56
|
-
|
57
|
-
material.needsUpdate = true;
|
65
|
+
NEEDLE_progressive.getOrLoadTexture(context, source, material, slot, val, level).then(t => {
|
66
|
+
if (t?.isTexture === true) {
|
58
67
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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) {
|