Utilities for converting model files to the Three.js JSON format. It's necessary to install the esm npm package before you can use the converters.
Usage:
node -r esm obj2three.js model.obj
Usage:
node -r esm fbx2three.js model.fbx
import fs from 'fs'; | |
import path from 'path'; | |
import { FBXLoader } from '../../examples/jsm/loaders/FBXLoader.js'; | |
import { ImageLoader, ImageUtils, LoaderUtils } from '../../build/three.module.js'; | |
if ( process.argv.length <= 2 ) { | |
console.log( `Usage: ${path.basename( __filename )} model.fbx` ); | |
process.exit( - 1 ); | |
} | |
// | |
const PRECISION = 6; | |
function parseNumber( key, value ) { | |
return typeof value === 'number' ? parseFloat( value.toFixed( PRECISION ) ) : value; | |
} | |
global.window = { | |
innerWidth: 1024, | |
innerHeight: 768, | |
URL: { | |
createObjectURL: function () { | |
throw new Error( 'fbx2three: Images in binary format not yet supported.' ); | |
} | |
} | |
}; | |
// HTML Images are not available, so use a Buffer instead. | |
ImageLoader.prototype.load = function ( url, onLoad ) { | |
if ( this.path !== undefined ) url = this.path + url; | |
// If image isn't found, try to ignore it. | |
if ( ! fs.existsSync( url ) ) { | |
onLoad( new Buffer( '' ) ); | |
return; | |
} | |
onLoad( fs.readFileSync( url ) ); | |
}; | |
// Convert image buffer to data URL. | |
ImageUtils.getDataURL = function ( image ) { | |
if ( ! ( image instanceof Buffer ) ) { | |
throw new Error( 'fbx2three: Image should be loaded as Buffer.' ); | |
} | |
let dataURL = 'data:'; | |
dataURL += this.format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg'; | |
dataURL += ';base64,'; | |
dataURL += image.toString( 'base64' ); | |
return dataURL; | |
}; | |
// | |
const file = process.argv[ 2 ]; | |
const resourceDirectory = LoaderUtils.extractUrlBase( file ); | |
const loader = new FBXLoader(); | |
const arraybuffer = fs.readFileSync( file ).buffer; | |
const object = loader.parse( arraybuffer, resourceDirectory ); | |
const content = JSON.stringify( object.toJSON(), parseNumber ); | |
fs.writeFileSync( path.basename( file, '.fbx' ) + '.json', content, 'utf8' ); |
import fs from 'fs'; | |
import path from 'path'; | |
import { OBJLoader } from '../../examples/jsm/loaders/OBJLoader.js'; | |
if ( process.argv.length <= 2 ) { | |
console.log( "Usage: " + path.basename( __filename ) + " model.obj" ); | |
process.exit( - 1 ); | |
} | |
// | |
const PRECISION = 6; | |
function parseNumber( key, value ) { | |
return typeof value === 'number' ? parseFloat( value.toFixed( PRECISION ) ) : value; | |
} | |
const file = process.argv[ 2 ]; | |
const loader = new OBJLoader(); | |
const text = fs.readFileSync( file, 'utf8' ); | |
const content = JSON.stringify( loader.parse( text ).toJSON(), parseNumber ); | |
fs.writeFileSync( path.basename( file, '.obj' ) + '.json', content, 'utf8' ); |