Viewport.VR.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import * as THREE from '../../build/three.module.js';
  2. import { HTMLMesh } from '../../examples/jsm/interactive/HTMLMesh.js';
  3. import { InteractiveGroup } from '../../examples/jsm/interactive/InteractiveGroup.js';
  4. import { XRControllerModelFactory } from '../../examples/jsm/webxr/XRControllerModelFactory.js';
  5. class VR {
  6. constructor( editor ) {
  7. const signals = editor.signals;
  8. let group = null;
  9. let camera = null;
  10. let renderer = null;
  11. const intersectables = [];
  12. this.currentSession = null;
  13. const onSessionStarted = async ( session ) => {
  14. const sidebar = document.getElementById( 'sidebar' );
  15. sidebar.style.width = '300px';
  16. sidebar.style.height = '700px';
  17. //
  18. if ( group === null ) {
  19. group = new InteractiveGroup( renderer );
  20. editor.sceneHelpers.add( group );
  21. const mesh = new HTMLMesh( sidebar );
  22. mesh.position.set( 1, 1.5, - 0.5 );
  23. mesh.rotation.y = - 0.5;
  24. mesh.scale.setScalar( 2 );
  25. group.add( mesh );
  26. intersectables.push( mesh );
  27. // controllers
  28. const geometry = new THREE.BufferGeometry();
  29. geometry.setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 5 ) ] );
  30. const controller1 = renderer.xr.getController( 0 );
  31. controller1.add( new THREE.Line( geometry ) );
  32. group.add( controller1 );
  33. const controller2 = renderer.xr.getController( 1 );
  34. controller2.add( new THREE.Line( geometry ) );
  35. group.add( controller2 );
  36. //
  37. const controllerModelFactory = new XRControllerModelFactory();
  38. const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  39. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  40. group.add( controllerGrip1 );
  41. const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  42. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  43. group.add( controllerGrip2 );
  44. }
  45. camera = editor.camera.clone();
  46. group.visible = true;
  47. this.currentSession = session;
  48. this.currentSession.addEventListener( 'end', onSessionEnded );
  49. await renderer.xr.setSession( this.currentSession );
  50. };
  51. const onSessionEnded = async () => {
  52. const sidebar = document.getElementById( 'sidebar' );
  53. sidebar.style.width = '';
  54. sidebar.style.height = '';
  55. //
  56. editor.camera.copy( camera );
  57. group.visible = false;
  58. this.currentSession.removeEventListener( 'end', onSessionEnded );
  59. this.currentSession = null;
  60. await renderer.xr.setSession( null );
  61. signals.exitedVR.dispatch();
  62. };
  63. // signals
  64. signals.toggleVR.add( () => {
  65. if ( this.currentSession === null ) {
  66. const sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor' ] };
  67. navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted );
  68. } else {
  69. this.currentSession.end();
  70. }
  71. } );
  72. signals.rendererCreated.add( ( value ) => {
  73. renderer = value;
  74. renderer.xr.enabled = true;
  75. } );
  76. }
  77. }
  78. export { VR };