background-separate-scene-bad-aspect.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 bad 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 bgCamera = new THREE.OrthographicCamera(
  60. -1, // left
  61. 1, // right
  62. 1, // top
  63. -1, // bottom
  64. -1, // near,
  65. 1, // far
  66. );
  67. const bgScene = new THREE.Scene();
  68. const loader = new THREE.TextureLoader();
  69. const bgTexture = loader.load('resources/images/daikanyama.jpg');
  70. let bgMesh;
  71. {
  72. const plane = new THREE.PlaneGeometry(2, 2);
  73. const material = new THREE.MeshBasicMaterial({
  74. map: bgTexture,
  75. depthTest: false,
  76. });
  77. bgMesh = new THREE.Mesh(plane, material);
  78. bgScene.add(bgMesh);
  79. }
  80. function resizeRendererToDisplaySize(renderer) {
  81. const canvas = renderer.domElement;
  82. const width = canvas.clientWidth;
  83. const height = canvas.clientHeight;
  84. const needResize = canvas.width !== width || canvas.height !== height;
  85. if (needResize) {
  86. renderer.setSize(width, height, false);
  87. }
  88. return needResize;
  89. }
  90. function render(time) {
  91. time *= 0.001;
  92. if (resizeRendererToDisplaySize(renderer)) {
  93. const canvas = renderer.domElement;
  94. const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  95. camera.aspect = canvasAspect;
  96. camera.updateProjectionMatrix();
  97. }
  98. cubes.forEach((cube, ndx) => {
  99. const speed = 1 + ndx * .1;
  100. const rot = time * speed;
  101. cube.rotation.x = rot;
  102. cube.rotation.y = rot;
  103. });
  104. renderer.render(bgScene, bgCamera);
  105. renderer.render(scene, camera);
  106. requestAnimationFrame(render);
  107. }
  108. requestAnimationFrame(render);
  109. }
  110. main();
  111. </script>
  112. </html>