Material.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.Material = function () {
  6. Object.defineProperty( this, 'id', { value: THREE.MaterialIdCount ++ } );
  7. this.uuid = THREE.Math.generateUUID();
  8. this.name = '';
  9. this.type = 'Material';
  10. this.side = THREE.FrontSide;
  11. this.opacity = 1;
  12. this.transparent = false;
  13. this.blending = THREE.NormalBlending;
  14. this.blendSrc = THREE.SrcAlphaFactor;
  15. this.blendDst = THREE.OneMinusSrcAlphaFactor;
  16. this.blendEquation = THREE.AddEquation;
  17. this.depthTest = true;
  18. this.depthWrite = true;
  19. this.polygonOffset = false;
  20. this.polygonOffsetFactor = 0;
  21. this.polygonOffsetUnits = 0;
  22. this.alphaTest = 0;
  23. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  24. this.visible = true;
  25. this.needsUpdate = true;
  26. };
  27. THREE.Material.prototype = {
  28. constructor: THREE.Material,
  29. setValues: function ( values ) {
  30. if ( values === undefined ) return;
  31. for ( var key in values ) {
  32. var newValue = values[ key ];
  33. if ( newValue === undefined ) {
  34. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  35. continue;
  36. }
  37. if ( key in this ) {
  38. var currentValue = this[ key ];
  39. if ( currentValue instanceof THREE.Color ) {
  40. currentValue.set( newValue );
  41. } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
  42. currentValue.copy( newValue );
  43. } else if ( key == 'overdraw' ) {
  44. // ensure overdraw is backwards-compatable with legacy boolean type
  45. this[ key ] = Number( newValue );
  46. } else {
  47. this[ key ] = newValue;
  48. }
  49. }
  50. }
  51. },
  52. toJSON: function () {
  53. var output = {
  54. metadata: {
  55. version: 4.2,
  56. type: 'material',
  57. generator: 'MaterialExporter'
  58. },
  59. uuid: this.uuid,
  60. type: this.type
  61. };
  62. if ( this.name !== "" ) output.name = this.name;
  63. if ( this instanceof THREE.MeshBasicMaterial ) {
  64. output.color = this.color.getHex();
  65. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  66. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  67. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  68. } else if ( this instanceof THREE.MeshLambertMaterial ) {
  69. output.color = this.color.getHex();
  70. output.ambient = this.ambient.getHex();
  71. output.emissive = this.emissive.getHex();
  72. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  73. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  74. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  75. } else if ( this instanceof THREE.MeshPhongMaterial ) {
  76. output.color = this.color.getHex();
  77. output.ambient = this.ambient.getHex();
  78. output.emissive = this.emissive.getHex();
  79. output.specular = this.specular.getHex();
  80. output.shininess = this.shininess;
  81. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  82. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  83. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  84. } else if ( this instanceof THREE.MeshNormalMaterial ) {
  85. if ( this.shading !== THREE.FlatShading ) output.shading = this.shading;
  86. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  87. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  88. } else if ( this instanceof THREE.MeshDepthMaterial ) {
  89. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  90. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  91. } else if ( this instanceof THREE.ShaderMaterial ) {
  92. output.uniforms = this.uniforms;
  93. output.vertexShader = this.vertexShader;
  94. output.fragmentShader = this.fragmentShader;
  95. } else if ( this instanceof THREE.SpriteMaterial ) {
  96. output.color = this.color.getHex();
  97. }
  98. if ( this.opacity < 1 ) output.opacity = this.opacity;
  99. if ( this.transparent !== false ) output.transparent = this.transparent;
  100. if ( this.wireframe !== false ) output.wireframe = this.wireframe;
  101. return output;
  102. },
  103. clone: function ( material ) {
  104. if ( material === undefined ) material = new THREE.Material();
  105. material.name = this.name;
  106. material.side = this.side;
  107. material.opacity = this.opacity;
  108. material.transparent = this.transparent;
  109. material.blending = this.blending;
  110. material.blendSrc = this.blendSrc;
  111. material.blendDst = this.blendDst;
  112. material.blendEquation = this.blendEquation;
  113. material.depthTest = this.depthTest;
  114. material.depthWrite = this.depthWrite;
  115. material.polygonOffset = this.polygonOffset;
  116. material.polygonOffsetFactor = this.polygonOffsetFactor;
  117. material.polygonOffsetUnits = this.polygonOffsetUnits;
  118. material.alphaTest = this.alphaTest;
  119. material.overdraw = this.overdraw;
  120. material.visible = this.visible;
  121. return material;
  122. },
  123. dispose: function () {
  124. this.dispatchEvent( { type: 'dispose' } );
  125. }
  126. };
  127. THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );
  128. THREE.MaterialIdCount = 0;
粤ICP备19079148号