misc_controls_deviceorientation.html 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - controls - deviceorientation</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="overlay">
  11. <button id="startButton">Start Demo</button>
  12. </div>
  13. <div id="info">
  14. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - equirectangular panorama demo with DeviceOrientation controls.<br/>
  15. photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank" rel="noopener">Jón Ragnarsson</a>.
  16. </div>
  17. <script type="module">
  18. import * as THREE from '../build/three.module.js';
  19. import { DeviceOrientationControls } from './jsm/controls/DeviceOrientationControls.js';
  20. let camera, scene, renderer, controls;
  21. const startButton = document.getElementById( 'startButton' );
  22. startButton.addEventListener( 'click', function () {
  23. init();
  24. animate();
  25. } );
  26. function init() {
  27. const overlay = document.getElementById( 'overlay' );
  28. overlay.remove();
  29. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
  30. controls = new DeviceOrientationControls( camera );
  31. scene = new THREE.Scene();
  32. const geometry = new THREE.SphereGeometry( 500, 60, 40 );
  33. // invert the geometry on the x-axis so that all of the faces point inward
  34. geometry.scale( - 1, 1, 1 );
  35. const material = new THREE.MeshBasicMaterial( {
  36. map: new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' )
  37. } );
  38. const mesh = new THREE.Mesh( geometry, material );
  39. scene.add( mesh );
  40. const helperGeometry = new THREE.BoxGeometry( 100, 100, 100, 4, 4, 4 );
  41. const helperMaterial = new THREE.MeshBasicMaterial( { color: 0xff00ff, wireframe: true } );
  42. const helper = new THREE.Mesh( helperGeometry, helperMaterial );
  43. scene.add( helper );
  44. //
  45. renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. document.body.appendChild( renderer.domElement );
  49. //
  50. window.addEventListener( 'resize', onWindowResize );
  51. }
  52. function animate() {
  53. window.requestAnimationFrame( animate );
  54. controls.update();
  55. renderer.render( scene, camera );
  56. }
  57. function onWindowResize() {
  58. camera.aspect = window.innerWidth / window.innerHeight;
  59. camera.updateProjectionMatrix();
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. }
  62. </script>
  63. </body>
  64. </html>