webxr_vr_handinput.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  25. import { VRButton } from 'three/addons/webxr/VRButton.js';
  26. import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
  27. import { XRHandModelFactory } from 'three/addons/webxr/XRHandModelFactory.js';
  28. let container;
  29. let camera, scene, renderer;
  30. let hand1, hand2;
  31. let controller1, controller2;
  32. let controllerGrip1, controllerGrip2;
  33. let controls;
  34. init();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0x444444 );
  40. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  41. camera.position.set( 0, 1.6, 3 );
  42. controls = new OrbitControls( camera, container );
  43. controls.target.set( 0, 1.6, 0 );
  44. controls.update();
  45. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  46. const floorMaterial = new THREE.MeshStandardMaterial( { color: 0x666666 } );
  47. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  48. floor.rotation.x = - Math.PI / 2;
  49. floor.receiveShadow = true;
  50. scene.add( floor );
  51. scene.add( new THREE.HemisphereLight( 0xbcbcbc, 0xa5a5a5, 3 ) );
  52. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  53. light.position.set( 0, 6, 0 );
  54. light.castShadow = true;
  55. light.shadow.camera.top = 2;
  56. light.shadow.camera.bottom = - 2;
  57. light.shadow.camera.right = 2;
  58. light.shadow.camera.left = - 2;
  59. light.shadow.mapSize.set( 4096, 4096 );
  60. scene.add( light );
  61. //
  62. renderer = new THREE.WebGLRenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. renderer.setAnimationLoop( animate );
  66. renderer.shadowMap.enabled = true;
  67. renderer.xr.enabled = true;
  68. container.appendChild( renderer.domElement );
  69. const sessionInit = {
  70. requiredFeatures: [ 'hand-tracking' ]
  71. };
  72. document.body.appendChild( VRButton.createButton( renderer, sessionInit ) );
  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.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>