MaterialLoader.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { Color } from '../math/Color.js';
  2. import { Vector2 } from '../math/Vector2.js';
  3. import { Vector3 } from '../math/Vector3.js';
  4. import { Vector4 } from '../math/Vector4.js';
  5. import { Matrix4 } from '../math/Matrix4.js';
  6. import { FileLoader } from './FileLoader.js';
  7. import { DefaultLoadingManager } from './LoadingManager.js';
  8. import * as Materials from '../materials/Materials.js';
  9. /**
  10. * @author mrdoob / http://mrdoob.com/
  11. */
  12. function MaterialLoader( manager ) {
  13. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  14. this.textures = {};
  15. }
  16. Object.assign( MaterialLoader.prototype, {
  17. load: function ( url, onLoad, onProgress, onError ) {
  18. var scope = this;
  19. var loader = new FileLoader( scope.manager );
  20. loader.setPath( scope.path );
  21. loader.load( url, function ( text ) {
  22. onLoad( scope.parse( JSON.parse( text ) ) );
  23. }, onProgress, onError );
  24. },
  25. parse: function ( json ) {
  26. var textures = this.textures;
  27. function getTexture( name ) {
  28. if ( textures[ name ] === undefined ) {
  29. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  30. }
  31. return textures[ name ];
  32. }
  33. var material = new Materials[ json.type ]();
  34. if ( json.uuid !== undefined ) material.uuid = json.uuid;
  35. if ( json.name !== undefined ) material.name = json.name;
  36. if ( json.color !== undefined ) material.color.setHex( json.color );
  37. if ( json.roughness !== undefined ) material.roughness = json.roughness;
  38. if ( json.metalness !== undefined ) material.metalness = json.metalness;
  39. if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
  40. if ( json.specular !== undefined ) material.specular.setHex( json.specular );
  41. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  42. if ( json.clearCoat !== undefined ) material.clearCoat = json.clearCoat;
  43. if ( json.clearCoatRoughness !== undefined ) material.clearCoatRoughness = json.clearCoatRoughness;
  44. if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
  45. if ( json.fog !== undefined ) material.fog = json.fog;
  46. if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
  47. if ( json.blending !== undefined ) material.blending = json.blending;
  48. if ( json.combine !== undefined ) material.combine = json.combine;
  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.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
  66. if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
  67. if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
  68. if ( json.skinning !== undefined ) material.skinning = json.skinning;
  69. if ( json.morphTargets !== undefined ) material.morphTargets = json.morphTargets;
  70. if ( json.dithering !== undefined ) material.dithering = json.dithering;
  71. if ( json.visible !== undefined ) material.visible = json.visible;
  72. if ( json.userData !== undefined ) material.userData = json.userData;
  73. // Shader Material
  74. if ( json.uniforms !== undefined ) {
  75. for ( var name in json.uniforms ) {
  76. var uniform = json.uniforms[ name ];
  77. material.uniforms[ name ] = {};
  78. switch ( uniform.type ) {
  79. case 't':
  80. material.uniforms[ name ].value = getTexture( uniform.value );
  81. break;
  82. case 'c':
  83. material.uniforms[ name ].value = new Color().setHex( uniform.value );
  84. break;
  85. case 'v2':
  86. material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
  87. break;
  88. case 'v3':
  89. material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
  90. break;
  91. case 'v4':
  92. material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
  93. break;
  94. case 'm4':
  95. material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
  96. break;
  97. default:
  98. material.uniforms[ name ].value = uniform.value;
  99. }
  100. }
  101. }
  102. if ( json.defines !== undefined ) material.defines = json.defines;
  103. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  104. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  105. // Deprecated
  106. if ( json.shading !== undefined ) material.flatShading = json.shading === 1; // THREE.FlatShading
  107. // for PointsMaterial
  108. if ( json.size !== undefined ) material.size = json.size;
  109. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  110. // maps
  111. if ( json.map !== undefined ) material.map = getTexture( json.map );
  112. if ( json.alphaMap !== undefined ) {
  113. material.alphaMap = getTexture( json.alphaMap );
  114. material.transparent = true;
  115. }
  116. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  117. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  118. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  119. if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType;
  120. if ( json.normalScale !== undefined ) {
  121. var normalScale = json.normalScale;
  122. if ( Array.isArray( normalScale ) === false ) {
  123. // Blender exporter used to export a scalar. See #7459
  124. normalScale = [ normalScale, normalScale ];
  125. }
  126. material.normalScale = new Vector2().fromArray( normalScale );
  127. }
  128. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  129. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  130. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  131. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  132. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  133. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  134. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  135. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  136. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  137. if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity;
  138. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  139. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  140. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  141. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  142. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  143. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  144. return material;
  145. },
  146. setPath: function ( value ) {
  147. this.path = value;
  148. return this;
  149. },
  150. setTextures: function ( value ) {
  151. this.textures = value;
  152. return this;
  153. }
  154. } );
  155. export { MaterialLoader };
粤ICP备19079148号