MaterialLoader.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.load( url, function ( text ) {
  21. onLoad( scope.parse( JSON.parse( text ) ) );
  22. }, onProgress, onError );
  23. },
  24. setTextures: function ( value ) {
  25. this.textures = value;
  26. },
  27. parse: function ( json ) {
  28. var textures = this.textures;
  29. function getTexture( name ) {
  30. if ( textures[ name ] === undefined ) {
  31. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  32. }
  33. return textures[ name ];
  34. }
  35. var material = new Materials[ json.type ]();
  36. if ( json.uuid !== undefined ) material.uuid = json.uuid;
  37. if ( json.name !== undefined ) material.name = json.name;
  38. if ( json.color !== undefined ) material.color.setHex( json.color );
  39. if ( json.roughness !== undefined ) material.roughness = json.roughness;
  40. if ( json.metalness !== undefined ) material.metalness = json.metalness;
  41. if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
  42. if ( json.specular !== undefined ) material.specular.setHex( json.specular );
  43. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  44. if ( json.clearCoat !== undefined ) material.clearCoat = json.clearCoat;
  45. if ( json.clearCoatRoughness !== undefined ) material.clearCoatRoughness = json.clearCoatRoughness;
  46. if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
  47. if ( json.fog !== undefined ) material.fog = json.fog;
  48. if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
  49. if ( json.blending !== undefined ) material.blending = json.blending;
  50. if ( json.combine !== undefined ) material.combine = json.combine;
  51. if ( json.side !== undefined ) material.side = json.side;
  52. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  53. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  54. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  55. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  56. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  57. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  58. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  59. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  60. if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
  61. if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
  62. if ( json.rotation !== undefined ) material.rotation = json.rotation;
  63. if ( json.linewidth !== 1 ) material.linewidth = json.linewidth;
  64. if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
  65. if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
  66. if ( json.scale !== undefined ) material.scale = json.scale;
  67. if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
  68. if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
  69. if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
  70. if ( json.skinning !== undefined ) material.skinning = json.skinning;
  71. if ( json.morphTargets !== undefined ) material.morphTargets = json.morphTargets;
  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 'm4':
  97. material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
  98. break;
  99. default:
  100. material.uniforms[ name ].value = uniform.value;
  101. }
  102. }
  103. }
  104. if ( json.defines !== undefined ) material.defines = json.defines;
  105. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  106. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  107. // Deprecated
  108. if ( json.shading !== undefined ) material.flatShading = json.shading === 1; // THREE.FlatShading
  109. // for PointsMaterial
  110. if ( json.size !== undefined ) material.size = json.size;
  111. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  112. // maps
  113. if ( json.map !== undefined ) material.map = getTexture( json.map );
  114. if ( json.alphaMap !== undefined ) {
  115. material.alphaMap = getTexture( json.alphaMap );
  116. material.transparent = true;
  117. }
  118. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  119. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  120. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  121. if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType;
  122. if ( json.normalScale !== undefined ) {
  123. var normalScale = json.normalScale;
  124. if ( Array.isArray( normalScale ) === false ) {
  125. // Blender exporter used to export a scalar. See #7459
  126. normalScale = [ normalScale, normalScale ];
  127. }
  128. material.normalScale = new Vector2().fromArray( normalScale );
  129. }
  130. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  131. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  132. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  133. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  134. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  135. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  136. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  137. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  138. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  139. if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity;
  140. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  141. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  142. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  143. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  144. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  145. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  146. return material;
  147. }
  148. } );
  149. export { MaterialLoader };
粤ICP备19079148号