background-scene-background-fixed-aspect.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Background - Scene Background Fixed Aspect</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="module">
  24. import * as THREE from '../../build/three.module.js';
  25. function main() {
  26. const canvas = document.querySelector('#c');
  27. const renderer = new THREE.WebGLRenderer({canvas});
  28. renderer.autoClearColor = false;
  29. const fov = 75;
  30. const aspect = 2; // the canvas default
  31. const near = 0.1;
  32. const far = 5;
  33. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  34. camera.position.z = 2;
  35. const scene = new THREE.Scene();
  36. {
  37. const color = 0xFFFFFF;
  38. const intensity = 1;
  39. const light = new THREE.DirectionalLight(color, intensity);
  40. light.position.set(-1, 2, 4);
  41. scene.add(light);
  42. }
  43. const boxWidth = 1;
  44. const boxHeight = 1;
  45. const boxDepth = 1;
  46. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  47. function makeInstance(geometry, color, x) {
  48. const material = new THREE.MeshPhongMaterial({color});
  49. const cube = new THREE.Mesh(geometry, material);
  50. scene.add(cube);
  51. cube.position.x = x;
  52. return cube;
  53. }
  54. const cubes = [
  55. makeInstance(geometry, 0x44aa88, 0),
  56. makeInstance(geometry, 0x8844aa, -2),
  57. makeInstance(geometry, 0xaa8844, 2),
  58. ];
  59. const loader = new THREE.TextureLoader();
  60. const bgTexture = loader.load('resources/images/daikanyama.jpg');
  61. scene.background = bgTexture;
  62. function resizeRendererToDisplaySize(renderer) {
  63. const canvas = renderer.domElement;
  64. const width = canvas.clientWidth;
  65. const height = canvas.clientHeight;
  66. const needResize = canvas.width !== width || canvas.height !== height;
  67. if (needResize) {
  68. renderer.setSize(width, height, false);
  69. }
  70. return needResize;
  71. }
  72. function render(time) {
  73. time *= 0.001;
  74. if (resizeRendererToDisplaySize(renderer)) {
  75. const canvas = renderer.domElement;
  76. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  77. camera.updateProjectionMatrix();
  78. }
  79. // Set the repeat and offset properties of the background texture
  80. // to keep the image's aspect correct.
  81. // Note the image may not have loaded yet.
  82. const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  83. const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  84. const aspect = imageAspect / canvasAspect;
  85. bgTexture.offset.x = aspect > 1 ? (1 - 1 / aspect) / 2 : 0;
  86. bgTexture.repeat.x = aspect > 1 ? 1 / aspect : 1;
  87. bgTexture.offset.y = aspect > 1 ? 0 : (1 - aspect) / 2;
  88. bgTexture.repeat.y = aspect > 1 ? 1 : aspect;
  89. cubes.forEach((cube, ndx) => {
  90. const speed = 1 + ndx * .1;
  91. const rot = time * speed;
  92. cube.rotation.x = rot;
  93. cube.rotation.y = rot;
  94. });
  95. renderer.render(scene, camera);
  96. requestAnimationFrame(render);
  97. }
  98. requestAnimationFrame(render);
  99. }
  100. main();
  101. </script>
  102. </html>