webxr_vr_handinput_profiles.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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().setPath( "./models/fbx/" );
  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. handModelFactory.createHandModel( hand1, "oculus", { model: "lowpoly" } ),
  88. handModelFactory.createHandModel( hand1, "oculus" )
  89. ];
  90. handModels.left.forEach( model => {
  91. model.visible = false;
  92. hand1.add( model );
  93. } );
  94. handModels.left[ currentHandModel.left ].visible = true;
  95. function cycleHandModel( hand ) {
  96. handModels[ hand ][ currentHandModel[ hand ] ].visible = false;
  97. currentHandModel[ hand ] = ( currentHandModel[ hand ] + 1 ) % handModels[ hand ].length;
  98. handModels[ hand ][ currentHandModel[ hand ] ].visible = true;
  99. }
  100. hand1.addEventListener( 'pinchend', evt => {
  101. cycleHandModel( evt.handedness );
  102. } );
  103. // Hand 2
  104. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  105. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  106. scene.add( controllerGrip2 );
  107. hand2 = renderer.xr.getHand( 1 );
  108. scene.add( hand2 );
  109. handModels.right = [
  110. handModelFactory.createHandModel( hand2, "boxes" ),
  111. handModelFactory.createHandModel( hand2, "spheres" ),
  112. handModelFactory.createHandModel( hand2, "oculus", { model: "lowpoly" } ),
  113. handModelFactory.createHandModel( hand2, "oculus" )
  114. ];
  115. handModels.right.forEach( model => {
  116. model.visible = false;
  117. hand2.add( model );
  118. } );
  119. handModels.right[ currentHandModel.right ].visible = true;
  120. window.handModels = handModels;
  121. hand2.addEventListener( 'pinchend', evt => {
  122. cycleHandModel( evt.handedness );
  123. } );
  124. //
  125. window.hands = [ hand1, hand2 ];
  126. const geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  127. const line = new THREE.Line( geometry );
  128. line.name = 'line';
  129. line.scale.z = 5;
  130. controller1.add( line.clone() );
  131. controller2.add( line.clone() );
  132. //
  133. window.addEventListener( 'resize', onWindowResize );
  134. }
  135. function onWindowResize() {
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. }
  140. //
  141. function animate() {
  142. renderer.setAnimationLoop( render );
  143. }
  144. function render() {
  145. renderer.render( scene, camera );
  146. }
  147. </script>
  148. </body>
  149. </html>