Material.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import { EventDispatcher } from '../core/EventDispatcher.js';
  2. import { FrontSide, FlatShading, NormalBlending, LessEqualDepth, AddEquation, OneMinusSrcAlphaFactor, SrcAlphaFactor, AlwaysStencilFunc, KeepStencilOp } from '../constants.js';
  3. import * as MathUtils from '../math/MathUtils.js';
  4. let materialId = 0;
  5. class Material extends EventDispatcher {
  6. constructor() {
  7. super();
  8. Object.defineProperty( this, 'id', { value: materialId ++ } );
  9. this.uuid = MathUtils.generateUUID();
  10. this.name = '';
  11. this.type = 'Material';
  12. this.fog = true;
  13. this.blending = NormalBlending;
  14. this.side = FrontSide;
  15. this.vertexColors = false;
  16. this.opacity = 1;
  17. this.transparent = false;
  18. this.blendSrc = SrcAlphaFactor;
  19. this.blendDst = OneMinusSrcAlphaFactor;
  20. this.blendEquation = AddEquation;
  21. this.blendSrcAlpha = null;
  22. this.blendDstAlpha = null;
  23. this.blendEquationAlpha = null;
  24. this.depthFunc = LessEqualDepth;
  25. this.depthTest = true;
  26. this.depthWrite = true;
  27. this.stencilWriteMask = 0xff;
  28. this.stencilFunc = AlwaysStencilFunc;
  29. this.stencilRef = 0;
  30. this.stencilFuncMask = 0xff;
  31. this.stencilFail = KeepStencilOp;
  32. this.stencilZFail = KeepStencilOp;
  33. this.stencilZPass = KeepStencilOp;
  34. this.stencilWrite = false;
  35. this.clippingPlanes = null;
  36. this.clipIntersection = false;
  37. this.clipShadows = false;
  38. this.shadowSide = null;
  39. this.colorWrite = true;
  40. this.precision = null; // override the renderer's default precision for this material
  41. this.polygonOffset = false;
  42. this.polygonOffsetFactor = 0;
  43. this.polygonOffsetUnits = 0;
  44. this.dithering = false;
  45. this.alphaTest = 0;
  46. this.alphaToCoverage = false;
  47. this.premultipliedAlpha = false;
  48. this.visible = true;
  49. this.toneMapped = true;
  50. this.userData = {};
  51. this.version = 0;
  52. }
  53. onBuild( /* shaderobject, renderer */ ) {}
  54. onBeforeCompile( /* shaderobject, renderer */ ) {}
  55. customProgramCacheKey() {
  56. return this.onBeforeCompile.toString();
  57. }
  58. setValues( values ) {
  59. if ( values === undefined ) return;
  60. for ( const key in values ) {
  61. const newValue = values[ key ];
  62. if ( newValue === undefined ) {
  63. console.warn( 'THREE.Material: \'' + key + '\' parameter is undefined.' );
  64. continue;
  65. }
  66. // for backward compatability if shading is set in the constructor
  67. if ( key === 'shading' ) {
  68. console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
  69. this.flatShading = ( newValue === FlatShading ) ? true : false;
  70. continue;
  71. }
  72. const currentValue = this[ key ];
  73. if ( currentValue === undefined ) {
  74. console.warn( 'THREE.' + this.type + ': \'' + key + '\' is not a property of this material.' );
  75. continue;
  76. }
  77. if ( currentValue && currentValue.isColor ) {
  78. currentValue.set( newValue );
  79. } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
  80. currentValue.copy( newValue );
  81. } else {
  82. this[ key ] = newValue;
  83. }
  84. }
  85. }
  86. toJSON( meta ) {
  87. const isRoot = ( meta === undefined || typeof meta === 'string' );
  88. if ( isRoot ) {
  89. meta = {
  90. textures: {},
  91. images: {}
  92. };
  93. }
  94. const data = {
  95. metadata: {
  96. version: 4.5,
  97. type: 'Material',
  98. generator: 'Material.toJSON'
  99. }
  100. };
  101. // standard Material serialization
  102. data.uuid = this.uuid;
  103. data.type = this.type;
  104. if ( this.name !== '' ) data.name = this.name;
  105. if ( this.color && this.color.isColor ) data.color = this.color.getHex();
  106. if ( this.roughness !== undefined ) data.roughness = this.roughness;
  107. if ( this.metalness !== undefined ) data.metalness = this.metalness;
  108. if ( this.sheenTint && this.sheenTint.isColor ) data.sheenTint = this.sheenTint.getHex();
  109. if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex();
  110. if ( this.emissiveIntensity && this.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity;
  111. if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex();
  112. if ( this.specularIntensity !== undefined ) data.specularIntensity = this.specularIntensity;
  113. if ( this.specularTint && this.specularTint.isColor ) data.specularTint = this.specularTint.getHex();
  114. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  115. if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat;
  116. if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness;
  117. if ( this.clearcoatMap && this.clearcoatMap.isTexture ) {
  118. data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid;
  119. }
  120. if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) {
  121. data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid;
  122. }
  123. if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) {
  124. data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid;
  125. data.clearcoatNormalScale = this.clearcoatNormalScale.toArray();
  126. }
  127. if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid;
  128. if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid;
  129. if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  130. if ( this.lightMap && this.lightMap.isTexture ) {
  131. data.lightMap = this.lightMap.toJSON( meta ).uuid;
  132. data.lightMapIntensity = this.lightMapIntensity;
  133. }
  134. if ( this.aoMap && this.aoMap.isTexture ) {
  135. data.aoMap = this.aoMap.toJSON( meta ).uuid;
  136. data.aoMapIntensity = this.aoMapIntensity;
  137. }
  138. if ( this.bumpMap && this.bumpMap.isTexture ) {
  139. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  140. data.bumpScale = this.bumpScale;
  141. }
  142. if ( this.normalMap && this.normalMap.isTexture ) {
  143. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  144. data.normalMapType = this.normalMapType;
  145. data.normalScale = this.normalScale.toArray();
  146. }
  147. if ( this.displacementMap && this.displacementMap.isTexture ) {
  148. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  149. data.displacementScale = this.displacementScale;
  150. data.displacementBias = this.displacementBias;
  151. }
  152. if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  153. if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  154. if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  155. if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  156. if ( this.specularIntensityMap && this.specularIntensityMap.isTexture ) data.specularIntensityMap = this.specularIntensityMap.toJSON( meta ).uuid;
  157. if ( this.specularTintMap && this.specularTintMap.isTexture ) data.specularTintMap = this.specularTintMap.toJSON( meta ).uuid;
  158. if ( this.envMap && this.envMap.isTexture ) {
  159. data.envMap = this.envMap.toJSON( meta ).uuid;
  160. if ( this.combine !== undefined ) data.combine = this.combine;
  161. }
  162. if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity;
  163. if ( this.reflectivity !== undefined ) data.reflectivity = this.reflectivity;
  164. if ( this.refractionRatio !== undefined ) data.refractionRatio = this.refractionRatio;
  165. if ( this.gradientMap && this.gradientMap.isTexture ) {
  166. data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
  167. }
  168. if ( this.transmission !== undefined ) data.transmission = this.transmission;
  169. if ( this.transmissionMap && this.transmissionMap.isTexture ) data.transmissionMap = this.transmissionMap.toJSON( meta ).uuid;
  170. if ( this.thickness !== undefined ) data.thickness = this.thickness;
  171. if ( this.thicknessMap && this.thicknessMap.isTexture ) data.thicknessMap = this.thicknessMap.toJSON( meta ).uuid;
  172. if ( this.attenuationDistance !== undefined ) data.attenuationDistance = this.attenuationDistance;
  173. if ( this.attenuationTint !== undefined ) data.attenuationTint = this.attenuationTint.getHex();
  174. if ( this.size !== undefined ) data.size = this.size;
  175. if ( this.shadowSide !== null ) data.shadowSide = this.shadowSide;
  176. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  177. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  178. if ( this.side !== FrontSide ) data.side = this.side;
  179. if ( this.vertexColors ) data.vertexColors = true;
  180. if ( this.opacity < 1 ) data.opacity = this.opacity;
  181. if ( this.transparent === true ) data.transparent = this.transparent;
  182. data.depthFunc = this.depthFunc;
  183. data.depthTest = this.depthTest;
  184. data.depthWrite = this.depthWrite;
  185. data.colorWrite = this.colorWrite;
  186. data.stencilWrite = this.stencilWrite;
  187. data.stencilWriteMask = this.stencilWriteMask;
  188. data.stencilFunc = this.stencilFunc;
  189. data.stencilRef = this.stencilRef;
  190. data.stencilFuncMask = this.stencilFuncMask;
  191. data.stencilFail = this.stencilFail;
  192. data.stencilZFail = this.stencilZFail;
  193. data.stencilZPass = this.stencilZPass;
  194. // rotation (SpriteMaterial)
  195. if ( this.rotation && this.rotation !== 0 ) data.rotation = this.rotation;
  196. if ( this.polygonOffset === true ) data.polygonOffset = true;
  197. if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor;
  198. if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits;
  199. if ( this.linewidth && this.linewidth !== 1 ) data.linewidth = this.linewidth;
  200. if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
  201. if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
  202. if ( this.scale !== undefined ) data.scale = this.scale;
  203. if ( this.dithering === true ) data.dithering = true;
  204. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  205. if ( this.alphaToCoverage === true ) data.alphaToCoverage = this.alphaToCoverage;
  206. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  207. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  208. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  209. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  210. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  211. if ( this.flatShading === true ) data.flatShading = this.flatShading;
  212. if ( this.visible === false ) data.visible = false;
  213. if ( this.toneMapped === false ) data.toneMapped = false;
  214. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  215. // TODO: Copied from Object3D.toJSON
  216. function extractFromCache( cache ) {
  217. const values = [];
  218. for ( const key in cache ) {
  219. const data = cache[ key ];
  220. delete data.metadata;
  221. values.push( data );
  222. }
  223. return values;
  224. }
  225. if ( isRoot ) {
  226. const textures = extractFromCache( meta.textures );
  227. const images = extractFromCache( meta.images );
  228. if ( textures.length > 0 ) data.textures = textures;
  229. if ( images.length > 0 ) data.images = images;
  230. }
  231. return data;
  232. }
  233. clone() {
  234. return new this.constructor().copy( this );
  235. }
  236. copy( source ) {
  237. this.name = source.name;
  238. this.fog = source.fog;
  239. this.blending = source.blending;
  240. this.side = source.side;
  241. this.vertexColors = source.vertexColors;
  242. this.opacity = source.opacity;
  243. this.transparent = source.transparent;
  244. this.blendSrc = source.blendSrc;
  245. this.blendDst = source.blendDst;
  246. this.blendEquation = source.blendEquation;
  247. this.blendSrcAlpha = source.blendSrcAlpha;
  248. this.blendDstAlpha = source.blendDstAlpha;
  249. this.blendEquationAlpha = source.blendEquationAlpha;
  250. this.depthFunc = source.depthFunc;
  251. this.depthTest = source.depthTest;
  252. this.depthWrite = source.depthWrite;
  253. this.stencilWriteMask = source.stencilWriteMask;
  254. this.stencilFunc = source.stencilFunc;
  255. this.stencilRef = source.stencilRef;
  256. this.stencilFuncMask = source.stencilFuncMask;
  257. this.stencilFail = source.stencilFail;
  258. this.stencilZFail = source.stencilZFail;
  259. this.stencilZPass = source.stencilZPass;
  260. this.stencilWrite = source.stencilWrite;
  261. const srcPlanes = source.clippingPlanes;
  262. let dstPlanes = null;
  263. if ( srcPlanes !== null ) {
  264. const n = srcPlanes.length;
  265. dstPlanes = new Array( n );
  266. for ( let i = 0; i !== n; ++ i ) {
  267. dstPlanes[ i ] = srcPlanes[ i ].clone();
  268. }
  269. }
  270. this.clippingPlanes = dstPlanes;
  271. this.clipIntersection = source.clipIntersection;
  272. this.clipShadows = source.clipShadows;
  273. this.shadowSide = source.shadowSide;
  274. this.colorWrite = source.colorWrite;
  275. this.precision = source.precision;
  276. this.polygonOffset = source.polygonOffset;
  277. this.polygonOffsetFactor = source.polygonOffsetFactor;
  278. this.polygonOffsetUnits = source.polygonOffsetUnits;
  279. this.dithering = source.dithering;
  280. this.alphaTest = source.alphaTest;
  281. this.alphaToCoverage = source.alphaToCoverage;
  282. this.premultipliedAlpha = source.premultipliedAlpha;
  283. this.visible = source.visible;
  284. this.toneMapped = source.toneMapped;
  285. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  286. return this;
  287. }
  288. dispose() {
  289. this.dispatchEvent( { type: 'dispose' } );
  290. }
  291. set needsUpdate( value ) {
  292. if ( value === true ) this.version ++;
  293. }
  294. }
  295. Material.prototype.isMaterial = true;
  296. export { Material };