Material.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.depthFunc = THREE.LessEqualDepth;
  21. this.depthTest = true;
  22. this.depthWrite = true;
  23. this.colorWrite = true;
  24. this.polygonOffset = false;
  25. this.polygonOffsetFactor = 0;
  26. this.polygonOffsetUnits = 0;
  27. this.alphaTest = 0;
  28. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  29. this.visible = true;
  30. this._needsUpdate = true;
  31. };
  32. THREE.Material.prototype = {
  33. constructor: THREE.Material,
  34. get needsUpdate () {
  35. return this._needsUpdate;
  36. },
  37. set needsUpdate ( value ) {
  38. if ( value === true ) this.update();
  39. this._needsUpdate = value;
  40. },
  41. setValues: function ( values ) {
  42. if ( values === undefined ) return;
  43. for ( var key in values ) {
  44. var newValue = values[ key ];
  45. if ( newValue === undefined ) {
  46. THREE.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  47. continue;
  48. }
  49. if ( key in this ) {
  50. var currentValue = this[ key ];
  51. if ( currentValue instanceof THREE.Color ) {
  52. currentValue.set( newValue );
  53. } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
  54. currentValue.copy( newValue );
  55. } else if ( key == 'overdraw' ) {
  56. // ensure overdraw is backwards-compatable with legacy boolean type
  57. this[ key ] = Number( newValue );
  58. } else {
  59. this[ key ] = newValue;
  60. }
  61. }
  62. }
  63. },
  64. toJSON: function() {
  65. // we will store all serialization data on 'data'
  66. var data = {};
  67. // add metadata
  68. data.metadata = {
  69. version: 4.4,
  70. type: 'Material',
  71. generator: 'Material.toJSON'
  72. };
  73. // standard Material serialization
  74. data.type = this.type;
  75. data.uuid = this.uuid;
  76. if ( this.name !== '' ) data.name = this.name;
  77. if ( this.opacity < 1 ) data.opacity = this.opacity;
  78. if ( this.transparent !== false ) data.transparent = this.transparent;
  79. if ( this.wireframe !== false ) data.wireframe = this.wireframe;
  80. return data;
  81. },
  82. clone: function ( material ) {
  83. if ( material === undefined ) material = new THREE.Material();
  84. material.name = this.name;
  85. material.side = this.side;
  86. material.opacity = this.opacity;
  87. material.transparent = this.transparent;
  88. material.blending = this.blending;
  89. material.blendSrc = this.blendSrc;
  90. material.blendDst = this.blendDst;
  91. material.blendEquation = this.blendEquation;
  92. material.blendSrcAlpha = this.blendSrcAlpha;
  93. material.blendDstAlpha = this.blendDstAlpha;
  94. material.blendEquationAlpha = this.blendEquationAlpha;
  95. material.depthFunc = this.depthFunc;
  96. material.depthTest = this.depthTest;
  97. material.depthWrite = this.depthWrite;
  98. material.polygonOffset = this.polygonOffset;
  99. material.polygonOffsetFactor = this.polygonOffsetFactor;
  100. material.polygonOffsetUnits = this.polygonOffsetUnits;
  101. material.alphaTest = this.alphaTest;
  102. material.overdraw = this.overdraw;
  103. material.visible = this.visible;
  104. return material;
  105. },
  106. update: function () {
  107. this.dispatchEvent( { type: 'update' } );
  108. },
  109. dispose: function () {
  110. this.dispatchEvent( { type: 'dispose' } );
  111. }
  112. };
  113. THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );
  114. THREE.MaterialIdCount = 0;