From 52657c084f02ac830da3a65e7c5432c3dc2b142e Mon Sep 17 00:00:00 2001 From: JannisX11 Date: Fri, 25 Dec 2020 21:58:29 +0100 Subject: [PATCH] Export gltf models in block instead of pixel units --- js/io/formats/gltf.js | 3 ++- js/io/formats/obj.js | 7 ++++++- lib/GLTFExporter.js | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/js/io/formats/gltf.js b/js/io/formats/gltf.js index 30bf0dca..c4c169a2 100644 --- a/js/io/formats/gltf.js +++ b/js/io/formats/gltf.js @@ -162,7 +162,8 @@ var codec = new Codec('gltf', { trs: true, truncateDrawRange: false, forcePowerOfTwoTextures: true, - exportFaceColors: false + scale_factor: 1/16, + exportFaceColors: false, }); }, export() { diff --git a/js/io/formats/obj.js b/js/io/formats/obj.js index bba7eacc..bbd247dd 100644 --- a/js/io/formats/obj.js +++ b/js/io/formats/obj.js @@ -21,7 +21,12 @@ var codec = new Codec('obj', { if (!options) options = 0; var old_scene_position = new THREE.Vector3().copy(scene.position); - scene.position.set(0,0,0) + scene.position.set(0,0,0); + + /** + Based on: three.js obj exporter, MIT license + https://github.com/mrdoob/three.js/blob/dev/examples/js/exporters/OBJExporter.js + */ var output = '# Made in Blockbench '+appVersion+'\n'; var materials = {}; diff --git a/lib/GLTFExporter.js b/lib/GLTFExporter.js index 48966a3c..37ea6669 100644 --- a/lib/GLTFExporter.js +++ b/lib/GLTFExporter.js @@ -1305,6 +1305,17 @@ GLTFExporter.prototype = { } }) } + if ( options.scale_factor && attributeName === 'POSITION' ) { + + // Blockbench: Scaled export + modifiedAttribute = new BufferAttribute( new Float32Array( array ), attribute.itemSize, attribute.normalized ); + + modifiedAttribute.array.forEach((v, i) => { + + modifiedAttribute.array[i] *= options.scale_factor; + + }) + } var accessor = processAccessor( modifiedAttribute || attribute, geometry ); if ( accessor !== null ) { @@ -1833,6 +1844,9 @@ GLTFExporter.prototype = { if ( ! equalArray( position, [ 0, 0, 0 ] ) ) { + if (options.scale_factor) { + position = position.map(v => v * options.scale_factor); + } gltfNode.translation = position; }