Material.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.premultipliedAlpha = false;
  30. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  31. this.visible = true;
  32. this._needsUpdate = true;
  33. };
  34. THREE.Material.prototype = {
  35. constructor: THREE.Material,
  36. get needsUpdate () {
  37. return this._needsUpdate;
  38. },
  39. set needsUpdate ( value ) {
  40. if ( value === true ) this.update();
  41. this._needsUpdate = value;
  42. },
  43. setValues: function ( values ) {
  44. if ( values === undefined ) return;
  45. for ( var key in values ) {
  46. var newValue = values[ key ];
  47. if ( newValue === undefined ) {
  48. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  49. continue;
  50. }
  51. var currentValue = this[ key ];
  52. if ( currentValue === undefined ) {
  53. console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
  54. continue;
  55. }
  56. if ( currentValue instanceof THREE.Color ) {
  57. currentValue.set( newValue );
  58. } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
  59. currentValue.copy( newValue );
  60. } else if ( key === 'overdraw' ) {
  61. // ensure overdraw is backwards-compatible with legacy boolean type
  62. this[ key ] = Number( newValue );
  63. } else {
  64. this[ key ] = newValue;
  65. }
  66. }
  67. },
  68. toJSON: function ( meta ) {
  69. var isRoot = meta === undefined;
  70. if ( isRoot ) {
  71. meta = {
  72. textures: {},
  73. images: {}
  74. };
  75. }
  76. var data = {
  77. metadata: {
  78. version: 4.4,
  79. type: 'Material',
  80. generator: 'Material.toJSON'
  81. }
  82. };
  83. // standard Material serialization
  84. data.uuid = this.uuid;
  85. data.type = this.type;
  86. if ( this.name !== '' ) data.name = this.name;
  87. if ( this.color instanceof THREE.Color ) data.color = this.color.getHex();
  88. if ( this.roughness !== 0.5 ) data.roughness = this.roughness;
  89. if ( this.metalness !== 0.5 ) data.metalness = this.metalness;
  90. if ( this.emissive instanceof THREE.Color ) data.emissive = this.emissive.getHex();
  91. if ( this.specular instanceof THREE.Color ) data.specular = this.specular.getHex();
  92. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  93. if ( this.map instanceof THREE.Texture ) data.map = this.map.toJSON( meta ).uuid;
  94. if ( this.alphaMap instanceof THREE.Texture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  95. if ( this.lightMap instanceof THREE.Texture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
  96. if ( this.bumpMap instanceof THREE.Texture ) {
  97. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  98. data.bumpScale = this.bumpScale;
  99. }
  100. if ( this.normalMap instanceof THREE.Texture ) {
  101. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  102. data.normalScale = this.normalScale.toArray();
  103. }
  104. if ( this.displacementMap instanceof THREE.Texture ) {
  105. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  106. data.displacementScale = this.displacementScale;
  107. data.displacementBias = this.displacementBias;
  108. }
  109. if ( this.roughnessMap instanceof THREE.Texture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  110. if ( this.metalnessMap instanceof THREE.Texture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  111. if ( this.emissiveMap instanceof THREE.Texture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  112. if ( this.specularMap instanceof THREE.Texture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  113. if ( this.envMap instanceof THREE.Texture ) {
  114. data.envMap = this.envMap.toJSON( meta ).uuid;
  115. data.reflectivity = this.reflectivity; // Scale behind envMap
  116. }
  117. if ( this.size !== undefined ) data.size = this.size;
  118. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  119. if ( this.vertexColors !== undefined && this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
  120. if ( this.shading !== undefined && this.shading !== THREE.SmoothShading ) data.shading = this.shading;
  121. if ( this.blending !== undefined && this.blending !== THREE.NormalBlending ) data.blending = this.blending;
  122. if ( this.side !== undefined && this.side !== THREE.FrontSide ) data.side = this.side;
  123. if ( this.opacity < 1 ) data.opacity = this.opacity;
  124. if ( this.transparent === true ) data.transparent = this.transparent;
  125. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  126. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  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.colorWrite = source.colorWrite;
  166. this.precision = source.precision;
  167. this.polygonOffset = source.polygonOffset;
  168. this.polygonOffsetFactor = source.polygonOffsetFactor;
  169. this.polygonOffsetUnits = source.polygonOffsetUnits;
  170. this.alphaTest = source.alphaTest;
  171. this.premultipliedAlpha = source.premultipliedAlpha;
  172. this.overdraw = source.overdraw;
  173. this.visible = source.visible;
  174. return this;
  175. },
  176. update: function () {
  177. this.dispatchEvent( { type: 'update' } );
  178. },
  179. dispose: function () {
  180. this.dispatchEvent( { type: 'dispose' } );
  181. }
  182. };
  183. THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );
  184. THREE.MaterialIdCount = 0;
粤ICP备19079148号