fbx2three.js 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
var fs = require( 'fs' );
var path = require( 'path' );

if ( process.argv.length <= 2 ) {

	console.log( `Usage: ${path.basename( __filename )} model.fbx` );
	process.exit( - 1 );

}

//

var PRECISION = 6;

function parseNumber( key, value ) {

	return typeof value === 'number' ? parseFloat( value.toFixed( PRECISION ) ) : value;

}

THREE = require( '../../build/three.js' );
require( '../../examples/js/curves/NURBSCurve.js' );
require( '../../examples/js/curves/NURBSUtils.js' );
require( '../../examples/js/loaders/FBXLoader.js' );
global.Zlib = require( '../../examples/js/libs/inflate.min.js' ).Zlib;

global.window = {
	innerWidth: 1024,
	innerHeight: 768,
	URL: {
L
linbingquan 已提交
31

32
		createObjectURL: function () {
L
linbingquan 已提交
33

34
			throw new Error( 'fbx2three: Images in binary format not yet supported.' );
L
linbingquan 已提交
35

36
		}
L
linbingquan 已提交
37

38 39 40 41 42 43 44 45 46
	}
};

// HTML Images are not available, so use a Buffer instead.
THREE.ImageLoader.prototype.load = function ( url, onLoad ) {

	if ( this.path !== undefined ) url = this.path + url;

	// If image isn't found, try to ignore it.
L
linbingquan 已提交
47
	if ( ! fs.existsSync( url ) ) {
48 49 50 51 52 53 54 55 56 57 58

		onLoad( new Buffer( '' ) );
		return;

	}

	onLoad( fs.readFileSync( url ) );

};

// Convert image buffer to data URL.
59
THREE.ImageUtils.getDataURL = function ( image ) {
60

L
linbingquan 已提交
61
	if ( ! ( image instanceof Buffer ) ) {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

		throw new Error( 'fbx2three: Image should be loaded as Buffer.' );

	}

	var dataURL = 'data:';
	dataURL += this.format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg';
	dataURL += ';base64,';
	dataURL += image.toString( 'base64' );
	return dataURL;

};

//

var file = process.argv[ 2 ];
var resourceDirectory = THREE.LoaderUtils.extractUrlBase( file );
var loader = new THREE.FBXLoader();

var arraybuffer = fs.readFileSync( file ).buffer;
var object = loader.parse( arraybuffer, resourceDirectory );
var content = JSON.stringify( object.toJSON(), parseNumber );
fs.writeFileSync( path.basename( file, '.fbx' ) + '.json', content, 'utf8' );