fog.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <!DOCTYPE html><html lang="ko"><head>
  2. <meta charset="utf-8">
  3. <title>안개</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 – 안개">
  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="/manual/resources/lesson.css">
  12. <link rel="stylesheet" href="/manual/resources/lang.css">
  13. <link rel="stylesheet" href="/manual/ko/lang.css">
  14. </head>
  15. <body>
  16. <div class="container">
  17. <div class="lesson-title">
  18. <h1>안개</h1>
  19. </div>
  20. <div class="lesson">
  21. <div class="lesson-main">
  22. <p>※ 이 글은 Three.js의 튜토리얼 시리즈로서,
  23. 먼저 <a href="fundamentals.html">Three.js의 기본 구조에 관한 글</a>을
  24. 읽고 오길 권장합니다.</p>
  25. <p>※ 카메라에 대해 잘 모른다면, 먼저 <a href="cameras.html">카메라에 관한 글</a>을
  26. 먼저 읽기 바랍니다.</p>
  27. <p>3D 엔진에서 안개란, 일반적으로 카메라로부터의 거리에 따라 특정 색상으로
  28. 점차 변화하는 것을 말합니다. Three.js에서는 <a href="/docs/#api/ko/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a>나 <a href="/docs/#api/ko/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> 객체를
  29. 생성한 뒤, 장면(scene)의 <a href="/docs/#api/ko/scenes/Scene#fog"><code class="notranslate" translate="no">fog</code></a> 속성에 지정해 안개를 사용합니다.</p>
  30. <p><a href="/docs/#api/ko/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a>는 인자로 <code class="notranslate" translate="no">near</code>와 <code class="notranslate" translate="no">far</code>값을 받는데, 이는 카메라로부터의 거리값입니다.
  31. <code class="notranslate" translate="no">near</code>값보다 가까운 공간은 안개의 영향이 전혀 없고, <code class="notranslate" translate="no">far</code>값보다 먼 공간은
  32. 완전히 안개에 뒤덮입니다. <code class="notranslate" translate="no">near</code>와 <code class="notranslate" translate="no">far</code> 사이의 공간에 있는 물체 또는 물체의
  33. 일부는 점차 안개의 색으로 변화하죠.</p>
  34. <p><a href="/docs/#api/ko/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a>는 카메라에서 멀어질수록 안개의 강도가 강해집니다.</p>
  35. <p>두 가지 안개 모두 마찬가지로, 안개를 사용하려면 장면의 속성에 지정해야 합니다.</p>
  36. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  37. {
  38. const color = 0xFFFFFF; // 하양
  39. const near = 10;
  40. const far = 100;
  41. scene.fog = new THREE.Fog(color, near, far);
  42. }
  43. </pre>
  44. <p><a href="/docs/#api/ko/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a>의 경우는 다음처럼 쓸 수 있죠.</p>
  45. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  46. {
  47. const color = 0xFFFFFF;
  48. const density = 0.1;
  49. scene.fog = new THREE.FogExp2(color, density);
  50. }
  51. </pre>
  52. <p><a href="/docs/#api/ko/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a>가 더 현실적이긴 하나, 보통 안개의 범위를 특정하기 쉬운
  53. <a href="/docs/#api/ko/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a>를 더 많이 사용합니다.</p>
  54. <div class="spread">
  55. <div>
  56. <div data-diagram="fog"></div>
  57. <div class="code">THREE.Fog</div>
  58. </div>
  59. <div>
  60. <div data-diagram="fogExp2"></div>
  61. <div class="code">THREE.FogExp2</div>
  62. </div>
  63. </div>
  64. <p>한 가지 알아둬야 하는 건 안개는 <em>렌더링되는 물체</em>라는 점입니다.
  65. 안개는 물체의 픽셀을 렌더링할 때 같이 렌더링되는데, 이 말은 장면에
  66. 특정 색상의 안개 효과를 주려면 안개와 배경색 <strong>둘 다</strong> 같은 색으로
  67. 지정해야 한다는 겁니다. 배경색은 <a href="/docs/#api/ko/scenes/Scene#background"><code class="notranslate" translate="no">scene.background</code></a>
  68. 속성을 <a href="/docs/#api/ko/math/Color"><code class="notranslate" translate="no">THREE.Color</code></a> 인스턴스로 지정해 바꿀 수 있습니다.</p>
  69. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">scene.background = new THREE.Color('#F00'); // 빨강
  70. </pre>
  71. <div class="spread">
  72. <div>
  73. <div data-diagram="fogBlueBackgroundRed" style="height:300px" class="border"></div>
  74. <div class="code">파란 안개, 빨간 배경</div>
  75. </div>
  76. <div>
  77. <div data-diagram="fogBlueBackgroundBlue" style="height:300px" class="border"></div>
  78. <div class="code">파란 안개, 파란 배경</div>
  79. </div>
  80. </div>
  81. <p>아래는 이전에 사용했던 예제에 안개를 추가한 것입니다. 장면을 생성한 뒤
  82. 안개를 추가하고, 장면의 배경색을 바꾸기만 했죠.</p>
  83. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  84. +{
  85. + const near = 1;
  86. + const far = 2;
  87. + const color = 'lightblue';
  88. + scene.fog = new THREE.Fog(color, near, far);
  89. + scene.background = new THREE.Color(color);
  90. +}
  91. </pre>
  92. <p>아래 예제의 카메라는 <code class="notranslate" translate="no">near</code>값이 0.1, <code class="notranslate" translate="no">far</code>값이 5입니다. 카메라의 위치는
  93. <code class="notranslate" translate="no">z = 2</code>이죠. 정육면체의 크기는 1칸이고, z축의 원점에 있습니다. 여기서
  94. 안개를 <code class="notranslate" translate="no">near = 1</code>, <code class="notranslate" translate="no">far = 2</code>로 설정하면 정육면체가 중간부터 사라지기
  95. 시작하겠죠.</p>
  96. <p></p><div translate="no" class="threejs_example_container notranslate">
  97. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/fog.html"></iframe></div>
  98. <a class="threejs_center" href="/manual/examples/fog.html" target="_blank">새 탭에서 보기</a>
  99. </div>
  100. <p></p>
  101. <p>인터페이스를 추가해 안개를 조정할 수 있도록 하겠습니다. 이번에도 <a href="https://github.com/georgealways/lil-gui">lil-gui</a>를
  102. 사용할 거예요. lil-gui는 객체와 객체의 속성 키값을 받아 자동으로 인터페이스를
  103. 생성합니다. 단순히 안개의 <code class="notranslate" translate="no">near</code>와 <code class="notranslate" translate="no">far</code> 제어하도록 설정할 수도 있지만, <code class="notranslate" translate="no">near</code>값이
  104. <code class="notranslate" translate="no">far</code>값보다 큰 경우는 없기에 헬퍼를 만들어 <code class="notranslate" translate="no">near</code>값을 항상 <code class="notranslate" translate="no">far</code>보다 같거나
  105. 작게, <code class="notranslate" translate="no">far</code>값을 항상 <code class="notranslate" translate="no">near</code>보다 같거나 크게 설정하도록 하겠습니다.</p>
  106. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">/**
  107. * 이 클래스의 인스턴스를 lil-gui에 넘겨
  108. * near나 far 속성을 조정할 때 항상
  109. * near는 never &gt;= far, far는 never &lt;= near가 되도록 합니다
  110. **/
  111. class FogGUIHelper {
  112. constructor(fog) {
  113. this.fog = fog;
  114. }
  115. get near() {
  116. return this.fog.near;
  117. }
  118. set near(v) {
  119. this.fog.near = v;
  120. this.fog.far = Math.max(this.fog.far, v);
  121. }
  122. get far() {
  123. return this.fog.far;
  124. }
  125. set far(v) {
  126. this.fog.far = v;
  127. this.fog.near = Math.min(this.fog.near, v);
  128. }
  129. }
  130. </pre>
  131. <p>방금 만든 클래스를 아래처럼 활용합니다.</p>
  132. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  133. const near = 1;
  134. const far = 2;
  135. const color = 'lightblue';
  136. scene.fog = new THREE.Fog(color, near, far);
  137. scene.background = new THREE.Color(color);
  138. +
  139. + const fogGUIHelper = new FogGUIHelper(scene.fog);
  140. + gui.add(fogGUIHelper, 'near', near, far).listen();
  141. + gui.add(fogGUIHelper, 'far', near, far).listen();
  142. }
  143. </pre>
  144. <p><code class="notranslate" translate="no">near</code>와 <code class="notranslate" translate="no">far</code> 인자는 각 안개 속성의 최솟값과 최댓값입니다.</p>
  145. <p>마지막 2줄의 <code class="notranslate" translate="no">.listen()</code> 메서드를 호출하면 lil-gui가 변화를 <em>감지</em>합니다.
  146. <code class="notranslate" translate="no">near</code> 속성을 바꿀 때 동시에 <code class="notranslate" translate="no">far</code> 속성을 재할당하고, <code class="notranslate" translate="no">far</code> 속성을 바꿀 때도
  147. 동시에 <code class="notranslate" translate="no">near</code>를 재할당하는데, 이 메서드를 호출하면 조작한 속성 외의 다른
  148. 속성의 변화도 UI에 업데이트됩니다.</p>
  149. <p>여기에 안개의 색까지 조정할 수 있으면 금상첨화겠네요. 하지만 아까 설명했듯
  150. 안개의 색을 바꾸려면 배경색도 같이 바꿔야 합니다. 헬퍼 클래스에 <em>가상</em> 속성을
  151. 하나 만들어 lil-gui가 이 속성을 변경할 때 배경색과 안개색을 같은 값으로
  152. 바꿔주면 어떨까요?</p>
  153. <p>lil-gui의 색상 타입은 4가지입니다. 하나는 CSS의 6자리 16진수 문자열(hex string, 예: <code class="notranslate" translate="no">#f8f8f8</code>)이고,
  154. 하나는 hue(색상), saturation(채도), value 객체(예: <code class="notranslate" translate="no">{ h: 60, s: 1, v: 0 }</code>),
  155. 하나는 RGB 배열(예: <code class="notranslate" translate="no">[ 255, 128, 64 ]</code>) 또는 RGBA 색상 배열(예: <code class="notranslate" translate="no">[ 127, 200, 75, 0.3 ]</code>)이죠.</p>
  156. <p><code class="notranslate" translate="no">lil-gui</code>가 하나의 값만 조작하도록 하는 게 제일 간단하니, 16진수 문자열을 사용하겠습니다.
  157. 다행히 <a href="/docs/#api/ko/math/Color"><code class="notranslate" translate="no">THREE.Color</code></a>에는 <a href="/docs/#api/ko/math/Color#getHexString"><code class="notranslate" translate="no">getHexString</code></a> 메서드가 있어 색상을
  158. 문자열로 쉽게 바꿀 수 있죠. 앞에 '#'만 덧붙이면 됩니다.</p>
  159. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">/**
  160. * 이 클래스의 인스턴스를 lil-gui에 넘겨
  161. * near나 far 속성을 조정할 때 항상
  162. * near는 never &gt;= far, far는 never &lt;= near가 되도록 합니다
  163. **/
  164. +// 또 lil-gui가 color 속성을 조작할 때 안개와 배경색을 동시에 변경합니다
  165. class FogGUIHelper {
  166. * constructor(fog, backgroundColor) {
  167. this.fog = fog;
  168. + this.backgroundColor = backgroundColor;
  169. }
  170. get near() {
  171. return this.fog.near;
  172. }
  173. set near(v) {
  174. this.fog.near = v;
  175. this.fog.far = Math.max(this.fog.far, v);
  176. }
  177. get far() {
  178. return this.fog.far;
  179. }
  180. set far(v) {
  181. this.fog.far = v;
  182. this.fog.near = Math.min(this.fog.near, v);
  183. }
  184. + get color() {
  185. + return `#${this.fog.color.getHexString()}`;
  186. + }
  187. + set color(hexString) {
  188. + this.fog.color.set(hexString);
  189. + this.backgroundColor.set(hexString);
  190. + }
  191. }
  192. </pre>
  193. <p>이번에는 <code class="notranslate" translate="no">gui.addColor</code> 메서드를 호출합니다. 색상 UI를 생성하는 메서드로,
  194. 방금 추가한 가상 속성을 조작하도록 설정합니다.</p>
  195. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  196. const near = 1;
  197. const far = 2;
  198. const color = 'lightblue';
  199. scene.fog = new THREE.Fog(color, near, far);
  200. scene.background = new THREE.Color(color);
  201. * const fogGUIHelper = new FogGUIHelper(scene.fog, scene.background);
  202. gui.add(fogGUIHelper, 'near', near, far).listen();
  203. gui.add(fogGUIHelper, 'far', near, far).listen();
  204. + gui.addColor(fogGUIHelper, 'color');
  205. }
  206. </pre>
  207. <p></p><div translate="no" class="threejs_example_container notranslate">
  208. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/fog-gui.html"></iframe></div>
  209. <a class="threejs_center" href="/manual/examples/fog-gui.html" target="_blank">새 탭에서 보기</a>
  210. </div>
  211. <p></p>
  212. <p><code class="notranslate" translate="no">near</code>를 1.9 정도, <code class="notranslate" translate="no">far</code>를 2.0 정도로 설정하면 안개의 경계가 굉장히
  213. 선명해질 겁니다. 정육면체들이 카메라에서 2칸 떨어져 있으므로 <code class="notranslate" translate="no">near</code>를
  214. 1.1, <code class="notranslate" translate="no">far</code>를 2.9 정도로 설정하면 경계가 가장 부드러운 것이라고 할 수
  215. 있죠.</p>
  216. <p>추가로 재질(material)에는 불린 타입의 <a href="/docs/#api/ko/materials/Material#fog"><code class="notranslate" translate="no">fog</code></a> 속성이 있습니다.
  217. 해당 재질로 렌더링되는 물체가 안개의 영향을 받을지의 여부를 결정하는 속성이죠.
  218. "안개 효과를 없애버리면 그만 아닌가?" 생각할 수 있지만, 3D 운전 시뮬레이터를
  219. 만드는 경우를 상상해봅시다. 차 밖은 안개가 자욱하더라도 차 안에서 볼 때 차 내부는
  220. 깔끔해야 할 수도 있죠.</p>
  221. <p>안개가 짙은 날, 집 안에서 창 밖을 바라보는 장면이 더 와닿을지도 모르겠네요.
  222. 안개가 카메라로부터 2미터 이후부터 끼기 시작하고(near = 2), 4미터 이후에는
  223. 완전히 안개에 덮히도록(far = 4) 설정합니다. 방은 2미터이고, 집은 최소 4미터입니다.
  224. 여기서 집 안의 재질이 안개의 영향을 받도록 놔둔다면 방 끝에서 창 밖을 바라볼
  225. 때 방 안도 안개가 낀 것처럼 보이겠죠.</p>
  226. <div class="spread">
  227. <div>
  228. <div data-diagram="fogHouseAll" style="height: 300px;" class="border"></div>
  229. <div class="code">모든 재질의 fog: true</div>
  230. </div>
  231. </div>
  232. <p>방 끝 쪽 천장과 벽에 안개가 낀 것이 보일 겁니다. 집 내부 재질의 fog 옵션을 끄면
  233. 안개를 없앨 수 있죠.</p>
  234. <div class="spread">
  235. <div>
  236. <div data-diagram="fogHouseInsideNoFog" style="height: 300px;" class="border"></div>
  237. <div class="code">집 밖 물체의 재질만 fog: true</div>
  238. </div>
  239. </div>
  240. <p><canvas id="c"></canvas></p>
  241. <script type="module" src="../resources/threejs-fog.js"></script>
  242. </div>
  243. </div>
  244. </div>
  245. <script src="/manual/resources/prettify.js"></script>
  246. <script src="/manual/resources/lesson.js"></script>
  247. </body></html>