babylon.vrExperienceHelper.ts 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. module BABYLON {
  2. /**
  3. * Options to modify the vr teleportation behavior.
  4. */
  5. export interface VRTeleportationOptions {
  6. /**
  7. * The name of the mesh which should be used as the teleportation floor. (default: null)
  8. */
  9. floorMeshName?: string;
  10. /**
  11. * A list of meshes to be used as the teleportation floor. (default: empty)
  12. */
  13. floorMeshes?: Mesh[];
  14. }
  15. /**
  16. * Options to modify the vr experience helper's behavior.
  17. */
  18. export interface VRExperienceHelperOptions extends WebVROptions {
  19. /**
  20. * Create a DeviceOrientationCamera to be used as your out of vr camera.
  21. */
  22. createDeviceOrientationCamera?: boolean;
  23. /**
  24. * Create a VRDeviceOrientationFreeCamera to be used for VR when no external HMD is found.
  25. */
  26. createFallbackVRDeviceOrientationFreeCamera?: boolean;
  27. }
  28. /**
  29. * Helps to quickly add VR support to an existing scene.
  30. * See http://doc.babylonjs.com/how_to/webvr_helper
  31. */
  32. export class VRExperienceHelper {
  33. private _scene: Scene;
  34. private _position: Vector3;
  35. private _btnVR: HTMLButtonElement;
  36. private _btnVRDisplayed: boolean;
  37. // Can the system support WebVR, even if a headset isn't plugged in?
  38. private _webVRsupported = false;
  39. // If WebVR is supported, is a headset plugged in and are we ready to present?
  40. private _webVRready = false;
  41. // Are we waiting for the requestPresent callback to complete?
  42. private _webVRrequesting = false;
  43. // Are we presenting to the headset right now?
  44. private _webVRpresenting = false;
  45. // Are we presenting in the fullscreen fallback?
  46. private _fullscreenVRpresenting = false;
  47. private _canvas: Nullable<HTMLCanvasElement>;
  48. private _webVRCamera: WebVRFreeCamera;
  49. private _vrDeviceOrientationCamera: Nullable<VRDeviceOrientationFreeCamera>;
  50. private _deviceOrientationCamera: Nullable<DeviceOrientationCamera>;
  51. private _existingCamera: Camera;
  52. private _onKeyDown: (event: KeyboardEvent) => void;
  53. private _onVrDisplayPresentChange: any;
  54. private _onVRDisplayChanged: (eventArgs: IDisplayChangedEventArgs) => void;
  55. private _onVRRequestPresentStart: () => void;
  56. private _onVRRequestPresentComplete: (success: boolean) => void;
  57. /**
  58. * Observable raised when entering VR.
  59. */
  60. public onEnteringVRObservable = new Observable<VRExperienceHelper>();
  61. /**
  62. * Observable raised when exiting VR.
  63. */
  64. public onExitingVRObservable = new Observable<VRExperienceHelper>();
  65. /**
  66. * Observable raised when controller mesh is loaded.
  67. */
  68. public onControllerMeshLoadedObservable = new Observable<WebVRController>();
  69. /** Return this.onEnteringVRObservable
  70. * Note: This one is for backward compatibility. Please use onEnteringVRObservable directly
  71. */
  72. public get onEnteringVR(): Observable<VRExperienceHelper> {
  73. return this.onEnteringVRObservable;
  74. }
  75. /** Return this.onExitingVRObservable
  76. * Note: This one is for backward compatibility. Please use onExitingVRObservable directly
  77. */
  78. public get onExitingVR(): Observable<VRExperienceHelper> {
  79. return this.onExitingVRObservable;
  80. }
  81. /** Return this.onControllerMeshLoadedObservable
  82. * Note: This one is for backward compatibility. Please use onControllerMeshLoadedObservable directly
  83. */
  84. public get onControllerMeshLoaded(): Observable<WebVRController> {
  85. return this.onControllerMeshLoadedObservable;
  86. }
  87. private _rayLength: number;
  88. private _useCustomVRButton: boolean = false;
  89. private _teleportationRequested: boolean = false;
  90. private _teleportationEnabledOnLeftController: boolean = false;
  91. private _teleportationEnabledOnRightController: boolean = false;
  92. private _interactionsEnabledOnLeftController: boolean = false;
  93. private _interactionsEnabledOnRightController: boolean = false;
  94. private _leftControllerReady: boolean = false;
  95. private _rightControllerReady: boolean = false;
  96. private _floorMeshName: string;
  97. private _floorMeshesCollection: Mesh[] = [];
  98. private _teleportationAllowed: boolean = false;
  99. private _rotationAllowed: boolean = true;
  100. private _teleportationRequestInitiated = false;
  101. private _teleportationBackRequestInitiated = false;
  102. private teleportBackwardsVector = new Vector3(0, -1, -1);
  103. private _rotationRightAsked = false;
  104. private _rotationLeftAsked = false;
  105. private _teleportationTarget: Mesh;
  106. private _isDefaultTeleportationTarget = true;
  107. private _postProcessMove: ImageProcessingPostProcess;
  108. private _passProcessMove: PassPostProcess;
  109. private _teleportationFillColor: string = "#444444";
  110. private _teleportationBorderColor: string = "#FFFFFF";
  111. private _rotationAngle: number = 0;
  112. private _haloCenter = new Vector3(0, 0, 0);
  113. private _gazeTracker: Mesh;
  114. private _padSensibilityUp = 0.65;
  115. private _padSensibilityDown = 0.35;
  116. private _leftLaserPointer: Nullable<Mesh>;
  117. private _rightLaserPointer: Nullable<Mesh>;
  118. private _currentMeshSelected: Nullable<AbstractMesh>;
  119. /**
  120. * Observable raised when a new mesh is selected based on meshSelectionPredicate
  121. */
  122. public onNewMeshSelected = new Observable<AbstractMesh>();
  123. /**
  124. * Observable raised when a new mesh is picked based on meshSelectionPredicate
  125. */
  126. public onNewMeshPicked = new Observable<PickingInfo>();
  127. private _circleEase: CircleEase;
  128. /**
  129. * Observable raised before camera teleportation
  130. */
  131. public onBeforeCameraTeleport = new Observable<Vector3>();
  132. /**
  133. * Observable raised after camera teleportation
  134. */
  135. public onAfterCameraTeleport = new Observable<Vector3>();
  136. /**
  137. * Observable raised when current selected mesh gets unselected
  138. */
  139. public onSelectedMeshUnselected = new Observable<AbstractMesh>();
  140. private _raySelectionPredicate: (mesh: AbstractMesh) => boolean;
  141. /**
  142. * To be optionaly changed by user to define custom ray selection
  143. */
  144. public raySelectionPredicate: (mesh: AbstractMesh) => boolean;
  145. /**
  146. * To be optionaly changed by user to define custom selection logic (after ray selection)
  147. */
  148. public meshSelectionPredicate: (mesh: AbstractMesh) => boolean;
  149. /**
  150. * Set teleportation enabled. If set to false camera teleportation will be disabled but camera rotation will be kept.
  151. */
  152. public teleportationEnabled: boolean = true;
  153. private _currentHit: Nullable<PickingInfo>;
  154. private _pointerDownOnMeshAsked = false;
  155. private _isActionableMesh = false;
  156. private _defaultHeight: number;
  157. private _teleportationInitialized = false;
  158. private _interactionsEnabled = false;
  159. private _interactionsRequested = false;
  160. private _displayGaze = true;
  161. private _displayLaserPointer = true;
  162. private _dpadPressed = true;
  163. /**
  164. * The mesh used to display where the user is going to teleport.
  165. */
  166. public get teleportationTarget(): Mesh {
  167. return this._teleportationTarget;
  168. }
  169. /**
  170. * Sets the mesh to be used to display where the user is going to teleport.
  171. */
  172. public set teleportationTarget(value: Mesh) {
  173. if (value) {
  174. value.name = "teleportationTarget";
  175. this._isDefaultTeleportationTarget = false;
  176. this._teleportationTarget = value;
  177. }
  178. }
  179. /**
  180. * The mesh used to display where the user is selecting,
  181. * when set bakeCurrentTransformIntoVertices will be called on the mesh.
  182. * See http://doc.babylonjs.com/resources/baking_transformations
  183. */
  184. public get gazeTrackerMesh(): Mesh {
  185. return this._gazeTracker;
  186. }
  187. public set gazeTrackerMesh(value: Mesh) {
  188. if (value) {
  189. this._gazeTracker = value;
  190. this._gazeTracker.bakeCurrentTransformIntoVertices();
  191. this._gazeTracker.isPickable = false;
  192. this._gazeTracker.isVisible = false;
  193. }
  194. }
  195. /**
  196. * If the ray of the gaze should be displayed.
  197. */
  198. public get displayGaze(): boolean {
  199. return this._displayGaze;
  200. }
  201. /**
  202. * Sets if the ray of the gaze should be displayed.
  203. */
  204. public set displayGaze(value: boolean) {
  205. this._displayGaze = value;
  206. if (!value) {
  207. this._gazeTracker.isVisible = false;
  208. }
  209. }
  210. /**
  211. * If the ray of the LaserPointer should be displayed.
  212. */
  213. public get displayLaserPointer(): boolean {
  214. return this._displayLaserPointer;
  215. }
  216. /**
  217. * Sets if the ray of the LaserPointer should be displayed.
  218. */
  219. public set displayLaserPointer(value: boolean) {
  220. this._displayLaserPointer = value;
  221. if (!value) {
  222. if (this._rightLaserPointer) {
  223. this._rightLaserPointer.isVisible = false;
  224. }
  225. if (this._leftLaserPointer) {
  226. this._leftLaserPointer.isVisible = false;
  227. }
  228. }
  229. else {
  230. if (this._rightLaserPointer) {
  231. this._rightLaserPointer.isVisible = true;
  232. }
  233. else if (this._leftLaserPointer) {
  234. this._leftLaserPointer.isVisible = true;
  235. }
  236. }
  237. }
  238. /**
  239. * The deviceOrientationCamera used as the camera when not in VR.
  240. */
  241. public get deviceOrientationCamera(): Nullable<DeviceOrientationCamera> {
  242. return this._deviceOrientationCamera;
  243. }
  244. /**
  245. * Based on the current WebVR support, returns the current VR camera used.
  246. */
  247. public get currentVRCamera(): Nullable<Camera> {
  248. if (this._webVRready) {
  249. return this._webVRCamera;
  250. }
  251. else {
  252. return this._scene.activeCamera;
  253. }
  254. }
  255. /**
  256. * The webVRCamera which is used when in VR.
  257. */
  258. public get webVRCamera(): WebVRFreeCamera {
  259. return this._webVRCamera;
  260. }
  261. /**
  262. * The deviceOrientationCamera that is used as a fallback when vr device is not connected.
  263. */
  264. public get vrDeviceOrientationCamera(): Nullable<VRDeviceOrientationFreeCamera> {
  265. return this._vrDeviceOrientationCamera;
  266. }
  267. /**
  268. * Instantiates a VRExperienceHelper.
  269. * Helps to quickly add VR support to an existing scene.
  270. * @param scene The scene the VRExperienceHelper belongs to.
  271. * @param webVROptions Options to modify the vr experience helper's behavior.
  272. */
  273. constructor(scene: Scene, /** Options to modify the vr experience helper's behavior. */public webVROptions: VRExperienceHelperOptions = {}) {
  274. this._scene = scene;
  275. this._canvas = scene.getEngine().getRenderingCanvas();
  276. // Parse options
  277. if (webVROptions.createFallbackVRDeviceOrientationFreeCamera === undefined) {
  278. webVROptions.createFallbackVRDeviceOrientationFreeCamera = true;
  279. }
  280. if (webVROptions.createDeviceOrientationCamera === undefined) {
  281. webVROptions.createDeviceOrientationCamera = true;
  282. }
  283. if (webVROptions.defaultHeight === undefined) {
  284. webVROptions.defaultHeight = 1.7;
  285. }
  286. if (webVROptions.useCustomVRButton) {
  287. this._useCustomVRButton = true;
  288. if (webVROptions.customVRButton) {
  289. this._btnVR = webVROptions.customVRButton;
  290. }
  291. }
  292. if (webVROptions.rayLength) {
  293. this._rayLength = webVROptions.rayLength
  294. }
  295. this._defaultHeight = webVROptions.defaultHeight;
  296. // Set position
  297. if (this._scene.activeCamera) {
  298. this._position = this._scene.activeCamera.position.clone();
  299. } else {
  300. this._position = new Vector3(0, this._defaultHeight, 0);
  301. }
  302. // Set non-vr camera
  303. if (webVROptions.createDeviceOrientationCamera || !this._scene.activeCamera) {
  304. this._deviceOrientationCamera = new DeviceOrientationCamera("deviceOrientationVRHelper", this._position.clone(), scene);
  305. // Copy data from existing camera
  306. if (this._scene.activeCamera) {
  307. this._deviceOrientationCamera.minZ = this._scene.activeCamera.minZ;
  308. this._deviceOrientationCamera.maxZ = this._scene.activeCamera.maxZ;
  309. // Set rotation from previous camera
  310. if (this._scene.activeCamera instanceof TargetCamera && this._scene.activeCamera.rotation) {
  311. var targetCamera = this._scene.activeCamera;
  312. if (targetCamera.rotationQuaternion) {
  313. this._deviceOrientationCamera.rotationQuaternion.copyFrom(targetCamera.rotationQuaternion);
  314. } else {
  315. this._deviceOrientationCamera.rotationQuaternion.copyFrom(Quaternion.RotationYawPitchRoll(targetCamera.rotation.y, targetCamera.rotation.x, targetCamera.rotation.z));
  316. }
  317. this._deviceOrientationCamera.rotation = targetCamera.rotation.clone();
  318. }
  319. }
  320. this._scene.activeCamera = this._deviceOrientationCamera;
  321. if (this._canvas) {
  322. this._scene.activeCamera.attachControl(this._canvas);
  323. }
  324. } else {
  325. this._existingCamera = this._scene.activeCamera;
  326. }
  327. // Create VR cameras
  328. if (webVROptions.createFallbackVRDeviceOrientationFreeCamera) {
  329. this._vrDeviceOrientationCamera = new VRDeviceOrientationFreeCamera("VRDeviceOrientationVRHelper", this._position, this._scene);
  330. }
  331. this._webVRCamera = new WebVRFreeCamera("WebVRHelper", this._position, this._scene, webVROptions);
  332. this._webVRCamera.useStandingMatrix()
  333. // Create default button
  334. if (!this._useCustomVRButton) {
  335. this._btnVR = <HTMLButtonElement>document.createElement("BUTTON");
  336. this._btnVR.className = "babylonVRicon";
  337. this._btnVR.id = "babylonVRiconbtn";
  338. this._btnVR.title = "Click to switch to VR";
  339. var css = ".babylonVRicon { position: absolute; right: 20px; height: 50px; width: 80px; background-color: rgba(51,51,51,0.7); background-image: url(data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%222048%22%20height%3D%221152%22%20viewBox%3D%220%200%202048%201152%22%20version%3D%221.1%22%3E%3Cpath%20transform%3D%22rotate%28180%201024%2C576.0000000000001%29%22%20d%3D%22m1109%2C896q17%2C0%2030%2C-12t13%2C-30t-12.5%2C-30.5t-30.5%2C-12.5l-170%2C0q-18%2C0%20-30.5%2C12.5t-12.5%2C30.5t13%2C30t30%2C12l170%2C0zm-85%2C256q59%2C0%20132.5%2C-1.5t154.5%2C-5.5t164.5%2C-11.5t163%2C-20t150%2C-30t124.5%2C-41.5q23%2C-11%2042%2C-24t38%2C-30q27%2C-25%2041%2C-61.5t14%2C-72.5l0%2C-257q0%2C-123%20-47%2C-232t-128%2C-190t-190%2C-128t-232%2C-47l-81%2C0q-37%2C0%20-68.5%2C14t-60.5%2C34.5t-55.5%2C45t-53%2C45t-53%2C34.5t-55.5%2C14t-55.5%2C-14t-53%2C-34.5t-53%2C-45t-55.5%2C-45t-60.5%2C-34.5t-68.5%2C-14l-81%2C0q-123%2C0%20-232%2C47t-190%2C128t-128%2C190t-47%2C232l0%2C257q0%2C68%2038%2C115t97%2C73q54%2C24%20124.5%2C41.5t150%2C30t163%2C20t164.5%2C11.5t154.5%2C5.5t132.5%2C1.5zm939%2C-298q0%2C39%20-24.5%2C67t-58.5%2C42q-54%2C23%20-122%2C39.5t-143.5%2C28t-155.5%2C19t-157%2C11t-148.5%2C5t-129.5%2C1.5q-59%2C0%20-130%2C-1.5t-148%2C-5t-157%2C-11t-155.5%2C-19t-143.5%2C-28t-122%2C-39.5q-34%2C-14%20-58.5%2C-42t-24.5%2C-67l0%2C-257q0%2C-106%2040.5%2C-199t110%2C-162.5t162.5%2C-109.5t199%2C-40l81%2C0q27%2C0%2052%2C14t50%2C34.5t51%2C44.5t55.5%2C44.5t63.5%2C34.5t74%2C14t74%2C-14t63.5%2C-34.5t55.5%2C-44.5t51%2C-44.5t50%2C-34.5t52%2C-14l14%2C0q37%2C0%2070%2C0.5t64.5%2C4.5t63.5%2C12t68%2C23q71%2C30%20128.5%2C78.5t98.5%2C110t63.5%2C133.5t22.5%2C149l0%2C257z%22%20fill%3D%22white%22%20/%3E%3C/svg%3E%0A); background-size: 80%; background-repeat:no-repeat; background-position: center; border: none; outline: none; transition: transform 0.125s ease-out } .babylonVRicon:hover { transform: scale(1.05) } .babylonVRicon:active {background-color: rgba(51,51,51,1) } .babylonVRicon:focus {background-color: rgba(51,51,51,1) }";
  340. css += ".babylonVRicon.vrdisplaypresenting { display: none; }";
  341. // TODO: Add user feedback so that they know what state the VRDisplay is in (disconnected, connected, entering-VR)
  342. // css += ".babylonVRicon.vrdisplaysupported { }";
  343. // css += ".babylonVRicon.vrdisplayready { }";
  344. // css += ".babylonVRicon.vrdisplayrequesting { }";
  345. var style = document.createElement('style');
  346. style.appendChild(document.createTextNode(css));
  347. document.getElementsByTagName('head')[0].appendChild(style);
  348. this.moveButtonToBottomRight();
  349. }
  350. // VR button click event
  351. if (this._btnVR) {
  352. this._btnVR.addEventListener("click", () => {
  353. if (!this.isInVRMode) {
  354. this.enterVR();
  355. } else {
  356. this.exitVR();
  357. }
  358. });
  359. }
  360. // Window events
  361. window.addEventListener("resize", this._onResize);
  362. document.addEventListener("fullscreenchange", this._onFullscreenChange, false);
  363. document.addEventListener("mozfullscreenchange", this._onFullscreenChange, false);
  364. document.addEventListener("webkitfullscreenchange", this._onFullscreenChange, false);
  365. document.addEventListener("msfullscreenchange", this._onFullscreenChange, false);
  366. // Display vr button when headset is connected
  367. if (webVROptions.createFallbackVRDeviceOrientationFreeCamera) {
  368. this.displayVRButton();
  369. } else {
  370. this._scene.getEngine().onVRDisplayChangedObservable.add((e) => {
  371. if (e.vrDisplay) {
  372. this.displayVRButton();
  373. }
  374. })
  375. }
  376. // Exiting VR mode using 'ESC' key on desktop
  377. this._onKeyDown = (event: KeyboardEvent) => {
  378. if (event.keyCode === 27 && this.isInVRMode) {
  379. this.exitVR();
  380. }
  381. };
  382. document.addEventListener("keydown", this._onKeyDown);
  383. // Exiting VR mode double tapping the touch screen
  384. this._scene.onPrePointerObservable.add((pointerInfo, eventState) => {
  385. if (this.isInVRMode) {
  386. this.exitVR();
  387. if (this._fullscreenVRpresenting) {
  388. this._scene.getEngine().switchFullscreen(true);
  389. }
  390. }
  391. }, PointerEventTypes.POINTERDOUBLETAP, false);
  392. // Listen for WebVR display changes
  393. this._onVRDisplayChanged = (eventArgs: IDisplayChangedEventArgs) => this.onVRDisplayChanged(eventArgs);
  394. this._onVrDisplayPresentChange = () => this.onVrDisplayPresentChange();
  395. this._onVRRequestPresentStart = () => {
  396. this._webVRrequesting = true;
  397. this.updateButtonVisibility();
  398. }
  399. this._onVRRequestPresentComplete = (success: boolean) => {
  400. this._webVRrequesting = false;
  401. this.updateButtonVisibility();
  402. };
  403. scene.getEngine().onVRDisplayChangedObservable.add(this._onVRDisplayChanged);
  404. scene.getEngine().onVRRequestPresentStart.add(this._onVRRequestPresentStart);
  405. scene.getEngine().onVRRequestPresentComplete.add(this._onVRRequestPresentComplete);
  406. window.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  407. scene.onDisposeObservable.add(() => {
  408. this.dispose();
  409. })
  410. // Gamepad connection events
  411. this._webVRCamera.onControllerMeshLoadedObservable.add((webVRController) => this._onDefaultMeshLoaded(webVRController));
  412. this._scene.gamepadManager.onGamepadConnectedObservable.add(this._onNewGamepadConnected);
  413. this._scene.gamepadManager.onGamepadDisconnectedObservable.add(this._onNewGamepadDisconnected);
  414. this.updateButtonVisibility();
  415. //create easing functions
  416. this._circleEase = new CircleEase();
  417. this._circleEase.setEasingMode(EasingFunction.EASINGMODE_EASEINOUT);
  418. }
  419. // Raised when one of the controller has loaded successfully its associated default mesh
  420. private _onDefaultMeshLoaded(webVRController: WebVRController) {
  421. this._tryEnableInteractionOnController(webVRController);
  422. try {
  423. this.onControllerMeshLoadedObservable.notifyObservers(webVRController);
  424. }
  425. catch (err) {
  426. Tools.Warn("Error in your custom logic onControllerMeshLoaded: " + err);
  427. }
  428. }
  429. private _onResize = () => {
  430. this.moveButtonToBottomRight();
  431. if (this._fullscreenVRpresenting && this._webVRready) {
  432. this.exitVR();
  433. }
  434. }
  435. private _onFullscreenChange = () => {
  436. if (document.fullscreen !== undefined) {
  437. this._fullscreenVRpresenting = document.fullscreen;
  438. } else if (document.mozFullScreen !== undefined) {
  439. this._fullscreenVRpresenting = document.mozFullScreen;
  440. } else if (document.webkitIsFullScreen !== undefined) {
  441. this._fullscreenVRpresenting = document.webkitIsFullScreen;
  442. } else if (document.msIsFullScreen !== undefined) {
  443. this._fullscreenVRpresenting = document.msIsFullScreen;
  444. }
  445. if (!this._fullscreenVRpresenting && this._canvas) {
  446. this.exitVR();
  447. if (!this._useCustomVRButton) {
  448. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  449. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  450. }
  451. }
  452. }
  453. /**
  454. * Gets a value indicating if we are currently in VR mode.
  455. */
  456. public get isInVRMode(): boolean {
  457. return this._webVRpresenting || this._fullscreenVRpresenting;
  458. }
  459. private onVrDisplayPresentChange() {
  460. var vrDisplay = this._scene.getEngine().getVRDevice();
  461. if (vrDisplay) {
  462. var wasPresenting = this._webVRpresenting;
  463. // A VR display is connected
  464. this._webVRpresenting = vrDisplay.isPresenting;
  465. if (wasPresenting && !this._webVRpresenting)
  466. this.exitVR();
  467. } else {
  468. Tools.Warn('Detected VRDisplayPresentChange on an unknown VRDisplay. Did you can enterVR on the vrExperienceHelper?');
  469. }
  470. this.updateButtonVisibility();
  471. }
  472. private onVRDisplayChanged(eventArgs: IDisplayChangedEventArgs) {
  473. this._webVRsupported = eventArgs.vrSupported;
  474. this._webVRready = !!eventArgs.vrDisplay;
  475. this._webVRpresenting = eventArgs.vrDisplay && eventArgs.vrDisplay.isPresenting;
  476. this.updateButtonVisibility();
  477. }
  478. private moveButtonToBottomRight() {
  479. if (this._canvas && !this._useCustomVRButton) {
  480. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  481. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  482. }
  483. }
  484. private displayVRButton() {
  485. if (!this._useCustomVRButton && !this._btnVRDisplayed) {
  486. document.body.appendChild(this._btnVR);
  487. this._btnVRDisplayed = true;
  488. }
  489. }
  490. private updateButtonVisibility() {
  491. if (!this._btnVR || this._useCustomVRButton) {
  492. return;
  493. }
  494. this._btnVR.className = "babylonVRicon";
  495. if (this.isInVRMode) {
  496. this._btnVR.className += " vrdisplaypresenting";
  497. } else {
  498. if (this._webVRready) this._btnVR.className += " vrdisplayready";
  499. if (this._webVRsupported) this._btnVR.className += " vrdisplaysupported";
  500. if (this._webVRrequesting) this._btnVR.className += " vrdisplayrequesting";
  501. }
  502. }
  503. /**
  504. * Attempt to enter VR. If a headset is connected and ready, will request present on that.
  505. * Otherwise, will use the fullscreen API.
  506. */
  507. public enterVR() {
  508. if (this.onEnteringVRObservable) {
  509. try {
  510. this.onEnteringVRObservable.notifyObservers(this);
  511. }
  512. catch (err) {
  513. Tools.Warn("Error in your custom logic onEnteringVR: " + err);
  514. }
  515. }
  516. if (this._scene.activeCamera) {
  517. this._position = this._scene.activeCamera.position.clone();
  518. // make sure that we return to the last active camera
  519. this._existingCamera = this._scene.activeCamera;
  520. }
  521. if (this._webVRrequesting)
  522. return;
  523. // If WebVR is supported and a headset is connected
  524. if (this._webVRready) {
  525. if (!this._webVRpresenting) {
  526. this._webVRCamera.position = this._position;
  527. this._scene.activeCamera = this._webVRCamera;
  528. }
  529. }
  530. else if (this._vrDeviceOrientationCamera) {
  531. this._vrDeviceOrientationCamera.position = this._position;
  532. this._scene.activeCamera = this._vrDeviceOrientationCamera;
  533. this._scene.getEngine().switchFullscreen(true);
  534. this.updateButtonVisibility();
  535. }
  536. if (this._scene.activeCamera && this._canvas) {
  537. this._scene.activeCamera.attachControl(this._canvas);
  538. }
  539. if (this._interactionsEnabled) {
  540. this._scene.registerBeforeRender(this.beforeRender);
  541. }
  542. }
  543. /**
  544. * Attempt to exit VR, or fullscreen.
  545. */
  546. public exitVR() {
  547. if (this.onExitingVRObservable) {
  548. try {
  549. this.onExitingVRObservable.notifyObservers(this);
  550. }
  551. catch (err) {
  552. Tools.Warn("Error in your custom logic onExitingVR: " + err);
  553. }
  554. }
  555. if (this._webVRpresenting) {
  556. this._scene.getEngine().disableVR();
  557. }
  558. if (this._scene.activeCamera) {
  559. this._position = this._scene.activeCamera.position.clone();
  560. }
  561. if (this._deviceOrientationCamera) {
  562. this._deviceOrientationCamera.position = this._position;
  563. this._scene.activeCamera = this._deviceOrientationCamera;
  564. if (this._canvas) {
  565. this._scene.activeCamera.attachControl(this._canvas);
  566. }
  567. } else if (this._existingCamera) {
  568. this._existingCamera.position = this._position;
  569. this._scene.activeCamera = this._existingCamera;
  570. }
  571. this.updateButtonVisibility();
  572. if (this._interactionsEnabled) {
  573. this._scene.unregisterBeforeRender(this.beforeRender);
  574. }
  575. }
  576. /**
  577. * The position of the vr experience helper.
  578. */
  579. public get position(): Vector3 {
  580. return this._position;
  581. }
  582. /**
  583. * Sets the position of the vr experience helper.
  584. */
  585. public set position(value: Vector3) {
  586. this._position = value;
  587. if (this._scene.activeCamera) {
  588. this._scene.activeCamera.position = value;
  589. }
  590. }
  591. /**
  592. * Enables controllers and user interactions suck as selecting and object or clicking on an object.
  593. */
  594. public enableInteractions() {
  595. if (!this._interactionsEnabled) {
  596. this._interactionsRequested = true;
  597. if (this._leftControllerReady && this._webVRCamera.leftController) {
  598. this._enableInteractionOnController(this._webVRCamera.leftController)
  599. }
  600. if (this._rightControllerReady && this._webVRCamera.rightController) {
  601. this._enableInteractionOnController(this._webVRCamera.rightController)
  602. }
  603. if(!this._gazeTracker){
  604. this._createGazeTracker();
  605. }
  606. this.raySelectionPredicate = (mesh) => {
  607. return mesh.isVisible;
  608. }
  609. this.meshSelectionPredicate = (mesh) => {
  610. return true;
  611. }
  612. this._raySelectionPredicate = (mesh) => {
  613. if (this._isTeleportationFloor(mesh) || (mesh != this._gazeTracker
  614. && mesh.name.indexOf("teleportationTarget") === -1
  615. && mesh.name.indexOf("torusTeleportation") === -1
  616. && mesh.name.indexOf("laserPointer") === -1)) {
  617. return this.raySelectionPredicate(mesh);
  618. }
  619. return false;
  620. }
  621. this._interactionsEnabled = true;
  622. }
  623. }
  624. private beforeRender = () => {
  625. this._castRayAndSelectObject();
  626. }
  627. private _isTeleportationFloor(mesh: AbstractMesh): boolean {
  628. for (var i = 0; i < this._floorMeshesCollection.length; i++) {
  629. if (this._floorMeshesCollection[i].id === mesh.id) {
  630. return true;
  631. }
  632. }
  633. if (this._floorMeshName && mesh.name === this._floorMeshName) {
  634. return true;
  635. }
  636. return false;
  637. }
  638. /**
  639. * Adds a floor mesh to be used for teleportation.
  640. * @param floorMesh the mesh to be used for teleportation.
  641. */
  642. public addFloorMesh(floorMesh: Mesh): void {
  643. if (!this._floorMeshesCollection) {
  644. return;
  645. }
  646. if (this._floorMeshesCollection.indexOf(floorMesh) > -1) {
  647. return;
  648. }
  649. this._floorMeshesCollection.push(floorMesh);
  650. }
  651. /**
  652. * Removes a floor mesh from being used for teleportation.
  653. * @param floorMesh the mesh to be removed.
  654. */
  655. public removeFloorMesh(floorMesh: Mesh): void {
  656. if (!this._floorMeshesCollection) {
  657. return
  658. }
  659. const meshIndex = this._floorMeshesCollection.indexOf(floorMesh);
  660. if (meshIndex !== -1) {
  661. this._floorMeshesCollection.splice(meshIndex, 1);
  662. }
  663. }
  664. /**
  665. * Enables interactions and teleportation using the VR controllers and gaze.
  666. * @param vrTeleportationOptions options to modify teleportation behavior.
  667. */
  668. public enableTeleportation(vrTeleportationOptions: VRTeleportationOptions = {}) {
  669. if (!this._teleportationInitialized) {
  670. this._teleportationRequested = true;
  671. this.enableInteractions();
  672. if (vrTeleportationOptions.floorMeshName) {
  673. this._floorMeshName = vrTeleportationOptions.floorMeshName;
  674. }
  675. if (vrTeleportationOptions.floorMeshes) {
  676. this._floorMeshesCollection = vrTeleportationOptions.floorMeshes;
  677. }
  678. if (this._leftControllerReady && this._webVRCamera.leftController) {
  679. this._enableTeleportationOnController(this._webVRCamera.leftController)
  680. }
  681. if (this._rightControllerReady && this._webVRCamera.rightController) {
  682. this._enableTeleportationOnController(this._webVRCamera.rightController)
  683. }
  684. // Creates an image processing post process for the vignette not relying
  685. // on the main scene configuration for image processing to reduce setup and spaces
  686. // (gamma/linear) conflicts.
  687. const imageProcessingConfiguration = new ImageProcessingConfiguration();
  688. imageProcessingConfiguration.vignetteColor = new Color4(0, 0, 0, 0);
  689. imageProcessingConfiguration.vignetteEnabled = true;
  690. this._postProcessMove = new ImageProcessingPostProcess("postProcessMove",
  691. 1.0,
  692. this._webVRCamera,
  693. undefined,
  694. undefined,
  695. undefined,
  696. undefined,
  697. imageProcessingConfiguration);
  698. this._webVRCamera.detachPostProcess(this._postProcessMove)
  699. this._passProcessMove = new PassPostProcess("pass", 1.0, this._webVRCamera);
  700. this._teleportationInitialized = true;
  701. if (this._isDefaultTeleportationTarget) {
  702. this._createTeleportationCircles();
  703. }
  704. }
  705. }
  706. private _onNewGamepadConnected = (gamepad: Gamepad) => {
  707. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  708. if (gamepad.leftStick) {
  709. gamepad.onleftstickchanged((stickValues) => {
  710. if (this._teleportationInitialized && this.teleportationEnabled) {
  711. // Listening to classic/xbox gamepad only if no VR controller is active
  712. if ((!this._leftLaserPointer && !this._rightLaserPointer) ||
  713. ((this._leftLaserPointer && !this._leftLaserPointer.isVisible) &&
  714. (this._rightLaserPointer && !this._rightLaserPointer.isVisible))) {
  715. this._checkTeleportWithRay(stickValues);
  716. this._checkTeleportBackwards(stickValues);
  717. }
  718. }
  719. });
  720. }
  721. if (gamepad.rightStick) {
  722. gamepad.onrightstickchanged((stickValues) => {
  723. if (this._teleportationInitialized) {
  724. this._checkRotate(stickValues);
  725. }
  726. });
  727. }
  728. if (gamepad.type === Gamepad.XBOX) {
  729. (<Xbox360Pad>gamepad).onbuttondown((buttonPressed: Xbox360Button) => {
  730. if (this._interactionsEnabled && buttonPressed === Xbox360Button.A) {
  731. this._selectionPointerDown();
  732. }
  733. });
  734. (<Xbox360Pad>gamepad).onbuttonup((buttonPressed: Xbox360Button) => {
  735. if (this._interactionsEnabled && buttonPressed === Xbox360Button.A) {
  736. this._selectionPointerUp();
  737. }
  738. });
  739. }
  740. } else {
  741. var webVRController = <WebVRController>gamepad;
  742. this._tryEnableInteractionOnController(webVRController);
  743. }
  744. }
  745. // This only succeeds if the controller's mesh exists for the controller so this must be called whenever new controller is connected or when mesh is loaded
  746. private _tryEnableInteractionOnController = (webVRController: WebVRController) => {
  747. if (webVRController.hand === "left") {
  748. this._leftControllerReady = true;
  749. if (this._interactionsRequested && !this._interactionsEnabledOnLeftController) {
  750. this._enableInteractionOnController(webVRController);
  751. }
  752. if (this._teleportationRequested && !this._teleportationEnabledOnLeftController) {
  753. this._enableTeleportationOnController(webVRController);
  754. }
  755. }
  756. if (webVRController.hand === "right") {
  757. this._rightControllerReady = true;
  758. if (this._interactionsRequested && !this._interactionsEnabledOnRightController) {
  759. this._enableInteractionOnController(webVRController);
  760. }
  761. if (this._teleportationRequested && !this._teleportationEnabledOnRightController) {
  762. this._enableTeleportationOnController(webVRController);
  763. }
  764. }
  765. }
  766. private _onNewGamepadDisconnected = (gamepad: Gamepad) => {
  767. if (gamepad instanceof WebVRController) {
  768. if (gamepad.hand === "left") {
  769. this._interactionsEnabledOnLeftController = false;
  770. this._teleportationEnabledOnLeftController = false;
  771. this._leftControllerReady = false;
  772. if (this._leftLaserPointer) {
  773. this._leftLaserPointer.dispose();
  774. }
  775. }
  776. if (gamepad.hand === "right") {
  777. this._interactionsEnabledOnRightController = false;
  778. this._teleportationEnabledOnRightController = false;
  779. this._rightControllerReady = false;
  780. if (this._rightLaserPointer) {
  781. this._rightLaserPointer.dispose();
  782. }
  783. }
  784. }
  785. }
  786. private _enableInteractionOnController(webVRController: WebVRController) {
  787. var controllerMesh = webVRController.mesh;
  788. if (controllerMesh) {
  789. var makeNotPick = (root: AbstractMesh) => {
  790. root.name += " laserPointer";
  791. root.getChildMeshes().forEach((c) => {
  792. makeNotPick(c);
  793. });
  794. }
  795. makeNotPick(controllerMesh);
  796. var childMeshes = controllerMesh.getChildMeshes();
  797. for (var i = 0; i < childMeshes.length; i++) {
  798. if (childMeshes[i].name && childMeshes[i].name.indexOf("POINTING_POSE") >= 0) {
  799. controllerMesh = childMeshes[i];
  800. break;
  801. }
  802. }
  803. var laserPointer = Mesh.CreateCylinder("laserPointer", 1, 0.004, 0.0002, 20, 1, this._scene, false);
  804. var laserPointerMaterial = new StandardMaterial("laserPointerMat", this._scene);
  805. laserPointerMaterial.emissiveColor = new Color3(0.7, 0.7, 0.7);
  806. laserPointerMaterial.alpha = 0.6;
  807. laserPointer.material = laserPointerMaterial;
  808. laserPointer.rotation.x = Math.PI / 2;
  809. laserPointer.parent = controllerMesh;
  810. laserPointer.position.z = -0.5;
  811. laserPointer.isVisible = false;
  812. if (webVRController.hand === "left") {
  813. this._leftLaserPointer = laserPointer;
  814. this._interactionsEnabledOnLeftController = true;
  815. if (!this._rightLaserPointer) {
  816. this._leftLaserPointer.isVisible = true;
  817. }
  818. }
  819. else {
  820. this._rightLaserPointer = laserPointer;
  821. this._interactionsEnabledOnRightController = true;
  822. if (!this._leftLaserPointer) {
  823. this._rightLaserPointer.isVisible = true;
  824. }
  825. }
  826. webVRController.onMainButtonStateChangedObservable.add((stateObject) => {
  827. // Enabling / disabling laserPointer
  828. if (this._displayLaserPointer && stateObject.value === 1) {
  829. laserPointer.isVisible = !laserPointer.isVisible;
  830. // Laser pointer can only be active on left or right, not both at the same time
  831. if (webVRController.hand === "left" && this._rightLaserPointer) {
  832. this._rightLaserPointer.isVisible = false;
  833. }
  834. else if (this._leftLaserPointer) {
  835. this._leftLaserPointer.isVisible = false;
  836. }
  837. }
  838. });
  839. webVRController.onTriggerStateChangedObservable.add((stateObject) => {
  840. if (!this._pointerDownOnMeshAsked) {
  841. if (stateObject.value > this._padSensibilityUp) {
  842. this._selectionPointerDown();
  843. }
  844. } else if (stateObject.value < this._padSensibilityDown) {
  845. this._selectionPointerUp();
  846. }
  847. });
  848. }
  849. }
  850. private _checkTeleportWithRay(stateObject: StickValues, webVRController: Nullable<WebVRController> = null) {
  851. if (!this._teleportationRequestInitiated) {
  852. if (stateObject.y < -this._padSensibilityUp && this._dpadPressed) {
  853. if (webVRController) {
  854. // If laser pointer wasn't enabled yet
  855. if (this._displayLaserPointer && webVRController.hand === "left" && this._leftLaserPointer) {
  856. this._leftLaserPointer.isVisible = true;
  857. if (this._rightLaserPointer) {
  858. this._rightLaserPointer.isVisible = false;
  859. }
  860. } else if (this._displayLaserPointer && this._rightLaserPointer) {
  861. this._rightLaserPointer.isVisible = true;
  862. if (this._leftLaserPointer) {
  863. this._leftLaserPointer.isVisible = false;
  864. }
  865. }
  866. }
  867. this._teleportationRequestInitiated = true;
  868. }
  869. } else {
  870. // Listening to the proper controller values changes to confirm teleportation
  871. if (webVRController == null
  872. || (webVRController.hand === "left" && this._leftLaserPointer && this._leftLaserPointer.isVisible)
  873. || (webVRController.hand === "right" && this._rightLaserPointer && this._rightLaserPointer.isVisible)) {
  874. if (Math.sqrt(stateObject.y * stateObject.y + stateObject.x * stateObject.x) < this._padSensibilityDown) {
  875. if (this._teleportationAllowed) {
  876. this._teleportationAllowed = false;
  877. this._teleportCamera();
  878. }
  879. this._teleportationRequestInitiated = false;
  880. }
  881. }
  882. }
  883. }
  884. private _selectionPointerDown() {
  885. this._pointerDownOnMeshAsked = true;
  886. if (this._currentMeshSelected && this._currentHit) {
  887. this._scene.simulatePointerDown(this._currentHit);
  888. }
  889. }
  890. private _selectionPointerUp() {
  891. if (this._currentMeshSelected && this._currentHit) {
  892. this._scene.simulatePointerUp(this._currentHit);
  893. }
  894. this._pointerDownOnMeshAsked = false;
  895. }
  896. private _checkRotate(stateObject: StickValues) {
  897. // Only rotate when user is not currently selecting a teleportation location
  898. if (this._teleportationRequestInitiated) {
  899. return;
  900. }
  901. if (!this._rotationLeftAsked) {
  902. if (stateObject.x < -this._padSensibilityUp && this._dpadPressed) {
  903. this._rotationLeftAsked = true;
  904. if (this._rotationAllowed) {
  905. this._rotateCamera(false);
  906. }
  907. }
  908. } else {
  909. if (stateObject.x > -this._padSensibilityDown) {
  910. this._rotationLeftAsked = false;
  911. }
  912. }
  913. if (!this._rotationRightAsked) {
  914. if (stateObject.x > this._padSensibilityUp && this._dpadPressed) {
  915. this._rotationRightAsked = true;
  916. if (this._rotationAllowed) {
  917. this._rotateCamera(true);
  918. }
  919. }
  920. } else {
  921. if (stateObject.x < this._padSensibilityDown) {
  922. this._rotationRightAsked = false;
  923. }
  924. }
  925. }
  926. private _checkTeleportBackwards(stateObject: StickValues) {
  927. // Only teleport backwards when user is not currently selecting a teleportation location
  928. if (this._teleportationRequestInitiated) {
  929. return;
  930. }
  931. // Teleport backwards
  932. if (stateObject.y > this._padSensibilityUp && this._dpadPressed) {
  933. if (!this._teleportationBackRequestInitiated) {
  934. if (!this.currentVRCamera) {
  935. return;
  936. }
  937. // Get rotation and position of the current camera
  938. var rotation = Quaternion.FromRotationMatrix(this.currentVRCamera.getWorldMatrix().getRotationMatrix());
  939. var position = this.currentVRCamera.position;
  940. // If the camera has device position, use that instead
  941. if ((<WebVRFreeCamera>this.currentVRCamera).devicePosition && (<WebVRFreeCamera>this.currentVRCamera).deviceRotationQuaternion) {
  942. rotation = (<WebVRFreeCamera>this.currentVRCamera).deviceRotationQuaternion;
  943. position = (<WebVRFreeCamera>this.currentVRCamera).devicePosition;
  944. }
  945. // Get matrix with only the y rotation of the device rotation
  946. rotation.toEulerAnglesToRef(this._workingVector);
  947. this._workingVector.z = 0;
  948. this._workingVector.x = 0;
  949. Quaternion.RotationYawPitchRollToRef(this._workingVector.y, this._workingVector.x, this._workingVector.z, this._workingQuaternion);
  950. this._workingQuaternion.toRotationMatrix(this._workingMatrix);
  951. // Rotate backwards ray by device rotation to cast at the ground behind the user
  952. Vector3.TransformCoordinatesToRef(this.teleportBackwardsVector, this._workingMatrix, this._workingVector);
  953. // Teleport if ray hit the ground and is not to far away eg. backwards off a cliff
  954. var ray = new Ray(position, this._workingVector);
  955. var hit = this._scene.pickWithRay(ray, this._raySelectionPredicate);
  956. if (hit && hit.pickedPoint && hit.pickedMesh && this._isTeleportationFloor(hit.pickedMesh) && hit.distance < 5) {
  957. this._teleportCamera(hit.pickedPoint);
  958. }
  959. this._teleportationBackRequestInitiated = true;
  960. }
  961. } else {
  962. this._teleportationBackRequestInitiated = false;
  963. }
  964. }
  965. private _enableTeleportationOnController(webVRController: WebVRController) {
  966. var controllerMesh = webVRController.mesh;
  967. if (controllerMesh) {
  968. if (webVRController.hand === "left") {
  969. if (!this._interactionsEnabledOnLeftController) {
  970. this._enableInteractionOnController(webVRController);
  971. }
  972. this._teleportationEnabledOnLeftController = true;
  973. }
  974. else {
  975. if (!this._interactionsEnabledOnRightController) {
  976. this._enableInteractionOnController(webVRController);
  977. }
  978. this._teleportationEnabledOnRightController = true;
  979. }
  980. if (webVRController.controllerType === PoseEnabledControllerType.VIVE) {
  981. this._dpadPressed = false;
  982. webVRController.onPadStateChangedObservable.add((stateObject) => {
  983. this._dpadPressed = stateObject.pressed;
  984. if (!this._dpadPressed) {
  985. this._rotationLeftAsked = false;
  986. this._rotationRightAsked = false;
  987. this._teleportationBackRequestInitiated = false;
  988. }
  989. });
  990. }
  991. webVRController.onPadValuesChangedObservable.add((stateObject) => {
  992. if (this.teleportationEnabled) {
  993. this._checkTeleportBackwards(stateObject);
  994. this._checkTeleportWithRay(stateObject, webVRController);
  995. }
  996. this._checkRotate(stateObject);
  997. });
  998. }
  999. }
  1000. // Gaze support used to point to teleport or to interact with an object
  1001. private _createGazeTracker() {
  1002. this._gazeTracker = Mesh.CreateTorus("gazeTracker", 0.0035, 0.0025, 20, this._scene, false);
  1003. this._gazeTracker.bakeCurrentTransformIntoVertices();
  1004. this._gazeTracker.isPickable = false;
  1005. this._gazeTracker.isVisible = false;
  1006. var targetMat = new StandardMaterial("targetMat", this._scene);
  1007. targetMat.specularColor = Color3.Black();
  1008. targetMat.emissiveColor = new Color3(0.7, 0.7, 0.7)
  1009. targetMat.backFaceCulling = false;
  1010. this._gazeTracker.material = targetMat;
  1011. }
  1012. private _createTeleportationCircles() {
  1013. this._teleportationTarget = Mesh.CreateGround("teleportationTarget", 2, 2, 2, this._scene);
  1014. this._teleportationTarget.isPickable = false;
  1015. var length = 512;
  1016. var dynamicTexture = new DynamicTexture("DynamicTexture", length, this._scene, true);
  1017. dynamicTexture.hasAlpha = true;
  1018. var context = dynamicTexture.getContext();
  1019. var centerX = length / 2;
  1020. var centerY = length / 2;
  1021. var radius = 200;
  1022. context.beginPath();
  1023. context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  1024. context.fillStyle = this._teleportationFillColor;
  1025. context.fill();
  1026. context.lineWidth = 10;
  1027. context.strokeStyle = this._teleportationBorderColor;
  1028. context.stroke();
  1029. context.closePath();
  1030. dynamicTexture.update();
  1031. var teleportationCircleMaterial = new StandardMaterial("TextPlaneMaterial", this._scene);
  1032. teleportationCircleMaterial.diffuseTexture = dynamicTexture;
  1033. this._teleportationTarget.material = teleportationCircleMaterial;
  1034. var torus = Mesh.CreateTorus("torusTeleportation", 0.75, 0.1, 25, this._scene, false);
  1035. torus.isPickable = false;
  1036. torus.parent = this._teleportationTarget;
  1037. var animationInnerCircle = new Animation("animationInnerCircle", "position.y", 30, Animation.ANIMATIONTYPE_FLOAT, Animation.ANIMATIONLOOPMODE_CYCLE);
  1038. var keys = [];
  1039. keys.push({
  1040. frame: 0,
  1041. value: 0
  1042. });
  1043. keys.push({
  1044. frame: 30,
  1045. value: 0.4
  1046. });
  1047. keys.push({
  1048. frame: 60,
  1049. value: 0
  1050. });
  1051. animationInnerCircle.setKeys(keys);
  1052. var easingFunction = new SineEase();
  1053. easingFunction.setEasingMode(EasingFunction.EASINGMODE_EASEINOUT);
  1054. animationInnerCircle.setEasingFunction(easingFunction);
  1055. torus.animations = [];
  1056. torus.animations.push(animationInnerCircle);
  1057. this._scene.beginAnimation(torus, 0, 60, true);
  1058. this._hideTeleportationTarget();
  1059. }
  1060. private _displayTeleportationTarget() {
  1061. if (this._teleportationInitialized) {
  1062. this._teleportationTarget.isVisible = true;
  1063. if (this._isDefaultTeleportationTarget) {
  1064. (<Mesh>this._teleportationTarget.getChildren()[0]).isVisible = true;
  1065. }
  1066. }
  1067. }
  1068. private _hideTeleportationTarget() {
  1069. if (this._teleportationInitialized) {
  1070. this._teleportationTarget.isVisible = false;
  1071. if (this._isDefaultTeleportationTarget) {
  1072. (<Mesh>this._teleportationTarget.getChildren()[0]).isVisible = false;
  1073. }
  1074. }
  1075. }
  1076. private _rotateCamera(right: boolean) {
  1077. if (!(this.currentVRCamera instanceof FreeCamera)) {
  1078. return;
  1079. }
  1080. if (right) {
  1081. this._rotationAngle++;
  1082. }
  1083. else {
  1084. this._rotationAngle--;
  1085. }
  1086. this.currentVRCamera.animations = [];
  1087. var target = Quaternion.FromRotationMatrix(Matrix.RotationY(Math.PI / 4 * this._rotationAngle));
  1088. var animationRotation = new Animation("animationRotation", "rotationQuaternion", 90, Animation.ANIMATIONTYPE_QUATERNION,
  1089. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1090. var animationRotationKeys = [];
  1091. animationRotationKeys.push({
  1092. frame: 0,
  1093. value: this.currentVRCamera.rotationQuaternion
  1094. });
  1095. animationRotationKeys.push({
  1096. frame: 6,
  1097. value: target
  1098. });
  1099. animationRotation.setKeys(animationRotationKeys);
  1100. animationRotation.setEasingFunction(this._circleEase);
  1101. this.currentVRCamera.animations.push(animationRotation);
  1102. this._postProcessMove.animations = [];
  1103. var animationPP = new Animation("animationPP", "vignetteWeight", 90, Animation.ANIMATIONTYPE_FLOAT,
  1104. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1105. var vignetteWeightKeys = [];
  1106. vignetteWeightKeys.push({
  1107. frame: 0,
  1108. value: 0
  1109. });
  1110. vignetteWeightKeys.push({
  1111. frame: 3,
  1112. value: 4
  1113. });
  1114. vignetteWeightKeys.push({
  1115. frame: 6,
  1116. value: 0
  1117. });
  1118. animationPP.setKeys(vignetteWeightKeys);
  1119. animationPP.setEasingFunction(this._circleEase);
  1120. this._postProcessMove.animations.push(animationPP);
  1121. var animationPP2 = new Animation("animationPP2", "vignetteStretch", 90, Animation.ANIMATIONTYPE_FLOAT,
  1122. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1123. var vignetteStretchKeys = [];
  1124. vignetteStretchKeys.push({
  1125. frame: 0,
  1126. value: 0
  1127. });
  1128. vignetteStretchKeys.push({
  1129. frame: 3,
  1130. value: 10
  1131. });
  1132. vignetteStretchKeys.push({
  1133. frame: 6,
  1134. value: 0
  1135. });
  1136. animationPP2.setKeys(vignetteStretchKeys);
  1137. animationPP2.setEasingFunction(this._circleEase);
  1138. this._postProcessMove.animations.push(animationPP2);
  1139. this._postProcessMove.imageProcessingConfiguration.vignetteWeight = 0;
  1140. this._postProcessMove.imageProcessingConfiguration.vignetteStretch = 0;
  1141. this._webVRCamera.attachPostProcess(this._postProcessMove)
  1142. this._scene.beginAnimation(this._postProcessMove, 0, 6, false, 1, () => {
  1143. this._webVRCamera.detachPostProcess(this._postProcessMove)
  1144. });
  1145. this._scene.beginAnimation(this.currentVRCamera, 0, 6, false, 1);
  1146. }
  1147. private _moveTeleportationSelectorTo(hit: PickingInfo) {
  1148. if (hit.pickedPoint) {
  1149. this._teleportationAllowed = true;
  1150. if (this._teleportationRequestInitiated) {
  1151. this._displayTeleportationTarget();
  1152. }
  1153. else {
  1154. this._hideTeleportationTarget();
  1155. }
  1156. this._haloCenter.copyFrom(hit.pickedPoint);
  1157. this._teleportationTarget.position.copyFrom(hit.pickedPoint);
  1158. var pickNormal = hit.getNormal(true, false);
  1159. if (pickNormal) {
  1160. var axis1 = Vector3.Cross(Axis.Y, pickNormal);
  1161. var axis2 = Vector3.Cross(pickNormal, axis1);
  1162. Vector3.RotationFromAxisToRef(axis2, pickNormal, axis1, this._teleportationTarget.rotation);
  1163. }
  1164. this._teleportationTarget.position.y += 0.1;
  1165. }
  1166. }
  1167. private _workingVector = Vector3.Zero();
  1168. private _workingQuaternion = Quaternion.Identity();
  1169. private _workingMatrix = Matrix.Identity();
  1170. private _teleportCamera(location: Nullable<Vector3> = null) {
  1171. if (!(this.currentVRCamera instanceof FreeCamera)) {
  1172. return;
  1173. }
  1174. if (!location) {
  1175. location = this._haloCenter;
  1176. }
  1177. // Teleport the hmd to where the user is looking by moving the anchor to where they are looking minus the
  1178. // offset of the headset from the anchor.
  1179. if (this.webVRCamera.leftCamera) {
  1180. this._workingVector.copyFrom(this.webVRCamera.leftCamera.globalPosition);
  1181. this._workingVector.subtractInPlace(this.webVRCamera.position);
  1182. location.subtractToRef(this._workingVector, this._workingVector);
  1183. } else {
  1184. this._workingVector.copyFrom(location);
  1185. }
  1186. // Add height to account for user's height offset
  1187. if (this.isInVRMode) {
  1188. this._workingVector.y += this.webVRCamera.deviceDistanceToRoomGround();
  1189. } else {
  1190. this._workingVector.y += this._defaultHeight;
  1191. }
  1192. this.onBeforeCameraTeleport.notifyObservers(this._workingVector);
  1193. // Create animation from the camera's position to the new location
  1194. this.currentVRCamera.animations = [];
  1195. var animationCameraTeleportation = new Animation("animationCameraTeleportation", "position", 90, Animation.ANIMATIONTYPE_VECTOR3, Animation.ANIMATIONLOOPMODE_CONSTANT);
  1196. var animationCameraTeleportationKeys = [{
  1197. frame: 0,
  1198. value: this.currentVRCamera.position
  1199. },
  1200. {
  1201. frame: 11,
  1202. value: this._workingVector
  1203. }
  1204. ];
  1205. animationCameraTeleportation.setKeys(animationCameraTeleportationKeys);
  1206. animationCameraTeleportation.setEasingFunction(this._circleEase);
  1207. this.currentVRCamera.animations.push(animationCameraTeleportation);
  1208. this._postProcessMove.animations = [];
  1209. var animationPP = new Animation("animationPP", "vignetteWeight", 90, Animation.ANIMATIONTYPE_FLOAT,
  1210. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1211. var vignetteWeightKeys = [];
  1212. vignetteWeightKeys.push({
  1213. frame: 0,
  1214. value: 0
  1215. });
  1216. vignetteWeightKeys.push({
  1217. frame: 5,
  1218. value: 8
  1219. });
  1220. vignetteWeightKeys.push({
  1221. frame: 11,
  1222. value: 0
  1223. });
  1224. animationPP.setKeys(vignetteWeightKeys);
  1225. this._postProcessMove.animations.push(animationPP);
  1226. var animationPP2 = new Animation("animationPP2", "vignetteStretch", 90, Animation.ANIMATIONTYPE_FLOAT,
  1227. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1228. var vignetteStretchKeys = [];
  1229. vignetteStretchKeys.push({
  1230. frame: 0,
  1231. value: 0
  1232. });
  1233. vignetteStretchKeys.push({
  1234. frame: 5,
  1235. value: 10
  1236. });
  1237. vignetteStretchKeys.push({
  1238. frame: 11,
  1239. value: 0
  1240. });
  1241. animationPP2.setKeys(vignetteStretchKeys);
  1242. this._postProcessMove.animations.push(animationPP2);
  1243. this._postProcessMove.imageProcessingConfiguration.vignetteWeight = 0;
  1244. this._postProcessMove.imageProcessingConfiguration.vignetteStretch = 0;
  1245. this._webVRCamera.attachPostProcess(this._postProcessMove)
  1246. this._scene.beginAnimation(this._postProcessMove, 0, 11, false, 1, () => {
  1247. this._webVRCamera.detachPostProcess(this._postProcessMove)
  1248. });
  1249. this._scene.beginAnimation(this.currentVRCamera, 0, 11, false, 1, () => {
  1250. this.onAfterCameraTeleport.notifyObservers(this._workingVector);
  1251. });
  1252. }
  1253. private _castRayAndSelectObject() {
  1254. if (!(this.currentVRCamera instanceof FreeCamera)) {
  1255. return;
  1256. }
  1257. var ray: Ray;
  1258. if (this._leftLaserPointer && this._leftLaserPointer.isVisible && (<any>this.currentVRCamera).leftController) {
  1259. ray = (<any>this.currentVRCamera).leftController.getForwardRay(this._rayLength);
  1260. }
  1261. else if (this._rightLaserPointer && this._rightLaserPointer.isVisible && (<any>this.currentVRCamera).rightController) {
  1262. ray = (<any>this.currentVRCamera).rightController.getForwardRay(this._rayLength);
  1263. } else {
  1264. ray = this.currentVRCamera.getForwardRay(this._rayLength);
  1265. }
  1266. var hit = this._scene.pickWithRay(ray, this._raySelectionPredicate);
  1267. // Moving the gazeTracker on the mesh face targetted
  1268. if (hit && hit.pickedPoint) {
  1269. if (this._displayGaze) {
  1270. let multiplier = 1;
  1271. this._gazeTracker.isVisible = true;
  1272. if (this._isActionableMesh) {
  1273. multiplier = 3;
  1274. }
  1275. this._gazeTracker.scaling.x = hit.distance * multiplier;
  1276. this._gazeTracker.scaling.y = hit.distance * multiplier;
  1277. this._gazeTracker.scaling.z = hit.distance * multiplier;
  1278. var pickNormal = hit.getNormal();
  1279. // To avoid z-fighting
  1280. let deltaFighting = 0.002;
  1281. if (pickNormal) {
  1282. var axis1 = Vector3.Cross(Axis.Y, pickNormal);
  1283. var axis2 = Vector3.Cross(pickNormal, axis1);
  1284. Vector3.RotationFromAxisToRef(axis2, pickNormal, axis1, this._gazeTracker.rotation);
  1285. }
  1286. this._gazeTracker.position.copyFrom(hit.pickedPoint);
  1287. if (this._gazeTracker.position.x < 0) {
  1288. this._gazeTracker.position.x += deltaFighting;
  1289. }
  1290. else {
  1291. this._gazeTracker.position.x -= deltaFighting;
  1292. }
  1293. if (this._gazeTracker.position.y < 0) {
  1294. this._gazeTracker.position.y += deltaFighting;
  1295. }
  1296. else {
  1297. this._gazeTracker.position.y -= deltaFighting;
  1298. }
  1299. if (this._gazeTracker.position.z < 0) {
  1300. this._gazeTracker.position.z += deltaFighting;
  1301. }
  1302. else {
  1303. this._gazeTracker.position.z -= deltaFighting;
  1304. }
  1305. }
  1306. // Changing the size of the laser pointer based on the distance from the targetted point
  1307. if (this._rightLaserPointer && this._rightLaserPointer.isVisible) {
  1308. this._rightLaserPointer.scaling.y = hit.distance;
  1309. this._rightLaserPointer.position.z = -hit.distance / 2;
  1310. }
  1311. if (this._leftLaserPointer && this._leftLaserPointer.isVisible) {
  1312. this._leftLaserPointer.scaling.y = hit.distance;
  1313. this._leftLaserPointer.position.z = -hit.distance / 2;
  1314. }
  1315. }
  1316. else {
  1317. this._gazeTracker.isVisible = false;
  1318. }
  1319. if (hit && hit.pickedMesh) {
  1320. this._currentHit = hit;
  1321. if (this._pointerDownOnMeshAsked) {
  1322. this._scene.simulatePointerMove(this._currentHit);
  1323. }
  1324. // The object selected is the floor, we're in a teleportation scenario
  1325. if (this._teleportationInitialized && this._isTeleportationFloor(hit.pickedMesh) && hit.pickedPoint) {
  1326. // Moving the teleportation area to this targetted point
  1327. //Raise onSelectedMeshUnselected observable if ray collided floor mesh/meshes and a non floor mesh was previously selected
  1328. if (this._currentMeshSelected && !this._isTeleportationFloor(this._currentMeshSelected)) {
  1329. this._notifySelectedMeshUnselected();
  1330. }
  1331. this._currentMeshSelected = null;
  1332. this._moveTeleportationSelectorTo(hit);
  1333. return;
  1334. }
  1335. // If not, we're in a selection scenario
  1336. this._hideTeleportationTarget();
  1337. this._teleportationAllowed = false;
  1338. if (hit.pickedMesh !== this._currentMeshSelected) {
  1339. if (this.meshSelectionPredicate(hit.pickedMesh)) {
  1340. this.onNewMeshPicked.notifyObservers(hit);
  1341. this._currentMeshSelected = hit.pickedMesh;
  1342. if (hit.pickedMesh.isPickable && hit.pickedMesh.actionManager) {
  1343. this.changeGazeColor(new Color3(0, 0, 1));
  1344. this.changeLaserColor(new Color3(0.2, 0.2, 1));
  1345. this._isActionableMesh = true;
  1346. }
  1347. else {
  1348. this.changeGazeColor(new Color3(0.7, 0.7, 0.7));
  1349. this.changeLaserColor(new Color3(0.7, 0.7, 0.7));
  1350. this._isActionableMesh = false;
  1351. }
  1352. try {
  1353. this.onNewMeshSelected.notifyObservers(this._currentMeshSelected);
  1354. }
  1355. catch (err) {
  1356. Tools.Warn("Error in your custom logic onNewMeshSelected: " + err);
  1357. }
  1358. }
  1359. else {
  1360. this._notifySelectedMeshUnselected();
  1361. this._currentMeshSelected = null;
  1362. this.changeGazeColor(new Color3(0.7, 0.7, 0.7));
  1363. this.changeLaserColor(new Color3(0.7, 0.7, 0.7));
  1364. }
  1365. }
  1366. }
  1367. else {
  1368. this._currentHit = null;
  1369. this._notifySelectedMeshUnselected();
  1370. this._currentMeshSelected = null;
  1371. this._teleportationAllowed = false;
  1372. this._hideTeleportationTarget();
  1373. this.changeGazeColor(new Color3(0.7, 0.7, 0.7));
  1374. this.changeLaserColor(new Color3(0.7, 0.7, 0.7));
  1375. }
  1376. }
  1377. private _notifySelectedMeshUnselected( ) {
  1378. if(this._currentMeshSelected) {
  1379. this.onSelectedMeshUnselected.notifyObservers(this._currentMeshSelected);
  1380. }
  1381. }
  1382. /**
  1383. * Sets the color of the laser ray from the vr controllers.
  1384. * @param color new color for the ray.
  1385. */
  1386. public changeLaserColor(color: Color3) {
  1387. if (this._leftLaserPointer && this._leftLaserPointer.material) {
  1388. (<StandardMaterial>this._leftLaserPointer.material).emissiveColor = color;
  1389. }
  1390. if (this._rightLaserPointer && this._rightLaserPointer.material) {
  1391. (<StandardMaterial>this._rightLaserPointer.material).emissiveColor = color;
  1392. }
  1393. }
  1394. /**
  1395. * Sets the color of the ray from the vr headsets gaze.
  1396. * @param color new color for the ray.
  1397. */
  1398. public changeGazeColor(color: Color3) {
  1399. if (this._gazeTracker.material) {
  1400. (<StandardMaterial>this._gazeTracker.material).emissiveColor = color;
  1401. }
  1402. }
  1403. /**
  1404. * Exits VR and disposes of the vr experience helper
  1405. */
  1406. public dispose() {
  1407. if (this.isInVRMode) {
  1408. this.exitVR();
  1409. }
  1410. if (this._passProcessMove) {
  1411. this._passProcessMove.dispose();
  1412. }
  1413. if (this._postProcessMove) {
  1414. this._postProcessMove.dispose();
  1415. }
  1416. if (this._webVRCamera) {
  1417. this._webVRCamera.dispose();
  1418. }
  1419. if (this._vrDeviceOrientationCamera) {
  1420. this._vrDeviceOrientationCamera.dispose();
  1421. }
  1422. if (!this._useCustomVRButton && this._btnVR.parentNode) {
  1423. document.body.removeChild(this._btnVR);
  1424. }
  1425. if (this._deviceOrientationCamera && (this._scene.activeCamera != this._deviceOrientationCamera)) {
  1426. this._deviceOrientationCamera.dispose();
  1427. }
  1428. if (this._gazeTracker) {
  1429. this._gazeTracker.dispose();
  1430. }
  1431. if (this._teleportationTarget) {
  1432. this._teleportationTarget.dispose();
  1433. }
  1434. this._floorMeshesCollection = [];
  1435. document.removeEventListener("keydown", this._onKeyDown);
  1436. window.removeEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  1437. window.removeEventListener("resize", this._onResize);
  1438. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  1439. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  1440. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  1441. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  1442. this._scene.getEngine().onVRDisplayChangedObservable.removeCallback(this._onVRDisplayChanged);
  1443. this._scene.getEngine().onVRRequestPresentStart.removeCallback(this._onVRRequestPresentStart);
  1444. this._scene.getEngine().onVRRequestPresentComplete.removeCallback(this._onVRRequestPresentComplete);
  1445. window.removeEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  1446. this._scene.gamepadManager.onGamepadConnectedObservable.removeCallback(this._onNewGamepadConnected);
  1447. this._scene.gamepadManager.onGamepadDisconnectedObservable.removeCallback(this._onNewGamepadDisconnected);
  1448. this._scene.unregisterBeforeRender(this.beforeRender);
  1449. }
  1450. /**
  1451. * Gets the name of the VRExperienceHelper class
  1452. * @returns "VRExperienceHelper"
  1453. */
  1454. public getClassName(): string {
  1455. return "VRExperienceHelper";
  1456. }
  1457. }
  1458. }