Material.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.Material = function () {
  6. Object.defineProperty( this, 'id', { value: THREE.MaterialIdCount ++ } );
  7. this.uuid = THREE.Math.generateUUID();
  8. this.name = '';
  9. this.type = 'Material';
  10. this.side = THREE.FrontSide;
  11. this.opacity = 1;
  12. this.transparent = false;
  13. this.blending = THREE.NormalBlending;
  14. this.blendSrc = THREE.SrcAlphaFactor;
  15. this.blendDst = THREE.OneMinusSrcAlphaFactor;
  16. this.blendEquation = THREE.AddEquation;
  17. this.blendSrcAlpha = null;
  18. this.blendDstAlpha = null;
  19. this.blendEquationAlpha = null;
  20. this.depthFunc = THREE.LessEqualDepth;
  21. this.depthTest = true;
  22. this.depthWrite = true;
  23. this.clippingPlanes = null;
  24. this.clipShadows = false;
  25. this.colorWrite = true;
  26. this.precision = null; // override the renderer's default precision for this material
  27. this.polygonOffset = false;
  28. this.polygonOffsetFactor = 0;
  29. this.polygonOffsetUnits = 0;
  30. this.alphaTest = 0;
  31. this.premultipliedAlpha = false;
  32. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  33. this.visible = true;
  34. this._needsUpdate = true;
  35. };
  36. THREE.Material.prototype = {
  37. constructor: THREE.Material,
  38. get needsUpdate () {
  39. return this._needsUpdate;
  40. },
  41. set needsUpdate ( value ) {
  42. if ( value === true ) this.update();
  43. this._needsUpdate = value;
  44. },
  45. setValues: function ( values ) {
  46. if ( values === undefined ) return;
  47. for ( var key in values ) {
  48. var newValue = values[ key ];
  49. if ( newValue === undefined ) {
  50. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  51. continue;
  52. }
  53. var currentValue = this[ key ];
  54. if ( currentValue === undefined ) {
  55. console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
  56. continue;
  57. }
  58. if ( currentValue instanceof THREE.Color ) {
  59. currentValue.set( newValue );
  60. } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
  61. currentValue.copy( newValue );
  62. } else if ( key === 'overdraw' ) {
  63. // ensure overdraw is backwards-compatible with legacy boolean type
  64. this[ key ] = Number( newValue );
  65. } else {
  66. this[ key ] = newValue;
  67. }
  68. }
  69. },
  70. toJSON: function ( meta ) {
  71. var isRoot = meta === undefined;
  72. if ( isRoot ) {
  73. meta = {
  74. textures: {},
  75. images: {}
  76. };
  77. }
  78. var data = {
  79. metadata: {
  80. version: 4.4,
  81. type: 'Material',
  82. generator: 'Material.toJSON'
  83. }
  84. };
  85. // standard Material serialization
  86. data.uuid = this.uuid;
  87. data.type = this.type;
  88. if ( this.name !== '' ) data.name = this.name;
  89. if ( this.color instanceof THREE.Color ) data.color = this.color.getHex();
  90. if ( this.roughness !== 0.5 ) data.roughness = this.roughness;
  91. if ( this.metalness !== 0.5 ) data.metalness = this.metalness;
  92. if ( this.emissive instanceof THREE.Color ) data.emissive = this.emissive.getHex();
  93. if ( this.specular instanceof THREE.Color ) data.specular = this.specular.getHex();
  94. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  95. if ( this.map instanceof THREE.Texture ) data.map = this.map.toJSON( meta ).uuid;
  96. if ( this.alphaMap instanceof THREE.Texture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  97. if ( this.lightMap instanceof THREE.Texture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
  98. if ( this.bumpMap instanceof THREE.Texture ) {
  99. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  100. data.bumpScale = this.bumpScale;
  101. }
  102. if ( this.normalMap instanceof THREE.Texture ) {
  103. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  104. data.normalScale = this.normalScale.toArray();
  105. }
  106. if ( this.displacementMap instanceof THREE.Texture ) {
  107. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  108. data.displacementScale = this.displacementScale;
  109. data.displacementBias = this.displacementBias;
  110. }
  111. if ( this.roughnessMap instanceof THREE.Texture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  112. if ( this.metalnessMap instanceof THREE.Texture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  113. if ( this.emissiveMap instanceof THREE.Texture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  114. if ( this.specularMap instanceof THREE.Texture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  115. if ( this.envMap instanceof THREE.Texture ) {
  116. data.envMap = this.envMap.toJSON( meta ).uuid;
  117. data.reflectivity = this.reflectivity; // Scale behind envMap
  118. }
  119. if ( this.size !== undefined ) data.size = this.size;
  120. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  121. if ( this.vertexColors !== undefined && this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
  122. if ( this.shading !== undefined && this.shading !== THREE.SmoothShading ) data.shading = this.shading;
  123. if ( this.blending !== undefined && this.blending !== THREE.NormalBlending ) data.blending = this.blending;
  124. if ( this.side !== undefined && this.side !== THREE.FrontSide ) data.side = this.side;
  125. if ( this.opacity < 1 ) data.opacity = this.opacity;
  126. if ( this.transparent === true ) data.transparent = this.transparent;
  127. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  128. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
  129. if ( this.wireframe === true ) data.wireframe = this.wireframe;
  130. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  131. // TODO: Copied from Object3D.toJSON
  132. function extractFromCache ( cache ) {
  133. var values = [];
  134. for ( var key in cache ) {
  135. var data = cache[ key ];
  136. delete data.metadata;
  137. values.push( data );
  138. }
  139. return values;
  140. }
  141. if ( isRoot ) {
  142. var textures = extractFromCache( meta.textures );
  143. var images = extractFromCache( meta.images );
  144. if ( textures.length > 0 ) data.textures = textures;
  145. if ( images.length > 0 ) data.images = images;
  146. }
  147. return data;
  148. },
  149. clone: function () {
  150. return new this.constructor().copy( this );
  151. },
  152. copy: function ( source ) {
  153. this.name = source.name;
  154. this.side = source.side;
  155. this.opacity = source.opacity;
  156. this.transparent = source.transparent;
  157. this.blending = source.blending;
  158. this.blendSrc = source.blendSrc;
  159. this.blendDst = source.blendDst;
  160. this.blendEquation = source.blendEquation;
  161. this.blendSrcAlpha = source.blendSrcAlpha;
  162. this.blendDstAlpha = source.blendDstAlpha;
  163. this.blendEquationAlpha = source.blendEquationAlpha;
  164. this.depthFunc = source.depthFunc;
  165. this.depthTest = source.depthTest;
  166. this.depthWrite = source.depthWrite;
  167. this.colorWrite = source.colorWrite;
  168. this.precision = source.precision;
  169. this.polygonOffset = source.polygonOffset;
  170. this.polygonOffsetFactor = source.polygonOffsetFactor;
  171. this.polygonOffsetUnits = source.polygonOffsetUnits;
  172. this.alphaTest = source.alphaTest;
  173. this.premultipliedAlpha = source.premultipliedAlpha;
  174. this.overdraw = source.overdraw;
  175. this.visible = source.visible;
  176. this.clipShadows = source.clipShadows;
  177. var srcPlanes = source.clippingPlanes,
  178. dstPlanes = null;
  179. if ( srcPlanes !== null ) {
  180. var n = srcPlanes.length;
  181. dstPlanes = new Array( n );
  182. for ( var i = 0; i !== n; ++ i )
  183. dstPlanes[ i ] = srcPlanes[ i ].clone();
  184. }
  185. this.clippingPlanes = dstPlanes;
  186. return this;
  187. },
  188. update: function () {
  189. this.dispatchEvent( { type: 'update' } );
  190. },
  191. dispose: function () {
  192. this.dispatchEvent( { type: 'dispose' } );
  193. }
  194. };
  195. THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );
  196. THREE.MaterialIdCount = 0;