Material.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.blendSrcAlpha = null;
  18. this.blendDstAlpha = null;
  19. this.blendEquationAlpha = null;
  20. this.depthTest = true;
  21. this.depthWrite = true;
  22. this.colorWrite = true;
  23. this.polygonOffset = false;
  24. this.polygonOffsetFactor = 0;
  25. this.polygonOffsetUnits = 0;
  26. this.alphaTest = 0;
  27. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  28. this.visible = true;
  29. this._needsUpdate = true;
  30. };
  31. THREE.Material.prototype = {
  32. constructor: THREE.Material,
  33. get needsUpdate () {
  34. return this._needsUpdate;
  35. },
  36. set needsUpdate ( value ) {
  37. if ( value === true ) this.update();
  38. this._needsUpdate = value;
  39. },
  40. setValues: function ( values ) {
  41. if ( values === undefined ) return;
  42. for ( var key in values ) {
  43. var newValue = values[ key ];
  44. if ( newValue === undefined ) {
  45. THREE.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  46. continue;
  47. }
  48. if ( key in this ) {
  49. var currentValue = this[ key ];
  50. if ( currentValue instanceof THREE.Color ) {
  51. currentValue.set( newValue );
  52. } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
  53. currentValue.copy( newValue );
  54. } else if ( key == 'overdraw' ) {
  55. // ensure overdraw is backwards-compatable with legacy boolean type
  56. this[ key ] = Number( newValue );
  57. } else {
  58. this[ key ] = newValue;
  59. }
  60. }
  61. }
  62. },
  63. toJSON: function () {
  64. var output = {
  65. metadata: {
  66. version: 4.2,
  67. type: 'material',
  68. generator: 'MaterialExporter'
  69. },
  70. uuid: this.uuid,
  71. type: this.type
  72. };
  73. if ( this.name !== "" ) output.name = this.name;
  74. if ( this instanceof THREE.MeshBasicMaterial ) {
  75. output.color = this.color.getHex();
  76. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  77. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  78. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  79. } else if ( this instanceof THREE.MeshLambertMaterial ) {
  80. output.color = this.color.getHex();
  81. output.emissive = this.emissive.getHex();
  82. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  83. if ( this.shading !== THREE.SmoothShading ) output.shading = this.shading;
  84. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  85. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  86. } else if ( this instanceof THREE.MeshPhongMaterial ) {
  87. output.color = this.color.getHex();
  88. output.emissive = this.emissive.getHex();
  89. output.specular = this.specular.getHex();
  90. output.shininess = this.shininess;
  91. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  92. if ( this.shading !== THREE.SmoothShading ) output.shading = this.shading;
  93. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  94. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  95. } else if ( this instanceof THREE.MeshNormalMaterial ) {
  96. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  97. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  98. } else if ( this instanceof THREE.MeshDepthMaterial ) {
  99. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  100. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  101. } else if ( this instanceof THREE.PointCloudMaterial ) {
  102. output.size = this.size;
  103. output.sizeAttenuation = this.sizeAttenuation;
  104. output.color = this.color.getHex();
  105. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  106. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  107. } else if ( this instanceof THREE.ShaderMaterial ) {
  108. output.uniforms = this.uniforms;
  109. output.vertexShader = this.vertexShader;
  110. output.fragmentShader = this.fragmentShader;
  111. } else if ( this instanceof THREE.SpriteMaterial ) {
  112. output.color = this.color.getHex();
  113. }
  114. if ( this.opacity < 1 ) output.opacity = this.opacity;
  115. if ( this.transparent !== false ) output.transparent = this.transparent;
  116. if ( this.wireframe !== false ) output.wireframe = this.wireframe;
  117. return output;
  118. },
  119. clone: function ( material ) {
  120. if ( material === undefined ) material = new THREE.Material();
  121. material.name = this.name;
  122. material.side = this.side;
  123. material.opacity = this.opacity;
  124. material.transparent = this.transparent;
  125. material.blending = this.blending;
  126. material.blendSrc = this.blendSrc;
  127. material.blendDst = this.blendDst;
  128. material.blendEquation = this.blendEquation;
  129. material.blendSrcAlpha = this.blendSrcAlpha;
  130. material.blendDstAlpha = this.blendDstAlpha;
  131. material.blendEquationAlpha = this.blendEquationAlpha;
  132. material.depthTest = this.depthTest;
  133. material.depthWrite = this.depthWrite;
  134. material.polygonOffset = this.polygonOffset;
  135. material.polygonOffsetFactor = this.polygonOffsetFactor;
  136. material.polygonOffsetUnits = this.polygonOffsetUnits;
  137. material.alphaTest = this.alphaTest;
  138. material.overdraw = this.overdraw;
  139. material.visible = this.visible;
  140. return material;
  141. },
  142. update: function () {
  143. this.dispatchEvent( { type: 'update' } );
  144. },
  145. dispose: function () {
  146. this.dispatchEvent( { type: 'dispose' } );
  147. }
  148. };
  149. THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );
  150. THREE.MaterialIdCount = 0;
粤ICP备19079148号