background-equirectangularmap.html 3.1 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.TextureLoader();
  72. const texture = loader.load(
  73. 'resources/images/equirectangularmaps/tears_of_steel_bridge_2k.jpg',
  74. () => {
  75. texture.mapping = THREE.EquirectangularReflectionMapping;
  76. texture.colorSpace = THREE.SRGBColorSpace;
  77. scene.background = texture;
  78. } );
  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. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  95. camera.updateProjectionMatrix();
  96. }
  97. cubes.forEach( ( cube, ndx ) => {
  98. const speed = 1 + ndx * .1;
  99. const rot = time * speed;
  100. cube.rotation.x = rot;
  101. cube.rotation.y = rot;
  102. } );
  103. renderer.render( scene, camera );
  104. requestAnimationFrame( render );
  105. }
  106. requestAnimationFrame( render );
  107. }
  108. main();
  109. </script>
  110. </html>