fog.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Fog</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 – Fog">
  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>Fog</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. If you haven't read about cameras you might
  32. want to start with <a href="cameras.html">this article</a>.</p>
  33. <p>Fog in a 3D engine is generally a way of fading to a specific color
  34. based on the distance from the camera. In three.js you add fog by
  35. creating <a href="/docs/#api/en/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a> or <a href="/docs/#api/en/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> object and setting it on the scene's
  36. <a href="/docs/#api/en/scenes/Scene#fog"><code class="notranslate" translate="no">fog</code></a> property.</p>
  37. <p><a href="/docs/#api/en/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a> lets you choose <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> settings which are distances
  38. from the camera. Anything closer than <code class="notranslate" translate="no">near</code> is unaffected by fog.
  39. Anything further than <code class="notranslate" translate="no">far</code> is completely the fog color. Parts between
  40. <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> fade from their material color to the fog color.</p>
  41. <p>There's also <a href="/docs/#api/en/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> which grows exponentially with distance from the camera.</p>
  42. <p>To use either type of fog you create one and and assign it to the scene as in</p>
  43. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  44. {
  45. const color = 0xFFFFFF; // white
  46. const near = 10;
  47. const far = 100;
  48. scene.fog = new THREE.Fog(color, near, far);
  49. }
  50. </pre>
  51. <p>or for <a href="/docs/#api/en/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> it would be</p>
  52. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  53. {
  54. const color = 0xFFFFFF;
  55. const density = 0.1;
  56. scene.fog = new THREE.FogExp2(color, density);
  57. }
  58. </pre>
  59. <p><a href="/docs/#api/en/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> is closer to reality but <a href="/docs/#api/en/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a> is used
  60. more commonly since it lets you choose a place to apply
  61. the fog so you can decide to show a clear scene
  62. up to a certain distance and then fade out to some color
  63. past that distance.</p>
  64. <div class="spread">
  65. <div>
  66. <div data-diagram="fog" style="height: 300px;"></div>
  67. <div class="code">THREE.Fog</div>
  68. </div>
  69. <div>
  70. <div data-diagram="fogExp2" style="height: 300px;"></div>
  71. <div class="code">THREE.FogExp2</div>
  72. </div>
  73. </div>
  74. <p>It's important to note that the fog is applied to <em>things that are rendered</em>.
  75. It is part of the calculation of each pixel of the color of the object.
  76. What that means is if you want your scene to fade to a certain color you
  77. need to set the fog <strong>and</strong> the background color to the same color.
  78. The background color is set using the
  79. <a href="/docs/#api/en/scenes/Scene#background"><code class="notranslate" translate="no">scene.background</code></a>
  80. property. To pick a background color you attach a <a href="/docs/#api/en/math/Color"><code class="notranslate" translate="no">THREE.Color</code></a> to it. For example</p>
  81. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">scene.background = new THREE.Color('#F00'); // red
  82. </pre>
  83. <div class="spread">
  84. <div>
  85. <div data-diagram="fogBlueBackgroundRed" style="height: 300px;" class="border"></div>
  86. <div class="code">fog blue, background red</div>
  87. </div>
  88. <div>
  89. <div data-diagram="fogBlueBackgroundBlue" style="height: 300px;" class="border"></div>
  90. <div class="code">fog blue, background blue</div>
  91. </div>
  92. </div>
  93. <p>Here is one of our previous examples with fog added. The only addition
  94. is right after setting up the scene we add the fog and set the scene's
  95. background color</p>
  96. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  97. +{
  98. + const near = 1;
  99. + const far = 2;
  100. + const color = 'lightblue';
  101. + scene.fog = new THREE.Fog(color, near, far);
  102. + scene.background = new THREE.Color(color);
  103. +}
  104. </pre>
  105. <p>In the example below the camera's <code class="notranslate" translate="no">near</code> is 0.1 and its <code class="notranslate" translate="no">far</code> is 5.
  106. The camera is at <code class="notranslate" translate="no">z = 2</code>. The cubes are 1 unit large and at Z = 0.
  107. This means with a fog setting of <code class="notranslate" translate="no">near = 1</code> and <code class="notranslate" translate="no">far = 2</code> the cubes
  108. will fade out right around their center.</p>
  109. <p></p><div translate="no" class="threejs_example_container notranslate">
  110. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/fog.html"></iframe></div>
  111. <a class="threejs_center" href="/manual/examples/fog.html" target="_blank">click here to open in a separate window</a>
  112. </div>
  113. <p></p>
  114. <p>Let's add an interface so we can adjust the fog. Again we'll use
  115. <a href="https://github.com/georgealways/lil-gui">lil-gui</a>. lil-gui takes
  116. an object and a property and automagically makes an interface
  117. for that type of property. We could just simply let it manipulate
  118. the fog's <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> properties but it's invalid to have
  119. <code class="notranslate" translate="no">near</code> be greater than <code class="notranslate" translate="no">far</code> so let's make a helper so lil-gui
  120. can manipulate a <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> property but we'll make sure <code class="notranslate" translate="no">near</code>
  121. is less than or equal to <code class="notranslate" translate="no">far</code> and <code class="notranslate" translate="no">far</code> is greater than or equal <code class="notranslate" translate="no">near</code>.</p>
  122. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">// We use this class to pass to lil-gui
  123. // so when it manipulates near or far
  124. // near is never &gt; far and far is never &lt; near
  125. class FogGUIHelper {
  126. constructor(fog) {
  127. this.fog = fog;
  128. }
  129. get near() {
  130. return this.fog.near;
  131. }
  132. set near(v) {
  133. this.fog.near = v;
  134. this.fog.far = Math.max(this.fog.far, v);
  135. }
  136. get far() {
  137. return this.fog.far;
  138. }
  139. set far(v) {
  140. this.fog.far = v;
  141. this.fog.near = Math.min(this.fog.near, v);
  142. }
  143. }
  144. </pre>
  145. <p>We can then add it like this</p>
  146. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  147. const near = 1;
  148. const far = 2;
  149. const color = 'lightblue';
  150. scene.fog = new THREE.Fog(color, near, far);
  151. scene.background = new THREE.Color(color);
  152. +
  153. + const fogGUIHelper = new FogGUIHelper(scene.fog);
  154. + gui.add(fogGUIHelper, 'near', near, far).listen();
  155. + gui.add(fogGUIHelper, 'far', near, far).listen();
  156. }
  157. </pre>
  158. <p>The <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> parameters set the minimum and maximum values
  159. for adjusting the fog. They are set when we setup the camera.</p>
  160. <p>The <code class="notranslate" translate="no">.listen()</code> at the end of the last 2 lines tells lil-gui to <em>listen</em>
  161. for changes. That way when we change <code class="notranslate" translate="no">near</code> because of an edit to <code class="notranslate" translate="no">far</code>
  162. or we change <code class="notranslate" translate="no">far</code> in response to an edit to <code class="notranslate" translate="no">near</code> lil-gui will update
  163. the other property's UI for us.</p>
  164. <p>It might also be nice to be able to change the fog color but like was
  165. mentioned above we need to keep both the fog color and the background
  166. color in sync. So, let's add another <em>virtual</em> property to our helper
  167. that will set both colors when lil-gui manipulates it.</p>
  168. <p>lil-gui can manipulate colors in 4 ways, as a CSS 6 digit hex string (eg: <code class="notranslate" translate="no">#112233</code>). As an hue, saturation, value, object (eg: <code class="notranslate" translate="no">{h: 60, s: 1, v: }</code>).
  169. As an RGB array (eg: <code class="notranslate" translate="no">[255, 128, 64]</code>). Or, as an RGBA array (eg: <code class="notranslate" translate="no">[127, 200, 75, 0.3]</code>).</p>
  170. <p>It's easiest for our purpose to use the hex string version since that way
  171. lil-gui is only manipulating a single value. Fortunately <a href="/docs/#api/en/math/Color"><code class="notranslate" translate="no">THREE.Color</code></a>
  172. as a <a href="/docs/#api/en/math/Color#getHexString"><code class="notranslate" translate="no">getHexString</code></a> method
  173. we get use to easily get such a string, we just have to prepend a '#' to the front.</p>
  174. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">// We use this class to pass to lil-gui
  175. // so when it manipulates near or far
  176. // near is never &gt; far and far is never &lt; near
  177. +// Also when lil-gui manipulates color we'll
  178. +// update both the fog and background colors.
  179. class FogGUIHelper {
  180. * constructor(fog, backgroundColor) {
  181. this.fog = fog;
  182. + this.backgroundColor = backgroundColor;
  183. }
  184. get near() {
  185. return this.fog.near;
  186. }
  187. set near(v) {
  188. this.fog.near = v;
  189. this.fog.far = Math.max(this.fog.far, v);
  190. }
  191. get far() {
  192. return this.fog.far;
  193. }
  194. set far(v) {
  195. this.fog.far = v;
  196. this.fog.near = Math.min(this.fog.near, v);
  197. }
  198. + get color() {
  199. + return `#${this.fog.color.getHexString()}`;
  200. + }
  201. + set color(hexString) {
  202. + this.fog.color.set(hexString);
  203. + this.backgroundColor.set(hexString);
  204. + }
  205. }
  206. </pre>
  207. <p>We then call <code class="notranslate" translate="no">gui.addColor</code> to add a color UI for our helper's virtual property.</p>
  208. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  209. const near = 1;
  210. const far = 2;
  211. const color = 'lightblue';
  212. scene.fog = new THREE.Fog(color, near, far);
  213. scene.background = new THREE.Color(color);
  214. * const fogGUIHelper = new FogGUIHelper(scene.fog, scene.background);
  215. gui.add(fogGUIHelper, 'near', near, far).listen();
  216. gui.add(fogGUIHelper, 'far', near, far).listen();
  217. + gui.addColor(fogGUIHelper, 'color');
  218. }
  219. </pre>
  220. <p></p><div translate="no" class="threejs_example_container notranslate">
  221. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/fog-gui.html"></iframe></div>
  222. <a class="threejs_center" href="/manual/examples/fog-gui.html" target="_blank">click here to open in a separate window</a>
  223. </div>
  224. <p></p>
  225. <p>You can see setting <code class="notranslate" translate="no">near</code> to like 1.9 and <code class="notranslate" translate="no">far</code> to 2.0 gives
  226. a very sharp transition between un-fogged and completely fogged.
  227. where as <code class="notranslate" translate="no">near</code> = 1.1 and <code class="notranslate" translate="no">far</code> = 2.9 should just about be
  228. the smoothest given our cubes are spinning 2 units away from the camera.</p>
  229. <p>One last thing, there is a boolean <a href="/docs/#api/en/materials/Material#fog"><code class="notranslate" translate="no">fog</code></a>
  230. property on a material for whether or not objects rendered
  231. with that material are affected by fog. It defaults to <code class="notranslate" translate="no">true</code>
  232. for most materials. As an example of why you might want
  233. to turn the fog off, imagine you're making a 3D vehicle
  234. simulator with a view from the driver's seat or cockpit.
  235. You probably want the fog off for everything inside the vehicle when
  236. viewing from inside the vehicle.</p>
  237. <p>A better example might be a house
  238. and thick fog outside house. Let's say the fog is set to start
  239. 2 meters away (near = 2) and completely fogged out at 4 meters (far = 4).
  240. Rooms are longer than 2 meters and the house is probably longer
  241. than 4 meters so you need to set the materials for the inside
  242. of the house to not apply fog otherwise when standing inside the
  243. house looking outside the wall at the far end of the room will look
  244. like it's in the fog.</p>
  245. <div class="spread">
  246. <div>
  247. <div data-diagram="fogHouseAll" style="height: 300px;" class="border"></div>
  248. <div class="code">fog: true, all</div>
  249. </div>
  250. </div>
  251. <p>Notice the walls and ceiling at the far end of the room are getting fog applied.
  252. By turning fog off on the materials for the house we can fix that issue.</p>
  253. <div class="spread">
  254. <div>
  255. <div data-diagram="fogHouseInsideNoFog" style="height: 300px;" class="border"></div>
  256. <div class="code">fog: true, only outside materials</div>
  257. </div>
  258. </div>
  259. <p><canvas id="c"></canvas></p>
  260. <script type="module" src="../resources/threejs-fog.js"></script>
  261. </div>
  262. </div>
  263. </div>
  264. <script src="../resources/prettify.js"></script>
  265. <script src="../resources/lesson.js"></script>
  266. </body></html>