Viewport.VR.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import * as THREE from '../../build/three.module.js';
  2. import { HTMLMesh } from './libs/three.html.js';
  3. import { XRControllerModelFactory } from '../../examples/jsm/webxr/XRControllerModelFactory.js';
  4. class VR {
  5. constructor( editor ) {
  6. const signals = editor.signals;
  7. let group = null;
  8. let camera = null;
  9. let renderer = null;
  10. const intersectables = [];
  11. this.currentSession = null;
  12. const onSessionStarted = async ( session ) => {
  13. const sidebar = document.getElementById( 'sidebar' );
  14. sidebar.style.width = '300px';
  15. sidebar.style.height = '700px';
  16. //
  17. if ( group === null ) {
  18. group = new THREE.Group();
  19. editor.sceneHelpers.add( group );
  20. const mesh = new HTMLMesh( sidebar );
  21. mesh.position.set( 1, 1.5, - 0.5 );
  22. mesh.rotation.y = - 0.5;
  23. group.add( mesh );
  24. intersectables.push( mesh );
  25. // controllers
  26. const controller1 = renderer.xr.getController( 0 );
  27. controller1.addEventListener( 'select', onSelect );
  28. group.add( controller1 );
  29. const controller2 = renderer.xr.getController( 1 );
  30. controller2.addEventListener( 'selectstart', onSelect );
  31. group.add( controller2 );
  32. const geometry = new THREE.BufferGeometry();
  33. geometry.setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 5 ) ] );
  34. controller1.add( new THREE.Line( geometry ) );
  35. controller2.add( new THREE.Line( geometry ) );
  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. //
  64. function onSelect( event ) {
  65. const controller = event.target;
  66. const intersections = getIntersections( controller );
  67. if ( intersections.length > 0 ) {
  68. const intersection = intersections[ 0 ];
  69. const object = intersection.object;
  70. const uv = intersection.uv;
  71. object.material.map.click( uv.x, 1 - uv.y );
  72. }
  73. }
  74. const raycaster = new THREE.Raycaster();
  75. const tempMatrix = new THREE.Matrix4();
  76. function getIntersections( controller ) {
  77. tempMatrix.identity().extractRotation( controller.matrixWorld );
  78. raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  79. raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  80. return raycaster.intersectObjects( intersectables );
  81. }
  82. // signals
  83. signals.toggleVR.add( () => {
  84. if ( this.currentSession === null ) {
  85. const sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor' ] };
  86. navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted );
  87. } else {
  88. this.currentSession.end();
  89. }
  90. } );
  91. signals.rendererChanged.add( ( value ) => {
  92. renderer = value;
  93. renderer.xr.enabled = true;
  94. } );
  95. }
  96. }
  97. export { VR };