Skip to content

Instantly share code, notes, and snippets.

@mrdoob
Created December 23, 2020 10:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdoob/44c1473ecb6cd636deeaa93a1bb7475e to your computer and use it in GitHub Desktop.
Save mrdoob/44c1473ecb6cd636deeaa93a1bb7475e to your computer and use it in GitHub Desktop.
Three.js Converters (Node.js)

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.

obj2three.js

Usage:

node -r esm obj2three.js model.obj

fbx2three.js

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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment