MaterialLoader.js 8.6 KB

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