webxr_vr_handinput_cubes.html 7.4 KB

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