backgrounds.html 14 KB

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