misc_controls_deviceorientation.html 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. <div>
  12. <button id="startButton">Start Demo</button>
  13. <p>Using device orientation might require a user interaction.</p>
  14. </div>
  15. </div>
  16. <div id="info">
  17. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - equirectangular panorama demo with DeviceOrientation controls.<br/>
  18. photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank" rel="noopener">Jón Ragnarsson</a>.
  19. </div>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. import { DeviceOrientationControls } from './jsm/controls/DeviceOrientationControls.js';
  23. var camera, scene, renderer, controls;
  24. var startButton = document.getElementById( 'startButton' );
  25. startButton.addEventListener( 'click', function () {
  26. init();
  27. animate();
  28. }, false );
  29. function init() {
  30. var overlay = document.getElementById( 'overlay' );
  31. overlay.remove();
  32. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
  33. controls = new DeviceOrientationControls( camera );
  34. scene = new THREE.Scene();
  35. var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 );
  36. // invert the geometry on the x-axis so that all of the faces point inward
  37. geometry.scale( - 1, 1, 1 );
  38. var material = new THREE.MeshBasicMaterial( {
  39. map: new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' )
  40. } );
  41. var mesh = new THREE.Mesh( geometry, material );
  42. scene.add( mesh );
  43. var helperGeometry = new THREE.BoxBufferGeometry( 100, 100, 100, 4, 4, 4 );
  44. var helperMaterial = new THREE.MeshBasicMaterial( { color: 0xff00ff, wireframe: true } );
  45. var helper = new THREE.Mesh( helperGeometry, helperMaterial );
  46. scene.add( helper );
  47. //
  48. renderer = new THREE.WebGLRenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. document.body.appendChild( renderer.domElement );
  52. //
  53. window.addEventListener( 'resize', onWindowResize, false );
  54. }
  55. function animate() {
  56. window.requestAnimationFrame( animate );
  57. controls.update();
  58. renderer.render( scene, camera );
  59. }
  60. function onWindowResize() {
  61. camera.aspect = window.innerWidth / window.innerHeight;
  62. camera.updateProjectionMatrix();
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. }
  65. </script>
  66. </body>
  67. </html>