MaterialLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. class MaterialLoader extends Loader {
  11. constructor( manager ) {
  12. super( manager );
  13. this.textures = {};
  14. }
  15. load( url, onLoad, onProgress, onError ) {
  16. const scope = this;
  17. const loader = new FileLoader( scope.manager );
  18. loader.setPath( scope.path );
  19. loader.setRequestHeader( scope.requestHeader );
  20. loader.setWithCredentials( scope.withCredentials );
  21. loader.load( url, function ( text ) {
  22. try {
  23. onLoad( scope.parse( JSON.parse( text ) ) );
  24. } catch ( e ) {
  25. if ( onError ) {
  26. onError( e );
  27. } else {
  28. console.error( e );
  29. }
  30. scope.manager.itemError( url );
  31. }
  32. }, onProgress, onError );
  33. }
  34. parse( json ) {
  35. const textures = this.textures;
  36. function getTexture( name ) {
  37. if ( textures[ name ] === undefined ) {
  38. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  39. }
  40. return textures[ name ];
  41. }
  42. const material = new Materials[ json.type ]();
  43. if ( json.uuid !== undefined ) material.uuid = json.uuid;
  44. if ( json.name !== undefined ) material.name = json.name;
  45. if ( json.color !== undefined && material.color !== undefined ) material.color.setHex( json.color );
  46. if ( json.roughness !== undefined ) material.roughness = json.roughness;
  47. if ( json.metalness !== undefined ) material.metalness = json.metalness;
  48. if ( json.sheen !== undefined ) material.sheen = new Color().setHex( json.sheen );
  49. if ( json.emissive !== undefined && material.emissive !== undefined ) material.emissive.setHex( json.emissive );
  50. if ( json.specular !== undefined && material.specular !== undefined ) material.specular.setHex( json.specular );
  51. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  52. if ( json.clearcoat !== undefined ) material.clearcoat = json.clearcoat;
  53. if ( json.clearcoatRoughness !== undefined ) material.clearcoatRoughness = json.clearcoatRoughness;
  54. if ( json.fog !== undefined ) material.fog = json.fog;
  55. if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
  56. if ( json.blending !== undefined ) material.blending = json.blending;
  57. if ( json.combine !== undefined ) material.combine = json.combine;
  58. if ( json.side !== undefined ) material.side = json.side;
  59. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  60. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  61. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  62. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  63. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  64. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  65. if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite;
  66. if ( json.stencilWriteMask !== undefined ) material.stencilWriteMask = json.stencilWriteMask;
  67. if ( json.stencilFunc !== undefined ) material.stencilFunc = json.stencilFunc;
  68. if ( json.stencilRef !== undefined ) material.stencilRef = json.stencilRef;
  69. if ( json.stencilFuncMask !== undefined ) material.stencilFuncMask = json.stencilFuncMask;
  70. if ( json.stencilFail !== undefined ) material.stencilFail = json.stencilFail;
  71. if ( json.stencilZFail !== undefined ) material.stencilZFail = json.stencilZFail;
  72. if ( json.stencilZPass !== undefined ) material.stencilZPass = json.stencilZPass;
  73. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  74. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  75. if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
  76. if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
  77. if ( json.rotation !== undefined ) material.rotation = json.rotation;
  78. if ( json.linewidth !== 1 ) material.linewidth = json.linewidth;
  79. if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
  80. if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
  81. if ( json.scale !== undefined ) material.scale = json.scale;
  82. if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
  83. if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
  84. if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
  85. if ( json.skinning !== undefined ) material.skinning = json.skinning;
  86. if ( json.morphTargets !== undefined ) material.morphTargets = json.morphTargets;
  87. if ( json.morphNormals !== undefined ) material.morphNormals = json.morphNormals;
  88. if ( json.dithering !== undefined ) material.dithering = json.dithering;
  89. if ( json.alphaToCoverage !== undefined ) material.alphaToCoverage = json.alphaToCoverage;
  90. if ( json.premultipliedAlpha !== undefined ) material.premultipliedAlpha = json.premultipliedAlpha;
  91. if ( json.vertexTangents !== undefined ) material.vertexTangents = json.vertexTangents;
  92. if ( json.visible !== undefined ) material.visible = json.visible;
  93. if ( json.toneMapped !== undefined ) material.toneMapped = json.toneMapped;
  94. if ( json.userData !== undefined ) material.userData = json.userData;
  95. if ( json.vertexColors !== undefined ) {
  96. if ( typeof json.vertexColors === 'number' ) {
  97. material.vertexColors = ( json.vertexColors > 0 ) ? true : false;
  98. } else {
  99. material.vertexColors = json.vertexColors;
  100. }
  101. }
  102. // Shader Material
  103. if ( json.uniforms !== undefined ) {
  104. for ( const name in json.uniforms ) {
  105. const uniform = json.uniforms[ name ];
  106. material.uniforms[ name ] = {};
  107. switch ( uniform.type ) {
  108. case 't':
  109. material.uniforms[ name ].value = getTexture( uniform.value );
  110. break;
  111. case 'c':
  112. material.uniforms[ name ].value = new Color().setHex( uniform.value );
  113. break;
  114. case 'v2':
  115. material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
  116. break;
  117. case 'v3':
  118. material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
  119. break;
  120. case 'v4':
  121. material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
  122. break;
  123. case 'm3':
  124. material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value );
  125. break;
  126. case 'm4':
  127. material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
  128. break;
  129. default:
  130. material.uniforms[ name ].value = uniform.value;
  131. }
  132. }
  133. }
  134. if ( json.defines !== undefined ) material.defines = json.defines;
  135. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  136. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  137. if ( json.extensions !== undefined ) {
  138. for ( const key in json.extensions ) {
  139. material.extensions[ key ] = json.extensions[ key ];
  140. }
  141. }
  142. // Deprecated
  143. if ( json.shading !== undefined ) material.flatShading = json.shading === 1; // THREE.FlatShading
  144. // for PointsMaterial
  145. if ( json.size !== undefined ) material.size = json.size;
  146. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  147. // maps
  148. if ( json.map !== undefined ) material.map = getTexture( json.map );
  149. if ( json.matcap !== undefined ) material.matcap = getTexture( json.matcap );
  150. if ( json.alphaMap !== undefined ) material.alphaMap = getTexture( json.alphaMap );
  151. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  152. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  153. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  154. if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType;
  155. if ( json.normalScale !== undefined ) {
  156. let normalScale = json.normalScale;
  157. if ( Array.isArray( normalScale ) === false ) {
  158. // Blender exporter used to export a scalar. See #7459
  159. normalScale = [ normalScale, normalScale ];
  160. }
  161. material.normalScale = new Vector2().fromArray( normalScale );
  162. }
  163. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  164. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  165. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  166. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  167. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  168. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  169. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  170. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  171. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  172. if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity;
  173. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  174. if ( json.refractionRatio !== undefined ) material.refractionRatio = json.refractionRatio;
  175. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  176. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  177. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  178. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  179. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  180. if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap );
  181. if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap );
  182. if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap );
  183. if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale );
  184. if ( json.transmission !== undefined ) material.transmission = json.transmission;
  185. if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap );
  186. return material;
  187. }
  188. setTextures( value ) {
  189. this.textures = value;
  190. return this;
  191. }
  192. }
  193. export { MaterialLoader };