backgrounds.html 13 KB

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