MaterialLoader.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { Vector2 } from '../math/Vector2.js';
  2. import { FileLoader } from './FileLoader.js';
  3. import { DefaultLoadingManager } from './LoadingManager.js';
  4. import * as Materials from '../materials/Materials.js';
  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.flatShading !== undefined ) material.flatShading = json.flatShading;
  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.rotation !== undefined ) material.rotation = json.rotation;
  61. if ( json.linewidth !== 1 ) material.linewidth = json.linewidth;
  62. if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
  63. if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
  64. if ( json.scale !== undefined ) material.scale = json.scale;
  65. if ( json.skinning !== undefined ) material.skinning = json.skinning;
  66. if ( json.morphTargets !== undefined ) material.morphTargets = json.morphTargets;
  67. if ( json.dithering !== undefined ) material.dithering = json.dithering;
  68. if ( json.visible !== undefined ) material.visible = json.visible;
  69. if ( json.userData !== undefined ) material.userData = json.userData;
  70. // Deprecated
  71. if ( json.shading !== undefined ) material.flatShading = json.shading === 1; // THREE.FlatShading
  72. // for PointsMaterial
  73. if ( json.size !== undefined ) material.size = json.size;
  74. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  75. // maps
  76. if ( json.map !== undefined ) material.map = getTexture( json.map );
  77. if ( json.alphaMap !== undefined ) {
  78. material.alphaMap = getTexture( json.alphaMap );
  79. material.transparent = true;
  80. }
  81. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  82. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  83. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  84. if ( json.normalScale !== undefined ) {
  85. var normalScale = json.normalScale;
  86. if ( Array.isArray( normalScale ) === false ) {
  87. // Blender exporter used to export a scalar. See #7459
  88. normalScale = [ normalScale, normalScale ];
  89. }
  90. material.normalScale = new Vector2().fromArray( normalScale );
  91. }
  92. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  93. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  94. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  95. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  96. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  97. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  98. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  99. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  100. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  101. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  102. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  103. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  104. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  105. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  106. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  107. return material;
  108. }
  109. } );
  110. export { MaterialLoader };
粤ICP备19079148号