-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
104 lines (100 loc) · 3.43 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { GLTFExporter } from "three/addons/exporters/GLTFExporter.js";
import { OBJExporter } from "three/addons/exporters/OBJExporter.js";
import { OBJLoader } from "three/addons/loaders/OBJLoader.js";
import { STLExporter } from "three/addons/exporters/STLExporter.js";
import { STLLoader } from "three/addons/loaders/STLLoader.js";
import { PLYLoader } from "three/addons/loaders/PLYLoader.js";
import { PLYExporter } from "three/addons/exporters/PLYExporter.js";
import * as THREE from "three";
import { downloadFile } from "./utils/downloadFile";
const loader = async (extension,input) =>{
let loader,data;
const scene = new THREE.Scene();
switch (extension) {
case "glb":
loader = new GLTFLoader();
data = await loader.loadAsync(input);
console.log("data", data);
return data
case "gltf":
loader = new GLTFLoader();
data = await loader.loadAsync(input);
console.log("data", data);
return data;
case "stl":
loader = new STLLoader();
data = await loader.loadAsync(input);
data = new THREE.Mesh(data, new THREE.MeshStandardMaterial());
console.log("data", data);
return data;
case "obj":
loader = new OBJLoader();
data = await loader.loadAsync(input);
scene.add(data);
data = scene;
console.log("data", data);
return data;
case "ply":
loader = new PLYLoader();
data = await loader.loadAsync(input);
data = new THREE.Mesh(data, new THREE.MeshStandardMaterial());
console.log("data", data);
return data;
default:
console.log("Unsupported Input Format");
throw new Error('Unsupported Input Format')
}
}
const exporter = async (outputFormat,data) =>{
let exporter,result;
switch (outputFormat) {
case "glb":
exporter = new GLTFExporter();
result = exporter.parse(data, (result) => {
const jsonString = JSON.stringify(result);
downloadFile(jsonString, "converted-model.glb");
});
break;
case "gltf":
exporter = new GLTFExporter();
result = exporter.parse(scene);
console.log("output", result);
downloadFile(result, "converted-model.gltf");
break;
case "stl":
exporter = new STLExporter();
result = exporter.parse(data.scene || data);
console.log("output", result);
downloadFile(result, "converted-model.stl");
break;
case "obj":
exporter = new OBJExporter();
result = exporter.parse(data.scene || data);
console.log("output", result);
downloadFile(result, "converted-model.obj");
break;
case "ply":
exporter = new PLYExporter();
const scene = new THREE.Scene();
if (data.scene) {
scene.add(data.scene);
} else {
scene.add(data);
}
result = exporter.parse(scene);
console.log("output", result);
downloadFile(result, "converted-model.ply");
break;
default:
console.log("Unsupported Output Format");
}
}
const convertFromat = async (input, outputFormat) => {
const inputFormat = input.lastIndexOf(".");
const extension = input.substring(inputFormat + 1);
console.log(extension);
const data = await loader(extension,input);
await exporter(outputFormat,data);
};
convertFromat("./models/damaged-helmet.stl", "glb");