backgrounds.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Backgrounds and Skyboxes</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 – Backgrounds and Skyboxes">
  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>Backgrounds and Skyboxes</h1>
  18. </div>
  19. <div class="lesson">
  20. <div class="lesson-main">
  21. <p>Most of the articles here use a solid color for a background.</p>
  22. <p>Adding as static background can be as simple as setting some CSS. Taking
  23. an example from <a href="responsive.html">the article on making THREE.js responsive</a>
  24. we only need to change 2 things.</p>
  25. <p>We need to add some CSS to our canvas to set its background to an image</p>
  26. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;style&gt;
  27. body {
  28. margin: 0;
  29. }
  30. #c {
  31. width: 100%;
  32. height: 100%;
  33. display: block;
  34. + background: url(resources/images/daikanyama.jpg) no-repeat center center;
  35. + background-size: cover;
  36. }
  37. &lt;/style&gt;
  38. </pre>
  39. <p>and we need to tell the <a href="/docs/#api/en/renderers/WebGLRenderer"><code class="notranslate" translate="no">WebGLRenderer</code></a> to use <code class="notranslate" translate="no">alpha</code> so places we are not
  40. drawing anything are transparent.</p>
  41. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
  42. const canvas = document.querySelector('#c');
  43. - const renderer = new THREE.WebGLRenderer({canvas});
  44. + const renderer = new THREE.WebGLRenderer({
  45. + canvas,
  46. + alpha: true,
  47. + });
  48. </pre>
  49. <p>And we get a background.</p>
  50. <p></p><div translate="no" class="threejs_example_container notranslate">
  51. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/background-css.html"></iframe></div>
  52. <a class="threejs_center" href="/manual/examples/background-css.html" target="_blank">click here to open in a separate window</a>
  53. </div>
  54. <p></p>
  55. <p>If we want the background to be able to be affected by <a href="post-processing.html">post processing
  56. effects</a> then we need to draw the background using
  57. THREE.js.</p>
  58. <p>THREE.js makes this some what simple. We can just set the background of the scene to
  59. a texture.</p>
  60. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const loader = new THREE.TextureLoader();
  61. const bgTexture = loader.load('resources/images/daikanyama.jpg');
  62. scene.background = bgTexture;
  63. </pre>
  64. <p>which gives us</p>
  65. <p></p><div translate="no" class="threejs_example_container notranslate">
  66. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/background-scene-background.html"></iframe></div>
  67. <a class="threejs_center" href="/manual/examples/background-scene-background.html" target="_blank">click here to open in a separate window</a>
  68. </div>
  69. <p></p>
  70. <p>This gets us a background image but its stretched to fit the screen.</p>
  71. <p>We can solve this issue by setting the <code class="notranslate" translate="no">repeat</code> and <code class="notranslate" translate="no">offset</code> properties of
  72. the texture to show only a portion of image.</p>
  73. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  74. ...
  75. + // Set the repeat and offset properties of the background texture
  76. + // to keep the image's aspect correct.
  77. + // Note the image may not have loaded yet.
  78. + const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  79. + const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  80. + const aspect = imageAspect / canvasAspect;
  81. +
  82. + bgTexture.offset.x = aspect &gt; 1 ? (1 - 1 / aspect) / 2 : 0;
  83. + bgTexture.repeat.x = aspect &gt; 1 ? 1 / aspect : 1;
  84. +
  85. + bgTexture.offset.y = aspect &gt; 1 ? 0 : (1 - aspect) / 2;
  86. + bgTexture.repeat.y = aspect &gt; 1 ? 1 : aspect;
  87. ...
  88. renderer.render(scene, camera);
  89. requestAnimationFrame(render);
  90. }
  91. </pre>
  92. <p>and now THREE.js drawing the background. There is no visible difference from
  93. the CSS version at the top but now if we used a <a href="post-processing.html">post processing
  94. effect</a> the background would be affected too.</p>
  95. <p></p><div translate="no" class="threejs_example_container notranslate">
  96. <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>
  97. <a class="threejs_center" href="/manual/examples/background-scene-background-fixed-aspect.html" target="_blank">click here to open in a separate window</a>
  98. </div>
  99. <p></p>
  100. <p>Of course a static background is not usually what we want in a 3D scene. Instead
  101. we usually want some kind of <em>skybox</em>. A skybox is just that, box with the sky
  102. draw on it. We put the camera inside the box and it looks like there is a sky in
  103. the background.</p>
  104. <p>The most common way to implement a skybox is to make a cube, apply a texture to
  105. it, draw it from the inside. On each side of the cube put a texture (using
  106. texture coordinates) that looks like some image of the horizon. It's also often
  107. common to use a sky sphere or a sky dome with a texture drawn on it. You can
  108. probably figure that one out on your own. Just make a cube or sphere,
  109. <a href="textures.html">apply a texture</a>, mark it as <code class="notranslate" translate="no">THREE.BackSide</code> so we
  110. render the inside instead of the outside, and either put it in your scene directly
  111. or like above, or, make 2 scenes, a special one to draw the skybox/sphere/dome and the
  112. normal one to draw everything else. You'd use your normal <a href="/docs/#api/en/cameras/PerspectiveCamera"><code class="notranslate" translate="no">PerspectiveCamera</code></a> to
  113. draw. No need for the <a href="/docs/#api/en/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a>.</p>
  114. <p>Another solution is to use a <em>Cubemap</em>. A Cubemap is a special kind of texture
  115. that has 6 sides, the sides of a cube. Instead of using standard texture
  116. coordinates it uses a direction from the center pointing outward to decide where
  117. to get a color.</p>
  118. <p>Here are the 6 images of a cubemap from the computer history museum in Mountain
  119. View, California.</p>
  120. <div class="threejs_center">
  121. <img src="../examples/resources/images/cubemaps/computer-history-museum/pos-x.jpg" style="width: 200px" class="border">
  122. <img src="../examples/resources/images/cubemaps/computer-history-museum/neg-x.jpg" style="width: 200px" class="border">
  123. <img src="../examples/resources/images/cubemaps/computer-history-museum/pos-y.jpg" style="width: 200px" class="border">
  124. </div>
  125. <div class="threejs_center">
  126. <img src="../examples/resources/images/cubemaps/computer-history-museum/neg-y.jpg" style="width: 200px" class="border">
  127. <img src="../examples/resources/images/cubemaps/computer-history-museum/pos-z.jpg" style="width: 200px" class="border">
  128. <img src="../examples/resources/images/cubemaps/computer-history-museum/neg-z.jpg" style="width: 200px" class="border">
  129. </div>
  130. <p>To use them we use <a href="/docs/#api/en/loaders/CubeTextureLoader"><code class="notranslate" translate="no">CubeTextureLoader</code></a> to load them and then use that as a the
  131. scene's background.</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>At render time we don't need to adjust the texture like we did above</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>Let's add some controls in so we can rotate the camera.</p>
  166. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import {OrbitControls} from '/examples/jsm/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>and try it out. Drag on the example to rotate the camera and see the cubemap
  181. surrounds us.</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">click here to open in a separate window</a>
  185. </div>
  186. <p></p>
  187. <p>Another option is to use an Equirectangular map. This is the kind of picture a
  188. <a href="https://google.com/search?q=360+camera">360 camera</a> takes.</p>
  189. <p><a href="https://hdrihaven.com/hdri/?h=tears_of_steel_bridge">Here's one</a> I found from
  190. <a href="https://hdrihaven.com">this site</a>.</p>
  191. <div class="threejs_center"><img src="../examples/resources/images/equirectangularmaps/tears_of_steel_bridge_2k.jpg" style="width: 600px"></div>
  192. <p>It's not much different. First we load the equirectangular image as a texture
  193. and then, in the callback after it has loaded, we can call <a href="/docs/#api/en/renderers/WebGLCubeRenderTarget.fromEquirectangularTexture"><code class="notranslate" translate="no">WebGLCubeRenderTarget.fromEquirectangularTexture</code></a>
  194. which will generate a cubemap from the equirectangular texture for us.
  195. We pass in the size we want the cubemap to be to <a href="/docs/#api/en/renderers/WebGLCubeRenderTarget"><code class="notranslate" translate="no">WebGLCubeRenderTarget</code></a>.
  196. Passing in the height of the equirectangular image seems like a good bet.</p>
  197. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  198. - const loader = new THREE.CubeTextureLoader();
  199. - const texture = loader.load([
  200. - 'resources/images/cubemaps/computer-history-museum/pos-x.jpg',
  201. - 'resources/images/cubemaps/computer-history-museum/neg-x.jpg',
  202. - 'resources/images/cubemaps/computer-history-museum/pos-y.jpg',
  203. - 'resources/images/cubemaps/computer-history-museum/neg-y.jpg',
  204. - 'resources/images/cubemaps/computer-history-museum/pos-z.jpg',
  205. - 'resources/images/cubemaps/computer-history-museum/neg-z.jpg',
  206. - ]);
  207. - scene.background = texture;
  208. + const loader = new THREE.TextureLoader();
  209. + const texture = loader.load(
  210. + 'resources/images/equirectangularmaps/tears_of_steel_bridge_2k.jpg',
  211. + () =&gt; {
  212. + const rt = new THREE.WebGLCubeRenderTarget(texture.image.height);
  213. + rt.fromEquirectangularTexture(renderer, texture);
  214. + scene.background = rt.texture;
  215. + });
  216. }
  217. </pre>
  218. <p>And that's all there is to it.</p>
  219. <p></p><div translate="no" class="threejs_example_container notranslate">
  220. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/background-equirectangularmap.html"></iframe></div>
  221. <a class="threejs_center" href="/manual/examples/background-equirectangularmap.html" target="_blank">click here to open in a separate window</a>
  222. </div>
  223. <p></p>
  224. <p>Rather than do it at load time you can also convert an equirectangular image
  225. to a cubemap beforehand. <a href="https://matheowis.github.io/HDRI-to-CubeMap/">Here's a site that will do it for you</a>.</p>
  226. </div>
  227. </div>
  228. </div>
  229. <script src="/manual/resources/prettify.js"></script>
  230. <script src="/manual/resources/lesson.js"></script>
  231. </body></html>