webxr_vr_handinput_profiles.html 5.7 KB

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