backgrounds.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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>このサイトのほとんどの記事では、背景に無地の色を使っています。</p>
  32. <p>静的な背景として追加するには、CSSを設定するだけで簡単にできます。
  33. <a href="responsive.html">Three.jsのレスポンシブデザインの記事</a>を例にすると、変更が必要なのは2箇所だけです。</p>
  34. <p>キャンバスにCSSを追加して背景を画像に設定する必要があります。</p>
  35. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;style&gt;
  36. body {
  37. margin: 0;
  38. }
  39. #c {
  40. width: 100%;
  41. height: 100%;
  42. display: block;
  43. + background: url(resources/images/daikanyama.jpg) no-repeat center center;
  44. + background-size: cover;
  45. }
  46. &lt;/style&gt;
  47. </pre>
  48. <p>そして、何も描画していない場所が透明になるように <a href="/docs/#api/ja/renderers/WebGLRenderer"><code class="notranslate" translate="no">WebGLRenderer</code></a> に <code class="notranslate" translate="no">alpha</code> を指定します。</p>
  49. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
  50. const canvas = document.querySelector('#c');
  51. - const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  52. + const renderer = new THREE.WebGLRenderer({
  53. + antialias: true,
  54. + canvas,
  55. + alpha: true,
  56. + });
  57. </pre>
  58. <p>これで背景を設定できました。</p>
  59. <p></p><div translate="no" class="threejs_example_container notranslate">
  60. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/background-css.html"></iframe></div>
  61. <a class="threejs_center" href="/manual/examples/background-css.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  62. </div>
  63. <p></p>
  64. <p>背景に<a href="post-processing.html">ポストプロセス効果</a>を与えたい場合は、Three.jsを使い背景を描画する必要があります。</p>
  65. <p>THREE.jsで簡単にできます。シーンの背景をテクスチャに設定すれば良いのです。</p>
  66. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const loader = new THREE.TextureLoader();
  67. const bgTexture = loader.load('resources/images/daikanyama.jpg');
  68. bgTexture.colorSpace = THREE.SRGBColorSpace;
  69. scene.background = bgTexture;
  70. </pre>
  71. <p>このようになります。</p>
  72. <p></p><div translate="no" class="threejs_example_container notranslate">
  73. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/background-scene-background.html"></iframe></div>
  74. <a class="threejs_center" href="/manual/examples/background-scene-background.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  75. </div>
  76. <p></p>
  77. <p>背景画像がありますが、画面に合わせて引き伸ばされています。</p>
  78. <p>この問題はテクスチャの <code class="notranslate" translate="no">repeat</code> と <code class="notranslate" translate="no">offset</code> プロパティを設定して画像の一部だけを表示する事で解決できます。</p>
  79. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  80. ...
  81. + // Set the repeat and offset properties of the background texture
  82. + // to keep the image's aspect correct.
  83. + // Note the image may not have loaded yet.
  84. + const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  85. + const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  86. + const aspect = imageAspect / canvasAspect;
  87. +
  88. + bgTexture.offset.x = aspect &gt; 1 ? (1 - 1 / aspect) / 2 : 0;
  89. + bgTexture.repeat.x = aspect &gt; 1 ? 1 / aspect : 1;
  90. +
  91. + bgTexture.offset.y = aspect &gt; 1 ? 0 : (1 - aspect) / 2;
  92. + bgTexture.repeat.y = aspect &gt; 1 ? 1 : aspect;
  93. ...
  94. renderer.render(scene, camera);
  95. requestAnimationFrame(render);
  96. }
  97. </pre>
  98. <p>今度はThree.jsで背景を描画してます。
  99. CSSの時と比べて目に見える違いはありませんが、<a href="post-processing.html">ポストプロセス効果</a>を使うと背景にも影響が出て違いがわかります。</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/background-scene-background-fixed-aspect.html"></iframe></div>
  102. <a class="threejs_center" href="/manual/examples/background-scene-background-fixed-aspect.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  103. </div>
  104. <p></p>
  105. <p>もちろん、静的な背景は通常は3Dシーンに求めるものではありません。
  106. その代わりに何らかの <em>スカイボックス</em> が欲しいです。
  107. スカイボックスとは空の絵が描かれたキューブの事です。
  108. キューブの中にカメラを入れると背景に空があるように見えます。</p>
  109. <p>スカイボックスを実装する最も一般的な方法は、キューブを作りテクスチャを適用して内側から描画する事です。
  110. キューブの各面に地平線の画像のようなテクスチャ(テクスチャ座標を使用)を貼り付けます。
  111. また、スカイボックスの代わりに天球やスカイドームを使う事もよくあります。
  112. それらでの実装は自分で調べればわかると思います。
  113. キューブや球体を作り<a href="textures.html">テクスチャを適用</a>し、<code class="notranslate" translate="no">THREE.BackSide</code> としてマークを付けます。
  114. そして、外側ではなく内側にレンダリングし、直接シーンに入れるか、上記のようにするか、2つのシーンを作るか、スカイボックス/球体/ドームを描くための特別なシーンと
  115. 他の全てのものを描画するために使用します。
  116. 描画には通常の <a href="/docs/#api/ja/cameras/PerspectiveCamera"><code class="notranslate" translate="no">PerspectiveCamera</code></a> を使います。<a href="/docs/#api/ja/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a> は必要ないです。</p>
  117. <p>もう1つの解決策は <em>キューブマップ</em> を使用する事です。
  118. キューブマップは、キューブの側面である6つの側面を持つ特殊な種類のテクスチャです。
  119. 標準的なテクスチャ座標を使用するのではなく、中心から外側に向けた方向を使用し、どこで色を取得するかを決定します。</p>
  120. <p>カリフォルニア州マウンテンビューにあるコンピュータ歴史博物館のキューブマップの画像6枚をご紹介します。</p>
  121. <div class="threejs_center">
  122. <img src="../examples/resources/images/cubemaps/computer-history-museum/pos-x.jpg" style="width: 200px" class="border">
  123. <img src="../examples/resources/images/cubemaps/computer-history-museum/neg-x.jpg" style="width: 200px" class="border">
  124. <img src="../examples/resources/images/cubemaps/computer-history-museum/pos-y.jpg" style="width: 200px" class="border">
  125. </div>
  126. <div class="threejs_center">
  127. <img src="../examples/resources/images/cubemaps/computer-history-museum/neg-y.jpg" style="width: 200px" class="border">
  128. <img src="../examples/resources/images/cubemaps/computer-history-museum/pos-z.jpg" style="width: 200px" class="border">
  129. <img src="../examples/resources/images/cubemaps/computer-history-museum/neg-z.jpg" style="width: 200px" class="border">
  130. </div>
  131. <p><a href="/docs/#api/ja/loaders/CubeTextureLoader"><code class="notranslate" translate="no">CubeTextureLoader</code></a> を使用してキューブマップ画像を読み込み、シーンの背景として使用します。</p>
  132. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  133. const loader = new THREE.CubeTextureLoader();
  134. const texture = loader.load([
  135. 'resources/images/cubemaps/computer-history-museum/pos-x.jpg',
  136. 'resources/images/cubemaps/computer-history-museum/neg-x.jpg',
  137. 'resources/images/cubemaps/computer-history-museum/pos-y.jpg',
  138. 'resources/images/cubemaps/computer-history-museum/neg-y.jpg',
  139. 'resources/images/cubemaps/computer-history-museum/pos-z.jpg',
  140. 'resources/images/cubemaps/computer-history-museum/neg-z.jpg',
  141. ]);
  142. scene.background = texture;
  143. }
  144. </pre>
  145. <p>レンダリング時には、repeatやoffsetプロパティでテクスチャを調整する必要はありません。</p>
  146. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  147. ...
  148. - // Set the repeat and offset properties of the background texture
  149. - // to keep the image's aspect correct.
  150. - // Note the image may not have loaded yet.
  151. - const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  152. - const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  153. - const aspect = imageAspect / canvasAspect;
  154. -
  155. - bgTexture.offset.x = aspect &gt; 1 ? (1 - 1 / aspect) / 2 : 0;
  156. - bgTexture.repeat.x = aspect &gt; 1 ? 1 / aspect : 1;
  157. -
  158. - bgTexture.offset.y = aspect &gt; 1 ? 0 : (1 - aspect) / 2;
  159. - bgTexture.repeat.y = aspect &gt; 1 ? 1 : aspect;
  160. ...
  161. renderer.render(scene, camera);
  162. requestAnimationFrame(render);
  163. }
  164. </pre>
  165. <p>カメラを回転できるようにコントロールを追加してみましょう。</p>
  166. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  167. </pre>
  168. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const fov = 75;
  169. const aspect = 2; // the canvas default
  170. const near = 0.1;
  171. -const far = 5;
  172. +const far = 100;
  173. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  174. -camera.position.z = 2;
  175. +camera.position.z = 3;
  176. +const controls = new OrbitControls(camera, canvas);
  177. +controls.target.set(0, 0, 0);
  178. +controls.update();
  179. </pre>
  180. <p>これを試してみて下さい。
  181. ドラッグしてカメラを回転させ、キューブマップが周囲を取り囲んでいるのを見てみましょう。</p>
  182. <p></p><div translate="no" class="threejs_example_container notranslate">
  183. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/background-cubemap.html"></iframe></div>
  184. <a class="threejs_center" href="/manual/examples/background-cubemap.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  185. </div>
  186. <p></p>
  187. <p>もう1つの方法は正距円筒図を使用する事です。
  188. <a href="https://google.com/search?q=360+camera">360カメラ</a>で撮るとこんな感じの写真になります。</p>
  189. <p><a href="https://hdrihaven.com">このサイト</a>で見つけたのが<a href="https://hdrihaven.com/hdri/?h=tears_of_steel_bridge">こちら</a>です。</p>
  190. <div class="threejs_center"><img src="../examples/resources/images/equirectangularmaps/tears_of_steel_bridge_2k.jpg" style="width: 600px"></div>
  191. <p>たいして変わらないですね。
  192. まず、正距円筒図をテクスチャとして読み込み、読み込み後のコールバックで <a href="/docs/#api/ja/renderers/WebGLCubeRenderTarget.fromEquirectangularTexture"><code class="notranslate" translate="no">WebGLCubeRenderTarget.fromEquirectangularTexture</code></a> を呼び出して、正距円筒図テクスチャからキューブマップを生成します。</p>
  193. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  194. - const loader = new THREE.CubeTextureLoader();
  195. - const texture = loader.load([
  196. - 'resources/images/cubemaps/computer-history-museum/pos-x.jpg',
  197. - 'resources/images/cubemaps/computer-history-museum/neg-x.jpg',
  198. - 'resources/images/cubemaps/computer-history-museum/pos-y.jpg',
  199. - 'resources/images/cubemaps/computer-history-museum/neg-y.jpg',
  200. - 'resources/images/cubemaps/computer-history-museum/pos-z.jpg',
  201. - 'resources/images/cubemaps/computer-history-museum/neg-z.jpg',
  202. - ]);
  203. - scene.background = texture;
  204. + const loader = new THREE.TextureLoader();
  205. + const texture = loader.load(
  206. + 'resources/images/equirectangularmaps/tears_of_steel_bridge_2k.jpg',
  207. + () =&gt; {
  208. + texture.mapping = THREE.EquirectangularReflectionMapping;
  209. + texture.colorSpace = THREE.SRGBColorSpace;
  210. + scene.background = texture;
  211. + });
  212. }
  213. </pre>
  214. <p>そして、これが全てです。</p>
  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/background-equirectangularmap.html"></iframe></div>
  217. <a class="threejs_center" href="/manual/examples/background-equirectangularmap.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  218. </div>
  219. <p></p>
  220. <p>テクスチャを読み込み時に行うのではなく、あらかじめ等角画像をキューブマップに変換しておく事ともできます。<a href="https://matheowis.github.io/HDRI-to-CubeMap/">こんなサイトもあります</a>。</p>
  221. </div>
  222. </div>
  223. </div>
  224. <script src="../resources/prettify.js"></script>
  225. <script src="../resources/lesson.js"></script>
  226. </body></html>