1
0

webxr_vr_handinput_cubes.html 7.5 KB

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