-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy paththreejs.ts
More file actions
119 lines (102 loc) · 3.4 KB
/
Copy paththreejs.ts
File metadata and controls
119 lines (102 loc) · 3.4 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import CommonFormats, { Category } from "src/CommonFormats.ts";
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import * as THREE from "three";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { OBJLoader } from "three/addons/loaders/OBJLoader.js";
import type { GLTF } from "three/addons/loaders/GLTFLoader.js";
class threejsHandler implements FormatHandler {
public name: string = "threejs";
public supportedFormats = [
{
name: "GL Transmission Format Binary",
format: "glb",
extension: "glb",
mime: "model/gltf-binary",
from: true,
to: false,
internal: "glb",
category: Category.MODEL,
lossless: false
},
{
name: "GL Transmission Format",
format: "gltf",
extension: "gltf",
mime: "model/gltf+json",
from: true,
to: false,
internal: "glb",
category: Category.MODEL,
lossless: false
},
{
name: "Wavefront OBJ",
format: "obj",
extension: "obj",
mime: "model/obj",
from: true,
to: false,
internal: "obj",
category: Category.MODEL,
lossless: false,
},
CommonFormats.PNG.supported("png", false, true),
CommonFormats.JPEG.supported("jpeg", false, true),
CommonFormats.WEBP.supported("webp", false, true)
];
public ready: boolean = false;
private scene = new THREE.Scene();
private camera = new THREE.PerspectiveCamera(90, 16 / 9, 0.1, 4096);
private renderer = new THREE.WebGLRenderer();
async init () {
this.renderer.setSize(960, 540);
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
for (const inputFile of inputFiles) {
const blob = new Blob([inputFile.bytes as BlobPart]);
const url = URL.createObjectURL(blob);
let object: THREE.Group<THREE.Object3DEventMap>;
switch (inputFormat.internal) {
case "glb": {
const gltf: GLTF = await new Promise((resolve, reject) => {
const loader = new GLTFLoader();
loader.load(url, resolve, undefined, reject);
});
object = gltf.scene;
break;
}
case "obj":
object = await new Promise((resolve, reject) => {
const loader = new OBJLoader();
loader.load(url, resolve, undefined, reject);
});
break;
default:
throw new TypeError(`Unsupported input format: ${inputFormat.internal}`);
}
const bbox = new THREE.Box3().setFromObject(object);
bbox.getCenter(this.camera.position);
this.camera.position.z = bbox.max.z * 2;
this.scene.background = new THREE.Color(0x424242);
this.scene.add(object);
this.renderer.render(this.scene, this.camera);
this.scene.remove(object);
const bytes: Uint8Array = await new Promise((resolve, reject) => {
this.renderer.domElement.toBlob((blob) => {
if (!blob) return reject("Canvas output failed");
blob.arrayBuffer().then(buf => resolve(new Uint8Array(buf)));
}, outputFormat.mime);
});
const name = inputFile.name.split(".").slice(0, -1).join(".") + "." + outputFormat.extension;
outputFiles.push({ bytes, name });
}
return outputFiles;
}
}
export default threejsHandler;