Material.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. function Material() {
  6. Object.defineProperty( this, 'id', { value: materialId ++ } );
  7. this.uuid = MathUtils.generateUUID();
  8. this.name = '';
  9. this.type = 'Material';
  10. this.fog = true;
  11. this.blending = NormalBlending;
  12. this.side = FrontSide;
  13. this.vertexColors = false;
  14. this.opacity = 1;
  15. this.transparent = false;
  16. this.blendSrc = SrcAlphaFactor;
  17. this.blendDst = OneMinusSrcAlphaFactor;
  18. this.blendEquation = AddEquation;
  19. this.blendSrcAlpha = null;
  20. this.blendDstAlpha = null;
  21. this.blendEquationAlpha = null;
  22. this.depthFunc = LessEqualDepth;
  23. this.depthTest = true;
  24. this.depthWrite = true;
  25. this.stencilWriteMask = 0xff;
  26. this.stencilFunc = AlwaysStencilFunc;
  27. this.stencilRef = 0;
  28. this.stencilFuncMask = 0xff;
  29. this.stencilFail = KeepStencilOp;
  30. this.stencilZFail = KeepStencilOp;
  31. this.stencilZPass = KeepStencilOp;
  32. this.stencilWrite = false;
  33. this.clippingPlanes = null;
  34. this.clipIntersection = false;
  35. this.clipShadows = false;
  36. this.shadowSide = null;
  37. this.colorWrite = true;
  38. this.precision = null; // override the renderer's default precision for this material
  39. this.polygonOffset = false;
  40. this.polygonOffsetFactor = 0;
  41. this.polygonOffsetUnits = 0;
  42. this.dithering = false;
  43. this.alphaTest = 0;
  44. this.alphaToCoverage = false;
  45. this.premultipliedAlpha = false;
  46. this.visible = true;
  47. this.toneMapped = true;
  48. this.userData = {};
  49. this.version = 0;
  50. }
  51. Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
  52. constructor: Material,
  53. isMaterial: true,
  54. onBeforeCompile: function ( /* shaderobject, renderer */ ) {},
  55. customProgramCacheKey: function () {
  56. return this.onBeforeCompile.toString();
  57. },
  58. setValues: function ( 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: function ( 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.sheen && this.sheen.isColor ) data.sheen = this.sheen.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.shininess !== undefined ) data.shininess = this.shininess;
  113. if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat;
  114. if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness;
  115. if ( this.clearcoatMap && this.clearcoatMap.isTexture ) {
  116. data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid;
  117. }
  118. if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) {
  119. data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid;
  120. }
  121. if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) {
  122. data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid;
  123. data.clearcoatNormalScale = this.clearcoatNormalScale.toArray();
  124. }
  125. if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid;
  126. if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid;
  127. if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  128. if ( this.lightMap && this.lightMap.isTexture ) {
  129. data.lightMap = this.lightMap.toJSON( meta ).uuid;
  130. data.lightMapIntensity = this.lightMapIntensity;
  131. }
  132. if ( this.aoMap && this.aoMap.isTexture ) {
  133. data.aoMap = this.aoMap.toJSON( meta ).uuid;
  134. data.aoMapIntensity = this.aoMapIntensity;
  135. }
  136. if ( this.bumpMap && this.bumpMap.isTexture ) {
  137. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  138. data.bumpScale = this.bumpScale;
  139. }
  140. if ( this.normalMap && this.normalMap.isTexture ) {
  141. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  142. data.normalMapType = this.normalMapType;
  143. data.normalScale = this.normalScale.toArray();
  144. }
  145. if ( this.displacementMap && this.displacementMap.isTexture ) {
  146. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  147. data.displacementScale = this.displacementScale;
  148. data.displacementBias = this.displacementBias;
  149. }
  150. if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  151. if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  152. if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  153. if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  154. if ( this.envMap && this.envMap.isTexture ) {
  155. data.envMap = this.envMap.toJSON( meta ).uuid;
  156. data.reflectivity = this.reflectivity; // Scale behind envMap
  157. data.refractionRatio = this.refractionRatio;
  158. if ( this.combine !== undefined ) data.combine = this.combine;
  159. if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity;
  160. }
  161. if ( this.gradientMap && this.gradientMap.isTexture ) {
  162. data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
  163. }
  164. if ( this.size !== undefined ) data.size = this.size;
  165. if ( this.shadowSide !== null ) data.shadowSide = this.shadowSide;
  166. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  167. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  168. if ( this.side !== FrontSide ) data.side = this.side;
  169. if ( this.vertexColors ) data.vertexColors = true;
  170. if ( this.opacity < 1 ) data.opacity = this.opacity;
  171. if ( this.transparent === true ) data.transparent = this.transparent;
  172. data.depthFunc = this.depthFunc;
  173. data.depthTest = this.depthTest;
  174. data.depthWrite = this.depthWrite;
  175. data.colorWrite = this.colorWrite;
  176. data.stencilWrite = this.stencilWrite;
  177. data.stencilWriteMask = this.stencilWriteMask;
  178. data.stencilFunc = this.stencilFunc;
  179. data.stencilRef = this.stencilRef;
  180. data.stencilFuncMask = this.stencilFuncMask;
  181. data.stencilFail = this.stencilFail;
  182. data.stencilZFail = this.stencilZFail;
  183. data.stencilZPass = this.stencilZPass;
  184. // rotation (SpriteMaterial)
  185. if ( this.rotation && this.rotation !== 0 ) data.rotation = this.rotation;
  186. if ( this.polygonOffset === true ) data.polygonOffset = true;
  187. if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor;
  188. if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits;
  189. if ( this.linewidth && this.linewidth !== 1 ) data.linewidth = this.linewidth;
  190. if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
  191. if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
  192. if ( this.scale !== undefined ) data.scale = this.scale;
  193. if ( this.dithering === true ) data.dithering = true;
  194. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  195. if ( this.alphaToCoverage === true ) data.alphaToCoverage = this.alphaToCoverage;
  196. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  197. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  198. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  199. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  200. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  201. if ( this.morphTargets === true ) data.morphTargets = true;
  202. if ( this.morphNormals === true ) data.morphNormals = true;
  203. if ( this.skinning === true ) data.skinning = true;
  204. if ( this.flatShading === true ) data.flatShading = this.flatShading;
  205. if ( this.visible === false ) data.visible = false;
  206. if ( this.toneMapped === false ) data.toneMapped = false;
  207. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  208. // TODO: Copied from Object3D.toJSON
  209. function extractFromCache( cache ) {
  210. const values = [];
  211. for ( const key in cache ) {
  212. const data = cache[ key ];
  213. delete data.metadata;
  214. values.push( data );
  215. }
  216. return values;
  217. }
  218. if ( isRoot ) {
  219. const textures = extractFromCache( meta.textures );
  220. const images = extractFromCache( meta.images );
  221. if ( textures.length > 0 ) data.textures = textures;
  222. if ( images.length > 0 ) data.images = images;
  223. }
  224. return data;
  225. },
  226. clone: function () {
  227. return new this.constructor().copy( this );
  228. },
  229. copy: function ( source ) {
  230. this.name = source.name;
  231. this.fog = source.fog;
  232. this.blending = source.blending;
  233. this.side = source.side;
  234. this.vertexColors = source.vertexColors;
  235. this.opacity = source.opacity;
  236. this.transparent = source.transparent;
  237. this.blendSrc = source.blendSrc;
  238. this.blendDst = source.blendDst;
  239. this.blendEquation = source.blendEquation;
  240. this.blendSrcAlpha = source.blendSrcAlpha;
  241. this.blendDstAlpha = source.blendDstAlpha;
  242. this.blendEquationAlpha = source.blendEquationAlpha;
  243. this.depthFunc = source.depthFunc;
  244. this.depthTest = source.depthTest;
  245. this.depthWrite = source.depthWrite;
  246. this.stencilWriteMask = source.stencilWriteMask;
  247. this.stencilFunc = source.stencilFunc;
  248. this.stencilRef = source.stencilRef;
  249. this.stencilFuncMask = source.stencilFuncMask;
  250. this.stencilFail = source.stencilFail;
  251. this.stencilZFail = source.stencilZFail;
  252. this.stencilZPass = source.stencilZPass;
  253. this.stencilWrite = source.stencilWrite;
  254. const srcPlanes = source.clippingPlanes;
  255. let dstPlanes = null;
  256. if ( srcPlanes !== null ) {
  257. const n = srcPlanes.length;
  258. dstPlanes = new Array( n );
  259. for ( let i = 0; i !== n; ++ i ) {
  260. dstPlanes[ i ] = srcPlanes[ i ].clone();
  261. }
  262. }
  263. this.clippingPlanes = dstPlanes;
  264. this.clipIntersection = source.clipIntersection;
  265. this.clipShadows = source.clipShadows;
  266. this.shadowSide = source.shadowSide;
  267. this.colorWrite = source.colorWrite;
  268. this.precision = source.precision;
  269. this.polygonOffset = source.polygonOffset;
  270. this.polygonOffsetFactor = source.polygonOffsetFactor;
  271. this.polygonOffsetUnits = source.polygonOffsetUnits;
  272. this.dithering = source.dithering;
  273. this.alphaTest = source.alphaTest;
  274. this.alphaToCoverage = source.alphaToCoverage;
  275. this.premultipliedAlpha = source.premultipliedAlpha;
  276. this.visible = source.visible;
  277. this.toneMapped = source.toneMapped;
  278. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  279. return this;
  280. },
  281. dispose: function () {
  282. this.dispatchEvent( { type: 'dispose' } );
  283. }
  284. } );
  285. Object.defineProperty( Material.prototype, 'needsUpdate', {
  286. set: function ( value ) {
  287. if ( value === true ) this.version ++;
  288. }
  289. } );
  290. export { Material };
粤ICP备19079148号