fbx2three.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var fs = require( 'fs' );
  2. var path = require( 'path' );
  3. if ( process.argv.length <= 2 ) {
  4. console.log( `Usage: ${path.basename( __filename )} model.fbx` );
  5. process.exit( - 1 );
  6. }
  7. //
  8. var PRECISION = 6;
  9. function parseNumber( key, value ) {
  10. return typeof value === 'number' ? parseFloat( value.toFixed( PRECISION ) ) : value;
  11. }
  12. THREE = require( '../../build/three.js' );
  13. require( '../../examples/js/curves/NURBSCurve.js' );
  14. require( '../../examples/js/curves/NURBSUtils.js' );
  15. require( '../../examples/js/loaders/FBXLoader.js' );
  16. global.Zlib = require( '../../examples/js/libs/inflate.min.js' ).Zlib;
  17. global.window = {
  18. innerWidth: 1024,
  19. innerHeight: 768,
  20. URL: {
  21. createObjectURL: function () {
  22. throw new Error( 'fbx2three: Images in binary format not yet supported.' );
  23. }
  24. }
  25. };
  26. // HTML Images are not available, so use a Buffer instead.
  27. THREE.ImageLoader.prototype.load = function ( url, onLoad ) {
  28. if ( this.path !== undefined ) url = this.path + url;
  29. // If image isn't found, try to ignore it.
  30. if ( ! fs.existsSync( url ) ) {
  31. onLoad( new Buffer( '' ) );
  32. return;
  33. }
  34. onLoad( fs.readFileSync( url ) );
  35. };
  36. // Convert image buffer to data URL.
  37. THREE.ImageUtils.getDataURL = function ( image ) {
  38. if ( ! ( image instanceof Buffer ) ) {
  39. throw new Error( 'fbx2three: Image should be loaded as Buffer.' );
  40. }
  41. var dataURL = 'data:';
  42. dataURL += this.format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg';
  43. dataURL += ';base64,';
  44. dataURL += image.toString( 'base64' );
  45. return dataURL;
  46. };
  47. //
  48. var file = process.argv[ 2 ];
  49. var resourceDirectory = THREE.LoaderUtils.extractUrlBase( file );
  50. var loader = new THREE.FBXLoader();
  51. var arraybuffer = fs.readFileSync( file ).buffer;
  52. var object = loader.parse( arraybuffer, resourceDirectory );
  53. var content = JSON.stringify( object.toJSON(), parseNumber );
  54. fs.writeFileSync( path.basename( file, '.fbx' ) + '.json', content, 'utf8' );