Browse Source

fbx2three: Create Node.js fbx2three converter.

Don McCurdy 7 years ago
parent
commit
5350a3cb6c
4 changed files with 132 additions and 44 deletions
  1. 2 2
      examples/js/loaders/FBXLoader.js
  2. 42 42
      src/textures/Texture.js
  3. 8 0
      utils/converters/README.md
  4. 80 0
      utils/converters/fbx2three.js

+ 2 - 2
examples/js/loaders/FBXLoader.js

@@ -51,7 +51,7 @@
 
 				} catch ( error ) {
 
-					window.setTimeout( function () {
+					setTimeout( function () {
 
 						if ( onError ) onError( error );
 
@@ -3448,7 +3448,7 @@
 
 					}
 
-					if ( window.Zlib === undefined ) {
+					if ( typeof Zlib === 'undefined' ) {
 
 						console.error( 'THREE.FBXLoader: External library Inflate.min.js required, obtain or import from https://github.com/imaya/zlib.js' );
 

+ 42 - 42
src/textures/Texture.js

@@ -130,46 +130,6 @@ Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
 		}
 
-		function getDataURL( image ) {
-
-			var canvas;
-
-			if ( image instanceof HTMLCanvasElement ) {
-
-				canvas = image;
-
-			} else {
-
-				canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
-				canvas.width = image.width;
-				canvas.height = image.height;
-
-				var context = canvas.getContext( '2d' );
-
-				if ( image instanceof ImageData ) {
-
-					context.putImageData( image, 0, 0 );
-
-				} else {
-
-					context.drawImage( image, 0, 0, image.width, image.height );
-
-				}
-
-			}
-
-			if ( canvas.width > 2048 || canvas.height > 2048 ) {
-
-				return canvas.toDataURL( 'image/jpeg', 0.6 );
-
-			} else {
-
-				return canvas.toDataURL( 'image/png' );
-
-			}
-
-		}
-
 		var output = {
 
 			metadata: {
@@ -223,7 +183,7 @@ Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
 					for ( var i = 0, l = image.length; i < l; i ++ ) {
 
-						url.push( getDataURL( image[ i ] ) );
+						url.push( this._getDataURL( image ) );
 
 					}
 
@@ -231,7 +191,7 @@ Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
 					// process single image
 
-					url = getDataURL( image );
+					url = this._getDataURL( image );
 
 				}
 
@@ -256,6 +216,46 @@ Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
 
 	},
 
+	_getDataURL: function ( image ) {
+
+		var canvas;
+
+		if ( image instanceof HTMLCanvasElement ) {
+
+			canvas = image;
+
+		} else {
+
+			canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
+			canvas.width = image.width;
+			canvas.height = image.height;
+
+			var context = canvas.getContext( '2d' );
+
+			if ( image instanceof ImageData ) {
+
+				context.putImageData( image, 0, 0 );
+
+			} else {
+
+				context.drawImage( image, 0, 0, image.width, image.height );
+
+			}
+
+		}
+
+		if ( canvas.width > 2048 || canvas.height > 2048 ) {
+
+			return canvas.toDataURL( 'image/jpeg', 0.6 );
+
+		} else {
+
+			return canvas.toDataURL( 'image/png' );
+
+		}
+
+	},
+
 	dispose: function () {
 
 		this.dispatchEvent( { type: 'dispose' } );

+ 8 - 0
utils/converters/README.md

@@ -7,3 +7,11 @@ Usage:
 ```
 node obj2three.js model.obj
 ```
+
+## fbx2three.js
+
+Usage:
+
+```
+node fbx2three.js model.fbx
+```

+ 80 - 0
utils/converters/fbx2three.js

@@ -0,0 +1,80 @@
+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: {
+		createObjectURL: function () {
+			throw new Error( 'fbx2three: Images in binary format not yet supported.' );
+		}
+	}
+};
+
+// 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.
+	if ( !fs.existsSync( url ) ) {
+
+		onLoad( new Buffer( '' ) );
+		return;
+
+	}
+
+	onLoad( fs.readFileSync( url ) );
+
+};
+
+// Convert image buffer to data URL.
+THREE.Texture.prototype._getDataURL = function ( image ) {
+
+	if ( !( image instanceof Buffer ) ) {
+
+		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' );