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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script src="resources/threejs/r102/three.min.js"></script>
  23. <script>
  24. 'use strict';
  25. /* global THREE */
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  29. renderer.autoClearColor = false;
  30. const fov = 75;
  31. const aspect = 2; // the canvas default
  32. const near = 0.1;
  33. const far = 5;
  34. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  35. camera.position.z = 2;
  36. const scene = new THREE.Scene();
  37. {
  38. const color = 0xFFFFFF;
  39. const intensity = 1;
  40. const light = new THREE.DirectionalLight(color, intensity);
  41. light.position.set(-1, 2, 4);
  42. scene.add(light);
  43. }
  44. const boxWidth = 1;
  45. const boxHeight = 1;
  46. const boxDepth = 1;
  47. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  48. function makeInstance(geometry, color, x) {
  49. const material = new THREE.MeshPhongMaterial({color});
  50. const cube = new THREE.Mesh(geometry, material);
  51. scene.add(cube);
  52. cube.position.x = x;
  53. return cube;
  54. }
  55. const cubes = [
  56. makeInstance(geometry, 0x44aa88, 0),
  57. makeInstance(geometry, 0x8844aa, -2),
  58. makeInstance(geometry, 0xaa8844, 2),
  59. ];
  60. const bgCamera = new THREE.OrthographicCamera(
  61. -1, // left
  62. 1, // right
  63. 1, // top
  64. -1, // bottom
  65. -1, // near,
  66. 1, // far
  67. );
  68. const bgScene = new THREE.Scene();
  69. const loader = new THREE.TextureLoader();
  70. const bgTexture = loader.load('resources/images/daikanyama.jpg');
  71. let bgMesh;
  72. {
  73. const plane = new THREE.PlaneBufferGeometry(2, 2);
  74. const material = new THREE.MeshBasicMaterial({
  75. map: bgTexture,
  76. depthTest: false,
  77. });
  78. bgMesh = new THREE.Mesh(plane, material);
  79. bgScene.add(bgMesh);
  80. }
  81. function resizeRendererToDisplaySize(renderer) {
  82. const canvas = renderer.domElement;
  83. const width = canvas.clientWidth;
  84. const height = canvas.clientHeight;
  85. const needResize = canvas.width !== width || canvas.height !== height;
  86. if (needResize) {
  87. renderer.setSize(width, height, false);
  88. }
  89. return needResize;
  90. }
  91. function render(time) {
  92. time *= 0.001;
  93. if (resizeRendererToDisplaySize(renderer)) {
  94. const canvas = renderer.domElement;
  95. const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  96. camera.aspect = canvasAspect;
  97. camera.updateProjectionMatrix();
  98. }
  99. cubes.forEach((cube, ndx) => {
  100. const speed = 1 + ndx * .1;
  101. const rot = time * speed;
  102. cube.rotation.x = rot;
  103. cube.rotation.y = rot;
  104. });
  105. renderer.render(bgScene, bgCamera);
  106. renderer.render(scene, camera);
  107. requestAnimationFrame(render);
  108. }
  109. requestAnimationFrame(render);
  110. }
  111. main();
  112. </script>
  113. </html>