fog.html 15 KB

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