MaterialLoader.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.iridescence !== undefined ) material.iridescence = json.iridescence;
  78. if ( json.iridescenceIOR !== undefined ) material.iridescenceIOR = json.iridescenceIOR;
  79. if ( json.iridescenceThicknessRange !== undefined ) material.iridescenceThicknessRange = json.iridescenceThicknessRange;
  80. if ( json.transmission !== undefined ) material.transmission = json.transmission;
  81. if ( json.thickness !== undefined ) material.thickness = json.thickness;
  82. if ( json.attenuationDistance !== undefined ) material.attenuationDistance = json.attenuationDistance;
  83. if ( json.attenuationColor !== undefined && material.attenuationColor !== undefined ) material.attenuationColor.setHex( json.attenuationColor );
  84. if ( json.anisotropy !== undefined ) material.anisotropy = json.anisotropy;
  85. if ( json.anisotropyRotation !== undefined ) material.anisotropyRotation = json.anisotropyRotation;
  86. if ( json.fog !== undefined ) material.fog = json.fog;
  87. if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
  88. if ( json.blending !== undefined ) material.blending = json.blending;
  89. if ( json.combine !== undefined ) material.combine = json.combine;
  90. if ( json.side !== undefined ) material.side = json.side;
  91. if ( json.shadowSide !== undefined ) material.shadowSide = json.shadowSide;
  92. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  93. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  94. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  95. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  96. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  97. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  98. if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite;
  99. if ( json.stencilWriteMask !== undefined ) material.stencilWriteMask = json.stencilWriteMask;
  100. if ( json.stencilFunc !== undefined ) material.stencilFunc = json.stencilFunc;
  101. if ( json.stencilRef !== undefined ) material.stencilRef = json.stencilRef;
  102. if ( json.stencilFuncMask !== undefined ) material.stencilFuncMask = json.stencilFuncMask;
  103. if ( json.stencilFail !== undefined ) material.stencilFail = json.stencilFail;
  104. if ( json.stencilZFail !== undefined ) material.stencilZFail = json.stencilZFail;
  105. if ( json.stencilZPass !== undefined ) material.stencilZPass = json.stencilZPass;
  106. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  107. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  108. if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
  109. if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
  110. if ( json.rotation !== undefined ) material.rotation = json.rotation;
  111. if ( json.linewidth !== 1 ) material.linewidth = json.linewidth;
  112. if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
  113. if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
  114. if ( json.scale !== undefined ) material.scale = json.scale;
  115. if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
  116. if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
  117. if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
  118. if ( json.dithering !== undefined ) material.dithering = json.dithering;
  119. if ( json.alphaToCoverage !== undefined ) material.alphaToCoverage = json.alphaToCoverage;
  120. if ( json.premultipliedAlpha !== undefined ) material.premultipliedAlpha = json.premultipliedAlpha;
  121. if ( json.forceSinglePass !== undefined ) material.forceSinglePass = json.forceSinglePass;
  122. if ( json.visible !== undefined ) material.visible = json.visible;
  123. if ( json.toneMapped !== undefined ) material.toneMapped = json.toneMapped;
  124. if ( json.userData !== undefined ) material.userData = json.userData;
  125. if ( json.vertexColors !== undefined ) {
  126. if ( typeof json.vertexColors === 'number' ) {
  127. material.vertexColors = ( json.vertexColors > 0 ) ? true : false;
  128. } else {
  129. material.vertexColors = json.vertexColors;
  130. }
  131. }
  132. // Shader Material
  133. if ( json.uniforms !== undefined ) {
  134. for ( const name in json.uniforms ) {
  135. const uniform = json.uniforms[ name ];
  136. material.uniforms[ name ] = {};
  137. switch ( uniform.type ) {
  138. case 't':
  139. material.uniforms[ name ].value = getTexture( uniform.value );
  140. break;
  141. case 'c':
  142. material.uniforms[ name ].value = new Color().setHex( uniform.value );
  143. break;
  144. case 'v2':
  145. material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
  146. break;
  147. case 'v3':
  148. material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
  149. break;
  150. case 'v4':
  151. material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
  152. break;
  153. case 'm3':
  154. material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value );
  155. break;
  156. case 'm4':
  157. material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
  158. break;
  159. default:
  160. material.uniforms[ name ].value = uniform.value;
  161. }
  162. }
  163. }
  164. if ( json.defines !== undefined ) material.defines = json.defines;
  165. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  166. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  167. if ( json.glslVersion !== undefined ) material.glslVersion = json.glslVersion;
  168. if ( json.extensions !== undefined ) {
  169. for ( const key in json.extensions ) {
  170. material.extensions[ key ] = json.extensions[ key ];
  171. }
  172. }
  173. if ( json.lights !== undefined ) material.lights = json.lights;
  174. if ( json.clipping !== undefined ) material.clipping = json.clipping;
  175. // for PointsMaterial
  176. if ( json.size !== undefined ) material.size = json.size;
  177. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  178. // maps
  179. if ( json.map !== undefined ) material.map = getTexture( json.map );
  180. if ( json.matcap !== undefined ) material.matcap = getTexture( json.matcap );
  181. if ( json.alphaMap !== undefined ) material.alphaMap = getTexture( json.alphaMap );
  182. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  183. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  184. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  185. if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType;
  186. if ( json.normalScale !== undefined ) {
  187. let normalScale = json.normalScale;
  188. if ( Array.isArray( normalScale ) === false ) {
  189. // Blender exporter used to export a scalar. See #7459
  190. normalScale = [ normalScale, normalScale ];
  191. }
  192. material.normalScale = new Vector2().fromArray( normalScale );
  193. }
  194. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  195. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  196. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  197. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  198. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  199. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  200. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  201. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  202. if ( json.specularIntensityMap !== undefined ) material.specularIntensityMap = getTexture( json.specularIntensityMap );
  203. if ( json.specularColorMap !== undefined ) material.specularColorMap = getTexture( json.specularColorMap );
  204. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  205. if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity;
  206. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  207. if ( json.refractionRatio !== undefined ) material.refractionRatio = json.refractionRatio;
  208. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  209. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  210. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  211. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  212. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  213. if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap );
  214. if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap );
  215. if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap );
  216. if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale );
  217. if ( json.iridescenceMap !== undefined ) material.iridescenceMap = getTexture( json.iridescenceMap );
  218. if ( json.iridescenceThicknessMap !== undefined ) material.iridescenceThicknessMap = getTexture( json.iridescenceThicknessMap );
  219. if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap );
  220. if ( json.thicknessMap !== undefined ) material.thicknessMap = getTexture( json.thicknessMap );
  221. if ( json.anisotropyMap !== undefined ) material.anisotropyMap = getTexture( json.anisotropyMap );
  222. if ( json.sheenColorMap !== undefined ) material.sheenColorMap = getTexture( json.sheenColorMap );
  223. if ( json.sheenRoughnessMap !== undefined ) material.sheenRoughnessMap = getTexture( json.sheenRoughnessMap );
  224. return material;
  225. }
  226. setTextures( value ) {
  227. this.textures = value;
  228. return this;
  229. }
  230. static createMaterialFromType( type ) {
  231. const materialLib = {
  232. ShadowMaterial,
  233. SpriteMaterial,
  234. RawShaderMaterial,
  235. ShaderMaterial,
  236. PointsMaterial,
  237. MeshPhysicalMaterial,
  238. MeshStandardMaterial,
  239. MeshPhongMaterial,
  240. MeshToonMaterial,
  241. MeshNormalMaterial,
  242. MeshLambertMaterial,
  243. MeshDepthMaterial,
  244. MeshDistanceMaterial,
  245. MeshBasicMaterial,
  246. MeshMatcapMaterial,
  247. LineDashedMaterial,
  248. LineBasicMaterial,
  249. Material
  250. };
  251. return new materialLib[ type ]();
  252. }
  253. }
  254. export { MaterialLoader };
粤ICP备19079148号