MaterialLoader.js 9.6 KB

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