Material.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.precision = null; // override the renderer's default precision for this material
  25. this.polygonOffset = false;
  26. this.polygonOffsetFactor = 0;
  27. this.polygonOffsetUnits = 0;
  28. this.alphaTest = 0;
  29. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  30. this.visible = true;
  31. this._needsUpdate = true;
  32. };
  33. THREE.Material.prototype = {
  34. constructor: THREE.Material,
  35. get needsUpdate () {
  36. return this._needsUpdate;
  37. },
  38. set needsUpdate ( value ) {
  39. if ( value === true ) this.update();
  40. this._needsUpdate = value;
  41. },
  42. setValues: function ( values ) {
  43. if ( values === undefined ) return;
  44. for ( var key in values ) {
  45. var newValue = values[ key ];
  46. if ( newValue === undefined ) {
  47. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  48. continue;
  49. }
  50. if ( key in this ) {
  51. var currentValue = this[ key ];
  52. if ( currentValue instanceof THREE.Color ) {
  53. currentValue.set( newValue );
  54. } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
  55. currentValue.copy( newValue );
  56. } else if ( key === 'overdraw' ) {
  57. // ensure overdraw is backwards-compatible with legacy boolean type
  58. this[ key ] = Number( newValue );
  59. } else {
  60. this[ key ] = newValue;
  61. }
  62. }
  63. }
  64. },
  65. toJSON: function ( meta ) {
  66. var data = {
  67. metadata: {
  68. version: 4.4,
  69. type: 'Material',
  70. generator: 'Material.toJSON'
  71. }
  72. };
  73. // standard Material serialization
  74. data.uuid = this.uuid;
  75. data.type = this.type;
  76. if ( this.name !== '' ) data.name = this.name;
  77. if ( this.color instanceof THREE.Color ) data.color = this.color.getHex();
  78. if ( this.emissive instanceof THREE.Color ) data.emissive = this.emissive.getHex();
  79. if ( this.specular instanceof THREE.Color ) data.specular = this.specular.getHex();
  80. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  81. if ( this.map instanceof THREE.Texture ) data.map = this.map.toJSON( meta ).uuid;
  82. if ( this.alphaMap instanceof THREE.Texture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  83. if ( this.lightMap instanceof THREE.Texture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
  84. if ( this.bumpMap instanceof THREE.Texture ) {
  85. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  86. data.bumpScale = this.bumpScale;
  87. }
  88. if ( this.normalMap instanceof THREE.Texture ) {
  89. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  90. data.normalScale = this.normalScale; // Removed for now, causes issue in editor ui.js
  91. }
  92. if ( this.specularMap instanceof THREE.Texture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  93. if ( this.envMap instanceof THREE.Texture ) {
  94. data.envMap = this.envMap.toJSON( meta ).uuid;
  95. data.reflectivity = this.reflectivity; // Scale behind envMap
  96. }
  97. if ( this.size !== undefined ) data.size = this.size;
  98. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  99. if ( this.vertexColors !== undefined && this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
  100. if ( this.shading !== undefined && this.shading !== THREE.SmoothShading ) data.shading = this.shading;
  101. if ( this.blending !== undefined && this.blending !== THREE.NormalBlending ) data.blending = this.blending;
  102. if ( this.side !== undefined && this.side !== THREE.FrontSide ) data.side = this.side;
  103. if ( this.opacity < 1 ) data.opacity = this.opacity;
  104. if ( this.transparent === true ) data.transparent = this.transparent;
  105. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  106. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  107. return data;
  108. },
  109. clone: function () {
  110. return new this.constructor().copy( this );
  111. },
  112. copy: function ( source ) {
  113. this.name = source.name;
  114. this.side = source.side;
  115. this.opacity = source.opacity;
  116. this.transparent = source.transparent;
  117. this.blending = source.blending;
  118. this.blendSrc = source.blendSrc;
  119. this.blendDst = source.blendDst;
  120. this.blendEquation = source.blendEquation;
  121. this.blendSrcAlpha = source.blendSrcAlpha;
  122. this.blendDstAlpha = source.blendDstAlpha;
  123. this.blendEquationAlpha = source.blendEquationAlpha;
  124. this.depthFunc = source.depthFunc;
  125. this.depthTest = source.depthTest;
  126. this.depthWrite = source.depthWrite;
  127. this.precision = source.precision;
  128. this.polygonOffset = source.polygonOffset;
  129. this.polygonOffsetFactor = source.polygonOffsetFactor;
  130. this.polygonOffsetUnits = source.polygonOffsetUnits;
  131. this.alphaTest = source.alphaTest;
  132. this.overdraw = source.overdraw;
  133. this.visible = source.visible;
  134. return this;
  135. },
  136. update: function () {
  137. this.dispatchEvent( { type: 'update' } );
  138. },
  139. dispose: function () {
  140. this.dispatchEvent( { type: 'dispose' } );
  141. }
  142. };
  143. THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );
  144. THREE.MaterialIdCount = 0;