materials.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Materials</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Materials">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>Materials</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>This article is part of a series of articles about three.js. The
  29. first article is <a href="fundamentals.html">three.js fundamentals</a>. If
  30. you haven't read that yet and you're new to three.js you might want to
  31. consider starting there.</p>
  32. <p>Three.js provides several types of materials.
  33. They define how objects will appear in the scene.
  34. Which materials you use really depends on what you're trying to
  35. accomplish.</p>
  36. <p>There are 2 ways to set most material properties. One at creation time which
  37. we've seen before.</p>
  38. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const material = new THREE.MeshPhongMaterial({
  39. color: 0xFF0000, // red (can also use a CSS color string here)
  40. flatShading: true,
  41. });
  42. </pre>
  43. <p>The other is after creation</p>
  44. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const material = new THREE.MeshPhongMaterial();
  45. material.color.setHSL(0, 1, .5); // red
  46. material.flatShading = true;
  47. </pre>
  48. <p>note that properties of type <a href="/docs/#api/en/math/Color"><code class="notranslate" translate="no">THREE.Color</code></a> have multiple ways to be set.</p>
  49. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">material.color.set(0x00FFFF); // same as CSS's #RRGGBB style
  50. material.color.set(cssString); // any CSS color, eg 'purple', '#F32',
  51. // 'rgb(255, 127, 64)',
  52. // 'hsl(180, 50%, 25%)'
  53. material.color.set(someColor) // some other THREE.Color
  54. material.color.setHSL(h, s, l) // where h, s, and l are 0 to 1
  55. material.color.setRGB(r, g, b) // where r, g, and b are 0 to 1
  56. </pre>
  57. <p>And at creation time you can pass either a hex number or a CSS string</p>
  58. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const m1 = new THREE.MeshBasicMaterial({color: 0xFF0000}); // red
  59. const m2 = new THREE.MeshBasicMaterial({color: 'red'}); // red
  60. const m3 = new THREE.MeshBasicMaterial({color: '#F00'}); // red
  61. const m4 = new THREE.MeshBasicMaterial({color: 'rgb(255,0,0)'}); // red
  62. const m5 = new THREE.MeshBasicMaterial({color: 'hsl(0,100%,50%)'}); // red
  63. </pre>
  64. <p>So let's go over three.js's set of materials.</p>
  65. <p>The <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> is not affected by lights.
  66. The <a href="/docs/#api/en/materials/MeshLambertMaterial"><code class="notranslate" translate="no">MeshLambertMaterial</code></a> computes lighting only at the vertices vs the <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a> which computes lighting at every pixel. The <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a>
  67. also supports specular highlights.</p>
  68. <div class="spread">
  69. <div>
  70. <div data-diagram="MeshBasicMaterial"></div>
  71. <div class="code">Basic</div>
  72. </div>
  73. <div>
  74. <div data-diagram="MeshLambertMaterial"></div>
  75. <div class="code">Lambert</div>
  76. </div>
  77. <div>
  78. <div data-diagram="MeshPhongMaterial"></div>
  79. <div class="code">Phong</div>
  80. </div>
  81. </div>
  82. <div class="spread">
  83. <div>
  84. <div data-diagram="MeshBasicMaterialLowPoly"></div>
  85. </div>
  86. <div>
  87. <div data-diagram="MeshLambertMaterialLowPoly"></div>
  88. </div>
  89. <div>
  90. <div data-diagram="MeshPhongMaterialLowPoly"></div>
  91. </div>
  92. </div>
  93. <div class="threejs_center code">low-poly models with same materials</div>
  94. <p>The <code class="notranslate" translate="no">shininess</code> setting of the <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a> determines the <em>shininess</em> of the specular highlight. It defaults to 30.</p>
  95. <div class="spread">
  96. <div>
  97. <div data-diagram="MeshPhongMaterialShininess0"></div>
  98. <div class="code">shininess: 0</div>
  99. </div>
  100. <div>
  101. <div data-diagram="MeshPhongMaterialShininess30"></div>
  102. <div class="code">shininess: 30</div>
  103. </div>
  104. <div>
  105. <div data-diagram="MeshPhongMaterialShininess150"></div>
  106. <div class="code">shininess: 150</div>
  107. </div>
  108. </div>
  109. <p>Note that setting the <code class="notranslate" translate="no">emissive</code> property to a color on either a
  110. <a href="/docs/#api/en/materials/MeshLambertMaterial"><code class="notranslate" translate="no">MeshLambertMaterial</code></a> or a <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a> and setting the <code class="notranslate" translate="no">color</code> to black
  111. (and <code class="notranslate" translate="no">shininess</code> to 0 for phong) ends up looking just like the <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a>.</p>
  112. <div class="spread">
  113. <div>
  114. <div data-diagram="MeshBasicMaterialCompare"></div>
  115. <div class="code">
  116. <div>Basic</div>
  117. <div>color: 'purple'</div>
  118. </div>
  119. </div>
  120. <div>
  121. <div data-diagram="MeshLambertMaterialCompare"></div>
  122. <div class="code">
  123. <div>Lambert</div>
  124. <div>color: 'black'</div>
  125. <div>emissive: 'purple'</div>
  126. </div>
  127. </div>
  128. <div>
  129. <div data-diagram="MeshPhongMaterialCompare"></div>
  130. <div class="code">
  131. <div>Phong</div>
  132. <div>color: 'black'</div>
  133. <div>emissive: 'purple'</div>
  134. <div>shininess: 0</div>
  135. </div>
  136. </div>
  137. </div>
  138. <p>Why have all 3 when <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a> can do the same things as <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a>
  139. and <a href="/docs/#api/en/materials/MeshLambertMaterial"><code class="notranslate" translate="no">MeshLambertMaterial</code></a>? The reason is the more sophisticated material
  140. takes more GPU power to draw. On a slower GPU like say a mobile phone
  141. you might want to reduce the GPU power needed to draw your scene by
  142. using one of the less complex materials. It also follows that if you
  143. don't need the extra features then use the simplest material. If you don't
  144. need the lighting and the specular highlight then use the <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a>.</p>
  145. <p>The <a href="/docs/#api/en/materials/MeshToonMaterial"><code class="notranslate" translate="no">MeshToonMaterial</code></a> is similar to the <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a>
  146. with one big difference. Rather than shading smoothly it uses a gradient map
  147. (an X by 1 texture) to decide how to shade. The default uses a gradient map
  148. that is 70% brightness for the first 70% and 100% after but you can supply your
  149. own gradient map. This ends up giving a 2 tone look that looks like a cartoon.</p>
  150. <div class="spread">
  151. <div data-diagram="MeshToonMaterial"></div>
  152. </div>
  153. <p>Next up there are 2 <em>physically based rendering</em> materials. Physically Based
  154. Rendering is often abbreviated PBR.</p>
  155. <p>The materials above use simple math to make materials that look 3D but they
  156. aren't what actually happens in real world. The 2 PBR materials use much more
  157. complex math to come close to what actually happens in the real world.</p>
  158. <p>The first one is <a href="/docs/#api/en/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a>. The biggest difference between
  159. <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a> and <a href="/docs/#api/en/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a> is it uses different parameters.
  160. <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a> had a <code class="notranslate" translate="no">shininess</code> setting. <a href="/docs/#api/en/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a> has 2
  161. settings <code class="notranslate" translate="no">roughness</code> and <code class="notranslate" translate="no">metalness</code>.</p>
  162. <p>At a basic level <a href="/docs/#api/en/materials/MeshStandardMaterial#roughness"><code class="notranslate" translate="no">roughness</code></a> is the opposite
  163. of <code class="notranslate" translate="no">shininess</code>. Something that has a high roughness, like a baseball doesn't
  164. have hard reflections whereas something that's not rough, like a billiard ball,
  165. is very shiny. Roughness goes from 0 to 1.</p>
  166. <p>The other setting, <a href="/docs/#api/en/materials/MeshStandardMaterial#metalness"><code class="notranslate" translate="no">metalness</code></a>, says
  167. how metal the material is. Metals behave differently than non-metals. 0
  168. for non-metal and 1 for metal.</p>
  169. <p>Here's a quick sample of <a href="/docs/#api/en/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a> with <code class="notranslate" translate="no">roughness</code> from 0 to 1
  170. across and <code class="notranslate" translate="no">metalness</code> from 0 to 1 down.</p>
  171. <div data-diagram="MeshStandardMaterial" style="min-height: 400px"></div>
  172. <p>The <a href="/docs/#api/en/materials/MeshPhysicalMaterial"><code class="notranslate" translate="no">MeshPhysicalMaterial</code></a> is same as the <a href="/docs/#api/en/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a> but it
  173. adds a <code class="notranslate" translate="no">clearcoat</code> parameter that goes from 0 to 1 for how much to
  174. apply a clearcoat gloss layer and a <code class="notranslate" translate="no">clearCoatRoughness</code> parameter
  175. that specifies how rough the gloss layer is.</p>
  176. <p>Here's the same grid of <code class="notranslate" translate="no">roughness</code> by <code class="notranslate" translate="no">metalness</code> as above but with
  177. <code class="notranslate" translate="no">clearcoat</code> and <code class="notranslate" translate="no">clearCoatRoughness</code> settings.</p>
  178. <div data-diagram="MeshPhysicalMaterial" style="min-height: 400px"></div>
  179. <p>The various standard materials progress from fastest to slowest
  180. <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> ➡ <a href="/docs/#api/en/materials/MeshLambertMaterial"><code class="notranslate" translate="no">MeshLambertMaterial</code></a> ➡ <a href="/docs/#api/en/materials/MeshPhongMaterial"><code class="notranslate" translate="no">MeshPhongMaterial</code></a> ➡
  181. <a href="/docs/#api/en/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a> ➡ <a href="/docs/#api/en/materials/MeshPhysicalMaterial"><code class="notranslate" translate="no">MeshPhysicalMaterial</code></a>. The slower materials
  182. can make more realistic looking scenes but you might need to design
  183. your code to use the faster materials on low powered or mobile machines.</p>
  184. <p>There are 3 materials that have special uses. <a href="/docs/#api/en/materials/ShadowMaterial"><code class="notranslate" translate="no">ShadowMaterial</code></a>
  185. is used to get the data created from shadows. We haven't
  186. covered shadows yet. When we do we'll use this material
  187. to take a peek at what's happening behind the scenes.</p>
  188. <p>The <a href="/docs/#api/en/materials/MeshDepthMaterial"><code class="notranslate" translate="no">MeshDepthMaterial</code></a> renders the depth of each pixel where
  189. pixels at negative <a href="/docs/#api/en/cameras/PerspectiveCamera#near"><code class="notranslate" translate="no">near</code></a> of the camera are 0 and negative <a href="/docs/#api/en/cameras/PerspectiveCamera#far"><code class="notranslate" translate="no">far</code></a> are 1. Certain special effects can use this data which we'll
  190. get into at another time.</p>
  191. <div class="spread">
  192. <div>
  193. <div data-diagram="MeshDepthMaterial"></div>
  194. </div>
  195. </div>
  196. <p>The <a href="/docs/#api/en/materials/MeshNormalMaterial"><code class="notranslate" translate="no">MeshNormalMaterial</code></a> will show you the <em>normals</em> of geometry.
  197. <em>Normals</em> are the direction a particular triangle or pixel faces.
  198. <a href="/docs/#api/en/materials/MeshNormalMaterial"><code class="notranslate" translate="no">MeshNormalMaterial</code></a> draws the view space normals (the normals relative to the camera).
  199. <span style="background: red;" class="color">x is red</span>,
  200. <span style="background: green;" class="dark-color">y is green</span>, and
  201. <span style="background: blue;" class="dark-color">z is blue</span> so things facing
  202. to the right will be <span style="background: #FF7F7F;" class="color">pink</span>,
  203. to the left will be <span style="background: #007F7F;" class="dark-color">aqua</span>,
  204. up will be <span style="background: #7FFF7F;" class="color">light green</span>,
  205. down will be <span style="background: #7F007F;" class="dark-color">purple</span>,
  206. and toward the screen will be <span style="background: #7F7FFF;" class="color">lavender</span>.</p>
  207. <div class="spread">
  208. <div>
  209. <div data-diagram="MeshNormalMaterial"></div>
  210. </div>
  211. </div>
  212. <p><a href="/docs/#api/en/materials/ShaderMaterial"><code class="notranslate" translate="no">ShaderMaterial</code></a> is for making custom materials using the three.js shader
  213. system. <a href="/docs/#api/en/materials/RawShaderMaterial"><code class="notranslate" translate="no">RawShaderMaterial</code></a> is for making entirely custom shaders with
  214. no help from three.js. Both of these topics are large and will be
  215. covered later.</p>
  216. <p>Most materials share a bunch of settings all defined by <a href="/docs/#api/en/materials/Material"><code class="notranslate" translate="no">Material</code></a>.
  217. <a href="/docs/#api/en/materials/Material">See the docs</a>
  218. for all of them but let's go over two of the most commonly used
  219. properties.</p>
  220. <p><a href="/docs/#api/en/materials/Material#flatShading"><code class="notranslate" translate="no">flatShading</code></a>:
  221. whether or not the object looks faceted or smooth. default = <code class="notranslate" translate="no">false</code>.</p>
  222. <div class="spread">
  223. <div>
  224. <div data-diagram="smoothShading"></div>
  225. <div class="code">flatShading: false</div>
  226. </div>
  227. <div>
  228. <div data-diagram="flatShading"></div>
  229. <div class="code">flatShading: true</div>
  230. </div>
  231. </div>
  232. <p><a href="/docs/#api/en/materials/Material#side"><code class="notranslate" translate="no">side</code></a>: which sides of triangles to show. The default is <code class="notranslate" translate="no">THREE.FrontSide</code>.
  233. Other options are <code class="notranslate" translate="no">THREE.BackSide</code> and <code class="notranslate" translate="no">THREE.DoubleSide</code> (both sides).
  234. Most 3D objects drawn in three are probably opaque solids so the back sides
  235. (the sides facing inside the solid) do not need to be drawn. The most common
  236. reason to set <code class="notranslate" translate="no">side</code> is for planes or other non-solid objects where it is
  237. common to see the back sides of triangles.</p>
  238. <p>Here are 6 planes drawn with <code class="notranslate" translate="no">THREE.FrontSide</code> and <code class="notranslate" translate="no">THREE.DoubleSide</code>.</p>
  239. <div class="spread">
  240. <div>
  241. <div data-diagram="sideDefault" style="height: 250px;"></div>
  242. <div class="code">side: THREE.FrontSide</div>
  243. </div>
  244. <div>
  245. <div data-diagram="sideDouble" style="height: 250px;"></div>
  246. <div class="code">side: THREE.DoubleSide</div>
  247. </div>
  248. </div>
  249. <p>There's really a lot to consider with materials and we actually still
  250. have a bunch more to go. In particular we've mostly ignored textures
  251. which open up a whole slew of options. Before we cover textures though
  252. we need to take a break and cover
  253. <a href="setup.html">setting up your development environment</a></p>
  254. <div class="threejs_bottombar">
  255. <h3>material.needsUpdate</h3>
  256. <p>
  257. This topic rarely affects most three.js apps but just as an FYI...
  258. Three.js applies material settings when a material is used where "used"
  259. means "something is rendered that uses the material". Some material settings are
  260. only applied once as changing them requires lots of work by three.js.
  261. In those cases you need to set <code class="notranslate" translate="no">material.needsUpdate = true</code> to tell
  262. three.js to apply your material changes. The most common settings
  263. that require you to set <code class="notranslate" translate="no">needsUpdate</code> if you change the settings after
  264. using the material are:
  265. </p>
  266. <ul>
  267. <li><code class="notranslate" translate="no">flatShading</code></li>
  268. <li>adding or removing a texture
  269. <p>
  270. Changing a texture is ok, but if want to switch from using no texture
  271. to using a texture or from using a texture to using no texture
  272. then you need to set <code class="notranslate" translate="no">needsUpdate = true</code>.
  273. </p>
  274. <p>In the case of going from texture to no-texture it is often
  275. just better to use a 1x1 pixel white texture.</p>
  276. </li>
  277. </ul>
  278. <p>As mentioned above most apps never run into these issues. Most apps
  279. do not switch between flat shaded and non flat shaded. Most apps also
  280. either use textures or a solid color for a given material, they rarely
  281. switch from using one to using the other.
  282. </p>
  283. </div>
  284. <p><canvas id="c"></canvas></p>
  285. <script type="module" src="../resources/threejs-materials.js"></script>
  286. </div>
  287. </div>
  288. </div>
  289. <script src="../resources/prettify.js"></script>
  290. <script src="../resources/lesson.js"></script>
  291. </body></html>