MeshStandardMaterial.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. import { 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 standard physically based material, using Metallic-Roughness workflow.
  8. *
  9. * Physically based rendering (PBR) has recently become the standard in many
  10. * 3D applications, such as [Unity](https://blogs.unity3d.com/2014/10/29/physically-based-shading-in-unity-5-a-primer/),
  11. * [Unreal](https://docs.unrealengine.com/latest/INT/Engine/Rendering/Materials/PhysicallyBased/) and
  12. * [3D Studio Max](http://area.autodesk.com/blogs/the-3ds-max-blog/what039s-new-for-rendering-in-3ds-max-2017).
  13. *
  14. * This approach differs from older approaches in that instead of using
  15. * approximations for the way in which light interacts with a surface, a
  16. * physically correct model is used. The idea is that, instead of tweaking
  17. * materials to look good under specific lighting, a material can be created
  18. * that will react 'correctly' under all lighting scenarios.
  19. *
  20. * In practice this gives a more accurate and realistic looking result than
  21. * the {@link MeshLambertMaterial} or {@link MeshPhongMaterial}, at the cost of
  22. * being somewhat more computationally expensive. `MeshStandardMaterial` uses per-fragment
  23. * shading.
  24. *
  25. * Note that for best results you should always specify an environment map when using this material.
  26. *
  27. * For a non-technical introduction to the concept of PBR and how to set up a
  28. * PBR material, check out these articles by the people at [marmoset](https://www.marmoset.co):
  29. *
  30. * - [Basic Theory of Physically Based Rendering](https://www.marmoset.co/posts/basic-theory-of-physically-based-rendering/)
  31. * - [Physically Based Rendering and You Can Too](https://www.marmoset.co/posts/physically-based-rendering-and-you-can-too/)
  32. *
  33. * Technical details of the approach used in three.js (and most other PBR systems) can be found is this
  34. * [paper from Disney](https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf)
  35. * (pdf), by Brent Burley.
  36. *
  37. * @augments Material
  38. * @demo scenes/material-browser.html#MeshStandardMaterial
  39. */
  40. class MeshStandardMaterial extends Material {
  41. /**
  42. * Constructs a new mesh standard material.
  43. *
  44. * @param {Object} [parameters] - An object with one or more properties
  45. * defining the material's appearance. Any property of the material
  46. * (including any property from inherited materials) can be passed
  47. * in here. Color values can be passed any type of value accepted
  48. * by {@link Color#set}.
  49. */
  50. constructor( parameters ) {
  51. super();
  52. /**
  53. * This flag can be used for type testing.
  54. *
  55. * @type {boolean}
  56. * @readonly
  57. * @default true
  58. */
  59. this.isMeshStandardMaterial = true;
  60. this.type = 'MeshStandardMaterial';
  61. this.defines = { 'STANDARD': '' };
  62. /**
  63. * Color of the material.
  64. *
  65. * @type {Color}
  66. * @default (1,1,1)
  67. */
  68. this.color = new Color( 0xffffff ); // diffuse
  69. /**
  70. * How rough the material appears. `0.0` means a smooth mirror reflection, `1.0`
  71. * means fully diffuse. If `roughnessMap` is also provided,
  72. * both values are multiplied.
  73. *
  74. * @type {number}
  75. * @default 1
  76. */
  77. this.roughness = 1.0;
  78. /**
  79. * How much the material is like a metal. Non-metallic materials such as wood
  80. * or stone use `0.0`, metallic use `1.0`, with nothing (usually) in between.
  81. * A value between `0.0` and `1.0` could be used for a rusty metal look.
  82. * If `metalnessMap` is also provided, both values are multiplied.
  83. *
  84. * @type {number}
  85. * @default 0
  86. */
  87. this.metalness = 0.0;
  88. /**
  89. * The color map. May optionally include an alpha channel, typically combined
  90. * with {@link Material#transparent} or {@link Material#alphaTest}. The texture map
  91. * color is modulated by the diffuse `color`.
  92. *
  93. * `map` represents color data, and the texture must be assigned a
  94. * {@link Texture#colorSpace}. Most `map` textures set
  95. * `texture.colorSpace = SRGBColorSpace`.
  96. *
  97. * @type {?Texture}
  98. * @default null
  99. */
  100. this.map = null;
  101. /**
  102. * The light map. Requires a second set of UVs.
  103. *
  104. * `lightMap` represents pre-baked illuminance data, and the texture must be assigned
  105. * a {@link Texture#colorSpace}. Most `lightMap` textures set
  106. * `texture.colorSpace = LinearSRGBColorSpace` and use float-type formats
  107. * such as `.exr` or `.hdr`.
  108. *
  109. * @type {?Texture}
  110. * @default null
  111. */
  112. this.lightMap = null;
  113. /**
  114. * Intensity of the baked light.
  115. *
  116. * @type {number}
  117. * @default 1
  118. */
  119. this.lightMapIntensity = 1.0;
  120. /**
  121. * The red channel of this texture is used as the ambient occlusion map.
  122. * Requires a second set of UVs.
  123. *
  124. * `aoMap` represents non-color data. Any texture assigned must have
  125. * `texture.colorSpace = NoColorSpace` (default).
  126. *
  127. * @type {?Texture}
  128. * @default null
  129. */
  130. this.aoMap = null;
  131. /**
  132. * Intensity of the ambient occlusion effect. Range is `[0,1]`, where `0`
  133. * disables ambient occlusion. Where intensity is `1` and the AO map's
  134. * red channel is also `1`, ambient light is fully occluded on a surface.
  135. *
  136. * @type {number}
  137. * @default 1
  138. */
  139. this.aoMapIntensity = 1.0;
  140. /**
  141. * Emissive (light) color of the material, essentially a solid color
  142. * unaffected by other lighting.
  143. *
  144. * @type {Color}
  145. * @default (0,0,0)
  146. */
  147. this.emissive = new Color( 0x000000 );
  148. /**
  149. * Intensity of the emissive light. Modulates the emissive color.
  150. *
  151. * @type {number}
  152. * @default 1
  153. */
  154. this.emissiveIntensity = 1.0;
  155. /**
  156. * Set emissive (glow) map. The emissive map color is modulated by the
  157. * emissive color and the emissive intensity. If you have an emissive map,
  158. * be sure to set the emissive color to something other than black.
  159. *
  160. * `emissiveMap` represents color data, and the texture must be assigned a
  161. * {@link Texture#colorSpace}. Most `emissiveMap` textures set
  162. * `texture.colorSpace = SRGBColorSpace`.
  163. *
  164. * @type {?Texture}
  165. * @default null
  166. */
  167. this.emissiveMap = null;
  168. /**
  169. * The texture to create a bump map. The black and white values map to the
  170. * perceived depth in relation to the lights. Bump doesn't actually affect
  171. * the geometry of the object, only the lighting. If a normal map is defined
  172. * this will be ignored.
  173. *
  174. * `bumpMap` represents non-color data. Any texture assigned must have
  175. * `texture.colorSpace = NoColorSpace` (default).
  176. *
  177. * @type {?Texture}
  178. * @default null
  179. */
  180. this.bumpMap = null;
  181. /**
  182. * How much the bump map affects the material. Typical range is `[0,1]`.
  183. *
  184. * @type {number}
  185. * @default 1
  186. */
  187. this.bumpScale = 1;
  188. /**
  189. * The texture to create a normal map. The RGB values affect the surface
  190. * normal for each pixel fragment and change the way the color is lit. Normal
  191. * maps do not change the actual shape of the surface, only the lighting. In
  192. * case the material has a normal map authored using the left handed
  193. * convention, the `y` component of `normalScale` should be negated to compensate
  194. * for the different handedness.
  195. *
  196. * `normalMap` represents non-color data. Any texture assigned must have
  197. * `texture.colorSpace = NoColorSpace` (default).
  198. *
  199. * @type {?Texture}
  200. * @default null
  201. */
  202. this.normalMap = null;
  203. /**
  204. * The type of normal map.
  205. *
  206. * @type {(TangentSpaceNormalMap|ObjectSpaceNormalMap)}
  207. * @default TangentSpaceNormalMap
  208. */
  209. this.normalMapType = TangentSpaceNormalMap;
  210. /**
  211. * How much the normal map affects the material. Typical value range is `[0,1]`.
  212. *
  213. * @type {Vector2}
  214. * @default (1,1)
  215. */
  216. this.normalScale = new Vector2( 1, 1 );
  217. /**
  218. * The displacement map affects the position of the mesh's vertices. Unlike
  219. * other maps which only affect the light and shade of the material the
  220. * displaced vertices can cast shadows, block other objects, and otherwise
  221. * act as real geometry. The displacement texture is an image where the value
  222. * of each pixel (white being the highest) is mapped against, and
  223. * repositions, the vertices of the mesh. For best results, pair a
  224. * displacement map with a matching normal map, since the renderer can
  225. * not recompute surface normals from the displaced vertices.
  226. *
  227. * `displacementMap` represents non-color data. Any texture assigned must have
  228. * `texture.colorSpace = NoColorSpace` (default).
  229. *
  230. * @type {?Texture}
  231. * @default null
  232. */
  233. this.displacementMap = null;
  234. /**
  235. * How much the displacement map affects the mesh (where black is no
  236. * displacement, and white is maximum displacement). Without a displacement
  237. * map set, this value is not applied.
  238. *
  239. * @type {number}
  240. * @default 0
  241. */
  242. this.displacementScale = 1;
  243. /**
  244. * The offset of the displacement map's values on the mesh's vertices.
  245. * The bias is added to the scaled sample of the displacement map.
  246. * Without a displacement map set, this value is not applied.
  247. *
  248. * @type {number}
  249. * @default 0
  250. */
  251. this.displacementBias = 0;
  252. /**
  253. * The green channel of this texture is used to alter the roughness of the
  254. * material.
  255. *
  256. * `roughnessMap` represents non-color data. Any texture assigned must have
  257. * `texture.colorSpace = NoColorSpace` (default).
  258. *
  259. * @type {?Texture}
  260. * @default null
  261. */
  262. this.roughnessMap = null;
  263. /**
  264. * The blue channel of this texture is used to alter the metalness of the
  265. * material.
  266. *
  267. * `metalnessMap` represents non-color data. Any texture assigned must have
  268. * `texture.colorSpace = NoColorSpace` (default).
  269. *
  270. * @type {?Texture}
  271. * @default null
  272. */
  273. this.metalnessMap = null;
  274. /**
  275. * The alpha map is a grayscale texture that controls the opacity across the
  276. * surface (black: fully transparent; white: fully opaque).
  277. *
  278. * Only the color of the texture is used, ignoring the alpha channel if one
  279. * exists. For RGB and RGBA textures, the renderer will use the green channel
  280. * when sampling this texture due to the extra bit of precision provided for
  281. * green in DXT-compressed and uncompressed RGB 565 formats. Luminance-only and
  282. * luminance/alpha textures will also still work as expected.
  283. *
  284. * `alphaMap` represents non-color data. Any texture assigned must have
  285. * `texture.colorSpace = NoColorSpace` (default).
  286. *
  287. * @type {?Texture}
  288. * @default null
  289. */
  290. this.alphaMap = null;
  291. /**
  292. * The environment map. To ensure a physically correct rendering, environment maps
  293. * are internally pre-processed with {@link PMREMGenerator}.
  294. *
  295. * `envMap` represents luminance data, and the texture must be assigned
  296. * a {@link Texture#colorSpace}. Most `envMap` textures set
  297. * `texture.colorSpace = LinearSRGBColorSpace` and use float-type formats
  298. * such as `.exr` or `.hdr`.
  299. *
  300. * @type {?Texture}
  301. * @default null
  302. */
  303. this.envMap = null;
  304. /**
  305. * The rotation of the environment map in radians.
  306. *
  307. * @type {Euler}
  308. * @default (0,0,0)
  309. */
  310. this.envMapRotation = new Euler();
  311. /**
  312. * Scales the effect of the environment map by multiplying its color.
  313. *
  314. * @type {number}
  315. * @default 1
  316. */
  317. this.envMapIntensity = 1.0;
  318. /**
  319. * Renders the geometry as a wireframe.
  320. *
  321. * @type {boolean}
  322. * @default false
  323. */
  324. this.wireframe = false;
  325. /**
  326. * Controls the thickness of the wireframe.
  327. *
  328. * Can only be used with {@link SVGRenderer}.
  329. *
  330. * @type {number}
  331. * @default 1
  332. */
  333. this.wireframeLinewidth = 1;
  334. /**
  335. * Defines appearance of wireframe ends.
  336. *
  337. * Can only be used with {@link SVGRenderer}.
  338. *
  339. * @type {('round'|'bevel'|'miter')}
  340. * @default 'round'
  341. */
  342. this.wireframeLinecap = 'round';
  343. /**
  344. * Defines appearance of wireframe joints.
  345. *
  346. * Can only be used with {@link SVGRenderer}.
  347. *
  348. * @type {('round'|'bevel'|'miter')}
  349. * @default 'round'
  350. */
  351. this.wireframeLinejoin = 'round';
  352. /**
  353. * Whether the material is rendered with flat shading or not.
  354. *
  355. * @type {boolean}
  356. * @default false
  357. */
  358. this.flatShading = false;
  359. /**
  360. * Whether the material is affected by fog or not.
  361. *
  362. * @type {boolean}
  363. * @default true
  364. */
  365. this.fog = true;
  366. this.setValues( parameters );
  367. }
  368. copy( source ) {
  369. super.copy( source );
  370. this.defines = { 'STANDARD': '' };
  371. this.color.copy( source.color );
  372. this.roughness = source.roughness;
  373. this.metalness = source.metalness;
  374. this.map = source.map;
  375. this.lightMap = source.lightMap;
  376. this.lightMapIntensity = source.lightMapIntensity;
  377. this.aoMap = source.aoMap;
  378. this.aoMapIntensity = source.aoMapIntensity;
  379. this.emissive.copy( source.emissive );
  380. this.emissiveMap = source.emissiveMap;
  381. this.emissiveIntensity = source.emissiveIntensity;
  382. this.bumpMap = source.bumpMap;
  383. this.bumpScale = source.bumpScale;
  384. this.normalMap = source.normalMap;
  385. this.normalMapType = source.normalMapType;
  386. this.normalScale.copy( source.normalScale );
  387. this.displacementMap = source.displacementMap;
  388. this.displacementScale = source.displacementScale;
  389. this.displacementBias = source.displacementBias;
  390. this.roughnessMap = source.roughnessMap;
  391. this.metalnessMap = source.metalnessMap;
  392. this.alphaMap = source.alphaMap;
  393. this.envMap = source.envMap;
  394. this.envMapRotation.copy( source.envMapRotation );
  395. this.envMapIntensity = source.envMapIntensity;
  396. this.wireframe = source.wireframe;
  397. this.wireframeLinewidth = source.wireframeLinewidth;
  398. this.wireframeLinecap = source.wireframeLinecap;
  399. this.wireframeLinejoin = source.wireframeLinejoin;
  400. this.flatShading = source.flatShading;
  401. this.fog = source.fog;
  402. return this;
  403. }
  404. }
  405. export { MeshStandardMaterial };
粤ICP备19079148号