NodeMaterialLoader.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import {
  2. DefaultLoadingManager,
  3. FileLoader
  4. } from '../../../build/three.module.js';
  5. import * as Nodes from '../nodes/Nodes.js';
  6. var NodeMaterialLoader = function ( manager, library ) {
  7. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  8. this.nodes = {};
  9. this.materials = {};
  10. this.passes = {};
  11. this.names = {};
  12. this.library = library || {};
  13. };
  14. var NodeMaterialLoaderUtils = {
  15. replaceUUIDObject: function ( object, uuid, value, recursive ) {
  16. recursive = recursive !== undefined ? recursive : true;
  17. if ( typeof uuid === 'object' ) uuid = uuid.uuid;
  18. if ( typeof object === 'object' ) {
  19. var keys = Object.keys( object );
  20. for ( var i = 0; i < keys.length; i ++ ) {
  21. var key = keys[ i ];
  22. if ( recursive ) {
  23. object[ key ] = this.replaceUUIDObject( object[ key ], uuid, value );
  24. }
  25. if ( key === uuid ) {
  26. object[ uuid ] = object[ key ];
  27. delete object[ key ];
  28. }
  29. }
  30. }
  31. return object === uuid ? value : object;
  32. },
  33. replaceUUID: function ( json, uuid, value ) {
  34. this.replaceUUIDObject( json, uuid, value, false );
  35. this.replaceUUIDObject( json.nodes, uuid, value );
  36. this.replaceUUIDObject( json.materials, uuid, value );
  37. this.replaceUUIDObject( json.passes, uuid, value );
  38. this.replaceUUIDObject( json.library, uuid, value, false );
  39. return json;
  40. }
  41. };
  42. Object.assign( NodeMaterialLoader.prototype, {
  43. load: function ( url, onLoad, onProgress, onError ) {
  44. var scope = this;
  45. var loader = new FileLoader( scope.manager );
  46. loader.setPath( scope.path );
  47. loader.load( url, function ( text ) {
  48. onLoad( scope.parse( JSON.parse( text ) ) );
  49. }, onProgress, onError );
  50. return this;
  51. },
  52. setPath: function ( value ) {
  53. this.path = value;
  54. return this;
  55. },
  56. getObjectByName: function ( uuid ) {
  57. return this.names[ uuid ];
  58. },
  59. getObjectById: function ( uuid ) {
  60. return this.library[ uuid ] ||
  61. this.nodes[ uuid ] ||
  62. this.materials[ uuid ] ||
  63. this.passes[ uuid ] ||
  64. this.names[ uuid ];
  65. },
  66. getNode: function ( uuid ) {
  67. var object = this.getObjectById( uuid );
  68. if ( ! object ) {
  69. console.warn( 'Node "' + uuid + '" not found.' );
  70. }
  71. return object;
  72. },
  73. resolve: function ( json ) {
  74. switch ( typeof json ) {
  75. case 'boolean':
  76. case 'number':
  77. return json;
  78. case 'string':
  79. if ( /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/i.test( json ) || this.library[ json ] ) {
  80. return this.getNode( json );
  81. }
  82. return json;
  83. default:
  84. if ( Array.isArray( json ) ) {
  85. for ( var i = 0; i < json.length; i ++ ) {
  86. json[ i ] = this.resolve( json[ i ] );
  87. }
  88. } else {
  89. for ( var prop in json ) {
  90. if ( prop === 'uuid' ) continue;
  91. json[ prop ] = this.resolve( json[ prop ] );
  92. }
  93. }
  94. }
  95. return json;
  96. },
  97. declare: function ( json ) {
  98. var uuid, node, object;
  99. for ( uuid in json.nodes ) {
  100. node = json.nodes[ uuid ];
  101. object = new Nodes[ node.nodeType + 'Node' ]();
  102. if ( node.name ) {
  103. object.name = node.name;
  104. this.names[ object.name ] = object;
  105. }
  106. this.nodes[ uuid ] = object;
  107. }
  108. for ( uuid in json.materials ) {
  109. node = json.materials[ uuid ];
  110. object = new Nodes[ node.type ]();
  111. if ( node.name ) {
  112. object.name = node.name;
  113. this.names[ object.name ] = object;
  114. }
  115. this.materials[ uuid ] = object;
  116. }
  117. for ( uuid in json.passes ) {
  118. node = json.passes[ uuid ];
  119. object = new Nodes[ node.type ]();
  120. if ( node.name ) {
  121. object.name = node.name;
  122. this.names[ object.name ] = object;
  123. }
  124. this.passes[ uuid ] = object;
  125. }
  126. if ( json.material ) this.material = this.materials[ json.material ];
  127. if ( json.pass ) this.pass = this.passes[ json.pass ];
  128. return json;
  129. },
  130. parse: function ( json ) {
  131. var uuid;
  132. json = this.resolve( this.declare( json ) );
  133. for ( uuid in json.nodes ) {
  134. this.nodes[ uuid ].copy( json.nodes[ uuid ] );
  135. }
  136. for ( uuid in json.materials ) {
  137. this.materials[ uuid ].copy( json.materials[ uuid ] );
  138. }
  139. for ( uuid in json.passes ) {
  140. this.passes[ uuid ].copy( json.passes[ uuid ] );
  141. }
  142. return this.material || this.pass || this;
  143. }
  144. } );
  145. export { NodeMaterialLoader, NodeMaterialLoaderUtils };