未验证 提交 db017959 编写于 作者: M Mr.doob 提交者: GitHub

Merge pull request #18728 from Mugen87/dev43

Utils: Move converters to modules.
Utilities for converting model files to the Three.js JSON format. Utilities for converting model files to the Three.js JSON format.
It's necessary to install the [esm](https://www.npmjs.com/package/esm) npm package before you can use the converters.
## obj2three.js ## obj2three.js
Usage: Usage:
``` ```
node obj2three.js model.obj node -r esm obj2three.js model.obj
``` ```
## fbx2three.js ## fbx2three.js
...@@ -13,5 +14,5 @@ node obj2three.js model.obj ...@@ -13,5 +14,5 @@ node obj2three.js model.obj
Usage: Usage:
``` ```
node fbx2three.js model.fbx node -r esm fbx2three.js model.fbx
``` ```
var fs = require( 'fs' ); import fs from 'fs';
var path = require( 'path' ); 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 ) { if ( process.argv.length <= 2 ) {
...@@ -10,7 +13,7 @@ if ( process.argv.length <= 2 ) { ...@@ -10,7 +13,7 @@ if ( process.argv.length <= 2 ) {
// //
var PRECISION = 6; const PRECISION = 6;
function parseNumber( key, value ) { function parseNumber( key, value ) {
...@@ -18,12 +21,6 @@ function parseNumber( key, value ) { ...@@ -18,12 +21,6 @@ function parseNumber( key, 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 = { global.window = {
innerWidth: 1024, innerWidth: 1024,
innerHeight: 768, innerHeight: 768,
...@@ -39,7 +36,7 @@ global.window = { ...@@ -39,7 +36,7 @@ global.window = {
}; };
// HTML Images are not available, so use a Buffer instead. // HTML Images are not available, so use a Buffer instead.
THREE.ImageLoader.prototype.load = function ( url, onLoad ) { ImageLoader.prototype.load = function ( url, onLoad ) {
if ( this.path !== undefined ) url = this.path + url; if ( this.path !== undefined ) url = this.path + url;
...@@ -56,7 +53,7 @@ THREE.ImageLoader.prototype.load = function ( url, onLoad ) { ...@@ -56,7 +53,7 @@ THREE.ImageLoader.prototype.load = function ( url, onLoad ) {
}; };
// Convert image buffer to data URL. // Convert image buffer to data URL.
THREE.ImageUtils.getDataURL = function ( image ) { ImageUtils.getDataURL = function ( image ) {
if ( ! ( image instanceof Buffer ) ) { if ( ! ( image instanceof Buffer ) ) {
...@@ -64,7 +61,7 @@ THREE.ImageUtils.getDataURL = function ( image ) { ...@@ -64,7 +61,7 @@ THREE.ImageUtils.getDataURL = function ( image ) {
} }
var dataURL = 'data:'; let dataURL = 'data:';
dataURL += this.format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg'; dataURL += this.format === THREE.RGBAFormat ? 'image/png' : 'image/jpeg';
dataURL += ';base64,'; dataURL += ';base64,';
dataURL += image.toString( 'base64' ); dataURL += image.toString( 'base64' );
...@@ -74,11 +71,11 @@ THREE.ImageUtils.getDataURL = function ( image ) { ...@@ -74,11 +71,11 @@ THREE.ImageUtils.getDataURL = function ( image ) {
// //
var file = process.argv[ 2 ]; const file = process.argv[ 2 ];
var resourceDirectory = THREE.LoaderUtils.extractUrlBase( file ); const resourceDirectory = LoaderUtils.extractUrlBase( file );
var loader = new THREE.FBXLoader(); const loader = new FBXLoader();
var arraybuffer = fs.readFileSync( file ).buffer; const arraybuffer = fs.readFileSync( file ).buffer;
var object = loader.parse( arraybuffer, resourceDirectory ); const object = loader.parse( arraybuffer, resourceDirectory );
var content = JSON.stringify( object.toJSON(), parseNumber ); const content = JSON.stringify( object.toJSON(), parseNumber );
fs.writeFileSync( path.basename( file, '.fbx' ) + '.json', content, 'utf8' ); fs.writeFileSync( path.basename( file, '.fbx' ) + '.json', content, 'utf8' );
var fs = require( 'fs' ); import fs from 'fs';
var path = require( 'path' ); import path from 'path';
import { OBJLoader } from '../../examples/jsm/loaders/OBJLoader.js';
if ( process.argv.length <= 2 ) { if ( process.argv.length <= 2 ) {
...@@ -10,7 +12,7 @@ if ( process.argv.length <= 2 ) { ...@@ -10,7 +12,7 @@ if ( process.argv.length <= 2 ) {
// //
var PRECISION = 6; const PRECISION = 6;
function parseNumber( key, value ) { function parseNumber( key, value ) {
...@@ -18,13 +20,10 @@ function parseNumber( key, value ) { ...@@ -18,13 +20,10 @@ function parseNumber( key, value ) {
} }
THREE = require( '../../build/three.js' ); const file = process.argv[ 2 ];
require( '../../examples/js/loaders/OBJLoader.js' ); const loader = new OBJLoader();
var file = process.argv[ 2 ];
var loader = new THREE.OBJLoader();
var text = fs.readFileSync( file, 'utf8' ); const text = fs.readFileSync( file, 'utf8' );
var content = JSON.stringify( loader.parse( text ).toJSON(), parseNumber ); const content = JSON.stringify( loader.parse( text ).toJSON(), parseNumber );
fs.writeFileSync( path.basename( file, '.obj' ) + '.json', content, 'utf8' ); fs.writeFileSync( path.basename( file, '.obj' ) + '.json', content, 'utf8' );
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册