Add gltf binary export option

This commit is contained in:
JannisX11 2023-02-24 18:54:25 +01:00
parent e7fcf8245e
commit 390da38f2f
2 changed files with 32 additions and 20 deletions

View File

@ -376,7 +376,11 @@ Object.assign(Blockbench, {
} else { } else {
//text or binary //text or binary
fs.writeFileSync(file_path, options.content) let content = options.content;
if (content instanceof ArrayBuffer) {
content = Buffer.from(content);
}
fs.writeFileSync(file_path, content)
if (cb) { if (cb) {
cb(file_path) cb(file_path)
} }

View File

@ -167,7 +167,11 @@ function buildAnimationTracks(do_quaternions = true) {
var codec = new Codec('gltf', { var codec = new Codec('gltf', {
name: 'GLTF Model', name: 'GLTF Model',
extension: 'gltf', extension: 'gltf',
async compile(options = 0) { export_options: {
encoding: {type: 'select', label: 'Encoding', options: {ascii: 'ASCII (glTF)', binary: 'Binary (glb)'}},
animations: {label: 'codec.fbx.export_animations', type: 'checkbox', value: true}
},
async compile(options = this.getExportOptions()) {
let scope = this; let scope = this;
let exporter = new THREE.GLTFExporter(); let exporter = new THREE.GLTFExporter();
let animations = []; let animations = [];
@ -186,11 +190,12 @@ var codec = new Codec('gltf', {
if (options.animations !== false) { if (options.animations !== false) {
animations = buildAnimationTracks(); animations = buildAnimationTracks();
} }
let json = await new Promise((resolve, reject) => { let result = await new Promise((resolve, reject) => {
exporter.parse(gl_scene, resolve, { exporter.parse(gl_scene, resolve, {
animations, animations,
onlyVisible: false, onlyVisible: false,
trs: true, trs: true,
binary: options.encoding == 'binary',
truncateDrawRange: false, truncateDrawRange: false,
forcePowerOfTwoTextures: true, forcePowerOfTwoTextures: true,
scale_factor: 1/Settings.get('model_export_scale'), scale_factor: 1/Settings.get('model_export_scale'),
@ -199,29 +204,32 @@ var codec = new Codec('gltf', {
}) })
scene.add(Project.model_3d); scene.add(Project.model_3d);
scope.dispatchEvent('compile', {model: json, options}); scope.dispatchEvent('compile', {model: result, options});
return JSON.stringify(json); if (options.encoding == 'binary') {
return result;
} else {
return JSON.stringify(result);
}
} catch (err) { } catch (err) {
scene.add(Project.model_3d); scene.add(Project.model_3d);
throw err; throw err;
} }
}, },
export() { async export() {
var scope = codec; await this.promptExportOptions();
scope.compile().then(content => { let content = await this.compile();
setTimeout(_ => { await new Promise(r => setTimeout(r, 20));
console.log(content)
Blockbench.export({ Blockbench.export({
resource_id: 'gltf', resource_id: 'gltf',
type: scope.name, type: this.name,
extensions: [scope.extension], extensions: [this.getExportOptions().encoding == 'binary' ? 'glb' : 'gltf'],
name: scope.fileName(), name: this.fileName(),
startpath: scope.startPath(), startpath: this.startPath(),
content, content,
custom_writer: isApp ? (a, b) => scope.write(a, b) : null, custom_writer: isApp ? (a, b) => this.write(a, b) : null,
}, path => scope.afterDownload(path)) }, path => this.afterDownload(path));
}, 20)
})
} }
}) })