backgrounds.html 13 KB

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