webxr_vr_handinput_mesh.html 4.3 KB

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