| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 | 
							- import {
 
- 	Loader,
 
- 	FileLoader
 
- } from '../../../build/three.module.js';
 
- import * as Nodes from '../nodes/Nodes.js';
 
- class NodeMaterialLoader extends Loader {
 
- 	constructor( manager, library = {} ) {
 
- 		super( manager );
 
- 		this.nodes = {};
 
- 		this.materials = {};
 
- 		this.passes = {};
 
- 		this.names = {};
 
- 		this.library = library;
 
- 	}
 
- 	load( url, onLoad, onProgress, onError ) {
 
- 		const scope = this;
 
- 		const loader = new FileLoader( scope.manager );
 
- 		loader.setPath( scope.path );
 
- 		loader.load( url, function ( text ) {
 
- 			onLoad( scope.parse( JSON.parse( text ) ) );
 
- 		}, onProgress, onError );
 
- 		return this;
 
- 	}
 
- 	getObjectByName( uuid ) {
 
- 		return this.names[ uuid ];
 
- 	}
 
- 	getObjectById( uuid ) {
 
- 		return this.library[ uuid ] ||
 
- 			this.nodes[ uuid ] ||
 
- 			this.materials[ uuid ] ||
 
- 			this.passes[ uuid ] ||
 
- 			this.names[ uuid ];
 
- 	}
 
- 	getNode( uuid ) {
 
- 		const object = this.getObjectById( uuid );
 
- 		if ( ! object ) {
 
- 			console.warn( 'Node "' + uuid + '" not found.' );
 
- 		}
 
- 		return object;
 
- 	}
 
- 	resolve( 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 ( let i = 0; i < json.length; i ++ ) {
 
- 						json[ i ] = this.resolve( json[ i ] );
 
- 					}
 
- 				} else {
 
- 					for ( const prop in json ) {
 
- 						if ( prop === 'uuid' ) continue;
 
- 						json[ prop ] = this.resolve( json[ prop ] );
 
- 					}
 
- 				}
 
- 		}
 
- 		return json;
 
- 	}
 
- 	declare( json ) {
 
- 		let uuid, node, object;
 
- 		for ( uuid in json.nodes ) {
 
- 			node = json.nodes[ uuid ];
 
- 			object = new Nodes[ 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 Nodes[ 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 Nodes[ 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( json ) {
 
- 		let 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;
 
- 	}
 
- }
 
- class NodeMaterialLoaderUtils {
 
- 	static replaceUUIDObject( object, uuid, value, recursive ) {
 
- 		recursive = recursive !== undefined ? recursive : true;
 
- 		if ( typeof uuid === 'object' ) uuid = uuid.uuid;
 
- 		if ( typeof object === 'object' ) {
 
- 			const keys = Object.keys( object );
 
- 			for ( let i = 0; i < keys.length; i ++ ) {
 
- 				const 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;
 
- 	}
 
- 	static replaceUUID( 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;
 
- 	}
 
- }
 
- export { NodeMaterialLoader, NodeMaterialLoaderUtils };
 
 
  |