Skip to content

Commit 66490d9

Browse files
committed
index.js: fix ArrayBuffer for handle_special_geometry
index.js: change return signature of merge_geometries + adapt handle_special_geometry + fix handling of multiple materials per group in handle_special_geometry + fix line 35 + simplify handle_special_geometry
1 parent 0b0e82a commit 66490d9

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

dist/main.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.min.js.THIRD_PARTY_LICENSES.json

+9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
"license": "MIT",
3636
"licenseText": "The MIT License\n\nCopyright © 2010-2024 three.js authors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n"
3737
},
38+
{
39+
"name": "three",
40+
"version": "0.160.1",
41+
"author": "mrdoob",
42+
"repository": "https://github.com/mrdoob/three.js",
43+
"source": "https://registry.npmjs.org/three/-/three-0.160.1.tgz",
44+
"license": "MIT",
45+
"licenseText": "The MIT License\n\nCopyright © 2010-2023 three.js authors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n"
46+
},
3847
{
3948
"name": "wtd-core",
4049
"version": "3.0.0",

src/index.js

+27-18
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ extensionCodec.register({
9898
return to_return
9999
},
100100
});
101+
101102
// Float32Array
102103
extensionCodec.register({
103104
type: 0x17,
@@ -136,20 +137,32 @@ function merge_geometries(object, preserve_materials = false) {
136137
}
137138
collectGeometries(object, root_transform);
138139
let result = null;
140+
let material;
139141
if (geometries.length == 1) {
140142
result = geometries[0];
141143
if (preserve_materials) {
142-
result.material = materials[0];
144+
material = materials[0];
143145
}
144146
} else if (geometries.length > 1) {
145147
result = mergeGeometries(geometries, true);
148+
const ngeom = result.groups.length;
149+
let mts = [];
146150
if (preserve_materials) {
147-
result.material = materials;
151+
for (let i = 0; i < ngeom; i++) {
152+
const group = result.groups[i];
153+
let m = materials[i];
154+
if (Array.isArray(m)) {
155+
mts.push(m[m.length - 1]);
156+
} else {
157+
mts.push(m);
158+
}
159+
}
160+
material = mts;
148161
}
149162
} else {
150163
result = new THREE.BufferGeometry();
151164
}
152-
return result;
165+
return [result, material];
153166
}
154167

155168
// Handler for special texture types that we want to support
@@ -194,30 +207,28 @@ function handle_special_geometry(geom) {
194207
console.warn("_meshfile is deprecated. Please use _meshfile_geometry for geometries and _meshfile_object for objects with geometry and material");
195208
geom.type = "_meshfile_geometry";
196209
}
210+
let geometry = null;
211+
let m = null;
197212
if (geom.type == "_meshfile_geometry") {
198213
if (geom.format == "obj") {
199214
let loader = new OBJLoader2();
200215
let obj = loader.parse(geom.data + "\n");
201-
let loaded_geom = merge_geometries(obj);
202-
loaded_geom.uuid = geom.uuid;
203-
return loaded_geom;
216+
[geometry, m] = merge_geometries(obj);
217+
geometry.uuid = geom.uuid;
204218
} else if (geom.format == "dae") {
205219
let loader = new ColladaLoader();
206220
let obj = loader.parse(geom.data);
207-
let result = merge_geometries(obj.scene);
208-
result.uuid = geom.uuid;
209-
return result;
221+
[geometry, m] = merge_geometries(obj.scene);
222+
geometry.uuid = geom.uuid;
210223
} else if (geom.format == "stl") {
211224
let loader = new STLLoader();
212-
let loaded_geom = loader.parse(geom.data.buffer);
213-
loaded_geom.uuid = geom.uuid;
214-
return loaded_geom;
225+
geometry = loader.parse(geom.data.buffer);
226+
geometry.uuid = geom.uuid;
215227
} else {
216228
console.error("Unsupported mesh type:", geom);
217-
return null;
218229
}
219230
}
220-
return null;
231+
return geometry;
221232
}
222233

223234
// The ExtensibleObjectLoader extends the THREE.ObjectLoader
@@ -285,16 +296,14 @@ class ExtensibleObjectLoader extends THREE.ObjectLoader {
285296
this.onTextureLoad();
286297
}
287298
let obj = loader.parse(json.data + "\n", path);
288-
geometry = merge_geometries(obj, true);
299+
[geometry, material] = merge_geometries(obj, true);
289300
geometry.uuid = json.uuid;
290-
material = geometry.material;
291301
} else if (json.format == "dae") {
292302
let loader = new ColladaLoader(manager);
293303
loader.onTextureLoad = this.onTextureLoad;
294304
let obj = loader.parse(json.data, path);
295-
geometry = merge_geometries(obj.scene, true);
305+
[geometry, material] = merge_geometries(obj.scene, true);
296306
geometry.uuid = json.uuid;
297-
material = geometry.material;
298307
} else if (json.format == "stl") {
299308
let loader = new STLLoader();
300309
geometry = loader.parse(json.data.buffer, path);

0 commit comments

Comments
 (0)