Material.js 8.7 KB

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