MaterialLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 = json.sheen;
  49. if ( json.sheenColor !== undefined ) material.sheenColor = new Color().setHex( json.sheenColor );
  50. if ( json.sheenRoughness !== undefined ) material.sheenRoughness = json.sheenRoughness;
  51. if ( json.emissive !== undefined && material.emissive !== undefined ) material.emissive.setHex( json.emissive );
  52. if ( json.specular !== undefined && material.specular !== undefined ) material.specular.setHex( json.specular );
  53. if ( json.specularIntensity !== undefined ) material.specularIntensity = json.specularIntensity;
  54. if ( json.specularColor !== undefined && material.specularColor !== undefined ) material.specularColor.setHex( json.specularColor );
  55. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  56. if ( json.clearcoat !== undefined ) material.clearcoat = json.clearcoat;
  57. if ( json.clearcoatRoughness !== undefined ) material.clearcoatRoughness = json.clearcoatRoughness;
  58. if ( json.transmission !== undefined ) material.transmission = json.transmission;
  59. if ( json.thickness !== undefined ) material.thickness = json.thickness;
  60. if ( json.attenuationDistance !== undefined ) material.attenuationDistance = json.attenuationDistance;
  61. if ( json.attenuationColor !== undefined && material.attenuationColor !== undefined ) material.attenuationColor.setHex( json.attenuationColor );
  62. if ( json.fog !== undefined ) material.fog = json.fog;
  63. if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
  64. if ( json.blending !== undefined ) material.blending = json.blending;
  65. if ( json.combine !== undefined ) material.combine = json.combine;
  66. if ( json.side !== undefined ) material.side = json.side;
  67. if ( json.shadowSide !== undefined ) material.shadowSide = json.shadowSide;
  68. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  69. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  70. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  71. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  72. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  73. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  74. if ( json.alphaWrite !== undefined ) material.alphaWrite = json.alphaWrite;
  75. if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite;
  76. if ( json.stencilWriteMask !== undefined ) material.stencilWriteMask = json.stencilWriteMask;
  77. if ( json.stencilFunc !== undefined ) material.stencilFunc = json.stencilFunc;
  78. if ( json.stencilRef !== undefined ) material.stencilRef = json.stencilRef;
  79. if ( json.stencilFuncMask !== undefined ) material.stencilFuncMask = json.stencilFuncMask;
  80. if ( json.stencilFail !== undefined ) material.stencilFail = json.stencilFail;
  81. if ( json.stencilZFail !== undefined ) material.stencilZFail = json.stencilZFail;
  82. if ( json.stencilZPass !== undefined ) material.stencilZPass = json.stencilZPass;
  83. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  84. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  85. if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
  86. if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
  87. if ( json.rotation !== undefined ) material.rotation = json.rotation;
  88. if ( json.linewidth !== 1 ) material.linewidth = json.linewidth;
  89. if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
  90. if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
  91. if ( json.scale !== undefined ) material.scale = json.scale;
  92. if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
  93. if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
  94. if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
  95. if ( json.dithering !== undefined ) material.dithering = json.dithering;
  96. if ( json.alphaToCoverage !== undefined ) material.alphaToCoverage = json.alphaToCoverage;
  97. if ( json.premultipliedAlpha !== undefined ) material.premultipliedAlpha = json.premultipliedAlpha;
  98. if ( json.visible !== undefined ) material.visible = json.visible;
  99. if ( json.toneMapped !== undefined ) material.toneMapped = json.toneMapped;
  100. if ( json.userData !== undefined ) material.userData = json.userData;
  101. if ( json.vertexColors !== undefined ) {
  102. if ( typeof json.vertexColors === 'number' ) {
  103. material.vertexColors = ( json.vertexColors > 0 ) ? true : false;
  104. } else {
  105. material.vertexColors = json.vertexColors;
  106. }
  107. }
  108. // Shader Material
  109. if ( json.uniforms !== undefined ) {
  110. for ( const name in json.uniforms ) {
  111. const uniform = json.uniforms[ name ];
  112. material.uniforms[ name ] = {};
  113. switch ( uniform.type ) {
  114. case 't':
  115. material.uniforms[ name ].value = getTexture( uniform.value );
  116. break;
  117. case 'c':
  118. material.uniforms[ name ].value = new Color().setHex( uniform.value );
  119. break;
  120. case 'v2':
  121. material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
  122. break;
  123. case 'v3':
  124. material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
  125. break;
  126. case 'v4':
  127. material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
  128. break;
  129. case 'm3':
  130. material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value );
  131. break;
  132. case 'm4':
  133. material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
  134. break;
  135. default:
  136. material.uniforms[ name ].value = uniform.value;
  137. }
  138. }
  139. }
  140. if ( json.defines !== undefined ) material.defines = json.defines;
  141. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  142. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  143. if ( json.extensions !== undefined ) {
  144. for ( const key in json.extensions ) {
  145. material.extensions[ key ] = json.extensions[ key ];
  146. }
  147. }
  148. // Deprecated
  149. if ( json.shading !== undefined ) material.flatShading = json.shading === 1; // THREE.FlatShading
  150. // for PointsMaterial
  151. if ( json.size !== undefined ) material.size = json.size;
  152. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  153. // maps
  154. if ( json.map !== undefined ) material.map = getTexture( json.map );
  155. if ( json.matcap !== undefined ) material.matcap = getTexture( json.matcap );
  156. if ( json.alphaMap !== undefined ) material.alphaMap = getTexture( json.alphaMap );
  157. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  158. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  159. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  160. if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType;
  161. if ( json.normalScale !== undefined ) {
  162. let normalScale = json.normalScale;
  163. if ( Array.isArray( normalScale ) === false ) {
  164. // Blender exporter used to export a scalar. See #7459
  165. normalScale = [ normalScale, normalScale ];
  166. }
  167. material.normalScale = new Vector2().fromArray( normalScale );
  168. }
  169. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  170. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  171. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  172. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  173. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  174. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  175. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  176. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  177. if ( json.specularIntensityMap !== undefined ) material.specularIntensityMap = getTexture( json.specularIntensityMap );
  178. if ( json.specularColorMap !== undefined ) material.specularColorMap = getTexture( json.specularColorMap );
  179. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  180. if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity;
  181. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  182. if ( json.refractionRatio !== undefined ) material.refractionRatio = json.refractionRatio;
  183. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  184. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  185. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  186. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  187. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  188. if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap );
  189. if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap );
  190. if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap );
  191. if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale );
  192. if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap );
  193. if ( json.thicknessMap !== undefined ) material.thicknessMap = getTexture( json.thicknessMap );
  194. if ( json.sheenColorMap !== undefined ) material.sheenColorMap = getTexture( json.sheenColorMap );
  195. if ( json.sheenRoughnessMap !== undefined ) material.sheenRoughnessMap = getTexture( json.sheenRoughnessMap );
  196. return material;
  197. }
  198. setTextures( value ) {
  199. this.textures = value;
  200. return this;
  201. }
  202. }
  203. export { MaterialLoader };