webxr_vr_handtracking.html 8.5 KB

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