fog.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <!DOCTYPE html><html lang="ja"><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. </head>
  14. <body>
  15. <div class="container">
  16. <div class="lesson-title">
  17. <h1>のフォグ</h1>
  18. </div>
  19. <div class="lesson">
  20. <div class="lesson-main">
  21. <p>この記事はThree.jsの連載記事の1つです。
  22. 最初の記事は<a href="fundamentals.html">Three.jsの基礎知識</a>です。
  23. まだ読んでいない場合、そこから始めると良いかもしれません。
  24. カメラの記事を読んでない方は、まずは<a href="cameras.html">この記事</a>を読んでみて下さい。</p>
  25. <p>一般的には3Dエンジンでのフォグ(霧)は、カメラからの距離によって特定の色にフェードアウトする方法です。
  26. three.jsでは <a href="/docs/#api/ja/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a> または <a href="/docs/#api/ja/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> オブジェクトを作成し、シーンに<a href="/docs/#api/ja/scenes/Scene#fog"><code class="notranslate" translate="no">fog</code></a> プロパティを設定してフォグを追加します。</p>
  27. <p><a href="/docs/#api/ja/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a> はカメラからの距離を表す <code class="notranslate" translate="no">near</code> と <code class="notranslate" translate="no">far</code> があります。
  28. <code class="notranslate" translate="no">near</code> よりも近いものはフォグの影響を受けません。
  29. <code class="notranslate" translate="no">far</code> より遠いものはフォグの影響を受けます。
  30. <code class="notranslate" translate="no">near</code> と <code class="notranslate" translate="no">far</code> の中間部分では、マテリアルの色からフォグの色にグラデーションします。</p>
  31. <p>また、カメラからの距離で急激にグラデーションする <a href="/docs/#api/ja/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> もあります。</p>
  32. <p>どちらのタイプのフォグも使用するにはフォグを作成してシーンに割り当てます。</p>
  33. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  34. {
  35. const color = 0xFFFFFF; // white
  36. const near = 10;
  37. const far = 100;
  38. scene.fog = new THREE.Fog(color, near, far);
  39. }
  40. </pre>
  41. <p><a href="/docs/#api/ja/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> の場合は次のようなコードになります。</p>
  42. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  43. {
  44. const color = 0xFFFFFF;
  45. const density = 0.1;
  46. scene.fog = new THREE.FogExp2(color, density);
  47. }
  48. </pre>
  49. <p><a href="/docs/#api/ja/scenes/FogExp2"><code class="notranslate" translate="no">FogExp2</code></a> は現実表現に近いですが、<a href="/docs/#api/ja/scenes/Fog"><code class="notranslate" translate="no">Fog</code></a> の方が一般的によく使われています。
  50. Fogは適用する場所を選択できるので、ある距離まではクリアなシーンを表示し、その距離を過ぎるとフェードアウトした色にできます。</p>
  51. <div class="spread">
  52. <div>
  53. <div data-diagram="fog" style="height: 300px;"></div>
  54. <div class="code">THREE.Fog</div>
  55. </div>
  56. <div>
  57. <div data-diagram="fogExp2" style="height: 300px;"></div>
  58. <div class="code">THREE.FogExp2</div>
  59. </div>
  60. </div>
  61. <p>ここで注意すべき点は、フォグは <em>レンダリングされる</em> ものに適用される事です。
  62. 以下はオブジェクトの色の各ピクセルの計算の一部です。
  63. つまり、シーンを特定の色にフェードさせたい場合、フォグ <strong>と</strong> 背景色を同じ色に設定します。
  64. 背景色は <a href="/docs/#api/ja/scenes/Scene#background"><code class="notranslate" translate="no">scene.background</code></a> プロパティで設定します。
  65. 背景色は <a href="/docs/#api/ja/math/Color"><code class="notranslate" translate="no">THREE.Color</code></a> で指定します。例えば</p>
  66. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">scene.background = new THREE.Color('#F00'); // red
  67. </pre>
  68. <div class="spread">
  69. <div>
  70. <div data-diagram="fogBlueBackgroundRed" style="height: 300px;" class="border"></div>
  71. <div class="code">fog blue, background red</div>
  72. </div>
  73. <div>
  74. <div data-diagram="fogBlueBackgroundBlue" style="height: 300px;" class="border"></div>
  75. <div class="code">fog blue, background blue</div>
  76. </div>
  77. </div>
  78. <p>以下はフォグを追加した例です。
  79. シーンにフォグを追加し背景色を設定します。</p>
  80. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  81. +{
  82. + const near = 1;
  83. + const far = 2;
  84. + const color = 'lightblue';
  85. + scene.fog = new THREE.Fog(color, near, far);
  86. + scene.background = new THREE.Color(color);
  87. +}
  88. </pre>
  89. <p>以下の例ではカメラの <code class="notranslate" translate="no">near</code> は 0.1、<code class="notranslate" translate="no">far</code> は 5です。
  90. カメラは <code class="notranslate" translate="no">z = 2</code> にあります。
  91. 立方体は1の大きさで z = 0 です。
  92. つまり、フォグを <code class="notranslate" translate="no">near = 1</code> と <code class="notranslate" translate="no">far = 2</code> と設定し、立方体の中心付近でフェードアウトしています。</p>
  93. <p></p><div translate="no" class="threejs_example_container notranslate">
  94. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/fog.html"></iframe></div>
  95. <a class="threejs_center" href="/manual/examples/fog.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  96. </div>
  97. <p></p>
  98. <p>フォグを調整するインターフェースを追加してみましょう。
  99. ここでも<a href="https://github.com/georgealways/lil-gui">lil-gui</a>を使用します。
  100. lil-guiはオブジェクトとプロパティを受け取り、インタフェースを自動生成します。
  101. これでフォグの <code class="notranslate" translate="no">near</code> と <code class="notranslate" translate="no">far</code> プロパティを簡単に操作できます。
  102. しかし、 <code class="notranslate" translate="no">near</code> が <code class="notranslate" translate="no">far</code> より大きい場合は無効になります。
  103. lil-guiで <code class="notranslate" translate="no">near</code> と <code class="notranslate" translate="no">far</code> を操作するヘルパーを作ってみましょう。</p>
  104. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">// We use this class to pass to lil-gui
  105. // so when it manipulates near or far
  106. // near is never &gt; far and far is never &lt; near
  107. class FogGUIHelper {
  108. constructor(fog) {
  109. this.fog = fog;
  110. }
  111. get near() {
  112. return this.fog.near;
  113. }
  114. set near(v) {
  115. this.fog.near = v;
  116. this.fog.far = Math.max(this.fog.far, v);
  117. }
  118. get far() {
  119. return this.fog.far;
  120. }
  121. set far(v) {
  122. this.fog.far = v;
  123. this.fog.near = Math.min(this.fog.near, v);
  124. }
  125. }
  126. </pre>
  127. <p>次のコードを追加します。</p>
  128. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  129. const near = 1;
  130. const far = 2;
  131. const color = 'lightblue';
  132. scene.fog = new THREE.Fog(color, near, far);
  133. scene.background = new THREE.Color(color);
  134. +
  135. + const fogGUIHelper = new FogGUIHelper(scene.fog);
  136. + gui.add(fogGUIHelper, 'near', near, far).listen();
  137. + gui.add(fogGUIHelper, 'far', near, far).listen();
  138. }
  139. </pre>
  140. <p><code class="notranslate" translate="no">near</code> と <code class="notranslate" translate="no">far</code> のパラメーターは、フォグを調整する最小値と最大値を設定します。
  141. これはカメラ設定時にセットします。</p>
  142. <p>最後の2行の <code class="notranslate" translate="no">.listen()</code> をlil-guiに変更し <em>listen</em> するようにします。
  143. これで <code class="notranslate" translate="no">near</code> や <code class="notranslate" translate="no">far</code> の変更時、lil-guiが他のプロパティのUIを更新してくれます。</p>
  144. <p>フォグの色を変更できますが、上記で述べたようにフォグの色と背景色を同期させる必要があります。
  145. lil-gui操作時に両方の色を変更する <em>virtual</em> プロパティをヘルパーに追加してみましょう。</p>
  146. <p>lil-guiでは4つの方法で色を操作できます。</p>
  147. <ol>
  148. <li><p>CSSの6桁の16進数(例: <code class="notranslate" translate="no">#112233</code>)</p>
  149. </li>
  150. <li><p>色相、彩度、値、オブジェクト (例: <code class="notranslate" translate="no">{h: 60, s: 1, v: }</code>)</p>
  151. </li>
  152. <li><p>RGB (例: <code class="notranslate" translate="no">[255, 128, 64]</code>)</p>
  153. </li>
  154. <li><p>RGBA(例:<code class="notranslate" translate="no">[127, 200, 75, 0.3]</code>)</p>
  155. </li>
  156. </ol>
  157. <p>lil-guiが単一の値を操作するので、16進数を使うのが一番簡単です。
  158. 幸運な事に <a href="/docs/#api/ja/math/Color"><code class="notranslate" translate="no">THREE.Color</code></a> で <a href="/docs/#api/ja/math/Color#getHexString"><code class="notranslate" translate="no">getHexString</code></a> を使用でき、文字列を簡単に取得できます。</p>
  159. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">// We use this class to pass to lil-gui
  160. // so when it manipulates near or far
  161. // near is never &gt; far and far is never &lt; near
  162. +// Also when lil-gui manipulates color we'll
  163. +// update both the fog and background colors.
  164. class FogGUIHelper {
  165. * constructor(fog, backgroundColor) {
  166. this.fog = fog;
  167. + this.backgroundColor = backgroundColor;
  168. }
  169. get near() {
  170. return this.fog.near;
  171. }
  172. set near(v) {
  173. this.fog.near = v;
  174. this.fog.far = Math.max(this.fog.far, v);
  175. }
  176. get far() {
  177. return this.fog.far;
  178. }
  179. set far(v) {
  180. this.fog.far = v;
  181. this.fog.near = Math.min(this.fog.near, v);
  182. }
  183. + get color() {
  184. + return `#${this.fog.color.getHexString()}`;
  185. + }
  186. + set color(hexString) {
  187. + this.fog.color.set(hexString);
  188. + this.backgroundColor.set(hexString);
  189. + }
  190. }
  191. </pre>
  192. <p><code class="notranslate" translate="no">gui.addColor</code> を呼び出し、ヘルパーのvirtualプロパティにcolorのlil-guiを追加します。</p>
  193. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  194. const near = 1;
  195. const far = 2;
  196. const color = 'lightblue';
  197. scene.fog = new THREE.Fog(color, near, far);
  198. scene.background = new THREE.Color(color);
  199. * const fogGUIHelper = new FogGUIHelper(scene.fog, scene.background);
  200. gui.add(fogGUIHelper, 'near', near, far).listen();
  201. gui.add(fogGUIHelper, 'far', near, far).listen();
  202. + gui.addColor(fogGUIHelper, 'color');
  203. }
  204. </pre>
  205. <p></p><div translate="no" class="threejs_example_container notranslate">
  206. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/fog-gui.html"></iframe></div>
  207. <a class="threejs_center" href="/manual/examples/fog-gui.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  208. </div>
  209. <p></p>
  210. <p><code class="notranslate" translate="no">near</code> を1.9、<code class="notranslate" translate="no">far</code> を2.0にすると以下のようになりました。
  211. 曇っていない状態と完全に曇っている状態との中間では、シャープなグラデーションします。
  212. <code class="notranslate" translate="no">near</code> = 1.1、<code class="notranslate" translate="no">far</code> = 2.9 とすると、カメラから 2 離れて回転する立方体で最も滑らかになります。</p>
  213. <p>最後に、マテリアルでレンダリングされたオブジェクトがフォグの影響を受けるか判断するために、マテリアルにはboolean型の<a href="/docs/#api/ja/materials/Material#fog"><code class="notranslate" translate="no">fog</code></a>プロパティがあります。
  214. そのマテリアルを使用してる場合は、フォグの影響を受けます。
  215. ほとんどのマテリアルのデフォルトは <code class="notranslate" translate="no">true</code> です。
  216. フォグを消去する理由は、運転席やコックピットからの視点で3Dの車のシミュレーターを作っている時を想像して下さい。
  217. 車内から見ると、車内の全てのものはフォグを外しておきたいと思うでしょう。</p>
  218. <p>フォグの良い例としては、家の外に濃いフォグが出ている場合が挙げられます。
  219. 例えば、フォグが2m先(near = 2)から始まり、4m先(far = 4)でフォグがあるとします。
  220. 部屋の長さは2メートル以上、家の長さは4メートル以上で、家の中にフォグがかからないように設定が必要です。
  221. 設定しない場合に家の中に立っている時に部屋の奥の壁の外を見ると、フォグの中にいるように見えてしまいます。</p>
  222. <div class="spread">
  223. <div>
  224. <div data-diagram="fogHouseAll" style="height: 300px;" class="border"></div>
  225. <div class="code">fog: true, all</div>
  226. </div>
  227. </div>
  228. <p>部屋の奥の壁と天井にフォグがかかっています。
  229. 家のマテリアルのフォグをオフにすると、この問題が解決できます。</p>
  230. <div class="spread">
  231. <div>
  232. <div data-diagram="fogHouseInsideNoFog" style="height: 300px;" class="border"></div>
  233. <div class="code">fog: true, only outside materials</div>
  234. </div>
  235. </div>
  236. <p><canvas id="c"></canvas></p>
  237. <script type="module" src="../resources/threejs-fog.js"></script>
  238. </div>
  239. </div>
  240. </div>
  241. <script src="/manual/resources/prettify.js"></script>
  242. <script src="/manual/resources/lesson.js"></script>
  243. </body></html>