MaterialLoader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.MaterialLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. this.textures = {};
  7. };
  8. THREE.MaterialLoader.prototype = {
  9. constructor: THREE.MaterialLoader,
  10. load: function ( url, onLoad, onProgress, onError ) {
  11. var scope = this;
  12. var loader = new THREE.XHRLoader( scope.manager );
  13. loader.load( url, function ( text ) {
  14. onLoad( scope.parse( JSON.parse( text ) ) );
  15. }, onProgress, onError );
  16. },
  17. setTextures: function ( value ) {
  18. this.textures = value;
  19. },
  20. getTexture: function ( name ) {
  21. var textures = this.textures;
  22. if ( textures[ name ] === undefined ) {
  23. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  24. }
  25. return textures[ name ];
  26. },
  27. parse: function ( json ) {
  28. var material = new THREE[ json.type ];
  29. if ( json.uuid !== undefined ) material.uuid = json.uuid;
  30. if ( json.name !== undefined ) material.name = json.name;
  31. if ( json.color !== undefined ) material.color.setHex( json.color );
  32. if ( json.roughness !== undefined ) material.roughness = json.roughness;
  33. if ( json.metalness !== undefined ) material.metalness = json.metalness;
  34. if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
  35. if ( json.specular !== undefined ) material.specular.setHex( json.specular );
  36. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  37. if ( json.uniforms !== undefined ) material.uniforms = json.uniforms;
  38. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  39. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  40. if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
  41. if ( json.shading !== undefined ) material.shading = json.shading;
  42. if ( json.blending !== undefined ) material.blending = json.blending;
  43. if ( json.side !== undefined ) material.side = json.side;
  44. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  45. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  46. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  47. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  48. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  49. if ( json.stencilTest !== undefined ) material.stencilTest = json.stencilTest;
  50. if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite;
  51. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  52. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  53. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  54. // for PointsMaterial
  55. if ( json.size !== undefined ) material.size = json.size;
  56. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  57. // maps
  58. if ( json.map !== undefined ) material.map = this.getTexture( json.map );
  59. if ( json.alphaMap !== undefined ) {
  60. material.alphaMap = this.getTexture( json.alphaMap );
  61. material.transparent = true;
  62. }
  63. if ( json.bumpMap !== undefined ) material.bumpMap = this.getTexture( json.bumpMap );
  64. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  65. if ( json.normalMap !== undefined ) material.normalMap = this.getTexture( json.normalMap );
  66. if ( json.normalScale !== undefined ) {
  67. var normalScale = json.normalScale;
  68. if ( Array.isArray( normalScale ) === false ) {
  69. // Blender exporter used to export a scalar. See #7459
  70. normalScale = [ normalScale, normalScale ];
  71. }
  72. material.normalScale = new THREE.Vector2().fromArray( normalScale );
  73. }
  74. if ( json.displacementMap !== undefined ) material.displacementMap = this.getTexture( json.displacementMap );
  75. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  76. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  77. if ( json.roughnessMap !== undefined ) material.roughnessMap = this.getTexture( json.roughnessMap );
  78. if ( json.metalnessMap !== undefined ) material.metalnessMap = this.getTexture( json.metalnessMap );
  79. if ( json.emissiveMap !== undefined ) material.emissiveMap = this.getTexture( json.emissiveMap );
  80. if ( json.specularMap !== undefined ) material.specularMap = this.getTexture( json.specularMap );
  81. if ( json.envMap !== undefined ) {
  82. material.envMap = this.getTexture( json.envMap );
  83. material.combine = THREE.MultiplyOperation;
  84. }
  85. if ( json.reflectivity ) material.reflectivity = json.reflectivity;
  86. if ( json.lightMap !== undefined ) material.lightMap = this.getTexture( json.lightMap );
  87. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  88. if ( json.aoMap !== undefined ) material.aoMap = this.getTexture( json.aoMap );
  89. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  90. // MeshFaceMaterial
  91. if ( json.materials !== undefined ) {
  92. for ( var i = 0, l = json.materials.length; i < l; i ++ ) {
  93. material.materials.push( this.parse( json.materials[ i ] ) );
  94. }
  95. }
  96. return material;
  97. }
  98. };
粤ICP备19079148号