1
0

webxr_vr_handinput.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - handinput</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<br/>
  12. (Oculus Browser with #webxr-hands flag enabled)
  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. let controls;
  26. init();
  27. animate();
  28. function init() {
  29. container = document.createElement( 'div' );
  30. document.body.appendChild( container );
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0x444444 );
  33. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  34. camera.position.set( 0, 1.6, 3 );
  35. controls = new OrbitControls( camera, container );
  36. controls.target.set( 0, 1.6, 0 );
  37. controls.update();
  38. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  39. const floorMaterial = new THREE.MeshStandardMaterial( { color: 0x222222 } );
  40. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  41. floor.rotation.x = - Math.PI / 2;
  42. floor.receiveShadow = true;
  43. scene.add( floor );
  44. scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
  45. const light = new THREE.DirectionalLight( 0xffffff );
  46. light.position.set( 0, 6, 0 );
  47. light.castShadow = true;
  48. light.shadow.camera.top = 2;
  49. light.shadow.camera.bottom = - 2;
  50. light.shadow.camera.right = 2;
  51. light.shadow.camera.left = - 2;
  52. light.shadow.mapSize.set( 4096, 4096 );
  53. scene.add( light );
  54. //
  55. renderer = new THREE.WebGLRenderer( { antialias: true } );
  56. renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. renderer.outputEncoding = THREE.sRGBEncoding;
  59. renderer.shadowMap.enabled = true;
  60. renderer.xr.enabled = true;
  61. container.appendChild( renderer.domElement );
  62. document.body.appendChild( VRButton.createButton( renderer ) );
  63. // controllers
  64. controller1 = renderer.xr.getController( 0 );
  65. scene.add( controller1 );
  66. controller2 = renderer.xr.getController( 1 );
  67. scene.add( controller2 );
  68. const controllerModelFactory = new XRControllerModelFactory();
  69. const handModelFactory = new XRHandModelFactory().setPath( "./models/fbx/" );
  70. // Hand 1
  71. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  72. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  73. scene.add( controllerGrip1 );
  74. hand1 = renderer.xr.getHand( 0 );
  75. hand1.add( handModelFactory.createHandModel( hand1 ) );
  76. scene.add( hand1 );
  77. // Hand 2
  78. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  79. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  80. scene.add( controllerGrip2 );
  81. hand2 = renderer.xr.getHand( 1 );
  82. hand2.add( handModelFactory.createHandModel( hand2 ) );
  83. scene.add( hand2 );
  84. //
  85. const geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  86. const line = new THREE.Line( geometry );
  87. line.name = 'line';
  88. line.scale.z = 5;
  89. controller1.add( line.clone() );
  90. controller2.add( line.clone() );
  91. //
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. //
  100. function animate() {
  101. renderer.setAnimationLoop( render );
  102. }
  103. function render() {
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>