NodeMaterialLoader.js 4.2 KB

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