threejs-background-separate-scene.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 CSS</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/r103/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. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  96. camera.updateProjectionMatrix();
  97. }
  98. // Scale the background plane to cover and keep the image's aspect correct.
  99. // Note the image may not have loaded yet.
  100. const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  101. const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  102. const aspect = imageAspect / canvasAspect;
  103. // choose an axis to have a scale of 1 and the other >= 1
  104. bgMesh.scale.x = aspect > 1 ? aspect : 1;
  105. bgMesh.scale.y = aspect > 1 ? 1 : 1 / aspect;
  106. cubes.forEach((cube, ndx) => {
  107. const speed = 1 + ndx * .1;
  108. const rot = time * speed;
  109. cube.rotation.x = rot;
  110. cube.rotation.y = rot;
  111. });
  112. renderer.render(bgScene, bgCamera);
  113. renderer.render(scene, camera);
  114. requestAnimationFrame(render);
  115. }
  116. requestAnimationFrame(render);
  117. }
  118. main();
  119. </script>
  120. </html>