Material.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import { EventDispatcher } from '../core/EventDispatcher.js';
  2. import { NoColors, FrontSide, FlatShading, NormalBlending, LessEqualDepth, AddEquation, OneMinusSrcAlphaFactor, SrcAlphaFactor } from '../constants.js';
  3. import { _Math } from '../math/Math.js';
  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.flatShading = false;
  19. this.vertexTangents = false;
  20. this.vertexColors = NoColors; // THREE.NoColors, THREE.VertexColors, THREE.FaceColors
  21. this.opacity = 1;
  22. this.transparent = false;
  23. this.blendSrc = SrcAlphaFactor;
  24. this.blendDst = OneMinusSrcAlphaFactor;
  25. this.blendEquation = AddEquation;
  26. this.blendSrcAlpha = null;
  27. this.blendDstAlpha = null;
  28. this.blendEquationAlpha = null;
  29. this.depthFunc = LessEqualDepth;
  30. this.depthTest = true;
  31. this.depthWrite = true;
  32. this.clippingPlanes = null;
  33. this.clipIntersection = false;
  34. this.clipShadows = false;
  35. this.shadowSide = null;
  36. this.colorWrite = true;
  37. this.precision = null; // override the renderer's default precision for this material
  38. this.polygonOffset = false;
  39. this.polygonOffsetFactor = 0;
  40. this.polygonOffsetUnits = 0;
  41. this.dithering = false;
  42. this.alphaTest = 0;
  43. this.premultipliedAlpha = false;
  44. this.visible = true;
  45. this.userData = {};
  46. this.needsUpdate = true;
  47. }
  48. Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
  49. constructor: Material,
  50. isMaterial: true,
  51. onBeforeCompile: function () {},
  52. setValues: function ( values ) {
  53. if ( values === undefined ) return;
  54. for ( var key in values ) {
  55. var newValue = values[ key ];
  56. if ( newValue === undefined ) {
  57. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  58. continue;
  59. }
  60. // for backward compatability if shading is set in the constructor
  61. if ( key === 'shading' ) {
  62. console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
  63. this.flatShading = ( newValue === FlatShading ) ? true : false;
  64. continue;
  65. }
  66. var currentValue = this[ key ];
  67. if ( currentValue === undefined ) {
  68. console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
  69. continue;
  70. }
  71. if ( currentValue && currentValue.isColor ) {
  72. currentValue.set( newValue );
  73. } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
  74. currentValue.copy( newValue );
  75. } else {
  76. this[ key ] = newValue;
  77. }
  78. }
  79. },
  80. toJSON: function ( meta ) {
  81. var isRoot = ( meta === undefined || typeof meta === 'string' );
  82. if ( isRoot ) {
  83. meta = {
  84. textures: {},
  85. images: {}
  86. };
  87. }
  88. var data = {
  89. metadata: {
  90. version: 4.5,
  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.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity;
  104. if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex();
  105. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  106. if ( this.clearCoat !== undefined ) data.clearCoat = this.clearCoat;
  107. if ( this.clearCoatRoughness !== undefined ) data.clearCoatRoughness = this.clearCoatRoughness;
  108. if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid;
  109. if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid;
  110. if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  111. if ( this.lightMap && this.lightMap.isTexture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
  112. if ( this.aoMap && this.aoMap.isTexture ) {
  113. data.aoMap = this.aoMap.toJSON( meta ).uuid;
  114. data.aoMapIntensity = this.aoMapIntensity;
  115. }
  116. if ( this.bumpMap && this.bumpMap.isTexture ) {
  117. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  118. data.bumpScale = this.bumpScale;
  119. }
  120. if ( this.normalMap && this.normalMap.isTexture ) {
  121. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  122. data.normalMapType = this.normalMapType;
  123. data.normalScale = this.normalScale.toArray();
  124. }
  125. if ( this.displacementMap && this.displacementMap.isTexture ) {
  126. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  127. data.displacementScale = this.displacementScale;
  128. data.displacementBias = this.displacementBias;
  129. }
  130. if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  131. if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  132. if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  133. if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  134. if ( this.envMap && this.envMap.isTexture ) {
  135. data.envMap = this.envMap.toJSON( meta ).uuid;
  136. data.reflectivity = this.reflectivity; // Scale behind envMap
  137. if ( this.combine !== undefined ) data.combine = this.combine;
  138. if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity;
  139. }
  140. if ( this.gradientMap && this.gradientMap.isTexture ) {
  141. data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
  142. }
  143. if ( this.size !== undefined ) data.size = this.size;
  144. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  145. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  146. if ( this.flatShading === true ) data.flatShading = this.flatShading;
  147. if ( this.side !== FrontSide ) data.side = this.side;
  148. if ( this.vertexColors !== NoColors ) data.vertexColors = this.vertexColors;
  149. if ( this.opacity < 1 ) data.opacity = this.opacity;
  150. if ( this.transparent === true ) data.transparent = this.transparent;
  151. data.depthFunc = this.depthFunc;
  152. data.depthTest = this.depthTest;
  153. data.depthWrite = this.depthWrite;
  154. // rotation (SpriteMaterial)
  155. if ( this.rotation !== 0 ) data.rotation = this.rotation;
  156. if ( this.polygonOffset === true ) data.polygonOffset = true;
  157. if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor;
  158. if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits;
  159. if ( this.linewidth !== 1 ) data.linewidth = this.linewidth;
  160. if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
  161. if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
  162. if ( this.scale !== undefined ) data.scale = this.scale;
  163. if ( this.dithering === true ) data.dithering = true;
  164. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  165. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  166. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  167. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  168. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  169. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  170. if ( this.morphTargets === true ) data.morphTargets = true;
  171. if ( this.skinning === true ) data.skinning = true;
  172. if ( this.visible === false ) data.visible = false;
  173. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  174. // TODO: Copied from Object3D.toJSON
  175. function extractFromCache( cache ) {
  176. var values = [];
  177. for ( var key in cache ) {
  178. var data = cache[ key ];
  179. delete data.metadata;
  180. values.push( data );
  181. }
  182. return values;
  183. }
  184. if ( isRoot ) {
  185. var textures = extractFromCache( meta.textures );
  186. var images = extractFromCache( meta.images );
  187. if ( textures.length > 0 ) data.textures = textures;
  188. if ( images.length > 0 ) data.images = images;
  189. }
  190. return data;
  191. },
  192. clone: function () {
  193. return new this.constructor().copy( this );
  194. },
  195. copy: function ( source ) {
  196. this.name = source.name;
  197. this.fog = source.fog;
  198. this.lights = source.lights;
  199. this.blending = source.blending;
  200. this.side = source.side;
  201. this.flatShading = source.flatShading;
  202. this.vertexColors = source.vertexColors;
  203. this.opacity = source.opacity;
  204. this.transparent = source.transparent;
  205. this.blendSrc = source.blendSrc;
  206. this.blendDst = source.blendDst;
  207. this.blendEquation = source.blendEquation;
  208. this.blendSrcAlpha = source.blendSrcAlpha;
  209. this.blendDstAlpha = source.blendDstAlpha;
  210. this.blendEquationAlpha = source.blendEquationAlpha;
  211. this.depthFunc = source.depthFunc;
  212. this.depthTest = source.depthTest;
  213. this.depthWrite = source.depthWrite;
  214. this.colorWrite = source.colorWrite;
  215. this.precision = source.precision;
  216. this.polygonOffset = source.polygonOffset;
  217. this.polygonOffsetFactor = source.polygonOffsetFactor;
  218. this.polygonOffsetUnits = source.polygonOffsetUnits;
  219. this.dithering = source.dithering;
  220. this.alphaTest = source.alphaTest;
  221. this.premultipliedAlpha = source.premultipliedAlpha;
  222. this.visible = source.visible;
  223. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  224. this.clipShadows = source.clipShadows;
  225. this.clipIntersection = source.clipIntersection;
  226. var srcPlanes = source.clippingPlanes,
  227. dstPlanes = null;
  228. if ( srcPlanes !== null ) {
  229. var n = srcPlanes.length;
  230. dstPlanes = new Array( n );
  231. for ( var i = 0; i !== n; ++ i )
  232. dstPlanes[ i ] = srcPlanes[ i ].clone();
  233. }
  234. this.clippingPlanes = dstPlanes;
  235. this.shadowSide = source.shadowSide;
  236. return this;
  237. },
  238. dispose: function () {
  239. this.dispatchEvent( { type: 'dispose' } );
  240. }
  241. } );
  242. export { Material };