Material.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import { EventDispatcher } from '../core/EventDispatcher';
  2. import { NoColors, FrontSide, FlatShading, 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.flatShading = false;
  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.dithering = false;
  40. this.alphaTest = 0;
  41. this.premultipliedAlpha = false;
  42. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  43. this.visible = true;
  44. this.userData = {};
  45. this.needsUpdate = true;
  46. }
  47. Object.assign( Material.prototype, EventDispatcher.prototype, {
  48. isMaterial: true,
  49. onBeforeCompile: function () {},
  50. setValues: function ( values ) {
  51. if ( values === undefined ) return;
  52. for ( var key in values ) {
  53. var newValue = values[ key ];
  54. if ( newValue === undefined ) {
  55. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  56. continue;
  57. }
  58. // for backward compatability if shading is set in the constructor
  59. if ( key === 'shading' ) {
  60. console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
  61. this.flatShading = ( newValue === FlatShading ) ? true : false;
  62. continue;
  63. }
  64. var currentValue = this[ key ];
  65. if ( currentValue === undefined ) {
  66. console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
  67. continue;
  68. }
  69. if ( currentValue && currentValue.isColor ) {
  70. currentValue.set( newValue );
  71. } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
  72. currentValue.copy( newValue );
  73. } else if ( key === 'overdraw' ) {
  74. // ensure overdraw is backwards-compatible with legacy boolean type
  75. this[ key ] = Number( newValue );
  76. } else {
  77. this[ key ] = newValue;
  78. }
  79. }
  80. },
  81. toJSON: function ( meta ) {
  82. var isRoot = meta === undefined;
  83. if ( isRoot ) {
  84. meta = {
  85. textures: {},
  86. images: {}
  87. };
  88. }
  89. var data = {
  90. metadata: {
  91. version: 4.5,
  92. type: 'Material',
  93. generator: 'Material.toJSON'
  94. }
  95. };
  96. // standard Material serialization
  97. data.uuid = this.uuid;
  98. data.type = this.type;
  99. if ( this.name !== '' ) data.name = this.name;
  100. if ( this.color && this.color.isColor ) data.color = this.color.getHex();
  101. if ( this.roughness !== undefined ) data.roughness = this.roughness;
  102. if ( this.metalness !== undefined ) data.metalness = this.metalness;
  103. if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex();
  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.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  110. if ( this.lightMap && this.lightMap.isTexture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
  111. if ( this.bumpMap && this.bumpMap.isTexture ) {
  112. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  113. data.bumpScale = this.bumpScale;
  114. }
  115. if ( this.normalMap && this.normalMap.isTexture ) {
  116. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  117. data.normalScale = this.normalScale.toArray();
  118. }
  119. if ( this.displacementMap && this.displacementMap.isTexture ) {
  120. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  121. data.displacementScale = this.displacementScale;
  122. data.displacementBias = this.displacementBias;
  123. }
  124. if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  125. if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  126. if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  127. if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  128. if ( this.envMap && this.envMap.isTexture ) {
  129. data.envMap = this.envMap.toJSON( meta ).uuid;
  130. data.reflectivity = this.reflectivity; // Scale behind envMap
  131. }
  132. if ( this.gradientMap && this.gradientMap.isTexture ) {
  133. data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
  134. }
  135. if ( this.size !== undefined ) data.size = this.size;
  136. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  137. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  138. if ( this.flatShading === true ) data.flatShading = this.flatShading;
  139. if ( this.side !== FrontSide ) data.side = this.side;
  140. if ( this.vertexColors !== NoColors ) data.vertexColors = this.vertexColors;
  141. if ( this.opacity < 1 ) data.opacity = this.opacity;
  142. if ( this.transparent === true ) data.transparent = this.transparent;
  143. data.depthFunc = this.depthFunc;
  144. data.depthTest = this.depthTest;
  145. data.depthWrite = this.depthWrite;
  146. if ( this.dithering === true ) data.dithering = true;
  147. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  148. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  149. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  150. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  151. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  152. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  153. if ( this.morphTargets === true ) data.morphTargets = true;
  154. if ( this.skinning === true ) data.skinning = true;
  155. if ( this.visible === false ) data.visible = false;
  156. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  157. // TODO: Copied from Object3D.toJSON
  158. function extractFromCache( cache ) {
  159. var values = [];
  160. for ( var key in cache ) {
  161. var data = cache[ key ];
  162. delete data.metadata;
  163. values.push( data );
  164. }
  165. return values;
  166. }
  167. if ( isRoot ) {
  168. var textures = extractFromCache( meta.textures );
  169. var images = extractFromCache( meta.images );
  170. if ( textures.length > 0 ) data.textures = textures;
  171. if ( images.length > 0 ) data.images = images;
  172. }
  173. return data;
  174. },
  175. clone: function () {
  176. return new this.constructor().copy( this );
  177. },
  178. copy: function ( source ) {
  179. this.name = source.name;
  180. this.fog = source.fog;
  181. this.lights = source.lights;
  182. this.blending = source.blending;
  183. this.side = source.side;
  184. this.flatShading = source.flatShading;
  185. this.vertexColors = source.vertexColors;
  186. this.opacity = source.opacity;
  187. this.transparent = source.transparent;
  188. this.blendSrc = source.blendSrc;
  189. this.blendDst = source.blendDst;
  190. this.blendEquation = source.blendEquation;
  191. this.blendSrcAlpha = source.blendSrcAlpha;
  192. this.blendDstAlpha = source.blendDstAlpha;
  193. this.blendEquationAlpha = source.blendEquationAlpha;
  194. this.depthFunc = source.depthFunc;
  195. this.depthTest = source.depthTest;
  196. this.depthWrite = source.depthWrite;
  197. this.colorWrite = source.colorWrite;
  198. this.precision = source.precision;
  199. this.polygonOffset = source.polygonOffset;
  200. this.polygonOffsetFactor = source.polygonOffsetFactor;
  201. this.polygonOffsetUnits = source.polygonOffsetUnits;
  202. this.dithering = source.dithering;
  203. this.alphaTest = source.alphaTest;
  204. this.premultipliedAlpha = source.premultipliedAlpha;
  205. this.overdraw = source.overdraw;
  206. this.visible = source.visible;
  207. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  208. this.clipShadows = source.clipShadows;
  209. this.clipIntersection = source.clipIntersection;
  210. var srcPlanes = source.clippingPlanes,
  211. dstPlanes = null;
  212. if ( srcPlanes !== null ) {
  213. var n = srcPlanes.length;
  214. dstPlanes = new Array( n );
  215. for ( var i = 0; i !== n; ++ i )
  216. dstPlanes[ i ] = srcPlanes[ i ].clone();
  217. }
  218. this.clippingPlanes = dstPlanes;
  219. return this;
  220. },
  221. dispose: function () {
  222. this.dispatchEvent( { type: 'dispose' } );
  223. }
  224. } );
  225. export { Material };
粤ICP备19079148号