webxr_vr_handinput_cubes.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 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. let controls;
  26. let grabbing = false;
  27. const scaling = {
  28. active: false,
  29. initialDistance: 0,
  30. object: null,
  31. initialScale: 1
  32. };
  33. const spheres = [];
  34. init();
  35. animate();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. scene = new THREE.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. hand1.addEventListener( 'pinchstart', onPinchStartLeft );
  84. hand1.addEventListener( 'pinchend', () => {
  85. scaling.active = false;
  86. } );
  87. hand1.add( handModelFactory.createHandModel( hand1 ) );
  88. scene.add( hand1 );
  89. // Hand 2
  90. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  91. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  92. scene.add( controllerGrip2 );
  93. hand2 = renderer.xr.getHand( 1 );
  94. hand2.addEventListener( 'pinchstart', onPinchStartRight );
  95. hand2.addEventListener( 'pinchend', onPinchEndRight );
  96. hand2.add( handModelFactory.createHandModel( hand2 ) );
  97. scene.add( hand2 );
  98. //
  99. const geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  100. const line = new THREE.Line( geometry );
  101. line.name = 'line';
  102. line.scale.z = 5;
  103. controller1.add( line.clone() );
  104. controller2.add( line.clone() );
  105. //
  106. window.addEventListener( 'resize', onWindowResize, false );
  107. }
  108. function onWindowResize() {
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. }
  113. const SphereRadius = 0.05;
  114. function onPinchStartLeft( event ) {
  115. const controller = event.target;
  116. if ( grabbing ) {
  117. const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
  118. const sphere = collideObject( indexTip );
  119. if ( sphere ) {
  120. const sphere2 = hand2.userData.selected;
  121. console.log( "sphere1", sphere, "sphere2", sphere2 );
  122. if ( sphere === sphere2 ) {
  123. scaling.active = true;
  124. scaling.object = sphere;
  125. scaling.initialScale = sphere.scale.x;
  126. scaling.initialDistance = indexTip.position.distanceTo( hand2.joints[ XRHand.INDEX_PHALANX_TIP ].position );
  127. return;
  128. }
  129. }
  130. }
  131. const geometry = new THREE.BoxGeometry( SphereRadius, SphereRadius, SphereRadius );
  132. const material = new THREE.MeshStandardMaterial( {
  133. color: Math.random() * 0xffffff,
  134. roughness: 1.0,
  135. metalness: 0.0
  136. } );
  137. const spawn = new THREE.Mesh( geometry, material );
  138. spawn.geometry.computeBoundingSphere();
  139. const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
  140. spawn.position.copy( indexTip.position );
  141. spawn.quaternion.copy( indexTip.quaternion );
  142. spheres.push( spawn );
  143. scene.add( spawn );
  144. }
  145. function collideObject( indexTip ) {
  146. for ( let i = 0; i < spheres.length; i ++ ) {
  147. const sphere = spheres[ i ];
  148. const distance = indexTip.getWorldPosition().distanceTo( sphere.getWorldPosition() );
  149. if ( distance < sphere.geometry.boundingSphere.radius * sphere.scale.x ) {
  150. return sphere;
  151. }
  152. }
  153. return null;
  154. }
  155. function onPinchStartRight( event ) {
  156. const controller = event.target;
  157. const indexTip = controller.joints[ XRHand.INDEX_PHALANX_TIP ];
  158. const object = collideObject( indexTip );
  159. if ( object ) {
  160. grabbing = true;
  161. indexTip.attach( object );
  162. controller.userData.selected = object;
  163. console.log( "Selected", object );
  164. }
  165. }
  166. function onPinchEndRight( event ) {
  167. const controller = event.target;
  168. if ( controller.userData.selected !== undefined ) {
  169. const object = controller.userData.selected;
  170. object.material.emissive.b = 0;
  171. scene.attach( object );
  172. controller.userData.selected = undefined;
  173. grabbing = false;
  174. }
  175. scaling.active = false;
  176. }
  177. //
  178. function animate() {
  179. renderer.setAnimationLoop( render );
  180. }
  181. function render() {
  182. if ( scaling.active ) {
  183. const indexTip1Pos = hand1.joints[ XRHand.INDEX_PHALANX_TIP ].position;
  184. const indexTip2Pos = hand2.joints[ XRHand.INDEX_PHALANX_TIP ].position;
  185. const distance = indexTip1Pos.distanceTo( indexTip2Pos );
  186. const newScale = scaling.initialScale + distance / scaling.initialDistance - 1;
  187. scaling.object.scale.setScalar( newScale );
  188. }
  189. renderer.render( scene, camera );
  190. }
  191. </script>
  192. </body>
  193. </html>