webxr_vr_handinput.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - handinput</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - handinput<br/>
  12. (Oculus Browser 15.1+)
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { VRButton } from 'three/addons/webxr/VRButton.js';
  29. import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
  30. import { XRHandModelFactory } from 'three/addons/webxr/XRHandModelFactory.js';
  31. let container;
  32. let camera, scene, renderer;
  33. let hand1, hand2;
  34. let controller1, controller2;
  35. let controllerGrip1, controllerGrip2;
  36. let controls;
  37. init();
  38. animate();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0x444444 );
  44. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  45. camera.position.set( 0, 1.6, 3 );
  46. controls = new OrbitControls( camera, container );
  47. controls.target.set( 0, 1.6, 0 );
  48. controls.update();
  49. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  50. const floorMaterial = new THREE.MeshStandardMaterial( { color: 0x666666 } );
  51. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  52. floor.rotation.x = - Math.PI / 2;
  53. floor.receiveShadow = true;
  54. scene.add( floor );
  55. scene.add( new THREE.HemisphereLight( 0xbcbcbc, 0xa5a5a5, 3 ) );
  56. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  57. light.position.set( 0, 6, 0 );
  58. light.castShadow = true;
  59. light.shadow.camera.top = 2;
  60. light.shadow.camera.bottom = - 2;
  61. light.shadow.camera.right = 2;
  62. light.shadow.camera.left = - 2;
  63. light.shadow.mapSize.set( 4096, 4096 );
  64. scene.add( light );
  65. //
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.shadowMap.enabled = true;
  70. renderer.xr.enabled = true;
  71. container.appendChild( renderer.domElement );
  72. document.body.appendChild( VRButton.createButton( renderer ) );
  73. // controllers
  74. controller1 = renderer.xr.getController( 0 );
  75. scene.add( controller1 );
  76. controller2 = renderer.xr.getController( 1 );
  77. scene.add( controller2 );
  78. const controllerModelFactory = new XRControllerModelFactory();
  79. const handModelFactory = new XRHandModelFactory();
  80. // Hand 1
  81. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  82. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  83. scene.add( controllerGrip1 );
  84. hand1 = renderer.xr.getHand( 0 );
  85. hand1.add( handModelFactory.createHandModel( hand1 ) );
  86. scene.add( hand1 );
  87. // Hand 2
  88. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  89. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  90. scene.add( controllerGrip2 );
  91. hand2 = renderer.xr.getHand( 1 );
  92. hand2.add( handModelFactory.createHandModel( hand2 ) );
  93. scene.add( hand2 );
  94. //
  95. const geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  96. const line = new THREE.Line( geometry );
  97. line.name = 'line';
  98. line.scale.z = 5;
  99. controller1.add( line.clone() );
  100. controller2.add( line.clone() );
  101. //
  102. window.addEventListener( 'resize', onWindowResize );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. //
  110. function animate() {
  111. renderer.setAnimationLoop( render );
  112. }
  113. function render() {
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>