webxr_vr_handinput_profiles.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - handinput - profiles</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 - profiles
  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. const handModels = {
  25. left: null,
  26. right: null
  27. };
  28. let controls;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. scene = new THREE.Scene();
  35. window.scene = scene;
  36. scene.background = new THREE.Color( 0x444444 );
  37. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  38. camera.position.set( 0, 1.6, 3 );
  39. controls = new OrbitControls( camera, container );
  40. controls.target.set( 0, 1.6, 0 );
  41. controls.update();
  42. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  43. const floorMaterial = new THREE.MeshStandardMaterial( { color: 0x222222 } );
  44. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  45. floor.rotation.x = - Math.PI / 2;
  46. floor.receiveShadow = true;
  47. scene.add( floor );
  48. scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
  49. const 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. const controllerModelFactory = new XRControllerModelFactory();
  73. const 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.userData.currentHandModel = 0;
  80. scene.add( hand1 );
  81. handModels.left = [
  82. handModelFactory.createHandModel( hand1, "boxes" ),
  83. handModelFactory.createHandModel( hand1, "spheres" ),
  84. handModelFactory.createHandModel( hand1, "mesh" )
  85. ];
  86. for ( let i = 0; i < 3; i ++ ) {
  87. const model = handModels.left[ i ];
  88. model.visible = i == 0;
  89. hand1.add( model );
  90. }
  91. hand1.addEventListener( 'pinchend', function ( event ) {
  92. handModels.left[ this.userData.currentHandModel ].visible = false;
  93. this.userData.currentHandModel = ( this.userData.currentHandModel + 1 ) % 3;
  94. handModels.left[ this.userData.currentHandModel ].visible = true;
  95. } );
  96. // Hand 2
  97. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  98. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  99. scene.add( controllerGrip2 );
  100. hand2 = renderer.xr.getHand( 1 );
  101. hand2.userData.currentHandModel = 0;
  102. scene.add( hand2 );
  103. handModels.right = [
  104. handModelFactory.createHandModel( hand2, "boxes" ),
  105. handModelFactory.createHandModel( hand2, "spheres" ),
  106. handModelFactory.createHandModel( hand2, "mesh" )
  107. ];
  108. for ( let i = 0; i < 3; i ++ ) {
  109. const model = handModels.right[ i ];
  110. model.visible = i == 0;
  111. hand2.add( model );
  112. }
  113. hand2.addEventListener( 'pinchend', function ( evevent ) {
  114. handModels.right[ this.userData.currentHandModel ].visible = false;
  115. this.userData.currentHandModel = ( this.userData.currentHandModel + 1 ) % 3;
  116. handModels.right[ this.userData.currentHandModel ].visible = true;
  117. } );
  118. //
  119. const geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  120. const line = new THREE.Line( geometry );
  121. line.name = 'line';
  122. line.scale.z = 5;
  123. controller1.add( line.clone() );
  124. controller2.add( line.clone() );
  125. //
  126. window.addEventListener( 'resize', onWindowResize );
  127. }
  128. function onWindowResize() {
  129. camera.aspect = window.innerWidth / window.innerHeight;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. //
  134. function animate() {
  135. renderer.setAnimationLoop( render );
  136. }
  137. function render() {
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>
粤ICP备19079148号