webxr_vr_handinput_cubes.html 7.5 KB

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