Viewport.VR.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if ( group === null ) {
  14. group = new THREE.Group();
  15. editor.sceneHelpers.add( group );
  16. const mesh = new HTMLMesh( document.getElementById( 'sidebar' ), 300, 700 );
  17. mesh.position.set( 1, 1.5, - 0.5 );
  18. mesh.rotation.y = - 0.5;
  19. group.add( mesh );
  20. intersectables.push( mesh );
  21. // controllers
  22. const controller1 = renderer.xr.getController( 0 );
  23. controller1.addEventListener( 'select', onSelect );
  24. group.add( controller1 );
  25. const controller2 = renderer.xr.getController( 1 );
  26. controller2.addEventListener( 'selectstart', onSelect );
  27. group.add( controller2 );
  28. const geometry = new THREE.BufferGeometry();
  29. geometry.setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 5 ) ] );
  30. controller1.add( new THREE.Line( geometry ) );
  31. controller2.add( new THREE.Line( geometry ) );
  32. //
  33. const controllerModelFactory = new XRControllerModelFactory();
  34. const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  35. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  36. group.add( controllerGrip1 );
  37. const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  38. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  39. group.add( controllerGrip2 );
  40. }
  41. camera = editor.camera.clone();
  42. group.visible = true;
  43. this.currentSession = session;
  44. this.currentSession.addEventListener( 'end', onSessionEnded );
  45. await renderer.xr.setSession( this.currentSession );
  46. };
  47. const onSessionEnded = async () => {
  48. editor.camera.copy( camera );
  49. group.visible = false;
  50. this.currentSession.removeEventListener( 'end', onSessionEnded );
  51. this.currentSession = null;
  52. await renderer.xr.setSession( null );
  53. signals.exitedVR.dispatch();
  54. };
  55. //
  56. function onSelect( event ) {
  57. const controller = event.target;
  58. const intersections = getIntersections( controller );
  59. if ( intersections.length > 0 ) {
  60. const intersection = intersections[ 0 ];
  61. const object = intersection.object;
  62. const uv = intersection.uv;
  63. object.material.map.click( uv.x, 1 - uv.y );
  64. }
  65. }
  66. const raycaster = new THREE.Raycaster();
  67. const tempMatrix = new THREE.Matrix4();
  68. function getIntersections( controller ) {
  69. tempMatrix.identity().extractRotation( controller.matrixWorld );
  70. raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  71. raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  72. return raycaster.intersectObjects( intersectables );
  73. }
  74. // signals
  75. signals.toggleVR.add( () => {
  76. if ( this.currentSession === null ) {
  77. const sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor' ] };
  78. navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted );
  79. } else {
  80. this.currentSession.end();
  81. }
  82. } );
  83. signals.rendererChanged.add( ( value ) => {
  84. renderer = value;
  85. renderer.xr.enabled = true;
  86. } );
  87. }
  88. }
  89. export { VR };