|
| 1 | +import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter"; |
| 2 | +import { optionsGltfExporterType } from "../types/types"; |
| 3 | + |
| 4 | +export default class ExportGltf { |
| 5 | + private gltfExporter: GLTFExporter = new GLTFExporter(); |
| 6 | + private options: optionsGltfExporterType = { |
| 7 | + trs: false, |
| 8 | + onlyVisible: true, |
| 9 | + truncateDrawRange: true, |
| 10 | + binary: false, |
| 11 | + maxTextureSize: 40960000000, |
| 12 | + animations: [], |
| 13 | + }; |
| 14 | + private link: HTMLAnchorElement; |
| 15 | + |
| 16 | + constructor(opts: optionsGltfExporterType) { |
| 17 | + Object.assign(this.options, opts); |
| 18 | + this.link = document.createElement("a"); |
| 19 | + this.link.style.display = "none"; |
| 20 | + document.body.appendChild(this.link); |
| 21 | + } |
| 22 | + |
| 23 | + export(input: any) { |
| 24 | + const name: string = input.name ? input.name : "export"; |
| 25 | + this.gltfExporter.parse( |
| 26 | + input, |
| 27 | + (result) => { |
| 28 | + if (result instanceof ArrayBuffer) { |
| 29 | + this.saveArrayBuffer(result, name + ".glb"); |
| 30 | + } else { |
| 31 | + const output = JSON.stringify(result, null, 2); |
| 32 | + this.saveString(output, name + ".gltf"); |
| 33 | + } |
| 34 | + }, |
| 35 | + function (error) { |
| 36 | + console.log("An error happened during parsing", error); |
| 37 | + }, |
| 38 | + this.options |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + save(blob: Blob, fileName: string) { |
| 43 | + this.link.href = URL.createObjectURL(blob); |
| 44 | + this.link.download = fileName; |
| 45 | + this.link.click(); |
| 46 | + } |
| 47 | + saveString(text: string, fileName: string) { |
| 48 | + this.save(new Blob([text], { type: "text/plain" }), fileName); |
| 49 | + } |
| 50 | + saveArrayBuffer(buffer: ArrayBuffer, fileName: string) { |
| 51 | + this.save( |
| 52 | + new Blob([buffer], { type: "application/octet-stream" }), |
| 53 | + fileName |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
0 commit comments