@needle-tools/gltf-progressive 3.1.1-next.ddb979d → 3.1.1-next.fd37564

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.
@@ -0,0 +1,168 @@
1
+ // import { GLTFLoader } from "https://cdn.jsdelivr.net/npm/three@0.179.1/examples/jsm/loaders/GLTFLoader.js";
2
+
3
+ /** @ts-ignore */
4
+ import { GLTFLoader } from "https://esm.sh/three@0.168.0/examples/jsm/loaders/GLTFLoader.js";
5
+ /** @ts-ignore */
6
+ import { MeshoptDecoder } from 'https://esm.sh/three@0.168.0/examples/jsm/libs/meshopt_decoder.module.js';
7
+ /** @ts-ignore */
8
+ import { DRACOLoader } from 'https://esm.sh/three@0.168.0/examples/jsm/loaders/DRACOLoader.js';
9
+ /** @ts-ignore */
10
+ import { KTX2Loader } from 'https://esm.sh/three@0.168.0/examples/jsm/loaders/KTX2Loader.js';
11
+
12
+
13
+ let debug = false;
14
+
15
+
16
+ /**
17
+ * @typedef {import("./loader.mainthread").GLTFLoaderWorker_Message} GLTFLoaderWorker_Message
18
+ **/
19
+
20
+ self.onmessage = (msg) => {
21
+ /** @type {GLTFLoaderWorker_Message} */
22
+ const request = msg.data;
23
+
24
+ switch (request.type) {
25
+ case "init":
26
+ break;
27
+ case "load":
28
+ loadGLTF(request);
29
+ break;
30
+ default:
31
+ console.error("[Worker] Unknown message type:", request.type);
32
+ break;
33
+ }
34
+ };
35
+
36
+ self.onerror = (error) => {
37
+ console.error("[Worker] Error:", error);
38
+ };
39
+
40
+ /**
41
+ * @param {GLTFLoaderWorker_Message} data
42
+ */
43
+ function postMessage(data) {
44
+ self.postMessage(data);
45
+ }
46
+
47
+ /** @type {GLTFLoader | null} */
48
+ let loader = null;
49
+
50
+ /** @type {DRACOLoader | null} */
51
+ let dracoLoader = null;
52
+
53
+ /** @type {KTX2Loader | null } */
54
+ let ktx2Loader = null;
55
+
56
+ /**
57
+ * @param {GLTFLoaderWorker_Message & { type: "load"}} req
58
+ */
59
+ async function loadGLTF(req) {
60
+ if(debug) console.debug("[Worker] Loading GLTF from URL:", req.dracoDecoderPath);
61
+
62
+ loader ??= new GLTFLoader();
63
+
64
+ loader.setMeshoptDecoder(MeshoptDecoder);
65
+
66
+ dracoLoader ??= new DRACOLoader();
67
+ dracoLoader.setDecoderConfig({ type: 'js' });
68
+ dracoLoader.setDecoderPath(req.dracoDecoderPath);
69
+ loader.setDRACOLoader(dracoLoader);
70
+
71
+ ktx2Loader ??= new KTX2Loader();
72
+ ktx2Loader.workerConfig = req.ktx2LoaderConfig;
73
+ ktx2Loader.setTranscoderPath(req.ktx2TranscoderPath);
74
+ loader.setKTX2Loader(ktx2Loader);
75
+
76
+
77
+ loader.load(req.url, gltf => {
78
+ if(debug) console.log("Loaded", req.url, gltf);
79
+
80
+ /** @type {GLTFLoaderWorker_Message & { type: "loaded-gltf"}} */
81
+ const data = {
82
+ type: "loaded-gltf",
83
+ result: {
84
+ url: req.url,
85
+ geometries: [],
86
+ textures: [],
87
+ }
88
+ }
89
+ collectData(gltf, data);
90
+ postMessage(data);
91
+ });
92
+ }
93
+
94
+
95
+ /**
96
+ * @param {import("three/examples/jsm/loaders/GLTFLoader").GLTF} gltf
97
+ * @param {GLTFLoaderWorker_Message & { type: "loaded-gltf"}} data
98
+ **/
99
+ function collectData(gltf, data) {
100
+
101
+ const { result } = data;
102
+
103
+ for (const key of gltf.parser.associations.keys()) {
104
+ const cache = gltf.parser.associations.get(key);
105
+
106
+ if (!cache) {
107
+ if(debug) console.warn("[Worker] No cache found for key:", key);
108
+ continue;
109
+ }
110
+
111
+ if ("isTexture" in key && key.isTexture) {
112
+ const texture = /** @type {import("three").Texture} */ ( /** @type {unknown} */ (key));
113
+ const gltf_texture = gltf.parser.json.textures[cache.textures ?? -1];
114
+ result.textures.push({
115
+ texture: texture,
116
+ textureIndex: cache.textures ?? -1,
117
+ extensions: gltf_texture?.extensions ?? {},
118
+ })
119
+ }
120
+ else if ("isMesh" in key && key.isMesh) {
121
+ const mesh = /** @type {import("three").Mesh} */ ( /** @type {unknown} */ (key));
122
+ const meshIndex = cache.meshes ?? -1;
123
+ const primitiveIndex = cache.primitives ?? -1;
124
+ const gltf_mesh = gltf.parser.json.meshes[meshIndex];
125
+ result.geometries.push({
126
+ geometry: mesh.geometry,
127
+ meshIndex: meshIndex,
128
+ primitiveIndex: primitiveIndex,
129
+ extensions: gltf_mesh?.extensions ?? {},
130
+ });
131
+ }
132
+ else if ("isMaterial" in key && key.isMaterial) {
133
+ // Nothing we need to do here
134
+ }
135
+ }
136
+ }
137
+
138
+ // function traverseAndDeleteFunctions(gltf) {
139
+ // const textures = [];
140
+ // gltf.traverse((child) => {
141
+ // if (child.isMesh) {
142
+ // geometries.push(child.geometry);
143
+ // if (child.material) {
144
+ // if (child.material.map) {
145
+ // textures.push(child.material.map);
146
+ // }
147
+ // }
148
+ // }
149
+ // });
150
+ // return {
151
+ // geometries: geometries,
152
+ // textures: textures,
153
+ // }
154
+ // }
155
+
156
+ // function traverseAndDeleteFunctions(obj, seen = new WeakSet()) {
157
+ // if (seen.has(obj)) return;
158
+ // seen.add(obj);
159
+
160
+ // for (const key in obj) {
161
+ // if (typeof obj[key] === "function") {
162
+ // delete obj[key];
163
+ // } else if (typeof obj[key] === "object" && obj[key] !== null) {
164
+ // traverseAndDeleteFunctions(obj[key], seen);
165
+ // }
166
+ // }
167
+ // return obj;
168
+ // }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/gltf-progressive",
3
- "version": "3.1.1-next.ddb979d",
3
+ "version": "3.1.1-next.fd37564",
4
4
  "description": "three.js support for loading glTF or GLB files that contain progressive loading data",
5
5
  "homepage": "https://needle.tools",
6
6
  "author": {
@@ -69,7 +69,7 @@
69
69
  "nodemon": "^3.1.4",
70
70
  "npm-watch": "^0.13.0",
71
71
  "three": ">= 0.160.0",
72
- "vite": "<= 4.3.9"
72
+ "vite": "7"
73
73
  },
74
74
  "types": "./lib/index.d.ts"
75
75
  }