background-cubemap.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Cubemap</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="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. function main() {
  35. const canvas = document.querySelector( '#c' );
  36. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  37. const fov = 75;
  38. const aspect = 2; // the canvas default
  39. const near = 0.1;
  40. const far = 100;
  41. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  42. camera.position.z = 3;
  43. const controls = new OrbitControls( camera, canvas );
  44. controls.target.set( 0, 0, 0 );
  45. controls.update();
  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. {
  71. const loader = new THREE.CubeTextureLoader();
  72. const texture = loader.load( [
  73. 'resources/images/cubemaps/computer-history-museum/pos-x.jpg',
  74. 'resources/images/cubemaps/computer-history-museum/neg-x.jpg',
  75. 'resources/images/cubemaps/computer-history-museum/pos-y.jpg',
  76. 'resources/images/cubemaps/computer-history-museum/neg-y.jpg',
  77. 'resources/images/cubemaps/computer-history-museum/pos-z.jpg',
  78. 'resources/images/cubemaps/computer-history-museum/neg-z.jpg',
  79. ] );
  80. scene.background = texture;
  81. }
  82. function resizeRendererToDisplaySize( renderer ) {
  83. const canvas = renderer.domElement;
  84. const width = canvas.clientWidth;
  85. const height = canvas.clientHeight;
  86. const needResize = canvas.width !== width || canvas.height !== height;
  87. if ( needResize ) {
  88. renderer.setSize( width, height, false );
  89. }
  90. return needResize;
  91. }
  92. function render( time ) {
  93. time *= 0.001;
  94. if ( resizeRendererToDisplaySize( renderer ) ) {
  95. const canvas = renderer.domElement;
  96. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  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( scene, camera );
  106. requestAnimationFrame( render );
  107. }
  108. requestAnimationFrame( render );
  109. }
  110. main();
  111. </script>
  112. </html>