MaterialLoader.js 5.8 KB

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