Material.js 6.9 KB

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