webxr_vr_handinput_profiles.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - handinput - profiles</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 - profiles<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 handModels = {
  34. left: null,
  35. right: null
  36. };
  37. let controls;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. scene = new THREE.Scene();
  44. window.scene = scene;
  45. scene.background = new THREE.Color( 0x444444 );
  46. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  47. camera.position.set( 0, 1.6, 3 );
  48. controls = new OrbitControls( camera, container );
  49. controls.target.set( 0, 1.6, 0 );
  50. controls.update();
  51. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  52. const floorMaterial = new THREE.MeshStandardMaterial( { color: 0x666666 } );
  53. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  54. floor.rotation.x = - Math.PI / 2;
  55. floor.receiveShadow = true;
  56. scene.add( floor );
  57. scene.add( new THREE.HemisphereLight( 0xbcbcbc, 0xa5a5a5, 3 ) );
  58. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  59. light.position.set( 0, 6, 0 );
  60. light.castShadow = true;
  61. light.shadow.camera.top = 2;
  62. light.shadow.camera.bottom = - 2;
  63. light.shadow.camera.right = 2;
  64. light.shadow.camera.left = - 2;
  65. light.shadow.mapSize.set( 4096, 4096 );
  66. scene.add( light );
  67. //
  68. renderer = new THREE.WebGLRenderer( { antialias: true } );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.shadowMap.enabled = true;
  72. renderer.xr.enabled = true;
  73. container.appendChild( renderer.domElement );
  74. document.body.appendChild( VRButton.createButton( renderer ) );
  75. // controllers
  76. controller1 = renderer.xr.getController( 0 );
  77. scene.add( controller1 );
  78. controller2 = renderer.xr.getController( 1 );
  79. scene.add( controller2 );
  80. const controllerModelFactory = new XRControllerModelFactory();
  81. const handModelFactory = new XRHandModelFactory();
  82. // Hand 1
  83. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  84. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  85. scene.add( controllerGrip1 );
  86. hand1 = renderer.xr.getHand( 0 );
  87. hand1.userData.currentHandModel = 0;
  88. scene.add( hand1 );
  89. handModels.left = [
  90. handModelFactory.createHandModel( hand1, 'boxes' ),
  91. handModelFactory.createHandModel( hand1, 'spheres' ),
  92. handModelFactory.createHandModel( hand1, 'mesh' )
  93. ];
  94. for ( let i = 0; i < 3; i ++ ) {
  95. const model = handModels.left[ i ];
  96. model.visible = i == 0;
  97. hand1.add( model );
  98. }
  99. hand1.addEventListener( 'pinchend', function () {
  100. handModels.left[ this.userData.currentHandModel ].visible = false;
  101. this.userData.currentHandModel = ( this.userData.currentHandModel + 1 ) % 3;
  102. handModels.left[ this.userData.currentHandModel ].visible = true;
  103. } );
  104. // Hand 2
  105. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  106. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  107. scene.add( controllerGrip2 );
  108. hand2 = renderer.xr.getHand( 1 );
  109. hand2.userData.currentHandModel = 0;
  110. scene.add( hand2 );
  111. handModels.right = [
  112. handModelFactory.createHandModel( hand2, 'boxes' ),
  113. handModelFactory.createHandModel( hand2, 'spheres' ),
  114. handModelFactory.createHandModel( hand2, 'mesh' )
  115. ];
  116. for ( let i = 0; i < 3; i ++ ) {
  117. const model = handModels.right[ i ];
  118. model.visible = i == 0;
  119. hand2.add( model );
  120. }
  121. hand2.addEventListener( 'pinchend', function () {
  122. handModels.right[ this.userData.currentHandModel ].visible = false;
  123. this.userData.currentHandModel = ( this.userData.currentHandModel + 1 ) % 3;
  124. handModels.right[ this.userData.currentHandModel ].visible = true;
  125. } );
  126. //
  127. const geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  128. const line = new THREE.Line( geometry );
  129. line.name = 'line';
  130. line.scale.z = 5;
  131. controller1.add( line.clone() );
  132. controller2.add( line.clone() );
  133. //
  134. window.addEventListener( 'resize', onWindowResize );
  135. }
  136. function onWindowResize() {
  137. camera.aspect = window.innerWidth / window.innerHeight;
  138. camera.updateProjectionMatrix();
  139. renderer.setSize( window.innerWidth, window.innerHeight );
  140. }
  141. //
  142. function animate() {
  143. renderer.setAnimationLoop( render );
  144. }
  145. function render() {
  146. renderer.render( scene, camera );
  147. }
  148. </script>
  149. </body>
  150. </html>