Material.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import { EventDispatcher } from '../core/EventDispatcher.js';
  2. import { NoColors, FrontSide, FlatShading, NormalBlending, LessEqualDepth, AddEquation, OneMinusSrcAlphaFactor, SrcAlphaFactor, AlwaysStencilFunc, KeepStencilOp } 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.stencilFunc = AlwaysStencilFunc;
  33. this.stencilRef = 0;
  34. this.stencilMask = 0xff;
  35. this.stencilFail = KeepStencilOp;
  36. this.stencilZFail = KeepStencilOp;
  37. this.stencilZPass = KeepStencilOp;
  38. this.stencilWrite = false;
  39. this.clippingPlanes = null;
  40. this.clipIntersection = false;
  41. this.clipShadows = false;
  42. this.shadowSide = null;
  43. this.colorWrite = true;
  44. this.precision = null; // override the renderer's default precision for this material
  45. this.polygonOffset = false;
  46. this.polygonOffsetFactor = 0;
  47. this.polygonOffsetUnits = 0;
  48. this.dithering = false;
  49. this.alphaTest = 0;
  50. this.premultipliedAlpha = false;
  51. this.visible = true;
  52. this.userData = {};
  53. this.needsUpdate = true;
  54. }
  55. Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
  56. constructor: Material,
  57. isMaterial: true,
  58. onBeforeCompile: function () {},
  59. setValues: function ( values ) {
  60. if ( values === undefined ) return;
  61. for ( var key in values ) {
  62. var newValue = values[ key ];
  63. if ( newValue === undefined ) {
  64. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  65. continue;
  66. }
  67. // for backward compatability if shading is set in the constructor
  68. if ( key === 'shading' ) {
  69. console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
  70. this.flatShading = ( newValue === FlatShading ) ? true : false;
  71. continue;
  72. }
  73. var currentValue = this[ key ];
  74. if ( currentValue === undefined ) {
  75. console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
  76. continue;
  77. }
  78. if ( currentValue && currentValue.isColor ) {
  79. currentValue.set( newValue );
  80. } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
  81. currentValue.copy( newValue );
  82. } else {
  83. this[ key ] = newValue;
  84. }
  85. }
  86. },
  87. toJSON: function ( meta ) {
  88. var isRoot = ( meta === undefined || typeof meta === 'string' );
  89. if ( isRoot ) {
  90. meta = {
  91. textures: {},
  92. images: {}
  93. };
  94. }
  95. var data = {
  96. metadata: {
  97. version: 4.5,
  98. type: 'Material',
  99. generator: 'Material.toJSON'
  100. }
  101. };
  102. // standard Material serialization
  103. data.uuid = this.uuid;
  104. data.type = this.type;
  105. if ( this.name !== '' ) data.name = this.name;
  106. if ( this.color && this.color.isColor ) data.color = this.color.getHex();
  107. if ( this.roughness !== undefined ) data.roughness = this.roughness;
  108. if ( this.metalness !== undefined ) data.metalness = this.metalness;
  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.clearCoatNormalMap && this.clearCoatNormalMap.isTexture ) {
  116. data.clearCoatNormalMap = this.clearCoatNormalMap.toJSON( meta ).uuid;
  117. data.clearCoatNormalScale = this.clearCoatNormalScale.toArray();
  118. }
  119. if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid;
  120. if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid;
  121. if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  122. if ( this.lightMap && this.lightMap.isTexture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
  123. if ( this.aoMap && this.aoMap.isTexture ) {
  124. data.aoMap = this.aoMap.toJSON( meta ).uuid;
  125. data.aoMapIntensity = this.aoMapIntensity;
  126. }
  127. if ( this.bumpMap && this.bumpMap.isTexture ) {
  128. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  129. data.bumpScale = this.bumpScale;
  130. }
  131. if ( this.normalMap && this.normalMap.isTexture ) {
  132. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  133. data.normalMapType = this.normalMapType;
  134. data.normalScale = this.normalScale.toArray();
  135. }
  136. if ( this.displacementMap && this.displacementMap.isTexture ) {
  137. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  138. data.displacementScale = this.displacementScale;
  139. data.displacementBias = this.displacementBias;
  140. }
  141. if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  142. if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  143. if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  144. if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  145. if ( this.envMap && this.envMap.isTexture ) {
  146. data.envMap = this.envMap.toJSON( meta ).uuid;
  147. data.reflectivity = this.reflectivity; // Scale behind envMap
  148. data.refractionRatio = this.refractionRatio;
  149. if ( this.combine !== undefined ) data.combine = this.combine;
  150. if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity;
  151. }
  152. if ( this.gradientMap && this.gradientMap.isTexture ) {
  153. data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
  154. }
  155. if ( this.size !== undefined ) data.size = this.size;
  156. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  157. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  158. if ( this.flatShading === true ) data.flatShading = this.flatShading;
  159. if ( this.side !== FrontSide ) data.side = this.side;
  160. if ( this.vertexColors !== NoColors ) data.vertexColors = this.vertexColors;
  161. if ( this.opacity < 1 ) data.opacity = this.opacity;
  162. if ( this.transparent === true ) data.transparent = this.transparent;
  163. data.depthFunc = this.depthFunc;
  164. data.depthTest = this.depthTest;
  165. data.depthWrite = this.depthWrite;
  166. data.stencilWrite = this.stencilWrite;
  167. data.stencilFunc = this.stencilFunc;
  168. data.stencilRef = this.stencilRef;
  169. data.stencilMask = this.stencilMask;
  170. data.stencilFail = this.stencilFail;
  171. data.stencilZFail = this.stencilZFail;
  172. data.stencilZPass = this.stencilZPass;
  173. // rotation (SpriteMaterial)
  174. if ( this.rotation && this.rotation !== 0 ) data.rotation = this.rotation;
  175. if ( this.polygonOffset === true ) data.polygonOffset = true;
  176. if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor;
  177. if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits;
  178. if ( this.linewidth && this.linewidth !== 1 ) data.linewidth = this.linewidth;
  179. if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
  180. if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
  181. if ( this.scale !== undefined ) data.scale = this.scale;
  182. if ( this.dithering === true ) data.dithering = true;
  183. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  184. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  185. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  186. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  187. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  188. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  189. if ( this.morphTargets === true ) data.morphTargets = true;
  190. if ( this.morphNormals === true ) data.morphNormals = true;
  191. if ( this.skinning === true ) data.skinning = true;
  192. if ( this.visible === false ) data.visible = false;
  193. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  194. // TODO: Copied from Object3D.toJSON
  195. function extractFromCache( cache ) {
  196. var values = [];
  197. for ( var key in cache ) {
  198. var data = cache[ key ];
  199. delete data.metadata;
  200. values.push( data );
  201. }
  202. return values;
  203. }
  204. if ( isRoot ) {
  205. var textures = extractFromCache( meta.textures );
  206. var images = extractFromCache( meta.images );
  207. if ( textures.length > 0 ) data.textures = textures;
  208. if ( images.length > 0 ) data.images = images;
  209. }
  210. return data;
  211. },
  212. clone: function () {
  213. return new this.constructor().copy( this );
  214. },
  215. copy: function ( source ) {
  216. this.name = source.name;
  217. this.fog = source.fog;
  218. this.lights = source.lights;
  219. this.blending = source.blending;
  220. this.side = source.side;
  221. this.flatShading = source.flatShading;
  222. this.vertexColors = source.vertexColors;
  223. this.opacity = source.opacity;
  224. this.transparent = source.transparent;
  225. this.blendSrc = source.blendSrc;
  226. this.blendDst = source.blendDst;
  227. this.blendEquation = source.blendEquation;
  228. this.blendSrcAlpha = source.blendSrcAlpha;
  229. this.blendDstAlpha = source.blendDstAlpha;
  230. this.blendEquationAlpha = source.blendEquationAlpha;
  231. this.depthFunc = source.depthFunc;
  232. this.depthTest = source.depthTest;
  233. this.depthWrite = source.depthWrite;
  234. this.stencilWrite = source.stencilWrite;
  235. this.stencilFunc = source.stencilFunc;
  236. this.stencilRef = source.stencilRef;
  237. this.stencilMask = source.stencilMask;
  238. this.stencilFail = source.stencilFail;
  239. this.stencilZFail = source.stencilZFail;
  240. this.stencilZPass = source.stencilZPass;
  241. this.colorWrite = source.colorWrite;
  242. this.precision = source.precision;
  243. this.polygonOffset = source.polygonOffset;
  244. this.polygonOffsetFactor = source.polygonOffsetFactor;
  245. this.polygonOffsetUnits = source.polygonOffsetUnits;
  246. this.dithering = source.dithering;
  247. this.alphaTest = source.alphaTest;
  248. this.premultipliedAlpha = source.premultipliedAlpha;
  249. this.visible = source.visible;
  250. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  251. this.clipShadows = source.clipShadows;
  252. this.clipIntersection = source.clipIntersection;
  253. var srcPlanes = source.clippingPlanes,
  254. dstPlanes = null;
  255. if ( srcPlanes !== null ) {
  256. var n = srcPlanes.length;
  257. dstPlanes = new Array( n );
  258. for ( var i = 0; i !== n; ++ i )
  259. dstPlanes[ i ] = srcPlanes[ i ].clone();
  260. }
  261. this.clippingPlanes = dstPlanes;
  262. this.shadowSide = source.shadowSide;
  263. return this;
  264. },
  265. dispose: function () {
  266. this.dispatchEvent( { type: 'dispose' } );
  267. }
  268. } );
  269. export { Material };
粤ICP备19079148号