webxr_vr_handinput.html 4.2 KB

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