MaterialLoader.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.setCrossOrigin( this.crossOrigin );
  14. loader.load( url, function ( text ) {
  15. onLoad( scope.parse( JSON.parse( text ) ) );
  16. }, onProgress, onError );
  17. },
  18. setCrossOrigin: function ( value ) {
  19. this.crossOrigin = value;
  20. },
  21. setTextures: function ( value ) {
  22. this.textures = value;
  23. },
  24. getTexture: function ( name ) {
  25. var textures = this.textures;
  26. if ( textures[ name ] === undefined ) {
  27. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  28. }
  29. return textures[ name ];
  30. },
  31. parse: function ( json ) {
  32. var material = new THREE[ json.type ];
  33. material.uuid = json.uuid;
  34. if ( json.name !== undefined ) material.name = json.name;
  35. if ( json.color !== undefined ) material.color.setHex( json.color );
  36. if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
  37. if ( json.specular !== undefined ) material.specular.setHex( json.specular );
  38. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  39. if ( json.uniforms !== undefined ) material.uniforms = json.uniforms;
  40. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  41. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  42. if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
  43. if ( json.shading !== undefined ) material.shading = json.shading;
  44. if ( json.blending !== undefined ) material.blending = json.blending;
  45. if ( json.side !== undefined ) material.side = json.side;
  46. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  47. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  48. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  49. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  50. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  51. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  52. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  53. // for PointsMaterial
  54. if ( json.size !== undefined ) material.size = json.size;
  55. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  56. // maps
  57. if ( json.map !== undefined ) material.map = this.getTexture( json.map );
  58. if ( json.alphaMap !== undefined ) {
  59. material.alphaMap = this.getTexture( json.alphaMap );
  60. material.transparent = true;
  61. }
  62. if ( json.bumpMap !== undefined ) material.bumpMap = this.getTexture( json.bumpMap );
  63. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  64. if ( json.normalMap !== undefined ) material.normalMap = this.getTexture( json.normalMap );
  65. if ( json.normalScale ) material.normalScale = new THREE.Vector2( json.normalScale, json.normalScale );
  66. if ( json.displacementMap !== undefined ) material.displacementMap = this.getTexture( json.displacementMap );
  67. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  68. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  69. if ( json.specularMap !== undefined ) material.specularMap = this.getTexture( json.specularMap );
  70. if ( json.envMap !== undefined ) {
  71. material.envMap = this.getTexture( json.envMap );
  72. material.combine = THREE.MultiplyOperation;
  73. }
  74. if ( json.reflectivity ) material.reflectivity = json.reflectivity;
  75. if ( json.lightMap !== undefined ) material.lightMap = this.getTexture( json.lightMap );
  76. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  77. if ( json.aoMap !== undefined ) material.aoMap = this.getTexture( json.aoMap );
  78. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  79. // MeshFaceMaterial
  80. if ( json.materials !== undefined ) {
  81. for ( var i = 0, l = json.materials.length; i < l; i ++ ) {
  82. material.materials.push( this.parse( json.materials[ i ] ) );
  83. }
  84. }
  85. return material;
  86. }
  87. };
粤ICP备19079148号