webxr_vr_handinput_profiles.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { VRButton } from './jsm/webxr/VRButton.js';
  17. import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
  18. import { XRHandModelFactory } from './jsm/webxr/XRHandModelFactory.js';
  19. let container;
  20. let camera, scene, renderer;
  21. let hand1, hand2;
  22. let controller1, controller2;
  23. let controllerGrip1, controllerGrip2;
  24. const currentHandModel = {
  25. left: 0,
  26. right: 0
  27. };
  28. const handModels = {
  29. left: null,
  30. right: null
  31. };
  32. let controls;
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. scene = new THREE.Scene();
  39. window.scene = scene;
  40. scene.background = new THREE.Color( 0x444444 );
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  42. camera.position.set( 0, 1.6, 3 );
  43. controls = new OrbitControls( camera, container );
  44. controls.target.set( 0, 1.6, 0 );
  45. controls.update();
  46. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  47. const floorMaterial = new THREE.MeshStandardMaterial( { color: 0x222222 } );
  48. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  49. floor.rotation.x = - Math.PI / 2;
  50. floor.receiveShadow = true;
  51. scene.add( floor );
  52. scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
  53. const light = new THREE.DirectionalLight( 0xffffff );
  54. light.position.set( 0, 6, 0 );
  55. light.castShadow = true;
  56. light.shadow.camera.top = 2;
  57. light.shadow.camera.bottom = - 2;
  58. light.shadow.camera.right = 2;
  59. light.shadow.camera.left = - 2;
  60. light.shadow.mapSize.set( 4096, 4096 );
  61. scene.add( light );
  62. //
  63. renderer = new THREE.WebGLRenderer( { antialias: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.outputEncoding = THREE.sRGBEncoding;
  67. renderer.shadowMap.enabled = true;
  68. renderer.xr.enabled = true;
  69. container.appendChild( renderer.domElement );
  70. document.body.appendChild( VRButton.createButton( renderer ) );
  71. // controllers
  72. controller1 = renderer.xr.getController( 0 );
  73. scene.add( controller1 );
  74. controller2 = renderer.xr.getController( 1 );
  75. scene.add( controller2 );
  76. const controllerModelFactory = new XRControllerModelFactory();
  77. const handModelFactory = new XRHandModelFactory();
  78. // Hand 1
  79. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  80. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  81. scene.add( controllerGrip1 );
  82. hand1 = renderer.xr.getHand( 0 );
  83. scene.add( hand1 );
  84. handModels.left = [
  85. handModelFactory.createHandModel( hand1, "boxes" ),
  86. handModelFactory.createHandModel( hand1, "spheres" ),
  87. // low poly option disabled until low poly hands model is fixed
  88. // handModelFactory.createHandModel( hand1, "oculus", { model: "lowpoly" } ),
  89. handModelFactory.createHandModel( hand1, "oculus" )
  90. ];
  91. handModels.left.forEach( model => {
  92. model.visible = false;
  93. hand1.add( model );
  94. } );
  95. handModels.left[ currentHandModel.left ].visible = true;
  96. function cycleHandModel( hand ) {
  97. handModels[ hand ][ currentHandModel[ hand ] ].visible = false;
  98. currentHandModel[ hand ] = ( currentHandModel[ hand ] + 1 ) % handModels[ hand ].length;
  99. handModels[ hand ][ currentHandModel[ hand ] ].visible = true;
  100. }
  101. hand1.addEventListener( 'pinchend', evt => {
  102. cycleHandModel( evt.handedness );
  103. } );
  104. // Hand 2
  105. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  106. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  107. scene.add( controllerGrip2 );
  108. hand2 = renderer.xr.getHand( 1 );
  109. scene.add( hand2 );
  110. handModels.right = [
  111. handModelFactory.createHandModel( hand2, "boxes" ),
  112. handModelFactory.createHandModel( hand2, "spheres" ),
  113. // low poly option disabled until low poly hands model is fixed
  114. // handModelFactory.createHandModel( hand2, "oculus", { model: "lowpoly" } ),
  115. handModelFactory.createHandModel( hand2, "oculus" )
  116. ];
  117. handModels.right.forEach( model => {
  118. model.visible = false;
  119. hand2.add( model );
  120. } );
  121. handModels.right[ currentHandModel.right ].visible = true;
  122. window.handModels = handModels;
  123. hand2.addEventListener( 'pinchend', evt => {
  124. cycleHandModel( evt.handedness );
  125. } );
  126. //
  127. window.hands = [ hand1, hand2 ];
  128. const geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  129. const line = new THREE.Line( geometry );
  130. line.name = 'line';
  131. line.scale.z = 5;
  132. controller1.add( line.clone() );
  133. controller2.add( line.clone() );
  134. //
  135. window.addEventListener( 'resize', onWindowResize );
  136. }
  137. function onWindowResize() {
  138. camera.aspect = window.innerWidth / window.innerHeight;
  139. camera.updateProjectionMatrix();
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. }
  142. //
  143. function animate() {
  144. renderer.setAnimationLoop( render );
  145. }
  146. function render() {
  147. renderer.render( scene, camera );
  148. }
  149. </script>
  150. </body>
  151. </html>