Material.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { EventDispatcher } from '../core/EventDispatcher';
  2. import { NoColors, FrontSide, SmoothShading, NormalBlending, LessEqualDepth, AddEquation, OneMinusSrcAlphaFactor, SrcAlphaFactor } from '../constants';
  3. import { _Math } from '../math/Math';
  4. /**
  5. * @author mrdoob / http://mrdoob.com/
  6. * @author alteredq / http://alteredqualia.com/
  7. */
  8. function Material() {
  9. Object.defineProperty( this, 'id', { value: MaterialIdCount() } );
  10. this.uuid = _Math.generateUUID();
  11. this.name = '';
  12. this.type = 'Material';
  13. this.fog = true;
  14. this.lights = true;
  15. this.blending = NormalBlending;
  16. this.side = FrontSide;
  17. this.shading = SmoothShading; // THREE.FlatShading, THREE.SmoothShading
  18. this.vertexColors = NoColors; // THREE.NoColors, THREE.VertexColors, THREE.FaceColors
  19. this.opacity = 1;
  20. this.transparent = false;
  21. this.blendSrc = SrcAlphaFactor;
  22. this.blendDst = OneMinusSrcAlphaFactor;
  23. this.blendEquation = AddEquation;
  24. this.blendSrcAlpha = null;
  25. this.blendDstAlpha = null;
  26. this.blendEquationAlpha = null;
  27. this.depthFunc = LessEqualDepth;
  28. this.depthTest = true;
  29. this.depthWrite = true;
  30. this.clippingPlanes = null;
  31. this.clipIntersection = false;
  32. this.clipShadows = false;
  33. this.colorWrite = true;
  34. this.precision = null; // override the renderer's default precision for this material
  35. this.polygonOffset = false;
  36. this.polygonOffsetFactor = 0;
  37. this.polygonOffsetUnits = 0;
  38. this.alphaTest = 0;
  39. this.premultipliedAlpha = false;
  40. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  41. this.visible = true;
  42. this._needsUpdate = true;
  43. }
  44. Material.prototype = {
  45. constructor: Material,
  46. isMaterial: true,
  47. get needsUpdate() {
  48. return this._needsUpdate;
  49. },
  50. set needsUpdate( value ) {
  51. if ( value === true ) this.update();
  52. this._needsUpdate = value;
  53. },
  54. setValues: function ( values ) {
  55. if ( values === undefined ) return;
  56. for ( var key in values ) {
  57. var newValue = values[ key ];
  58. if ( newValue === undefined ) {
  59. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  60. continue;
  61. }
  62. var currentValue = this[ key ];
  63. if ( currentValue === undefined ) {
  64. console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
  65. continue;
  66. }
  67. if ( (currentValue && currentValue.isColor) ) {
  68. currentValue.set( newValue );
  69. } else if ( (currentValue && currentValue.isVector3) && (newValue && newValue.isVector3) ) {
  70. currentValue.copy( newValue );
  71. } else if ( key === 'overdraw' ) {
  72. // ensure overdraw is backwards-compatible with legacy boolean type
  73. this[ key ] = Number( newValue );
  74. } else {
  75. this[ key ] = newValue;
  76. }
  77. }
  78. },
  79. toJSON: function ( meta ) {
  80. var isRoot = meta === undefined;
  81. if ( isRoot ) {
  82. meta = {
  83. textures: {},
  84. images: {}
  85. };
  86. }
  87. var data = {
  88. metadata: {
  89. version: 4.4,
  90. type: 'Material',
  91. generator: 'Material.toJSON'
  92. }
  93. };
  94. // standard Material serialization
  95. data.uuid = this.uuid;
  96. data.type = this.type;
  97. if ( this.name !== '' ) data.name = this.name;
  98. if ( (this.color && this.color.isColor) ) data.color = this.color.getHex();
  99. if ( this.roughness !== undefined ) data.roughness = this.roughness;
  100. if ( this.metalness !== undefined ) data.metalness = this.metalness;
  101. if ( (this.emissive && this.emissive.isColor) ) data.emissive = this.emissive.getHex();
  102. if ( (this.specular && this.specular.isColor) ) data.specular = this.specular.getHex();
  103. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  104. if ( (this.map && this.map.isTexture) ) data.map = this.map.toJSON( meta ).uuid;
  105. if ( (this.alphaMap && this.alphaMap.isTexture) ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  106. if ( (this.lightMap && this.lightMap.isTexture) ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
  107. if ( (this.bumpMap && this.bumpMap.isTexture) ) {
  108. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  109. data.bumpScale = this.bumpScale;
  110. }
  111. if ( (this.normalMap && this.normalMap.isTexture) ) {
  112. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  113. data.normalScale = this.normalScale.toArray();
  114. }
  115. if ( (this.displacementMap && this.displacementMap.isTexture) ) {
  116. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  117. data.displacementScale = this.displacementScale;
  118. data.displacementBias = this.displacementBias;
  119. }
  120. if ( (this.roughnessMap && this.roughnessMap.isTexture) ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  121. if ( (this.metalnessMap && this.metalnessMap.isTexture) ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  122. if ( (this.emissiveMap && this.emissiveMap.isTexture) ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  123. if ( (this.specularMap && this.specularMap.isTexture) ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  124. if ( (this.envMap && this.envMap.isTexture) ) {
  125. data.envMap = this.envMap.toJSON( meta ).uuid;
  126. data.reflectivity = this.reflectivity; // Scale behind envMap
  127. }
  128. if ( this.toonMap && this.toonMap.isTexture ) {
  129. data.toonMap = this.toonMap.toJSON( meta ).uuid;
  130. data.toonMapDirectionY = this.toonMapDirectionY;
  131. }
  132. if ( this.size !== undefined ) data.size = this.size;
  133. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  134. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  135. if ( this.shading !== SmoothShading ) data.shading = this.shading;
  136. if ( this.side !== FrontSide ) data.side = this.side;
  137. if ( this.vertexColors !== NoColors ) data.vertexColors = this.vertexColors;
  138. if ( this.opacity < 1 ) data.opacity = this.opacity;
  139. if ( this.transparent === true ) data.transparent = this.transparent;
  140. data.depthFunc = this.depthFunc;
  141. data.depthTest = this.depthTest;
  142. data.depthWrite = this.depthWrite;
  143. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  144. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  145. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  146. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  147. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  148. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  149. data.skinning = this.skinning;
  150. data.morphTargets = this.morphTargets;
  151. // TODO: Copied from Object3D.toJSON
  152. function extractFromCache( cache ) {
  153. var values = [];
  154. for ( var key in cache ) {
  155. var data = cache[ key ];
  156. delete data.metadata;
  157. values.push( data );
  158. }
  159. return values;
  160. }
  161. if ( isRoot ) {
  162. var textures = extractFromCache( meta.textures );
  163. var images = extractFromCache( meta.images );
  164. if ( textures.length > 0 ) data.textures = textures;
  165. if ( images.length > 0 ) data.images = images;
  166. }
  167. return data;
  168. },
  169. clone: function () {
  170. return new this.constructor().copy( this );
  171. },
  172. copy: function ( source ) {
  173. this.name = source.name;
  174. this.fog = source.fog;
  175. this.lights = source.lights;
  176. this.blending = source.blending;
  177. this.side = source.side;
  178. this.shading = source.shading;
  179. this.vertexColors = source.vertexColors;
  180. this.opacity = source.opacity;
  181. this.transparent = source.transparent;
  182. this.blendSrc = source.blendSrc;
  183. this.blendDst = source.blendDst;
  184. this.blendEquation = source.blendEquation;
  185. this.blendSrcAlpha = source.blendSrcAlpha;
  186. this.blendDstAlpha = source.blendDstAlpha;
  187. this.blendEquationAlpha = source.blendEquationAlpha;
  188. this.depthFunc = source.depthFunc;
  189. this.depthTest = source.depthTest;
  190. this.depthWrite = source.depthWrite;
  191. this.colorWrite = source.colorWrite;
  192. this.precision = source.precision;
  193. this.polygonOffset = source.polygonOffset;
  194. this.polygonOffsetFactor = source.polygonOffsetFactor;
  195. this.polygonOffsetUnits = source.polygonOffsetUnits;
  196. this.alphaTest = source.alphaTest;
  197. this.premultipliedAlpha = source.premultipliedAlpha;
  198. this.overdraw = source.overdraw;
  199. this.visible = source.visible;
  200. this.clipShadows = source.clipShadows;
  201. this.clipIntersection = source.clipIntersection;
  202. var srcPlanes = source.clippingPlanes,
  203. dstPlanes = null;
  204. if ( srcPlanes !== null ) {
  205. var n = srcPlanes.length;
  206. dstPlanes = new Array( n );
  207. for ( var i = 0; i !== n; ++ i )
  208. dstPlanes[ i ] = srcPlanes[ i ].clone();
  209. }
  210. this.clippingPlanes = dstPlanes;
  211. return this;
  212. },
  213. update: function () {
  214. this.dispatchEvent( { type: 'update' } );
  215. },
  216. dispose: function () {
  217. this.dispatchEvent( { type: 'dispose' } );
  218. }
  219. };
  220. Object.assign( Material.prototype, EventDispatcher.prototype );
  221. var count = 0;
  222. function MaterialIdCount() { return count++; };
  223. export { MaterialIdCount, Material };
粤ICP备19079148号