MaterialLoader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 {
  10. ShadowMaterial,
  11. SpriteMaterial,
  12. RawShaderMaterial,
  13. ShaderMaterial,
  14. PointsMaterial,
  15. MeshPhysicalMaterial,
  16. MeshStandardMaterial,
  17. MeshPhongMaterial,
  18. MeshToonMaterial,
  19. MeshNormalMaterial,
  20. MeshLambertMaterial,
  21. MeshDepthMaterial,
  22. MeshDistanceMaterial,
  23. MeshBasicMaterial,
  24. MeshMatcapMaterial,
  25. LineDashedMaterial,
  26. LineBasicMaterial,
  27. Material,
  28. } from '../materials/Materials.js';
  29. class MaterialLoader extends Loader {
  30. constructor( manager ) {
  31. super( manager );
  32. this.textures = {};
  33. }
  34. load( url, onLoad, onProgress, onError ) {
  35. const scope = this;
  36. const loader = new FileLoader( scope.manager );
  37. loader.setPath( scope.path );
  38. loader.setRequestHeader( scope.requestHeader );
  39. loader.setWithCredentials( scope.withCredentials );
  40. loader.load( url, function ( text ) {
  41. try {
  42. onLoad( scope.parse( JSON.parse( text ) ) );
  43. } catch ( e ) {
  44. if ( onError ) {
  45. onError( e );
  46. } else {
  47. console.error( e );
  48. }
  49. scope.manager.itemError( url );
  50. }
  51. }, onProgress, onError );
  52. }
  53. parse( json ) {
  54. const textures = this.textures;
  55. function getTexture( name ) {
  56. if ( textures[ name ] === undefined ) {
  57. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  58. }
  59. return textures[ name ];
  60. }
  61. const material = MaterialLoader.createMaterialFromType( json.type );
  62. if ( json.uuid !== undefined ) material.uuid = json.uuid;
  63. if ( json.name !== undefined ) material.name = json.name;
  64. if ( json.color !== undefined && material.color !== undefined ) material.color.setHex( json.color );
  65. if ( json.roughness !== undefined ) material.roughness = json.roughness;
  66. if ( json.metalness !== undefined ) material.metalness = json.metalness;
  67. if ( json.sheen !== undefined ) material.sheen = json.sheen;
  68. if ( json.sheenColor !== undefined ) material.sheenColor = new Color().setHex( json.sheenColor );
  69. if ( json.sheenRoughness !== undefined ) material.sheenRoughness = json.sheenRoughness;
  70. if ( json.emissive !== undefined && material.emissive !== undefined ) material.emissive.setHex( json.emissive );
  71. if ( json.specular !== undefined && material.specular !== undefined ) material.specular.setHex( json.specular );
  72. if ( json.specularIntensity !== undefined ) material.specularIntensity = json.specularIntensity;
  73. if ( json.specularColor !== undefined && material.specularColor !== undefined ) material.specularColor.setHex( json.specularColor );
  74. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  75. if ( json.clearcoat !== undefined ) material.clearcoat = json.clearcoat;
  76. if ( json.clearcoatRoughness !== undefined ) material.clearcoatRoughness = json.clearcoatRoughness;
  77. if ( json.dispersion !== undefined ) material.dispersion = json.dispersion;
  78. if ( json.iridescence !== undefined ) material.iridescence = json.iridescence;
  79. if ( json.iridescenceIOR !== undefined ) material.iridescenceIOR = json.iridescenceIOR;
  80. if ( json.iridescenceThicknessRange !== undefined ) material.iridescenceThicknessRange = json.iridescenceThicknessRange;
  81. if ( json.transmission !== undefined ) material.transmission = json.transmission;
  82. if ( json.thickness !== undefined ) material.thickness = json.thickness;
  83. if ( json.attenuationDistance !== undefined ) material.attenuationDistance = json.attenuationDistance;
  84. if ( json.attenuationColor !== undefined && material.attenuationColor !== undefined ) material.attenuationColor.setHex( json.attenuationColor );
  85. if ( json.anisotropy !== undefined ) material.anisotropy = json.anisotropy;
  86. if ( json.anisotropyRotation !== undefined ) material.anisotropyRotation = json.anisotropyRotation;
  87. if ( json.fog !== undefined ) material.fog = json.fog;
  88. if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
  89. if ( json.blending !== undefined ) material.blending = json.blending;
  90. if ( json.combine !== undefined ) material.combine = json.combine;
  91. if ( json.side !== undefined ) material.side = json.side;
  92. if ( json.shadowSide !== undefined ) material.shadowSide = json.shadowSide;
  93. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  94. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  95. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  96. if ( json.alphaHash !== undefined ) material.alphaHash = json.alphaHash;
  97. if ( json.depthFunc !== undefined ) material.depthFunc = json.depthFunc;
  98. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  99. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  100. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  101. if ( json.blendSrc !== undefined ) material.blendSrc = json.blendSrc;
  102. if ( json.blendDst !== undefined ) material.blendDst = json.blendDst;
  103. if ( json.blendEquation !== undefined ) material.blendEquation = json.blendEquation;
  104. if ( json.blendSrcAlpha !== undefined ) material.blendSrcAlpha = json.blendSrcAlpha;
  105. if ( json.blendDstAlpha !== undefined ) material.blendDstAlpha = json.blendDstAlpha;
  106. if ( json.blendEquationAlpha !== undefined ) material.blendEquationAlpha = json.blendEquationAlpha;
  107. if ( json.blendColor !== undefined && material.blendColor !== undefined ) material.blendColor.setHex( json.blendColor );
  108. if ( json.blendAlpha !== undefined ) material.blendAlpha = json.blendAlpha;
  109. if ( json.stencilWriteMask !== undefined ) material.stencilWriteMask = json.stencilWriteMask;
  110. if ( json.stencilFunc !== undefined ) material.stencilFunc = json.stencilFunc;
  111. if ( json.stencilRef !== undefined ) material.stencilRef = json.stencilRef;
  112. if ( json.stencilFuncMask !== undefined ) material.stencilFuncMask = json.stencilFuncMask;
  113. if ( json.stencilFail !== undefined ) material.stencilFail = json.stencilFail;
  114. if ( json.stencilZFail !== undefined ) material.stencilZFail = json.stencilZFail;
  115. if ( json.stencilZPass !== undefined ) material.stencilZPass = json.stencilZPass;
  116. if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite;
  117. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  118. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  119. if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
  120. if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
  121. if ( json.rotation !== undefined ) material.rotation = json.rotation;
  122. if ( json.linewidth !== undefined ) material.linewidth = json.linewidth;
  123. if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
  124. if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
  125. if ( json.scale !== undefined ) material.scale = json.scale;
  126. if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
  127. if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
  128. if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
  129. if ( json.dithering !== undefined ) material.dithering = json.dithering;
  130. if ( json.alphaToCoverage !== undefined ) material.alphaToCoverage = json.alphaToCoverage;
  131. if ( json.premultipliedAlpha !== undefined ) material.premultipliedAlpha = json.premultipliedAlpha;
  132. if ( json.forceSinglePass !== undefined ) material.forceSinglePass = json.forceSinglePass;
  133. if ( json.visible !== undefined ) material.visible = json.visible;
  134. if ( json.toneMapped !== undefined ) material.toneMapped = json.toneMapped;
  135. if ( json.userData !== undefined ) material.userData = json.userData;
  136. if ( json.vertexColors !== undefined ) {
  137. if ( typeof json.vertexColors === 'number' ) {
  138. material.vertexColors = ( json.vertexColors > 0 ) ? true : false;
  139. } else {
  140. material.vertexColors = json.vertexColors;
  141. }
  142. }
  143. // Shader Material
  144. if ( json.uniforms !== undefined ) {
  145. for ( const name in json.uniforms ) {
  146. const uniform = json.uniforms[ name ];
  147. material.uniforms[ name ] = {};
  148. switch ( uniform.type ) {
  149. case 't':
  150. material.uniforms[ name ].value = getTexture( uniform.value );
  151. break;
  152. case 'c':
  153. material.uniforms[ name ].value = new Color().setHex( uniform.value );
  154. break;
  155. case 'v2':
  156. material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
  157. break;
  158. case 'v3':
  159. material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
  160. break;
  161. case 'v4':
  162. material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
  163. break;
  164. case 'm3':
  165. material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value );
  166. break;
  167. case 'm4':
  168. material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
  169. break;
  170. default:
  171. material.uniforms[ name ].value = uniform.value;
  172. }
  173. }
  174. }
  175. if ( json.defines !== undefined ) material.defines = json.defines;
  176. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  177. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  178. if ( json.glslVersion !== undefined ) material.glslVersion = json.glslVersion;
  179. if ( json.extensions !== undefined ) {
  180. for ( const key in json.extensions ) {
  181. material.extensions[ key ] = json.extensions[ key ];
  182. }
  183. }
  184. if ( json.lights !== undefined ) material.lights = json.lights;
  185. if ( json.clipping !== undefined ) material.clipping = json.clipping;
  186. // for PointsMaterial
  187. if ( json.size !== undefined ) material.size = json.size;
  188. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  189. // maps
  190. if ( json.map !== undefined ) material.map = getTexture( json.map );
  191. if ( json.matcap !== undefined ) material.matcap = getTexture( json.matcap );
  192. if ( json.alphaMap !== undefined ) material.alphaMap = getTexture( json.alphaMap );
  193. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  194. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  195. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  196. if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType;
  197. if ( json.normalScale !== undefined ) {
  198. let normalScale = json.normalScale;
  199. if ( Array.isArray( normalScale ) === false ) {
  200. // Blender exporter used to export a scalar. See #7459
  201. normalScale = [ normalScale, normalScale ];
  202. }
  203. material.normalScale = new Vector2().fromArray( normalScale );
  204. }
  205. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  206. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  207. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  208. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  209. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  210. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  211. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  212. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  213. if ( json.specularIntensityMap !== undefined ) material.specularIntensityMap = getTexture( json.specularIntensityMap );
  214. if ( json.specularColorMap !== undefined ) material.specularColorMap = getTexture( json.specularColorMap );
  215. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  216. if ( json.envMapRotation !== undefined ) material.envMapRotation.fromArray( json.envMapRotation );
  217. if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity;
  218. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  219. if ( json.refractionRatio !== undefined ) material.refractionRatio = json.refractionRatio;
  220. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  221. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  222. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  223. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  224. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  225. if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap );
  226. if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap );
  227. if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap );
  228. if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale );
  229. if ( json.iridescenceMap !== undefined ) material.iridescenceMap = getTexture( json.iridescenceMap );
  230. if ( json.iridescenceThicknessMap !== undefined ) material.iridescenceThicknessMap = getTexture( json.iridescenceThicknessMap );
  231. if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap );
  232. if ( json.thicknessMap !== undefined ) material.thicknessMap = getTexture( json.thicknessMap );
  233. if ( json.anisotropyMap !== undefined ) material.anisotropyMap = getTexture( json.anisotropyMap );
  234. if ( json.sheenColorMap !== undefined ) material.sheenColorMap = getTexture( json.sheenColorMap );
  235. if ( json.sheenRoughnessMap !== undefined ) material.sheenRoughnessMap = getTexture( json.sheenRoughnessMap );
  236. return material;
  237. }
  238. setTextures( value ) {
  239. this.textures = value;
  240. return this;
  241. }
  242. static createMaterialFromType( type ) {
  243. const materialLib = {
  244. ShadowMaterial,
  245. SpriteMaterial,
  246. RawShaderMaterial,
  247. ShaderMaterial,
  248. PointsMaterial,
  249. MeshPhysicalMaterial,
  250. MeshStandardMaterial,
  251. MeshPhongMaterial,
  252. MeshToonMaterial,
  253. MeshNormalMaterial,
  254. MeshLambertMaterial,
  255. MeshDepthMaterial,
  256. MeshDistanceMaterial,
  257. MeshBasicMaterial,
  258. MeshMatcapMaterial,
  259. LineDashedMaterial,
  260. LineBasicMaterial,
  261. Material
  262. };
  263. return new materialLib[ type ]();
  264. }
  265. }
  266. export { MaterialLoader };