MaterialLoader.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.MaterialLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. this.textures = {};
  7. };
  8. Object.assign( THREE.MaterialLoader.prototype, {
  9. load: function ( url, onLoad, onProgress, onError ) {
  10. var scope = this;
  11. var loader = new THREE.XHRLoader( scope.manager );
  12. loader.load( url, function ( text ) {
  13. onLoad( scope.parse( JSON.parse( text ) ) );
  14. }, onProgress, onError );
  15. },
  16. setTextures: function ( value ) {
  17. this.textures = value;
  18. },
  19. getTexture: function ( name ) {
  20. var textures = this.textures;
  21. if ( textures[ name ] === undefined ) {
  22. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  23. }
  24. return textures[ name ];
  25. },
  26. parse: function ( json ) {
  27. var material = new THREE[ json.type ];
  28. if ( json.uuid !== undefined ) material.uuid = json.uuid;
  29. if ( json.name !== undefined ) material.name = json.name;
  30. if ( json.color !== undefined ) material.color.setHex( json.color );
  31. if ( json.roughness !== undefined ) material.roughness = json.roughness;
  32. if ( json.metalness !== undefined ) material.metalness = json.metalness;
  33. if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
  34. if ( json.specular !== undefined ) material.specular.setHex( json.specular );
  35. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  36. if ( json.uniforms !== undefined ) material.uniforms = json.uniforms;
  37. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  38. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  39. if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
  40. if ( json.shading !== undefined ) material.shading = json.shading;
  41. if ( json.blending !== undefined ) material.blending = json.blending;
  42. if ( json.side !== undefined ) material.side = json.side;
  43. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  44. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  45. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  46. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  47. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  48. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  49. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  50. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  51. // for PointsMaterial
  52. if ( json.size !== undefined ) material.size = json.size;
  53. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  54. // maps
  55. if ( json.map !== undefined ) material.map = this.getTexture( json.map );
  56. if ( json.alphaMap !== undefined ) {
  57. material.alphaMap = this.getTexture( json.alphaMap );
  58. material.transparent = true;
  59. }
  60. if ( json.bumpMap !== undefined ) material.bumpMap = this.getTexture( json.bumpMap );
  61. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  62. if ( json.normalMap !== undefined ) material.normalMap = this.getTexture( json.normalMap );
  63. if ( json.normalScale !== undefined ) {
  64. var normalScale = json.normalScale;
  65. if ( Array.isArray( normalScale ) === false ) {
  66. // Blender exporter used to export a scalar. See #7459
  67. normalScale = [ normalScale, normalScale ];
  68. }
  69. material.normalScale = new THREE.Vector2().fromArray( normalScale );
  70. }
  71. if ( json.displacementMap !== undefined ) material.displacementMap = this.getTexture( json.displacementMap );
  72. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  73. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  74. if ( json.roughnessMap !== undefined ) material.roughnessMap = this.getTexture( json.roughnessMap );
  75. if ( json.metalnessMap !== undefined ) material.metalnessMap = this.getTexture( json.metalnessMap );
  76. if ( json.emissiveMap !== undefined ) material.emissiveMap = this.getTexture( json.emissiveMap );
  77. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  78. if ( json.specularMap !== undefined ) material.specularMap = this.getTexture( json.specularMap );
  79. if ( json.envMap !== undefined ) {
  80. material.envMap = this.getTexture( json.envMap );
  81. material.combine = THREE.MultiplyOperation;
  82. }
  83. if ( json.reflectivity ) material.reflectivity = json.reflectivity;
  84. if ( json.lightMap !== undefined ) material.lightMap = this.getTexture( json.lightMap );
  85. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  86. if ( json.aoMap !== undefined ) material.aoMap = this.getTexture( json.aoMap );
  87. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  88. // MultiMaterial
  89. if ( json.materials !== undefined ) {
  90. for ( var i = 0, l = json.materials.length; i < l; i ++ ) {
  91. material.materials.push( this.parse( json.materials[ i ] ) );
  92. }
  93. }
  94. return material;
  95. }
  96. } );