MeshLambertMaterial.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import { MultiplyOperation, TangentSpaceNormalMap } from '../constants.js';
  2. import { Material } from './Material.js';
  3. import { Vector2 } from '../math/Vector2.js';
  4. import { Color } from '../math/Color.js';
  5. import { Euler } from '../math/Euler.js';
  6. /**
  7. * A material for non-shiny surfaces, without specular highlights.
  8. *
  9. * The material uses a non-physically based [Lambertian](https://en.wikipedia.org/wiki/Lambertian_reflectance)
  10. * model for calculating reflectance. This can simulate some surfaces (such
  11. * as untreated wood or stone) well, but cannot simulate shiny surfaces with
  12. * specular highlights (such as varnished wood). `MeshLambertMaterial` uses per-fragment
  13. * shading.
  14. *
  15. * Due to the simplicity of the reflectance and illumination models,
  16. * performance will be greater when using this material over the
  17. * {@link MeshPhongMaterial}, {@link MeshStandardMaterial} or
  18. * {@link MeshPhysicalMaterial}, at the cost of some graphical accuracy.
  19. *
  20. * @augments Material
  21. * @demo scenes/material-browser.html#MeshLambertMaterial
  22. */
  23. class MeshLambertMaterial extends Material {
  24. /**
  25. * Constructs a new mesh lambert material.
  26. *
  27. * @param {Object} [parameters] - An object with one or more properties
  28. * defining the material's appearance. Any property of the material
  29. * (including any property from inherited materials) can be passed
  30. * in here. Color values can be passed any type of value accepted
  31. * by {@link Color#set}.
  32. */
  33. constructor( parameters ) {
  34. super();
  35. /**
  36. * This flag can be used for type testing.
  37. *
  38. * @type {boolean}
  39. * @readonly
  40. * @default true
  41. */
  42. this.isMeshLambertMaterial = true;
  43. this.type = 'MeshLambertMaterial';
  44. /**
  45. * Color of the material.
  46. *
  47. * @type {Color}
  48. * @default (1,1,1)
  49. */
  50. this.color = new Color( 0xffffff ); // diffuse
  51. /**
  52. * The color map. May optionally include an alpha channel, typically combined
  53. * with {@link Material#transparent} or {@link Material#alphaTest}. The texture map
  54. * color is modulated by the diffuse `color`.
  55. *
  56. * `map` represents color data, and the texture must be assigned a
  57. * {@link Texture#colorSpace}. Most `map` textures set
  58. * `texture.colorSpace = SRGBColorSpace`.
  59. *
  60. * @type {?Texture}
  61. * @default null
  62. */
  63. this.map = null;
  64. /**
  65. * The light map. Requires a second set of UVs.
  66. *
  67. * `lightMap` represents pre-baked illuminance data, and the texture must be assigned
  68. * a {@link Texture#colorSpace}. Most `lightMap` textures set
  69. * `texture.colorSpace = LinearSRGBColorSpace` and use float-type formats
  70. * such as `.exr` or `.hdr`.
  71. *
  72. * @type {?Texture}
  73. * @default null
  74. */
  75. this.lightMap = null;
  76. /**
  77. * Intensity of the baked light.
  78. *
  79. * @type {number}
  80. * @default 1
  81. */
  82. this.lightMapIntensity = 1.0;
  83. /**
  84. * The red channel of this texture is used as the ambient occlusion map.
  85. * Requires a second set of UVs.
  86. *
  87. * `aoMap` represents non-color data. Any texture assigned must have
  88. * `texture.colorSpace = NoColorSpace` (default).
  89. *
  90. * @type {?Texture}
  91. * @default null
  92. */
  93. this.aoMap = null;
  94. /**
  95. * Intensity of the ambient occlusion effect. Range is `[0,1]`, where `0`
  96. * disables ambient occlusion. Where intensity is `1` and the AO map's
  97. * red channel is also `1`, ambient light is fully occluded on a surface.
  98. *
  99. * @type {number}
  100. * @default 1
  101. */
  102. this.aoMapIntensity = 1.0;
  103. /**
  104. * Emissive (light) color of the material, essentially a solid color
  105. * unaffected by other lighting.
  106. *
  107. * @type {Color}
  108. * @default (0,0,0)
  109. */
  110. this.emissive = new Color( 0x000000 );
  111. /**
  112. * Intensity of the emissive light. Modulates the emissive color.
  113. *
  114. * @type {number}
  115. * @default 1
  116. */
  117. this.emissiveIntensity = 1.0;
  118. /**
  119. * Set emissive (glow) map. The emissive map color is modulated by the
  120. * emissive color and the emissive intensity. If you have an emissive map,
  121. * be sure to set the emissive color to something other than black.
  122. *
  123. * `emissiveMap` represents color data, and the texture must be assigned a
  124. * {@link Texture#colorSpace}. Most `emissiveMap` textures set
  125. * `texture.colorSpace = SRGBColorSpace`.
  126. *
  127. * @type {?Texture}
  128. * @default null
  129. */
  130. this.emissiveMap = null;
  131. /**
  132. * The texture to create a bump map. The black and white values map to the
  133. * perceived depth in relation to the lights. Bump doesn't actually affect
  134. * the geometry of the object, only the lighting. If a normal map is defined
  135. * this will be ignored.
  136. *
  137. * `bumpMap` represents non-color data. Any texture assigned must have
  138. * `texture.colorSpace = NoColorSpace` (default).
  139. *
  140. * @type {?Texture}
  141. * @default null
  142. */
  143. this.bumpMap = null;
  144. /**
  145. * How much the bump map affects the material. Typical range is `[0,1]`.
  146. *
  147. * @type {number}
  148. * @default 1
  149. */
  150. this.bumpScale = 1;
  151. /**
  152. * The texture to create a normal map. The RGB values affect the surface
  153. * normal for each pixel fragment and change the way the color is lit. Normal
  154. * maps do not change the actual shape of the surface, only the lighting. In
  155. * case the material has a normal map authored using the left handed
  156. * convention, the `y` component of `normalScale` should be negated to compensate
  157. * for the different handedness.
  158. *
  159. * `normalMap` represents non-color data. Any texture assigned must have
  160. * `texture.colorSpace = NoColorSpace` (default).
  161. *
  162. * @type {?Texture}
  163. * @default null
  164. */
  165. this.normalMap = null;
  166. /**
  167. * The type of normal map.
  168. *
  169. * @type {(TangentSpaceNormalMap|ObjectSpaceNormalMap)}
  170. * @default TangentSpaceNormalMap
  171. */
  172. this.normalMapType = TangentSpaceNormalMap;
  173. /**
  174. * How much the normal map affects the material. Typical value range is `[0,1]`.
  175. *
  176. * @type {Vector2}
  177. * @default (1,1)
  178. */
  179. this.normalScale = new Vector2( 1, 1 );
  180. /**
  181. * The displacement map affects the position of the mesh's vertices. Unlike
  182. * other maps which only affect the light and shade of the material the
  183. * displaced vertices can cast shadows, block other objects, and otherwise
  184. * act as real geometry. The displacement texture is an image where the value
  185. * of each pixel (white being the highest) is mapped against, and
  186. * repositions, the vertices of the mesh. For best results, pair a
  187. * displacement map with a matching normal map, since the renderer can
  188. * not recompute surface normals from the displaced vertices.
  189. *
  190. * `displacementMap` represents non-color data. Any texture assigned must have
  191. * `texture.colorSpace = NoColorSpace` (default).
  192. *
  193. * @type {?Texture}
  194. * @default null
  195. */
  196. this.displacementMap = null;
  197. /**
  198. * How much the displacement map affects the mesh (where black is no
  199. * displacement, and white is maximum displacement). Without a displacement
  200. * map set, this value is not applied.
  201. *
  202. * @type {number}
  203. * @default 0
  204. */
  205. this.displacementScale = 1;
  206. /**
  207. * The offset of the displacement map's values on the mesh's vertices.
  208. * The bias is added to the scaled sample of the displacement map.
  209. * Without a displacement map set, this value is not applied.
  210. *
  211. * @type {number}
  212. * @default 0
  213. */
  214. this.displacementBias = 0;
  215. /**
  216. * Specular map used by the material.
  217. *
  218. * `specularMap` represents color data, and the texture must be assigned a
  219. * {@link Texture#colorSpace}. Most `specularMap` textures set
  220. * `texture.colorSpace = SRGBColorSpace`.
  221. *
  222. * @type {?Texture}
  223. * @default null
  224. */
  225. this.specularMap = null;
  226. /**
  227. * The alpha map is a grayscale texture that controls the opacity across the
  228. * surface (black: fully transparent; white: fully opaque).
  229. *
  230. * Only the color of the texture is used, ignoring the alpha channel if one
  231. * exists. For RGB and RGBA textures, the renderer will use the green channel
  232. * when sampling this texture due to the extra bit of precision provided for
  233. * green in DXT-compressed and uncompressed RGB 565 formats. Luminance-only and
  234. * luminance/alpha textures will also still work as expected.
  235. *
  236. * `alphaMap` represents non-color data. Any texture assigned must have
  237. * `texture.colorSpace = NoColorSpace` (default).
  238. *
  239. * @type {?Texture}
  240. * @default null
  241. */
  242. this.alphaMap = null;
  243. /**
  244. * The environment map.
  245. *
  246. * `envMap` represents luminance data, and the texture must be assigned
  247. * a {@link Texture#colorSpace}. Most `envMap` textures set
  248. * `texture.colorSpace = LinearSRGBColorSpace` and use float-type formats
  249. * such as `.exr` or `.hdr`.
  250. *
  251. * @type {?Texture}
  252. * @default null
  253. */
  254. this.envMap = null;
  255. /**
  256. * The rotation of the environment map in radians.
  257. *
  258. * @type {Euler}
  259. * @default (0,0,0)
  260. */
  261. this.envMapRotation = new Euler();
  262. /**
  263. * How to combine the result of the surface's color with the environment map, if any.
  264. *
  265. * When set to `MixOperation`, the {@link MeshBasicMaterial#reflectivity} is used to
  266. * blend between the two colors.
  267. *
  268. * @type {(MultiplyOperation|MixOperation|AddOperation)}
  269. * @default MultiplyOperation
  270. */
  271. this.combine = MultiplyOperation;
  272. /**
  273. * How much the environment map affects the surface.
  274. * The valid range is between `0` (no reflections) and `1` (full reflections).
  275. *
  276. * @type {number}
  277. * @default 1
  278. */
  279. this.reflectivity = 1;
  280. /**
  281. * Scales the effect of the environment map by multiplying its color.
  282. *
  283. * @type {number}
  284. * @default 1
  285. */
  286. this.envMapIntensity = 1.0;
  287. /**
  288. * The index of refraction (IOR) of air (approximately 1) divided by the
  289. * index of refraction of the material. It is used with environment mapping
  290. * modes {@link CubeRefractionMapping} and {@link EquirectangularRefractionMapping}.
  291. * The refraction ratio should not exceed `1`.
  292. *
  293. * @type {number}
  294. * @default 0.98
  295. */
  296. this.refractionRatio = 0.98;
  297. /**
  298. * Renders the geometry as a wireframe.
  299. *
  300. * @type {boolean}
  301. * @default false
  302. */
  303. this.wireframe = false;
  304. /**
  305. * Controls the thickness of the wireframe.
  306. *
  307. * Can only be used with {@link SVGRenderer}.
  308. *
  309. * @type {number}
  310. * @default 1
  311. */
  312. this.wireframeLinewidth = 1;
  313. /**
  314. * Defines appearance of wireframe ends.
  315. *
  316. * Can only be used with {@link SVGRenderer}.
  317. *
  318. * @type {('round'|'bevel'|'miter')}
  319. * @default 'round'
  320. */
  321. this.wireframeLinecap = 'round';
  322. /**
  323. * Defines appearance of wireframe joints.
  324. *
  325. * Can only be used with {@link SVGRenderer}.
  326. *
  327. * @type {('round'|'bevel'|'miter')}
  328. * @default 'round'
  329. */
  330. this.wireframeLinejoin = 'round';
  331. /**
  332. * Whether the material is rendered with flat shading or not.
  333. *
  334. * @type {boolean}
  335. * @default false
  336. */
  337. this.flatShading = false;
  338. /**
  339. * Whether the material is affected by fog or not.
  340. *
  341. * @type {boolean}
  342. * @default true
  343. */
  344. this.fog = true;
  345. this.setValues( parameters );
  346. }
  347. copy( source ) {
  348. super.copy( source );
  349. this.color.copy( source.color );
  350. this.map = source.map;
  351. this.lightMap = source.lightMap;
  352. this.lightMapIntensity = source.lightMapIntensity;
  353. this.aoMap = source.aoMap;
  354. this.aoMapIntensity = source.aoMapIntensity;
  355. this.emissive.copy( source.emissive );
  356. this.emissiveMap = source.emissiveMap;
  357. this.emissiveIntensity = source.emissiveIntensity;
  358. this.bumpMap = source.bumpMap;
  359. this.bumpScale = source.bumpScale;
  360. this.normalMap = source.normalMap;
  361. this.normalMapType = source.normalMapType;
  362. this.normalScale.copy( source.normalScale );
  363. this.displacementMap = source.displacementMap;
  364. this.displacementScale = source.displacementScale;
  365. this.displacementBias = source.displacementBias;
  366. this.specularMap = source.specularMap;
  367. this.alphaMap = source.alphaMap;
  368. this.envMap = source.envMap;
  369. this.envMapRotation.copy( source.envMapRotation );
  370. this.combine = source.combine;
  371. this.reflectivity = source.reflectivity;
  372. this.envMapIntensity = source.envMapIntensity;
  373. this.refractionRatio = source.refractionRatio;
  374. this.wireframe = source.wireframe;
  375. this.wireframeLinewidth = source.wireframeLinewidth;
  376. this.wireframeLinecap = source.wireframeLinecap;
  377. this.wireframeLinejoin = source.wireframeLinejoin;
  378. this.flatShading = source.flatShading;
  379. this.fog = source.fog;
  380. return this;
  381. }
  382. }
  383. export { MeshLambertMaterial };
粤ICP备19079148号