webxr_vr_handinput_cubes.html 7.7 KB

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