2
0

fbx2three.js 1.7 KB

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