webxr_vr_handinput_profiles.html 5.5 KB

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