backgrounds.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. Title: Three.js Backgrounds and Skyboxes
  2. Description: How to add a background in THREE.js
  3. TOC: Add a Background or Skybox
  4. Most of the articles here use a solid color for a background.
  5. Adding as static background can be as simple as setting some CSS. Taking
  6. an example from [the article on making THREE.js responsive](threejs-responsive.html)
  7. we only need to change 2 things.
  8. We need to add some CSS to our canvas to set its background to an image
  9. ```html
  10. <style>
  11. body {
  12. margin: 0;
  13. }
  14. #c {
  15. width: 100%;
  16. height: 100%;
  17. display: block;
  18. + background: url(resources/images/daikanyama.jpg) no-repeat center center;
  19. + background-size: cover;
  20. }
  21. </style>
  22. ```
  23. and we need to tell the `WebGLRenderer` to use `alpha` so places we are not
  24. drawing anything are transparent.
  25. ```js
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. - const renderer = new THREE.WebGLRenderer({canvas});
  29. + const renderer = new THREE.WebGLRenderer({
  30. + canvas,
  31. + alpha: true,
  32. + });
  33. ```
  34. And we get a background.
  35. {{{example url="../threejs-background-css.html" }}}
  36. If we want the background to be able to be affected by [post processing
  37. effects](threejs-post-processing.html) then we need to draw the background using
  38. THREE.js.
  39. THREE.js makes this some what simple. We can just set the background of the scene to
  40. a texture.
  41. ```js
  42. const loader = new THREE.TextureLoader();
  43. const bgTexture = loader.load('resources/images/daikanyama.jpg');
  44. scene.background = bgTexture;
  45. ```
  46. which gives us
  47. {{{example url="../threejs-background-scene-background.html" }}}
  48. This gets us a background image but its stretched to fit the screen.
  49. We can solve this issue by setting the `repeat` and `offset` properties of
  50. the texture to show only a portion of image.
  51. ```js
  52. function render(time) {
  53. ...
  54. + // Set the repeat and offset properties of the background texture
  55. + // to keep the image's aspect correct.
  56. + // Note the image may not have loaded yet.
  57. + const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  58. + const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  59. + const aspect = imageAspect / canvasAspect;
  60. +
  61. + bgTexture.offset.x = aspect > 1 ? (1 - 1 / aspect) / 2 : 0;
  62. + bgTexture.repeat.x = aspect > 1 ? 1 / aspect : 1;
  63. +
  64. + bgTexture.offset.y = aspect > 1 ? 0 : (1 - aspect) / 2;
  65. + bgTexture.repeat.y = aspect > 1 ? 1 : aspect;
  66. ...
  67. renderer.render(scene, camera);
  68. requestAnimationFrame(render);
  69. }
  70. ```
  71. and now THREE.js drawing the background. There is no visible difference from
  72. the CSS version at the top but now if we used a [post processing
  73. effect](threejs-post-processing.html) the background would be affected too.
  74. {{{example url="../threejs-background-scene-background-fixed-aspect.html" }}}
  75. Of course a static background is not usually what we want in a 3D scene. Instead
  76. we usually want some kind of *skybox*. A skybox is just that, box with the sky
  77. draw on it. We put the camera inside the box and it looks like there is a sky in
  78. the background.
  79. The most common way to implement a skybox is to make a cube, apply a texture to
  80. it, draw it from the inside. On each side of the cube put a texture (using
  81. texture coordinates) that looks like some image of the horizon. It's also often
  82. common to use a sky sphere or a sky dome with a texture drawn on it. You can
  83. probably figure that one out on your own. Just make a cube or sphere,
  84. [apply a texture](threejs-textures.html), mark it as `THREE.BackSide` so we
  85. render the inside instead of the outside, and either put it in your scene directly
  86. or like above, or, make 2 scenes, a special one to draw the skybox/sphere/dome and the
  87. normal one to draw everything else. You'd use your normal `PerspectiveCamera` to
  88. draw. No need for the `OrthographicCamera`.
  89. Another solution is to use a *Cubemap*. A Cubemap is a special kind of texture
  90. that has 6 sides, the sides of a cube. Instead of using standard texture
  91. coordinates it uses a direction from the center pointing outward to decide where
  92. to get a color.
  93. Here are the 6 images of a cubemap from the computer history museum in Mountain
  94. View, California.
  95. <div class="threejs_center">
  96. <img src="../resources/images/cubemaps/computer-history-museum/pos-x.jpg" style="width: 200px" class="border">
  97. <img src="../resources/images/cubemaps/computer-history-museum/neg-x.jpg" style="width: 200px" class="border">
  98. <img src="../resources/images/cubemaps/computer-history-museum/pos-y.jpg" style="width: 200px" class="border">
  99. </div>
  100. <div class="threejs_center">
  101. <img src="../resources/images/cubemaps/computer-history-museum/neg-y.jpg" style="width: 200px" class="border">
  102. <img src="../resources/images/cubemaps/computer-history-museum/pos-z.jpg" style="width: 200px" class="border">
  103. <img src="../resources/images/cubemaps/computer-history-museum/neg-z.jpg" style="width: 200px" class="border">
  104. </div>
  105. To use them we use `CubeTextureLoader` to load them and then use that as a the
  106. scene's background.
  107. ```js
  108. {
  109. const loader = new THREE.CubeTextureLoader();
  110. const texture = loader.load([
  111. 'resources/images/cubemaps/computer-history-museum/pos-x.jpg',
  112. 'resources/images/cubemaps/computer-history-museum/neg-x.jpg',
  113. 'resources/images/cubemaps/computer-history-museum/pos-y.jpg',
  114. 'resources/images/cubemaps/computer-history-museum/neg-y.jpg',
  115. 'resources/images/cubemaps/computer-history-museum/pos-z.jpg',
  116. 'resources/images/cubemaps/computer-history-museum/neg-z.jpg',
  117. ]);
  118. scene.background = texture;
  119. }
  120. ```
  121. At render time we don't need to adjust the texture like we did above
  122. ```js
  123. function render(time) {
  124. ...
  125. - // Set the repeat and offset properties of the background texture
  126. - // to keep the image's aspect correct.
  127. - // Note the image may not have loaded yet.
  128. - const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  129. - const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  130. - const aspect = imageAspect / canvasAspect;
  131. -
  132. - bgTexture.offset.x = aspect > 1 ? (1 - 1 / aspect) / 2 : 0;
  133. - bgTexture.repeat.x = aspect > 1 ? 1 / aspect : 1;
  134. -
  135. - bgTexture.offset.y = aspect > 1 ? 0 : (1 - aspect) / 2;
  136. - bgTexture.repeat.y = aspect > 1 ? 1 : aspect;
  137. ...
  138. renderer.render(scene, camera);
  139. requestAnimationFrame(render);
  140. }
  141. ```
  142. Let's add some controls in so we can rotate the camera.
  143. ```js
  144. import {OrbitControls} from './resources/threejs/r132/examples/jsm/controls/OrbitControls.js';
  145. ```
  146. ```js
  147. const fov = 75;
  148. const aspect = 2; // the canvas default
  149. const near = 0.1;
  150. -const far = 5;
  151. +const far = 100;
  152. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  153. -camera.position.z = 2;
  154. +camera.position.z = 3;
  155. +const controls = new OrbitControls(camera, canvas);
  156. +controls.target.set(0, 0, 0);
  157. +controls.update();
  158. ```
  159. and try it out. Drag on the example to rotate the camera and see the cubemap
  160. surrounds us.
  161. {{{example url="../threejs-background-cubemap.html" }}}
  162. Another option is to use an Equirectangular map. This is the kind of picture a
  163. [360 camera](https://google.com/search?q=360+camera) takes.
  164. [Here's one](https://hdrihaven.com/hdri/?h=tears_of_steel_bridge) I found from
  165. [this site](https://hdrihaven.com).
  166. <div class="threejs_center"><img src="../resources/images/equirectangularmaps/tears_of_steel_bridge_2k.jpg" style="width: 600px"></div>
  167. It's not much different. First we load the equirectangular image as a texture
  168. and then, in the callback after it has loaded, we can call `WebGLCubeRenderTarget.fromEquirectangularTexture`
  169. which will generate a cubemap from the equirectangular texture for us.
  170. We pass in the size we want the cubemap to be to `WebGLCubeRenderTarget`.
  171. Passing in the height of the equirectangular image seems like a good bet.
  172. ```js
  173. {
  174. - const loader = new THREE.CubeTextureLoader();
  175. - const texture = loader.load([
  176. - 'resources/images/cubemaps/computer-history-museum/pos-x.jpg',
  177. - 'resources/images/cubemaps/computer-history-museum/neg-x.jpg',
  178. - 'resources/images/cubemaps/computer-history-museum/pos-y.jpg',
  179. - 'resources/images/cubemaps/computer-history-museum/neg-y.jpg',
  180. - 'resources/images/cubemaps/computer-history-museum/pos-z.jpg',
  181. - 'resources/images/cubemaps/computer-history-museum/neg-z.jpg',
  182. - ]);
  183. - scene.background = texture;
  184. + const loader = new THREE.TextureLoader();
  185. + const texture = loader.load(
  186. + 'resources/images/equirectangularmaps/tears_of_steel_bridge_2k.jpg',
  187. + () => {
  188. + const rt = new THREE.WebGLCubeRenderTarget(texture.image.height);
  189. + rt.fromEquirectangularTexture(renderer, texture);
  190. + scene.background = rt.texture;
  191. + });
  192. }
  193. ```
  194. And that's all there is to it.
  195. {{{example url="../threejs-background-equirectangularmap.html" }}}
  196. Rather than do it at load time you can also convert an equirectangular image
  197. to a cubemap beforehand. [Here's a site that will do it for you](https://matheowis.github.io/HDRI-to-CubeMap/).