webxr_vr_handinput.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - hand and controller</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 - hand and controller support
  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/WebXRHandController.js';
  19. var container;
  20. var camera, scene, renderer;
  21. var hand1, hand2;
  22. var controller1, controller2;
  23. var controllerGrip1, controllerGrip2;
  24. var controls;
  25. var grabbing = false;
  26. var scaling = {
  27. active: false,
  28. initialDistance: 0,
  29. object: null,
  30. initialScale: 1
  31. };
  32. var spheres = [];
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0x808080 );
  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. var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
  46. var material = new THREE.MeshStandardMaterial( {
  47. color: 0xeeeeee,
  48. roughness: 1.0,
  49. metalness: 0.0
  50. } );
  51. var floor = new THREE.Mesh( geometry, material );
  52. floor.rotation.x = - Math.PI / 2;
  53. floor.receiveShadow = true;
  54. scene.add( floor );
  55. scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
  56. var light = new THREE.DirectionalLight( 0xffffff );
  57. light.position.set( 0, 6, 0 );
  58. light.castShadow = true;
  59. light.shadow.camera.top = 2;
  60. light.shadow.camera.bottom = - 2;
  61. light.shadow.camera.right = 2;
  62. light.shadow.camera.left = - 2;
  63. light.shadow.mapSize.set( 4096, 4096 );
  64. scene.add( light );
  65. //
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.outputEncoding = THREE.sRGBEncoding;
  70. renderer.shadowMap.enabled = true;
  71. renderer.xr.enabled = true;
  72. container.appendChild( renderer.domElement );
  73. document.body.appendChild( VRButton.createButton( renderer ) );
  74. // controllers
  75. controller1 = renderer.xr.getController( 0 );
  76. scene.add( controller1 );
  77. controller2 = renderer.xr.getController( 1 );
  78. scene.add( controller2 );
  79. var controllerModelFactory = new XRControllerModelFactory();
  80. var handModelFactory = new XRHandModelFactory();
  81. // Hand 1
  82. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  83. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  84. scene.add( controllerGrip1 );
  85. hand1 = renderer.xr.getHand( 0 );
  86. hand1.addEventListener( 'pinchstart', onPinchStartLeft );
  87. hand1.addEventListener( 'pinchend', () => {
  88. scaling.active = false;
  89. } );
  90. hand1.add( handModelFactory.createHandModel( hand1 ) );
  91. scene.add( hand1 );
  92. // Hand 2
  93. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  94. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  95. scene.add( controllerGrip2 );
  96. hand2 = renderer.xr.getHand( 1 );
  97. hand2.addEventListener( 'pinchstart', onPinchStartRight );
  98. hand2.addEventListener( 'pinchend', onPinchEndRight );
  99. hand2.add( handModelFactory.createHandModel( hand2 ) );
  100. scene.add( hand2 );
  101. //
  102. var geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  103. var line = new THREE.Line( geometry );
  104. line.name = 'line';
  105. line.scale.z = 5;
  106. controller1.add( line.clone() );
  107. controller2.add( line.clone() );
  108. //
  109. window.addEventListener( 'resize', onWindowResize, false );
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. const SphereRadius = 0.05;
  117. function onPinchStartLeft( event ) {
  118. var controller = event.target;
  119. if ( grabbing ) {
  120. const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
  121. const sphere = collideObject( indexTip );
  122. if ( sphere ) {
  123. const sphere2 = hand2.userData.selected;
  124. console.log( "sphere1", sphere, "sphere2", sphere2 );
  125. if ( sphere === sphere2 ) {
  126. scaling.active = true;
  127. scaling.object = sphere;
  128. scaling.initialScale = sphere.scale.x;
  129. scaling.initialDistance = indexTip.position.distanceTo( hand2.joints[ XRHand.INDEX_PHALANX_TIP ].position );
  130. return;
  131. }
  132. }
  133. }
  134. var geometry = new THREE.BoxBufferGeometry( SphereRadius, SphereRadius, SphereRadius );
  135. var material = new THREE.MeshStandardMaterial( {
  136. color: Math.random() * 0xffffff,
  137. roughness: 1.0,
  138. metalness: 0.0
  139. } );
  140. var spawn = new THREE.Mesh( geometry, material );
  141. spawn.geometry.computeBoundingSphere();
  142. const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
  143. spawn.position.copy( indexTip.position );
  144. spawn.quaternion.copy( indexTip.quaternion );
  145. spheres.push( spawn );
  146. scene.add( spawn );
  147. }
  148. function collideObject( indexTip ) {
  149. for ( var i = 0; i < spheres.length; i ++ ) {
  150. const sphere = spheres[ i ];
  151. const distance = indexTip.getWorldPosition().distanceTo( sphere.getWorldPosition() );
  152. if ( distance < sphere.geometry.boundingSphere.radius * sphere.scale.x ) {
  153. return sphere;
  154. }
  155. }
  156. return null;
  157. }
  158. function onPinchStartRight( event ) {
  159. var controller = event.target;
  160. const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
  161. const object = collideObject( indexTip );
  162. if ( object ) {
  163. grabbing = true;
  164. indexTip.attach( object );
  165. controller.userData.selected = object;
  166. console.log( "Selected", object );
  167. }
  168. }
  169. function onPinchEndRight( event ) {
  170. var controller = event.target;
  171. if ( controller.userData.selected !== undefined ) {
  172. var object = controller.userData.selected;
  173. object.material.emissive.b = 0;
  174. scene.attach( object );
  175. controller.userData.selected = undefined;
  176. grabbing = false;
  177. }
  178. scaling.active = false;
  179. }
  180. //
  181. function animate() {
  182. renderer.setAnimationLoop( render );
  183. }
  184. function render() {
  185. if ( scaling.active ) {
  186. const indexTip1Pos = hand1.joints[ XRHand.INDEX_PHALANX_TIP ].position;
  187. const indexTip2Pos = hand2.joints[ XRHand.INDEX_PHALANX_TIP ].position;
  188. const distance = indexTip1Pos.distanceTo( indexTip2Pos );
  189. const newScale = scaling.initialScale + distance / scaling.initialDistance - 1;
  190. scaling.object.scale.setScalar( newScale );
  191. }
  192. renderer.render( scene, camera );
  193. }
  194. </script>
  195. </body>
  196. </html>