webxr_vr_handinput_mesh.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - hand and controller - simple</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 - hand and controller support
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { VRButton } from './jsm/webxr/VRButton.js';
  17. import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
  18. import { XRHandModelFactory } from './jsm/webxr/WebXRHandController.js';
  19. var container;
  20. var camera, scene, renderer;
  21. var hand1, hand2;
  22. var controller1, controller2;
  23. var controllerGrip1, controllerGrip2;
  24. var currentHandModel = {
  25. left: 0,
  26. right: 0
  27. };
  28. var handModels = {
  29. left: null,
  30. right: null
  31. };
  32. var controls;
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. scene = new THREE.Scene();
  39. window.scene = scene;
  40. scene.background = new THREE.Color( 0x808080 );
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  42. camera.position.set( 0, 1.6, 3 );
  43. controls = new OrbitControls( camera, container );
  44. controls.target.set( 0, 1.6, 0 );
  45. controls.update();
  46. var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
  47. var material = new THREE.MeshStandardMaterial( {
  48. color: 0x333333,
  49. roughness: 1.0,
  50. metalness: 0.0
  51. } );
  52. var floor = new THREE.Mesh( geometry, material );
  53. floor.rotation.x = - Math.PI / 2;
  54. floor.receiveShadow = true;
  55. scene.add( floor );
  56. scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
  57. var light = new THREE.DirectionalLight( 0xffffff );
  58. light.position.set( 0, 6, 0 );
  59. light.castShadow = true;
  60. light.shadow.camera.top = 2;
  61. light.shadow.camera.bottom = - 2;
  62. light.shadow.camera.right = 2;
  63. light.shadow.camera.left = - 2;
  64. light.shadow.mapSize.set( 4096, 4096 );
  65. scene.add( light );
  66. //
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.outputEncoding = THREE.sRGBEncoding;
  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. var controllerModelFactory = new XRControllerModelFactory();
  81. var 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. scene.add( hand1 );
  88. handModels.left = [
  89. handModelFactory.createHandModel( hand1, "boxes" ),
  90. handModelFactory.createHandModel( hand1, "spheres" ),
  91. handModelFactory.createHandModel( hand1, "oculus", { model: "lowpoly" } ),
  92. handModelFactory.createHandModel( hand1, "oculus" )
  93. ];
  94. handModels.left.forEach( model => {
  95. model.visible = false;
  96. hand1.add( model );
  97. } );
  98. handModels.left[ currentHandModel.left ].visible = true;
  99. function cycleHandModel( hand ) {
  100. handModels[ hand ][ currentHandModel[ hand ] ].visible = false;
  101. currentHandModel[ hand ] = ( currentHandModel[ hand ] + 1 ) % handModels[ hand ].length;
  102. handModels[ hand ][ currentHandModel[ hand ] ].visible = true;
  103. }
  104. hand1.addEventListener( 'pinchend', evt => {
  105. cycleHandModel( evt.handedness );
  106. } );
  107. // Hand 2
  108. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  109. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  110. scene.add( controllerGrip2 );
  111. hand2 = renderer.xr.getHand( 1 );
  112. scene.add( hand2 );
  113. handModels.right = [
  114. handModelFactory.createHandModel( hand2, "boxes" ),
  115. handModelFactory.createHandModel( hand2, "spheres" ),
  116. handModelFactory.createHandModel( hand2, "oculus", { model: "lowpoly" } ),
  117. handModelFactory.createHandModel( hand2, "oculus" )
  118. ];
  119. handModels.right.forEach( model => {
  120. model.visible = false;
  121. hand2.add( model );
  122. } );
  123. handModels.right[ currentHandModel.right ].visible = true;
  124. window.handModels = handModels;
  125. hand2.addEventListener( 'pinchend', evt => {
  126. cycleHandModel( evt.handedness );
  127. } );
  128. //
  129. window.hands = [ hand1, hand2 ];
  130. var geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  131. var line = new THREE.Line( geometry );
  132. line.name = 'line';
  133. line.scale.z = 5;
  134. controller1.add( line.clone() );
  135. controller2.add( line.clone() );
  136. //
  137. window.addEventListener( 'resize', onWindowResize, false );
  138. }
  139. function onWindowResize() {
  140. camera.aspect = window.innerWidth / window.innerHeight;
  141. camera.updateProjectionMatrix();
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. }
  144. //
  145. function animate() {
  146. renderer.setAnimationLoop( render );
  147. }
  148. function render() {
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>