babylon.vrExperienceHelper.ts 72 KB

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