fog.html 15 KB

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