Material.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.stencilTest = false;
  24. this.stencilWrite = false;
  25. this.colorWrite = true;
  26. this.precision = null; // override the renderer's default precision for this material
  27. this.polygonOffset = false;
  28. this.polygonOffsetFactor = 0;
  29. this.polygonOffsetUnits = 0;
  30. this.alphaTest = 0;
  31. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  32. this.visible = true;
  33. this._needsUpdate = true;
  34. };
  35. THREE.Material.prototype = {
  36. constructor: THREE.Material,
  37. get needsUpdate () {
  38. return this._needsUpdate;
  39. },
  40. set needsUpdate ( value ) {
  41. if ( value === true ) this.update();
  42. this._needsUpdate = value;
  43. },
  44. setValues: function ( values ) {
  45. if ( values === undefined ) return;
  46. for ( var key in values ) {
  47. var newValue = values[ key ];
  48. if ( newValue === undefined ) {
  49. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  50. continue;
  51. }
  52. var currentValue = this[ key ];
  53. if ( currentValue === undefined ) {
  54. console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
  55. continue;
  56. }
  57. if ( currentValue instanceof THREE.Color ) {
  58. currentValue.set( newValue );
  59. } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
  60. currentValue.copy( newValue );
  61. } else if ( key === 'overdraw' ) {
  62. // ensure overdraw is backwards-compatible with legacy boolean type
  63. this[ key ] = Number( newValue );
  64. } else {
  65. this[ key ] = newValue;
  66. }
  67. }
  68. },
  69. toJSON: function ( meta ) {
  70. var isRoot = meta === undefined;
  71. if ( isRoot ) {
  72. meta = {
  73. textures: {},
  74. images: {}
  75. };
  76. }
  77. var data = {
  78. metadata: {
  79. version: 4.4,
  80. type: 'Material',
  81. generator: 'Material.toJSON'
  82. }
  83. };
  84. // standard Material serialization
  85. data.uuid = this.uuid;
  86. data.type = this.type;
  87. if ( this.name !== '' ) data.name = this.name;
  88. if ( this.color instanceof THREE.Color ) data.color = this.color.getHex();
  89. if ( this.roughness !== 0.5 ) data.roughness = this.roughness;
  90. if ( this.metalness > 0 ) data.metalness = this.metalness;
  91. if ( this.emissive instanceof THREE.Color ) data.emissive = this.emissive.getHex();
  92. if ( this.specular instanceof THREE.Color ) data.specular = this.specular.getHex();
  93. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  94. if ( this.map instanceof THREE.Texture ) data.map = this.map.toJSON( meta ).uuid;
  95. if ( this.alphaMap instanceof THREE.Texture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  96. if ( this.lightMap instanceof THREE.Texture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
  97. if ( this.bumpMap instanceof THREE.Texture ) {
  98. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  99. data.bumpScale = this.bumpScale;
  100. }
  101. if ( this.normalMap instanceof THREE.Texture ) {
  102. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  103. data.normalScale = this.normalScale.toArray();
  104. }
  105. if ( this.displacementMap instanceof THREE.Texture ) {
  106. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  107. data.displacementScale = this.displacementScale;
  108. data.displacementBias = this.displacementBias;
  109. }
  110. if ( this.roughnessMap instanceof THREE.Texture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  111. if ( this.metalnessMap instanceof THREE.Texture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  112. if ( this.emissiveMap instanceof THREE.Texture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  113. if ( this.specularMap instanceof THREE.Texture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  114. if ( this.envMap instanceof THREE.Texture ) {
  115. data.envMap = this.envMap.toJSON( meta ).uuid;
  116. data.reflectivity = this.reflectivity; // Scale behind envMap
  117. }
  118. if ( this.size !== undefined ) data.size = this.size;
  119. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  120. if ( this.vertexColors !== undefined && this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
  121. if ( this.shading !== undefined && this.shading !== THREE.SmoothShading ) data.shading = this.shading;
  122. if ( this.blending !== undefined && this.blending !== THREE.NormalBlending ) data.blending = this.blending;
  123. if ( this.side !== undefined && this.side !== THREE.FrontSide ) data.side = this.side;
  124. if ( this.opacity < 1 ) data.opacity = this.opacity;
  125. if ( this.transparent === true ) data.transparent = this.transparent;
  126. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  127. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  128. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  129. // TODO: Copied from Object3D.toJSON
  130. function extractFromCache ( cache ) {
  131. var values = [];
  132. for ( var key in cache ) {
  133. var data = cache[ key ];
  134. delete data.metadata;
  135. values.push( data );
  136. }
  137. return values;
  138. }
  139. if ( isRoot ) {
  140. var textures = extractFromCache( meta.textures );
  141. var images = extractFromCache( meta.images );
  142. if ( textures.length > 0 ) data.textures = textures;
  143. if ( images.length > 0 ) data.images = images;
  144. }
  145. return data;
  146. },
  147. clone: function () {
  148. return new this.constructor().copy( this );
  149. },
  150. copy: function ( source ) {
  151. this.name = source.name;
  152. this.side = source.side;
  153. this.opacity = source.opacity;
  154. this.transparent = source.transparent;
  155. this.blending = source.blending;
  156. this.blendSrc = source.blendSrc;
  157. this.blendDst = source.blendDst;
  158. this.blendEquation = source.blendEquation;
  159. this.blendSrcAlpha = source.blendSrcAlpha;
  160. this.blendDstAlpha = source.blendDstAlpha;
  161. this.blendEquationAlpha = source.blendEquationAlpha;
  162. this.depthFunc = source.depthFunc;
  163. this.depthTest = source.depthTest;
  164. this.depthWrite = source.depthWrite;
  165. this.stencilTest = source.stencilTest;
  166. this.stencilWrite = source.stencilWrite;
  167. this.colorWrite = source.colorWrite;
  168. this.precision = source.precision;
  169. this.polygonOffset = source.polygonOffset;
  170. this.polygonOffsetFactor = source.polygonOffsetFactor;
  171. this.polygonOffsetUnits = source.polygonOffsetUnits;
  172. this.alphaTest = source.alphaTest;
  173. this.overdraw = source.overdraw;
  174. this.visible = source.visible;
  175. return this;
  176. },
  177. update: function () {
  178. this.dispatchEvent( { type: 'update' } );
  179. },
  180. dispose: function () {
  181. this.dispatchEvent( { type: 'dispose' } );
  182. }
  183. };
  184. THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );
  185. THREE.MaterialIdCount = 0;
粤ICP备19079148号