1
0

webxr_vr_handinput_profiles.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - handinput - profiles</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 - profiles<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. const handModels = {
  34. left: null,
  35. right: null
  36. };
  37. let controls;
  38. init();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. scene = new THREE.Scene();
  43. window.scene = scene;
  44. scene.background = new THREE.Color( 0x444444 );
  45. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  46. camera.position.set( 0, 1.6, 3 );
  47. controls = new OrbitControls( camera, container );
  48. controls.target.set( 0, 1.6, 0 );
  49. controls.update();
  50. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  51. const floorMaterial = new THREE.MeshStandardMaterial( { color: 0x666666 } );
  52. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  53. floor.rotation.x = - Math.PI / 2;
  54. floor.receiveShadow = true;
  55. scene.add( floor );
  56. scene.add( new THREE.HemisphereLight( 0xbcbcbc, 0xa5a5a5, 3 ) );
  57. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  58. light.position.set( 0, 6, 0 );
  59. light.castShadow = true;
  60. light.shadow.camera.top = 2;
  61. light.shadow.camera.bottom = - 2;
  62. light.shadow.camera.right = 2;
  63. light.shadow.camera.left = - 2;
  64. light.shadow.mapSize.set( 4096, 4096 );
  65. scene.add( light );
  66. //
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.setAnimationLoop( animate );
  71. renderer.shadowMap.enabled = true;
  72. renderer.xr.enabled = true;
  73. container.appendChild( renderer.domElement );
  74. const sessionInit = {
  75. requiredFeatures: [ 'hand-tracking' ]
  76. };
  77. document.body.appendChild( VRButton.createButton( renderer, sessionInit ) );
  78. // controllers
  79. controller1 = renderer.xr.getController( 0 );
  80. scene.add( controller1 );
  81. controller2 = renderer.xr.getController( 1 );
  82. scene.add( controller2 );
  83. const controllerModelFactory = new XRControllerModelFactory();
  84. const handModelFactory = new XRHandModelFactory();
  85. // Hand 1
  86. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  87. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  88. scene.add( controllerGrip1 );
  89. hand1 = renderer.xr.getHand( 0 );
  90. hand1.userData.currentHandModel = 0;
  91. scene.add( hand1 );
  92. handModels.left = [
  93. handModelFactory.createHandModel( hand1, 'boxes' ),
  94. handModelFactory.createHandModel( hand1, 'spheres' ),
  95. handModelFactory.createHandModel( hand1, 'mesh' )
  96. ];
  97. for ( let i = 0; i < 3; i ++ ) {
  98. const model = handModels.left[ i ];
  99. model.visible = i == 0;
  100. hand1.add( model );
  101. }
  102. hand1.addEventListener( 'pinchend', function () {
  103. handModels.left[ this.userData.currentHandModel ].visible = false;
  104. this.userData.currentHandModel = ( this.userData.currentHandModel + 1 ) % 3;
  105. handModels.left[ this.userData.currentHandModel ].visible = true;
  106. } );
  107. // Hand 2
  108. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  109. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  110. scene.add( controllerGrip2 );
  111. hand2 = renderer.xr.getHand( 1 );
  112. hand2.userData.currentHandModel = 0;
  113. scene.add( hand2 );
  114. handModels.right = [
  115. handModelFactory.createHandModel( hand2, 'boxes' ),
  116. handModelFactory.createHandModel( hand2, 'spheres' ),
  117. handModelFactory.createHandModel( hand2, 'mesh' )
  118. ];
  119. for ( let i = 0; i < 3; i ++ ) {
  120. const model = handModels.right[ i ];
  121. model.visible = i == 0;
  122. hand2.add( model );
  123. }
  124. hand2.addEventListener( 'pinchend', function () {
  125. handModels.right[ this.userData.currentHandModel ].visible = false;
  126. this.userData.currentHandModel = ( this.userData.currentHandModel + 1 ) % 3;
  127. handModels.right[ this.userData.currentHandModel ].visible = true;
  128. } );
  129. //
  130. const geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  131. const line = new THREE.Line( geometry );
  132. line.name = 'line';
  133. line.scale.z = 5;
  134. controller1.add( line.clone() );
  135. controller2.add( line.clone() );
  136. //
  137. window.addEventListener( 'resize', onWindowResize );
  138. }
  139. function onWindowResize() {
  140. camera.aspect = window.innerWidth / window.innerHeight;
  141. camera.updateProjectionMatrix();
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. }
  144. //
  145. function animate() {
  146. renderer.render( scene, camera );
  147. }
  148. </script>
  149. </body>
  150. </html>