webxr_vr_handinput_cubes.html 7.3 KB

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