NodeLoader.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import * as Nodes from '../Nodes.js';
  2. import { FileLoader, Loader } from 'three';
  3. class NodeLoader extends Loader {
  4. constructor( manager ) {
  5. super( manager );
  6. this.textures = {};
  7. }
  8. load( url, onLoad, onProgress, onError ) {
  9. const loader = new FileLoader( this.manager );
  10. loader.setPath( this.path );
  11. loader.setRequestHeader( this.requestHeader );
  12. loader.setWithCredentials( this.withCredentials );
  13. loader.load( url, ( text ) => {
  14. try {
  15. onLoad( this.parse( JSON.parse( text ) ) );
  16. } catch ( e ) {
  17. if ( onError ) {
  18. onError( e );
  19. } else {
  20. console.error( e );
  21. }
  22. this.manager.itemError( url );
  23. }
  24. }, onProgress, onError );
  25. }
  26. parseNodes( json ) {
  27. const nodes = {};
  28. if ( json !== undefined ) {
  29. for ( const nodeJSON of json ) {
  30. const { uuid, type } = nodeJSON;
  31. nodes[ uuid ] = Nodes.fromType( type );
  32. nodes[ uuid ].uuid = uuid;
  33. }
  34. const meta = { nodes, textures: this.textures };
  35. for ( const nodeJSON of json ) {
  36. nodeJSON.meta = meta;
  37. const node = nodes[ nodeJSON.uuid ];
  38. node.deserialize( nodeJSON );
  39. delete nodeJSON.meta;
  40. }
  41. }
  42. return nodes;
  43. }
  44. parse( json ) {
  45. const node = Nodes.fromType( json.type );
  46. node.uuid = json.uuid;
  47. const nodes = this.parseNodes( json.inputNodes );
  48. const meta = { nodes, textures: this.textures };
  49. json.meta = meta;
  50. node.deserialize( json );
  51. delete json.meta;
  52. return node;
  53. }
  54. setTextures( value ) {
  55. this.textures = value;
  56. return this;
  57. }
  58. }
  59. export default NodeLoader;