webxr_vr_handinput.html 4.3 KB

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