background-separate-scene.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. 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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. function main() {
  36. const canvas = document.querySelector( '#c' );
  37. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  38. renderer.useLegacyLights = false;
  39. renderer.autoClearColor = false;
  40. const fov = 75;
  41. const aspect = 2; // the canvas default
  42. const near = 0.1;
  43. const far = 5;
  44. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  45. camera.position.z = 2;
  46. const scene = new THREE.Scene();
  47. {
  48. const color = 0xFFFFFF;
  49. const intensity = 3;
  50. const light = new THREE.DirectionalLight( color, intensity );
  51. light.position.set( - 1, 2, 4 );
  52. scene.add( light );
  53. }
  54. const boxWidth = 1;
  55. const boxHeight = 1;
  56. const boxDepth = 1;
  57. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  58. function makeInstance( geometry, color, x ) {
  59. const material = new THREE.MeshPhongMaterial( { color } );
  60. const cube = new THREE.Mesh( geometry, material );
  61. scene.add( cube );
  62. cube.position.x = x;
  63. return cube;
  64. }
  65. const cubes = [
  66. makeInstance( geometry, 0x44aa88, 0 ),
  67. makeInstance( geometry, 0x8844aa, - 2 ),
  68. makeInstance( geometry, 0xaa8844, 2 ),
  69. ];
  70. const bgCamera = new THREE.OrthographicCamera(
  71. - 1, // left
  72. 1, // right
  73. 1, // top
  74. - 1, // bottom
  75. - 1, // near,
  76. 1, // far
  77. );
  78. const bgScene = new THREE.Scene();
  79. const loader = new THREE.TextureLoader();
  80. const bgTexture = loader.load( 'resources/images/daikanyama.jpg' );
  81. let bgMesh;
  82. {
  83. const plane = new THREE.PlaneGeometry( 2, 2 );
  84. const material = new THREE.MeshBasicMaterial( {
  85. map: bgTexture,
  86. depthTest: false,
  87. } );
  88. bgMesh = new THREE.Mesh( plane, material );
  89. bgScene.add( bgMesh );
  90. }
  91. function resizeRendererToDisplaySize( renderer ) {
  92. const canvas = renderer.domElement;
  93. const width = canvas.clientWidth;
  94. const height = canvas.clientHeight;
  95. const needResize = canvas.width !== width || canvas.height !== height;
  96. if ( needResize ) {
  97. renderer.setSize( width, height, false );
  98. }
  99. return needResize;
  100. }
  101. function render( time ) {
  102. time *= 0.001;
  103. if ( resizeRendererToDisplaySize( renderer ) ) {
  104. const canvas = renderer.domElement;
  105. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  106. camera.updateProjectionMatrix();
  107. }
  108. // Scale the background plane to cover and keep the image's aspect correct.
  109. // Note the image may not have loaded yet.
  110. const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  111. const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  112. const aspect = imageAspect / canvasAspect;
  113. // choose an axis to have a scale of 1 and the other >= 1
  114. bgMesh.scale.x = aspect > 1 ? aspect : 1;
  115. bgMesh.scale.y = aspect > 1 ? 1 : 1 / aspect;
  116. cubes.forEach( ( cube, ndx ) => {
  117. const speed = 1 + ndx * .1;
  118. const rot = time * speed;
  119. cube.rotation.x = rot;
  120. cube.rotation.y = rot;
  121. } );
  122. renderer.render( bgScene, bgCamera );
  123. renderer.render( scene, camera );
  124. requestAnimationFrame( render );
  125. }
  126. requestAnimationFrame( render );
  127. }
  128. main();
  129. </script>
  130. </html>