123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- /**
- * @author sunag / http://www.sunag.com.br/
- */
- THREE.NodeMaterialLoader = function ( manager, library ) {
- this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
- this.nodes = {};
- this.materials = {};
- this.passes = {};
- this.names = {};
- this.library = library || {};
- };
- THREE.NodeMaterialLoaderUtils = {
- replaceUUIDObject: function ( object, uuid, value, recursive ) {
- recursive = recursive !== undefined ? recursive : true;
- if ( typeof uuid === "object" ) uuid = uuid.uuid;
- if ( typeof object === "object" ) {
- var keys = Object.keys( object );
- for ( var i = 0; i < keys.length; i ++ ) {
- var key = keys[ i ];
- if ( recursive ) {
- object[ key ] = this.replaceUUIDObject( object[ key ], uuid, value );
- }
- if ( key === uuid ) {
- object[ uuid ] = object[ key ];
- delete object[ key ];
- }
- }
- }
- return object === uuid ? value : object;
- },
- replaceUUID: function ( json, uuid, value ) {
- this.replaceUUIDObject( json, uuid, value, false );
- this.replaceUUIDObject( json.nodes, uuid, value );
- this.replaceUUIDObject( json.materials, uuid, value );
- this.replaceUUIDObject( json.passes, uuid, value );
- this.replaceUUIDObject( json.library, uuid, value, false );
- return json;
- }
- };
- Object.assign( THREE.NodeMaterialLoader.prototype, {
- load: function ( url, onLoad, onProgress, onError ) {
- var scope = this;
- var loader = new THREE.FileLoader( scope.manager );
- loader.setPath( scope.path );
- loader.load( url, function ( text ) {
- onLoad( scope.parse( JSON.parse( text ) ) );
- }, onProgress, onError );
- return this;
- },
- setPath: function ( value ) {
- this.path = value;
- return this;
- },
- getObjectByName: function ( uuid ) {
- return this.names[ uuid ];
- },
- getObjectById: function ( uuid ) {
- return this.library[ uuid ] ||
- this.nodes[ uuid ] ||
- this.materials[ uuid ] ||
- this.passes[ uuid ] ||
- this.names[ uuid ];
- },
- getNode: function ( uuid ) {
- var object = this.getObjectById( uuid );
- if ( ! object ) {
- console.warn( "Node \"" + uuid + "\" not found." );
- }
- return object;
- },
- resolve: function( json ) {
- switch( typeof json ) {
- case "boolean":
- case "number":
- return json;
- case "string":
- if (/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/i.test(json) || this.library[ json ]) {
- return this.getNode( json );
- }
- return json;
- default:
- if ( Array.isArray( json ) ) {
- for(var i = 0; i < json.length; i++) {
- json[i] = this.resolve( json[i] );
- }
- } else {
- for ( var prop in json ) {
- if (prop === "uuid") continue;
- json[ prop ] = this.resolve( json[ prop ] );
- }
- }
- }
- return json;
- },
- declare: function( json ) {
- var uuid, node, object;
- for ( uuid in json.nodes ) {
- node = json.nodes[ uuid ];
- object = new THREE[ node.nodeType + "Node" ]();
- if ( node.name ) {
- object.name = node.name;
- this.names[ object.name ] = object;
- }
- this.nodes[ uuid ] = object;
- }
- for ( uuid in json.materials ) {
- node = json.materials[ uuid ];
- object = new THREE[ node.type ]();
- if ( node.name ) {
- object.name = node.name;
- this.names[ object.name ] = object;
- }
- this.materials[ uuid ] = object;
- }
- for ( uuid in json.passes ) {
- node = json.passes[ uuid ];
- object = new THREE[ node.type ]();
- if ( node.name ) {
- object.name = node.name;
- this.names[ object.name ] = object;
- }
- this.passes[ uuid ] = object;
- }
- if ( json.material ) this.material = this.materials[ json.material ];
- if ( json.pass ) this.pass = this.passes[ json.pass ];
- return json;
- },
- parse: function ( json ) {
- var uuid;
- json = this.resolve( this.declare( json ) );
- for ( uuid in json.nodes ) {
- this.nodes[ uuid ].copy( json.nodes[ uuid ] );
- }
- for ( uuid in json.materials ) {
- this.materials[ uuid ].copy( json.materials[ uuid ] );
- }
- for ( uuid in json.passes ) {
- this.passes[ uuid ].copy( json.passes[ uuid ] );
- }
- return this.material || this.pass || this;
- }
- } );
|