NodeMaterialLoader.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.load( url, function ( text ) {
  45. onLoad( scope.parse( JSON.parse( text ) ) );
  46. }, onProgress, onError );
  47. return this;
  48. },
  49. getObjectByName: function ( uuid ) {
  50. return this.names[ uuid ];
  51. },
  52. getObjectById: function ( uuid ) {
  53. return this.library[ uuid ] ||
  54. this.nodes[ uuid ] ||
  55. this.materials[ uuid ] ||
  56. this.passes[ uuid ] ||
  57. this.names[ uuid ];
  58. },
  59. getNode: function ( uuid ) {
  60. var object = this.getObjectById( uuid );
  61. if ( ! object ) {
  62. console.warn( "Node \"" + uuid + "\" not found." );
  63. }
  64. return object;
  65. },
  66. resolve: function( json ) {
  67. switch( typeof json ) {
  68. case "boolean":
  69. case "number":
  70. return json;
  71. case "string":
  72. if (/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/i.test(json) || this.library[ json ]) {
  73. return this.getNode( json );
  74. }
  75. return json;
  76. default:
  77. if ( Array.isArray( json ) ) {
  78. for(var i = 0; i < json.length; i++) {
  79. json[i] = this.resolve( json[i] );
  80. }
  81. } else {
  82. for ( var prop in json ) {
  83. if (prop === "uuid") continue;
  84. json[ prop ] = this.resolve( json[ prop ] );
  85. }
  86. }
  87. }
  88. return json;
  89. },
  90. declare: function( json ) {
  91. var uuid, node, object;
  92. for ( uuid in json.nodes ) {
  93. node = json.nodes[ uuid ];
  94. object = new THREE[ node.nodeType + "Node" ]();
  95. if ( node.name ) {
  96. object.name = node.name;
  97. this.names[ object.name ] = object;
  98. }
  99. this.nodes[ uuid ] = object;
  100. }
  101. for ( uuid in json.materials ) {
  102. node = json.materials[ uuid ];
  103. object = new THREE[ node.type ]();
  104. if ( node.name ) {
  105. object.name = node.name;
  106. this.names[ object.name ] = object;
  107. }
  108. this.materials[ uuid ] = object;
  109. }
  110. for ( uuid in json.passes ) {
  111. node = json.passes[ uuid ];
  112. object = new THREE[ node.type ]();
  113. if ( node.name ) {
  114. object.name = node.name;
  115. this.names[ object.name ] = object;
  116. }
  117. this.passes[ uuid ] = object;
  118. }
  119. if ( json.material ) this.material = this.materials[ json.material ];
  120. if ( json.pass ) this.pass = this.passes[ json.pass ];
  121. return json;
  122. },
  123. parse: function ( json ) {
  124. var uuid;
  125. json = this.resolve( this.declare( json ) );
  126. for ( uuid in json.nodes ) {
  127. this.nodes[ uuid ].copy( json.nodes[ uuid ] );
  128. }
  129. for ( uuid in json.materials ) {
  130. this.materials[ uuid ].copy( json.materials[ uuid ] );
  131. }
  132. for ( uuid in json.passes ) {
  133. this.passes[ uuid ].copy( json.passes[ uuid ] );
  134. }
  135. return this.material || this.pass || this;
  136. }
  137. } );