backgrounds.html 13 KB

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