NodeMaterialLoader.js 4.0 KB

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