babylon.vrExperienceHelper.ts 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. module BABYLON {
  2. export interface VRTeleportationOptions {
  3. floorMeshName?: string; // If you'd like to provide a mesh acting as the floor
  4. floorMeshes?: Mesh[];
  5. }
  6. export interface VRExperienceHelperOptions extends WebVROptions {
  7. createDeviceOrientationCamera?: boolean; // Create a DeviceOrientationCamera to be used as your out of vr camera.
  8. createFallbackVRDeviceOrientationFreeCamera?: boolean; // Create a VRDeviceOrientationFreeCamera to be used for VR when no external HMD is found
  9. }
  10. export class VRExperienceHelper {
  11. private _scene: BABYLON.Scene;
  12. private _position: Vector3;
  13. private _btnVR: HTMLButtonElement;
  14. private _btnVRDisplayed: boolean;
  15. // Can the system support WebVR, even if a headset isn't plugged in?
  16. private _webVRsupported = false;
  17. // If WebVR is supported, is a headset plugged in and are we ready to present?
  18. private _webVRready = false;
  19. // Are we waiting for the requestPresent callback to complete?
  20. private _webVRrequesting = false;
  21. // Are we presenting to the headset right now?
  22. private _webVRpresenting = false;
  23. // Are we presenting in the fullscreen fallback?
  24. private _fullscreenVRpresenting = false;
  25. private _canvas: Nullable<HTMLCanvasElement>;
  26. private _webVRCamera: WebVRFreeCamera;
  27. private _vrDeviceOrientationCamera: Nullable<VRDeviceOrientationFreeCamera>;
  28. private _deviceOrientationCamera: Nullable<DeviceOrientationCamera>;
  29. private _existingCamera: Camera;
  30. private _onKeyDown: (event: KeyboardEvent) => void;
  31. private _onVrDisplayPresentChange: any;
  32. private _onVRDisplayChanged: (eventArgs: IDisplayChangedEventArgs) => void;
  33. private _onVRRequestPresentStart: () => void;
  34. private _onVRRequestPresentComplete: (success: boolean) => void;
  35. public onEnteringVR = new Observable<VRExperienceHelper>();
  36. public onExitingVR = new Observable<VRExperienceHelper>();
  37. public onControllerMeshLoaded = new Observable<WebVRController>();
  38. private _rayLength: number;
  39. private _useCustomVRButton: boolean = false;
  40. private _teleportationRequested: boolean = false;
  41. private _teleportationEnabledOnLeftController: boolean = false;
  42. private _teleportationEnabledOnRightController: boolean = false;
  43. private _interactionsEnabledOnLeftController: boolean = false;
  44. private _interactionsEnabledOnRightController: boolean = false;
  45. private _leftControllerReady: boolean = false;
  46. private _rightControllerReady: boolean = false;
  47. private _floorMeshName: string;
  48. private _floorMeshesCollection: Mesh[] = [];
  49. private _teleportationAllowed: boolean = false;
  50. private _rotationAllowed: boolean = true;
  51. private _teleportationRequestInitiated = false;
  52. private _rotationRightAsked = false;
  53. private _rotationLeftAsked = false;
  54. private _teleportationCircle: Mesh;
  55. private _postProcessMove: ImageProcessingPostProcess;
  56. private _passProcessMove: PassPostProcess;
  57. private _teleportationFillColor: string = "#444444";
  58. private _teleportationBorderColor: string = "#FFFFFF";
  59. private _rotationAngle: number = 0;
  60. private _haloCenter = new Vector3(0, 0, 0);
  61. private _gazeTracker: BABYLON.Mesh;
  62. private _padSensibilityUp = 0.65;
  63. private _padSensibilityDown = 0.35;
  64. private _leftLaserPointer: Nullable<Mesh>;
  65. private _rightLaserPointer: Nullable<Mesh>;
  66. private _currentMeshSelected: Nullable<AbstractMesh>;
  67. public onNewMeshSelected = new Observable<AbstractMesh>();
  68. private _circleEase: CircleEase;
  69. private _raySelectionPredicate: (mesh: AbstractMesh) => boolean;
  70. /**
  71. * To be optionaly changed by user to define custom ray selection
  72. */
  73. public raySelectionPredicate: (mesh: AbstractMesh) => boolean;
  74. /**
  75. * To be optionaly changed by user to define custom selection logic (after ray selection)
  76. */
  77. public meshSelectionPredicate: (mesh: AbstractMesh) => boolean;
  78. private _currentHit: Nullable<PickingInfo>;
  79. private _pointerDownOnMeshAsked = false;
  80. private _isActionableMesh = false;
  81. private _defaultHeight: number;
  82. private _teleportationEnabled = false;
  83. private _interactionsEnabled = false;
  84. private _interactionsRequested = false;
  85. private _displayGaze = true;
  86. private _displayLaserPointer = true;
  87. public get displayGaze(): boolean {
  88. return this._displayGaze;
  89. }
  90. public set displayGaze(value: boolean) {
  91. this._displayGaze = value;
  92. if (!value) {
  93. this._gazeTracker.isVisible = false;
  94. }
  95. }
  96. public get displayLaserPointer(): boolean {
  97. return this._displayLaserPointer;
  98. }
  99. public set displayLaserPointer(value: boolean) {
  100. this._displayLaserPointer = value;
  101. if (!value) {
  102. if (this._rightLaserPointer) {
  103. this._rightLaserPointer.isVisible = false;
  104. }
  105. if (this._leftLaserPointer) {
  106. this._leftLaserPointer.isVisible = false;
  107. }
  108. }
  109. }
  110. public get deviceOrientationCamera(): Nullable<DeviceOrientationCamera> {
  111. return this._deviceOrientationCamera;
  112. }
  113. // Based on the current WebVR support, returns the current VR camera used
  114. public get currentVRCamera(): Nullable<Camera> {
  115. if (this._webVRready) {
  116. return this._webVRCamera;
  117. }
  118. else {
  119. return this._scene.activeCamera;
  120. }
  121. }
  122. public get webVRCamera(): WebVRFreeCamera {
  123. return this._webVRCamera;
  124. }
  125. public get vrDeviceOrientationCamera(): Nullable<VRDeviceOrientationFreeCamera> {
  126. return this._vrDeviceOrientationCamera;
  127. }
  128. constructor(scene: Scene, public webVROptions: VRExperienceHelperOptions = {}) {
  129. this._scene = scene;
  130. this._canvas = scene.getEngine().getRenderingCanvas();
  131. this._defaultHeight = webVROptions.defaultHeight || 1.7;
  132. if (webVROptions.createFallbackVRDeviceOrientationFreeCamera === undefined) {
  133. webVROptions.createFallbackVRDeviceOrientationFreeCamera = true;
  134. }
  135. if (webVROptions.createDeviceOrientationCamera === undefined) {
  136. webVROptions.createDeviceOrientationCamera = true;
  137. }
  138. if (!this._scene.activeCamera || webVROptions.createDeviceOrientationCamera) {
  139. if (!this._scene.activeCamera || isNaN(this._scene.activeCamera.position.x)) {
  140. this._position = new BABYLON.Vector3(0, this._defaultHeight, 0);
  141. this._deviceOrientationCamera = new BABYLON.DeviceOrientationCamera("deviceOrientationVRHelper", this._position.clone(), scene);
  142. }
  143. else {
  144. this._position = this._scene.activeCamera.position.clone();
  145. this._deviceOrientationCamera = new BABYLON.DeviceOrientationCamera("deviceOrientationVRHelper", this._position.clone(), scene);
  146. this._deviceOrientationCamera.minZ = this._scene.activeCamera.minZ;
  147. this._deviceOrientationCamera.maxZ = this._scene.activeCamera.maxZ;
  148. // Set rotation from previous camera
  149. if (this._scene.activeCamera instanceof TargetCamera && this._scene.activeCamera.rotation) {
  150. var targetCamera = this._scene.activeCamera;
  151. if (targetCamera.rotationQuaternion) {
  152. this._deviceOrientationCamera.rotationQuaternion.copyFrom(targetCamera.rotationQuaternion);
  153. } else {
  154. this._deviceOrientationCamera.rotationQuaternion.copyFrom(Quaternion.RotationYawPitchRoll(targetCamera.rotation.y, targetCamera.rotation.x, targetCamera.rotation.z));
  155. }
  156. this._deviceOrientationCamera.rotation = targetCamera.rotation.clone();
  157. }
  158. }
  159. this._scene.activeCamera = this._deviceOrientationCamera;
  160. if (this._canvas) {
  161. this._scene.activeCamera.attachControl(this._canvas);
  162. }
  163. }
  164. else {
  165. this._existingCamera = this._scene.activeCamera;
  166. this._position = this._scene.activeCamera.position.clone();
  167. }
  168. if (webVROptions) {
  169. if (webVROptions.useCustomVRButton) {
  170. this._useCustomVRButton = true;
  171. if (webVROptions.customVRButton) {
  172. this._btnVR = webVROptions.customVRButton;
  173. }
  174. }
  175. if (webVROptions.rayLength) {
  176. this._rayLength = webVROptions.rayLength
  177. }
  178. }
  179. if (!this._useCustomVRButton) {
  180. this._btnVR = <HTMLButtonElement>document.createElement("BUTTON");
  181. this._btnVR.className = "babylonVRicon";
  182. this._btnVR.id = "babylonVRiconbtn";
  183. this._btnVR.title = "Click to switch to VR";
  184. 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) }";
  185. css += ".babylonVRicon.vrdisplaypresenting { display: none; }";
  186. // TODO: Add user feedback so that they know what state the VRDisplay is in (disconnected, connected, entering-VR)
  187. // css += ".babylonVRicon.vrdisplaysupported { }";
  188. // css += ".babylonVRicon.vrdisplayready { }";
  189. // css += ".babylonVRicon.vrdisplayrequesting { }";
  190. var style = document.createElement('style');
  191. style.appendChild(document.createTextNode(css));
  192. document.getElementsByTagName('head')[0].appendChild(style);
  193. }
  194. if (this._canvas) {
  195. if (!this._useCustomVRButton) {
  196. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  197. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  198. }
  199. if (this._btnVR) {
  200. this._btnVR.addEventListener("click", () => {
  201. this.enterVR();
  202. });
  203. }
  204. window.addEventListener("resize", () => {
  205. if (this._canvas && !this._useCustomVRButton) {
  206. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  207. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  208. }
  209. if (this._fullscreenVRpresenting && this._webVRready) {
  210. this.exitVR();
  211. }
  212. });
  213. }
  214. document.addEventListener("fullscreenchange", () => { this._onFullscreenChange() }, false);
  215. document.addEventListener("mozfullscreenchange", () => { this._onFullscreenChange() }, false);
  216. document.addEventListener("webkitfullscreenchange", () => { this._onFullscreenChange() }, false);
  217. document.addEventListener("msfullscreenchange", () => { this._onFullscreenChange() }, false);
  218. this._scene.getEngine().onVRDisplayChangedObservable.add((e) => {
  219. if (!this._useCustomVRButton && !this._btnVRDisplayed && e.vrDisplay) {
  220. document.body.appendChild(this._btnVR);
  221. this._btnVRDisplayed = true;
  222. }
  223. })
  224. if (!this._useCustomVRButton && !this._btnVRDisplayed && webVROptions.createFallbackVRDeviceOrientationFreeCamera) {
  225. document.body.appendChild(this._btnVR);
  226. this._btnVRDisplayed = true;
  227. }
  228. // Exiting VR mode using 'ESC' key on desktop
  229. this._onKeyDown = (event: KeyboardEvent) => {
  230. if (event.keyCode === 27 && this.isInVRMode) {
  231. this.exitVR();
  232. }
  233. };
  234. document.addEventListener("keydown", this._onKeyDown);
  235. // Exiting VR mode double tapping the touch screen
  236. this._scene.onPrePointerObservable.add((pointerInfo, eventState) => {
  237. if (this.isInVRMode) {
  238. this.exitVR();
  239. if (this._fullscreenVRpresenting) {
  240. this._scene.getEngine().switchFullscreen(true);
  241. }
  242. }
  243. }, BABYLON.PointerEventTypes.POINTERDOUBLETAP, false);
  244. // Listen for WebVR display changes
  245. this._onVRDisplayChanged = (eventArgs: IDisplayChangedEventArgs) => this.onVRDisplayChanged(eventArgs);
  246. this._onVrDisplayPresentChange = () => this.onVrDisplayPresentChange();
  247. this._onVRRequestPresentStart = () => {
  248. this._webVRrequesting = true;
  249. this.updateButtonVisibility();
  250. }
  251. this._onVRRequestPresentComplete = (success: boolean) => {
  252. this._webVRrequesting = false;
  253. this.updateButtonVisibility();
  254. };
  255. scene.getEngine().onVRDisplayChangedObservable.add(this._onVRDisplayChanged);
  256. scene.getEngine().onVRRequestPresentStart.add(this._onVRRequestPresentStart);
  257. scene.getEngine().onVRRequestPresentComplete.add(this._onVRRequestPresentComplete);
  258. window.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  259. // Create the cameras
  260. if (webVROptions.createFallbackVRDeviceOrientationFreeCamera) {
  261. this._vrDeviceOrientationCamera = new BABYLON.VRDeviceOrientationFreeCamera("VRDeviceOrientationVRHelper", this._position, this._scene);
  262. }
  263. this._webVRCamera = new BABYLON.WebVRFreeCamera("WebVRHelper", this._position, this._scene, webVROptions);
  264. this._webVRCamera.onControllerMeshLoadedObservable.add((webVRController) => this._onDefaultMeshLoaded(webVRController));
  265. this._scene.gamepadManager.onGamepadConnectedObservable.add((pad) => this._onNewGamepadConnected(pad));
  266. this._scene.gamepadManager.onGamepadDisconnectedObservable.add((pad) => this._onNewGamepadDisconnected(pad));
  267. this.updateButtonVisibility();
  268. //create easing functions
  269. this._circleEase = new BABYLON.CircleEase();
  270. this._circleEase.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT);
  271. }
  272. // Raised when one of the controller has loaded successfully its associated default mesh
  273. private _onDefaultMeshLoaded(webVRController: WebVRController) {
  274. if (webVRController.hand === "left") {
  275. this._leftControllerReady = true;
  276. if (this._interactionsRequested && !this._interactionsEnabledOnLeftController) {
  277. this._enableInteractionOnController(webVRController);
  278. }
  279. if (this._teleportationRequested && !this._teleportationEnabledOnLeftController) {
  280. this._enableTeleportationOnController(webVRController);
  281. }
  282. }
  283. if (webVRController.hand === "right") {
  284. this._rightControllerReady = true;
  285. if (this._interactionsRequested && !this._interactionsEnabledOnRightController) {
  286. this._enableInteractionOnController(webVRController);
  287. }
  288. if (this._teleportationRequested && !this._teleportationEnabledOnRightController) {
  289. this._enableTeleportationOnController(webVRController);
  290. }
  291. }
  292. try {
  293. this.onControllerMeshLoaded.notifyObservers(webVRController);
  294. }
  295. catch (err) {
  296. Tools.Warn("Error in your custom logic onControllerMeshLoaded: " + err);
  297. }
  298. }
  299. private _onFullscreenChange() {
  300. if (document.fullscreen !== undefined) {
  301. this._fullscreenVRpresenting = document.fullscreen;
  302. } else if (document.mozFullScreen !== undefined) {
  303. this._fullscreenVRpresenting = document.mozFullScreen;
  304. } else if (document.webkitIsFullScreen !== undefined) {
  305. this._fullscreenVRpresenting = document.webkitIsFullScreen;
  306. } else if (document.msIsFullScreen !== undefined) {
  307. this._fullscreenVRpresenting = document.msIsFullScreen;
  308. }
  309. if (!this._fullscreenVRpresenting && this._canvas) {
  310. this.exitVR();
  311. if (!this._useCustomVRButton) {
  312. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  313. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  314. }
  315. }
  316. }
  317. /**
  318. * Gets a value indicating if we are currently in VR mode.
  319. */
  320. public get isInVRMode(): boolean {
  321. return this._webVRpresenting || this._fullscreenVRpresenting;
  322. }
  323. private onVrDisplayPresentChange() {
  324. var vrDisplay = this._scene.getEngine().getVRDevice();
  325. if (vrDisplay) {
  326. var wasPresenting = this._webVRpresenting;
  327. // A VR display is connected
  328. this._webVRpresenting = vrDisplay.isPresenting;
  329. if (wasPresenting && !this._webVRpresenting)
  330. this.exitVR();
  331. } else {
  332. Tools.Warn('Detected VRDisplayPresentChange on an unknown VRDisplay. Did you can enterVR on the vrExperienceHelper?');
  333. }
  334. this.updateButtonVisibility();
  335. }
  336. private onVRDisplayChanged(eventArgs: IDisplayChangedEventArgs) {
  337. this._webVRsupported = eventArgs.vrSupported;
  338. this._webVRready = !!eventArgs.vrDisplay;
  339. this._webVRpresenting = eventArgs.vrDisplay && eventArgs.vrDisplay.isPresenting;
  340. this.updateButtonVisibility();
  341. }
  342. private updateButtonVisibility() {
  343. if (!this._btnVR) {
  344. return;
  345. }
  346. this._btnVR.className = "babylonVRicon";
  347. if (this.isInVRMode) {
  348. this._btnVR.className += " vrdisplaypresenting";
  349. } else {
  350. if (this._webVRready) this._btnVR.className += " vrdisplayready";
  351. if (this._webVRsupported) this._btnVR.className += " vrdisplaysupported";
  352. if (this._webVRrequesting) this._btnVR.className += " vrdisplayrequesting";
  353. }
  354. }
  355. /**
  356. * Attempt to enter VR. If a headset is connected and ready, will request present on that.
  357. * Otherwise, will use the fullscreen API.
  358. */
  359. public enterVR() {
  360. if (this._scene.activeCamera) {
  361. this._position = this._scene.activeCamera.position.clone();
  362. }
  363. if (this.onEnteringVR) {
  364. try {
  365. this.onEnteringVR.notifyObservers(this);
  366. }
  367. catch (err) {
  368. Tools.Warn("Error in your custom logic onEnteringVR: " + err);
  369. }
  370. }
  371. if (this._webVRrequesting)
  372. return;
  373. // If WebVR is supported and a headset is connected
  374. if (this._webVRready) {
  375. if (!this._webVRpresenting) {
  376. this._webVRCamera.position = this._position;
  377. this._scene.activeCamera = this._webVRCamera;
  378. }
  379. }
  380. else if (this._vrDeviceOrientationCamera) {
  381. this._vrDeviceOrientationCamera.position = this._position;
  382. this._scene.activeCamera = this._vrDeviceOrientationCamera;
  383. this._scene.getEngine().switchFullscreen(true);
  384. this.updateButtonVisibility();
  385. }
  386. if (this._scene.activeCamera && this._canvas) {
  387. this._scene.activeCamera.attachControl(this._canvas);
  388. }
  389. }
  390. /**
  391. * Attempt to exit VR, or fullscreen.
  392. */
  393. public exitVR() {
  394. if (this.onExitingVR) {
  395. try {
  396. this.onExitingVR.notifyObservers(this);
  397. }
  398. catch (err) {
  399. Tools.Warn("Error in your custom logic onExitingVR: " + err);
  400. }
  401. }
  402. if (this._webVRpresenting) {
  403. this._scene.getEngine().disableVR();
  404. }
  405. if (this._scene.activeCamera) {
  406. this._position = this._scene.activeCamera.position.clone();
  407. }
  408. if (this._deviceOrientationCamera) {
  409. this._deviceOrientationCamera.position = this._position;
  410. this._scene.activeCamera = this._deviceOrientationCamera;
  411. if (this._canvas) {
  412. this._scene.activeCamera.attachControl(this._canvas);
  413. }
  414. } else if (this._existingCamera) {
  415. this._existingCamera.position = this._position;
  416. this._scene.activeCamera = this._existingCamera;
  417. }
  418. this.updateButtonVisibility();
  419. }
  420. public get position(): Vector3 {
  421. return this._position;
  422. }
  423. public set position(value: Vector3) {
  424. this._position = value;
  425. if (this._scene.activeCamera) {
  426. this._scene.activeCamera.position = value;
  427. }
  428. }
  429. public enableInteractions() {
  430. if (!this._interactionsEnabled) {
  431. this._interactionsRequested = true;
  432. if (this._leftControllerReady && this._webVRCamera.leftController) {
  433. this._enableInteractionOnController(this._webVRCamera.leftController)
  434. }
  435. if (this._rightControllerReady && this._webVRCamera.rightController) {
  436. this._enableInteractionOnController(this._webVRCamera.rightController)
  437. }
  438. this._createGazeTracker();
  439. this.raySelectionPredicate = (mesh) => {
  440. return true;
  441. }
  442. this.meshSelectionPredicate = (mesh) => {
  443. return true;
  444. }
  445. this._raySelectionPredicate = (mesh) => {
  446. if (this._isTeleportationFloor(mesh) || (mesh.isVisible && mesh.name.indexOf("gazeTracker") === -1
  447. && mesh.name.indexOf("teleportationCircle") === -1
  448. && mesh.name.indexOf("torusTeleportation") === -1
  449. && mesh.name.indexOf("laserPointer") === -1)) {
  450. return this.raySelectionPredicate(mesh);
  451. }
  452. return false;
  453. }
  454. this._scene.registerBeforeRender(() => {
  455. this._castRayAndSelectObject();
  456. });
  457. this._interactionsEnabled = true;
  458. }
  459. }
  460. private _isTeleportationFloor(mesh: AbstractMesh): boolean {
  461. for (var i = 0; i < this._floorMeshesCollection.length; i++) {
  462. if (this._floorMeshesCollection[i].id === mesh.id) {
  463. return true;
  464. }
  465. }
  466. if (this._floorMeshName && mesh.name === this._floorMeshName) {
  467. return true;
  468. }
  469. return false;
  470. }
  471. public addFloorMesh(floorMesh: Mesh): void {
  472. if (!this._floorMeshesCollection) {
  473. return;
  474. }
  475. if (this._floorMeshesCollection.indexOf(floorMesh) > -1) {
  476. return;
  477. }
  478. this._floorMeshesCollection.push(floorMesh);
  479. }
  480. public removeFloorMesh(floorMesh: Mesh): void {
  481. if (!this._floorMeshesCollection) {
  482. return
  483. }
  484. const meshIndex = this._floorMeshesCollection.indexOf(floorMesh);
  485. if (meshIndex !== -1) {
  486. this._floorMeshesCollection.splice(meshIndex, 1);
  487. }
  488. }
  489. public enableTeleportation(vrTeleportationOptions: VRTeleportationOptions = {}) {
  490. if (!this._teleportationEnabled) {
  491. this._teleportationRequested = true;
  492. this.enableInteractions();
  493. if (vrTeleportationOptions) {
  494. if (vrTeleportationOptions.floorMeshName) {
  495. this._floorMeshName = vrTeleportationOptions.floorMeshName;
  496. }
  497. if (vrTeleportationOptions.floorMeshes) {
  498. this._floorMeshesCollection = vrTeleportationOptions.floorMeshes;
  499. }
  500. }
  501. if (this._leftControllerReady && this._webVRCamera.leftController) {
  502. this._enableTeleportationOnController(this._webVRCamera.leftController)
  503. }
  504. if (this._rightControllerReady && this._webVRCamera.rightController) {
  505. this._enableTeleportationOnController(this._webVRCamera.rightController)
  506. }
  507. // Creates an image processing post process for the vignette not relying
  508. // on the main scene configuration for image processing to reduce setup and spaces
  509. // (gamma/linear) conflicts.
  510. const imageProcessingConfiguration = new ImageProcessingConfiguration();
  511. imageProcessingConfiguration.vignetteColor = new BABYLON.Color4(0, 0, 0, 0);
  512. imageProcessingConfiguration.vignetteEnabled = true;
  513. this._postProcessMove = new BABYLON.ImageProcessingPostProcess("postProcessMove",
  514. 1.0,
  515. this._webVRCamera,
  516. undefined,
  517. undefined,
  518. undefined,
  519. undefined,
  520. imageProcessingConfiguration);
  521. this._webVRCamera.detachPostProcess(this._postProcessMove)
  522. this._passProcessMove = new BABYLON.PassPostProcess("pass", 1.0, this._webVRCamera);
  523. this._teleportationEnabled = true;
  524. this._createTeleportationCircles();
  525. }
  526. }
  527. private _onNewGamepadConnected(gamepad: Gamepad) {
  528. if (gamepad.type !== BABYLON.Gamepad.POSE_ENABLED) {
  529. if (gamepad.leftStick) {
  530. gamepad.onleftstickchanged((stickValues) => {
  531. if (this._teleportationEnabled) {
  532. // Listening to classic/xbox gamepad only if no VR controller is active
  533. if ((!this._leftLaserPointer && !this._rightLaserPointer) ||
  534. ((this._leftLaserPointer && !this._leftLaserPointer.isVisible) &&
  535. (this._rightLaserPointer && !this._rightLaserPointer.isVisible))) {
  536. if (!this._teleportationRequestInitiated) {
  537. if (stickValues.y < -this._padSensibilityUp) {
  538. this._teleportationRequestInitiated = true;
  539. }
  540. }
  541. else {
  542. if (stickValues.y > -this._padSensibilityDown) {
  543. if (this._teleportationAllowed) {
  544. this._teleportCamera();
  545. }
  546. this._teleportationRequestInitiated = false;
  547. }
  548. }
  549. }
  550. }
  551. });
  552. }
  553. if (gamepad.rightStick) {
  554. gamepad.onrightstickchanged((stickValues) => {
  555. if (this._teleportationEnabled) {
  556. if (!this._rotationLeftAsked) {
  557. if (stickValues.x < -this._padSensibilityUp) {
  558. this._rotationLeftAsked = true;
  559. if (this._rotationAllowed) {
  560. this._rotateCamera(false);
  561. }
  562. }
  563. }
  564. else {
  565. if (stickValues.x > -this._padSensibilityDown) {
  566. this._rotationLeftAsked = false;
  567. }
  568. }
  569. if (!this._rotationRightAsked) {
  570. if (stickValues.x > this._padSensibilityUp) {
  571. this._rotationRightAsked = true;
  572. if (this._rotationAllowed) {
  573. this._rotateCamera(true);
  574. }
  575. }
  576. }
  577. else {
  578. if (stickValues.x < this._padSensibilityDown) {
  579. this._rotationRightAsked = false;
  580. }
  581. }
  582. }
  583. });
  584. }
  585. if (gamepad.type === BABYLON.Gamepad.XBOX) {
  586. (<Xbox360Pad>gamepad).onbuttondown((buttonPressed: Xbox360Button) => {
  587. if (this._interactionsEnabled && buttonPressed === Xbox360Button.A) {
  588. this._pointerDownOnMeshAsked = true;
  589. if (this._currentMeshSelected && this._currentHit) {
  590. this._scene.simulatePointerDown(this._currentHit);
  591. }
  592. }
  593. });
  594. (<Xbox360Pad>gamepad).onbuttonup((buttonPressed: Xbox360Button) => {
  595. if (this._interactionsEnabled && buttonPressed === Xbox360Button.A) {
  596. if (this._currentMeshSelected && this._currentHit) {
  597. this._scene.simulatePointerUp(this._currentHit);
  598. }
  599. this._pointerDownOnMeshAsked = false;
  600. }
  601. });
  602. }
  603. }
  604. }
  605. private _onNewGamepadDisconnected(gamepad: Gamepad) {
  606. if (gamepad instanceof WebVRController) {
  607. if (gamepad.hand === "left") {
  608. this._interactionsEnabledOnLeftController = false;
  609. this._teleportationEnabledOnLeftController = false;
  610. this._leftControllerReady = false;
  611. if (this._leftLaserPointer) {
  612. this._leftLaserPointer.dispose();
  613. }
  614. }
  615. if (gamepad.hand === "right") {
  616. this._interactionsEnabledOnRightController = false;
  617. this._teleportationEnabledOnRightController = false;
  618. this._rightControllerReady = false;
  619. if (this._rightLaserPointer) {
  620. this._rightLaserPointer.dispose();
  621. }
  622. }
  623. }
  624. }
  625. private _enableInteractionOnController(webVRController: WebVRController) {
  626. var controllerMesh = webVRController.mesh;
  627. if (controllerMesh) {
  628. var childMeshes = controllerMesh.getChildMeshes();
  629. for (var i = 0; i < childMeshes.length; i++) {
  630. if (childMeshes[i].name === "POINTING_POSE") {
  631. controllerMesh = childMeshes[i];
  632. break;
  633. }
  634. }
  635. var laserPointer = BABYLON.Mesh.CreateCylinder("laserPointer", 1, 0.004, 0.0002, 20, 1, this._scene, false);
  636. var laserPointerMaterial = new BABYLON.StandardMaterial("laserPointerMat", this._scene);
  637. laserPointerMaterial.emissiveColor = new BABYLON.Color3(0.7, 0.7, 0.7);
  638. laserPointerMaterial.alpha = 0.6;
  639. laserPointer.material = laserPointerMaterial;
  640. laserPointer.rotation.x = Math.PI / 2;
  641. laserPointer.parent = controllerMesh;
  642. laserPointer.position.z = -0.5;
  643. laserPointer.position.y = 0;
  644. laserPointer.isVisible = false;
  645. if (webVRController.hand === "left") {
  646. this._leftLaserPointer = laserPointer;
  647. this._interactionsEnabledOnLeftController = true;
  648. }
  649. else {
  650. this._rightLaserPointer = laserPointer;
  651. this._interactionsEnabledOnRightController = true;
  652. }
  653. webVRController.onMainButtonStateChangedObservable.add((stateObject) => {
  654. // Enabling / disabling laserPointer
  655. if (this._displayLaserPointer && stateObject.value === 1) {
  656. laserPointer.isVisible = !laserPointer.isVisible;
  657. // Laser pointer can only be active on left or right, not both at the same time
  658. if (webVRController.hand === "left" && this._rightLaserPointer) {
  659. this._rightLaserPointer.isVisible = false;
  660. }
  661. else if (this._leftLaserPointer) {
  662. this._leftLaserPointer.isVisible = false;
  663. }
  664. }
  665. });
  666. webVRController.onTriggerStateChangedObservable.add((stateObject) => {
  667. if (!this._pointerDownOnMeshAsked) {
  668. if (stateObject.value > this._padSensibilityUp) {
  669. this._pointerDownOnMeshAsked = true;
  670. if (this._currentMeshSelected && this._currentHit) {
  671. this._scene.simulatePointerDown(this._currentHit);
  672. }
  673. }
  674. }
  675. else if (stateObject.value < this._padSensibilityDown) {
  676. if (this._currentMeshSelected && this._currentHit) {
  677. this._scene.simulatePointerUp(this._currentHit);
  678. }
  679. this._pointerDownOnMeshAsked = false;
  680. }
  681. });
  682. }
  683. }
  684. private _enableTeleportationOnController(webVRController: WebVRController) {
  685. var controllerMesh = webVRController.mesh;
  686. if (controllerMesh) {
  687. if (webVRController.hand === "left") {
  688. if (!this._interactionsEnabledOnLeftController) {
  689. this._enableInteractionOnController(webVRController);
  690. }
  691. this._teleportationEnabledOnLeftController = true;
  692. }
  693. else {
  694. if (!this._interactionsEnabledOnRightController) {
  695. this._enableInteractionOnController(webVRController);
  696. }
  697. this._teleportationEnabledOnRightController = true;
  698. }
  699. webVRController.onPadValuesChangedObservable.add((stateObject) => {
  700. if (!this._teleportationRequestInitiated) {
  701. if (stateObject.y < -this._padSensibilityUp) {
  702. // If laser pointer wasn't enabled yet
  703. if (this._displayLaserPointer && webVRController.hand === "left" && this._leftLaserPointer) {
  704. this._leftLaserPointer.isVisible = true;
  705. if (this._rightLaserPointer) {
  706. this._rightLaserPointer.isVisible = false;
  707. }
  708. }
  709. else if (this._displayLaserPointer && this._rightLaserPointer) {
  710. this._rightLaserPointer.isVisible = true;
  711. if (this._leftLaserPointer) {
  712. this._leftLaserPointer.isVisible = false;
  713. }
  714. }
  715. this._teleportationRequestInitiated = true;
  716. }
  717. }
  718. else {
  719. // Listening to the proper controller values changes to confirm teleportation
  720. if ((webVRController.hand === "left" && this._leftLaserPointer && this._leftLaserPointer.isVisible)
  721. || (webVRController.hand === "right" && this._rightLaserPointer && this._rightLaserPointer.isVisible)) {
  722. if (stateObject.y > -this._padSensibilityDown) {
  723. if (this._teleportationAllowed) {
  724. this._teleportationAllowed = false;
  725. this._teleportCamera();
  726. }
  727. this._teleportationRequestInitiated = false;
  728. }
  729. }
  730. }
  731. if (!this._rotationLeftAsked) {
  732. if (stateObject.x < -this._padSensibilityUp) {
  733. this._rotationLeftAsked = true;
  734. if (this._rotationAllowed) {
  735. this._rotateCamera(false);
  736. }
  737. }
  738. }
  739. else {
  740. if (stateObject.x > -this._padSensibilityDown) {
  741. this._rotationLeftAsked = false;
  742. }
  743. }
  744. if (!this._rotationRightAsked) {
  745. if (stateObject.x > this._padSensibilityUp) {
  746. this._rotationRightAsked = true;
  747. if (this._rotationAllowed) {
  748. this._rotateCamera(true);
  749. }
  750. }
  751. }
  752. else {
  753. if (stateObject.x < this._padSensibilityDown) {
  754. this._rotationRightAsked = false;
  755. }
  756. }
  757. });
  758. }
  759. }
  760. // Gaze support used to point to teleport or to interact with an object
  761. private _createGazeTracker() {
  762. this._gazeTracker = BABYLON.Mesh.CreateTorus("gazeTracker", 0.0035, 0.0025, 20, this._scene, false);
  763. this._gazeTracker.bakeCurrentTransformIntoVertices();
  764. this._gazeTracker.isPickable = false;
  765. this._gazeTracker.isVisible = false;
  766. var targetMat = new BABYLON.StandardMaterial("targetMat", this._scene);
  767. targetMat.specularColor = BABYLON.Color3.Black();
  768. targetMat.emissiveColor = new BABYLON.Color3(0.7, 0.7, 0.7)
  769. targetMat.backFaceCulling = false;
  770. this._gazeTracker.material = targetMat;
  771. }
  772. private _createTeleportationCircles() {
  773. this._teleportationCircle = BABYLON.Mesh.CreateGround("teleportationCircle", 2, 2, 2, this._scene);
  774. this._teleportationCircle.isPickable = false;
  775. var length = 512;
  776. var dynamicTexture = new BABYLON.DynamicTexture("DynamicTexture", length, this._scene, true);
  777. dynamicTexture.hasAlpha = true;
  778. var context = dynamicTexture.getContext();
  779. var centerX = length / 2;
  780. var centerY = length / 2;
  781. var radius = 200;
  782. context.beginPath();
  783. context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  784. context.fillStyle = this._teleportationFillColor;
  785. context.fill();
  786. context.lineWidth = 10;
  787. context.strokeStyle = this._teleportationBorderColor;
  788. context.stroke();
  789. context.closePath();
  790. dynamicTexture.update();
  791. var teleportationCircleMaterial = new BABYLON.StandardMaterial("TextPlaneMaterial", this._scene);
  792. teleportationCircleMaterial.diffuseTexture = dynamicTexture;
  793. this._teleportationCircle.material = teleportationCircleMaterial;
  794. var torus = BABYLON.Mesh.CreateTorus("torusTeleportation", 0.75, 0.1, 25, this._scene, false);
  795. torus.isPickable = false;
  796. torus.parent = this._teleportationCircle;
  797. var animationInnerCircle = new BABYLON.Animation("animationInnerCircle", "position.y", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  798. var keys = [];
  799. keys.push({
  800. frame: 0,
  801. value: 0
  802. });
  803. keys.push({
  804. frame: 30,
  805. value: 0.4
  806. });
  807. keys.push({
  808. frame: 60,
  809. value: 0
  810. });
  811. animationInnerCircle.setKeys(keys);
  812. var easingFunction = new BABYLON.SineEase();
  813. easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT);
  814. animationInnerCircle.setEasingFunction(easingFunction);
  815. torus.animations = [];
  816. torus.animations.push(animationInnerCircle);
  817. this._scene.beginAnimation(torus, 0, 60, true);
  818. this._hideTeleportationCircle();
  819. }
  820. private _displayTeleportationCircle() {
  821. if (this._teleportationEnabled) {
  822. this._teleportationCircle.isVisible = true;
  823. (<Mesh>this._teleportationCircle.getChildren()[0]).isVisible = true;
  824. }
  825. }
  826. private _hideTeleportationCircle() {
  827. if (this._teleportationEnabled) {
  828. this._teleportationCircle.isVisible = false;
  829. (<Mesh>this._teleportationCircle.getChildren()[0]).isVisible = false;
  830. }
  831. }
  832. private _rotateCamera(right: boolean) {
  833. if (!(this.currentVRCamera instanceof FreeCamera)) {
  834. return;
  835. }
  836. if (right) {
  837. this._rotationAngle++;
  838. }
  839. else {
  840. this._rotationAngle--;
  841. }
  842. this.currentVRCamera.animations = [];
  843. var target = BABYLON.Quaternion.FromRotationMatrix(BABYLON.Matrix.RotationY(Math.PI / 4 * this._rotationAngle));
  844. var animationRotation = new BABYLON.Animation("animationRotation", "rotationQuaternion", 90, BABYLON.Animation.ANIMATIONTYPE_QUATERNION,
  845. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  846. var animationRotationKeys = [];
  847. animationRotationKeys.push({
  848. frame: 0,
  849. value: this.currentVRCamera.rotationQuaternion
  850. });
  851. animationRotationKeys.push({
  852. frame: 6,
  853. value: target
  854. });
  855. animationRotation.setKeys(animationRotationKeys);
  856. animationRotation.setEasingFunction(this._circleEase);
  857. this.currentVRCamera.animations.push(animationRotation);
  858. this._postProcessMove.animations = [];
  859. var animationPP = new BABYLON.Animation("animationPP", "vignetteWeight", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  860. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  861. var vignetteWeightKeys = [];
  862. vignetteWeightKeys.push({
  863. frame: 0,
  864. value: 0
  865. });
  866. vignetteWeightKeys.push({
  867. frame: 3,
  868. value: 4
  869. });
  870. vignetteWeightKeys.push({
  871. frame: 6,
  872. value: 0
  873. });
  874. animationPP.setKeys(vignetteWeightKeys);
  875. animationPP.setEasingFunction(this._circleEase);
  876. this._postProcessMove.animations.push(animationPP);
  877. var animationPP2 = new BABYLON.Animation("animationPP2", "vignetteStretch", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  878. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  879. var vignetteStretchKeys = [];
  880. vignetteStretchKeys.push({
  881. frame: 0,
  882. value: 0
  883. });
  884. vignetteStretchKeys.push({
  885. frame: 3,
  886. value: 10
  887. });
  888. vignetteStretchKeys.push({
  889. frame: 6,
  890. value: 0
  891. });
  892. animationPP2.setKeys(vignetteStretchKeys);
  893. animationPP2.setEasingFunction(this._circleEase);
  894. this._postProcessMove.animations.push(animationPP2);
  895. this._postProcessMove.imageProcessingConfiguration.vignetteWeight = 0;
  896. this._postProcessMove.imageProcessingConfiguration.vignetteStretch = 0;
  897. this._webVRCamera.attachPostProcess(this._postProcessMove)
  898. this._scene.beginAnimation(this._postProcessMove, 0, 6, false, 1, () => {
  899. this._webVRCamera.detachPostProcess(this._postProcessMove)
  900. });
  901. this._scene.beginAnimation(this.currentVRCamera, 0, 6, false, 1);
  902. }
  903. private _moveTeleportationSelectorTo(hit: PickingInfo) {
  904. if (hit.pickedPoint) {
  905. this._teleportationAllowed = true;
  906. if (this._teleportationRequestInitiated) {
  907. this._displayTeleportationCircle();
  908. }
  909. else {
  910. this._hideTeleportationCircle();
  911. }
  912. this._haloCenter.copyFrom(hit.pickedPoint);
  913. this._teleportationCircle.position.copyFrom(hit.pickedPoint);
  914. var pickNormal = hit.getNormal(true, false);
  915. if (pickNormal) {
  916. var axis1 = BABYLON.Vector3.Cross(BABYLON.Axis.Y, pickNormal);
  917. var axis2 = BABYLON.Vector3.Cross(pickNormal, axis1);
  918. BABYLON.Vector3.RotationFromAxisToRef(axis2, pickNormal, axis1, this._teleportationCircle.rotation);
  919. }
  920. this._teleportationCircle.position.y += 0.1;
  921. }
  922. }
  923. private _workingVector = Vector3.Zero();
  924. private _teleportCamera() {
  925. if (!(this.currentVRCamera instanceof FreeCamera)) {
  926. return;
  927. }
  928. // Teleport the hmd to where the user is looking by moving the anchor to where they are looking minus the
  929. // offset of the headset from the anchor. Then add the helper's position to account for user's height offset
  930. if (this.webVRCamera.leftCamera) {
  931. this._workingVector.copyFrom(this.webVRCamera.leftCamera.globalPosition);
  932. this._workingVector.subtractInPlace(this.webVRCamera.position);
  933. this._haloCenter.subtractToRef(this._workingVector, this._workingVector);
  934. } else {
  935. this._workingVector.copyFrom(this._haloCenter);
  936. }
  937. this._workingVector.y += this._defaultHeight;
  938. // Create animation from the camera's position to the new location
  939. this.currentVRCamera.animations = [];
  940. var animationCameraTeleportation = new BABYLON.Animation("animationCameraTeleportation", "position", 90, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  941. var animationCameraTeleportationKeys = [{
  942. frame: 0,
  943. value: this.currentVRCamera.position
  944. },
  945. {
  946. frame: 11,
  947. value: this._workingVector
  948. }
  949. ];
  950. animationCameraTeleportation.setKeys(animationCameraTeleportationKeys);
  951. animationCameraTeleportation.setEasingFunction(this._circleEase);
  952. this.currentVRCamera.animations.push(animationCameraTeleportation);
  953. this._postProcessMove.animations = [];
  954. var animationPP = new BABYLON.Animation("animationPP", "vignetteWeight", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  955. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  956. var vignetteWeightKeys = [];
  957. vignetteWeightKeys.push({
  958. frame: 0,
  959. value: 0
  960. });
  961. vignetteWeightKeys.push({
  962. frame: 5,
  963. value: 8
  964. });
  965. vignetteWeightKeys.push({
  966. frame: 11,
  967. value: 0
  968. });
  969. animationPP.setKeys(vignetteWeightKeys);
  970. this._postProcessMove.animations.push(animationPP);
  971. var animationPP2 = new BABYLON.Animation("animationPP2", "vignetteStretch", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  972. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  973. var vignetteStretchKeys = [];
  974. vignetteStretchKeys.push({
  975. frame: 0,
  976. value: 0
  977. });
  978. vignetteStretchKeys.push({
  979. frame: 5,
  980. value: 10
  981. });
  982. vignetteStretchKeys.push({
  983. frame: 11,
  984. value: 0
  985. });
  986. animationPP2.setKeys(vignetteStretchKeys);
  987. this._postProcessMove.animations.push(animationPP2);
  988. this._postProcessMove.imageProcessingConfiguration.vignetteWeight = 0;
  989. this._postProcessMove.imageProcessingConfiguration.vignetteStretch = 0;
  990. this._webVRCamera.attachPostProcess(this._postProcessMove)
  991. this._scene.beginAnimation(this._postProcessMove, 0, 11, false, 1, () => {
  992. this._webVRCamera.detachPostProcess(this._postProcessMove)
  993. });
  994. this._scene.beginAnimation(this.currentVRCamera, 0, 11, false, 1);
  995. }
  996. private _castRayAndSelectObject() {
  997. if (!(this.currentVRCamera instanceof FreeCamera)) {
  998. return;
  999. }
  1000. var ray;
  1001. if (this._leftLaserPointer && this._leftLaserPointer.isVisible && (<any>this.currentVRCamera).leftController) {
  1002. ray = (<any>this.currentVRCamera).leftController.getForwardRay(this._rayLength);
  1003. }
  1004. else if (this._rightLaserPointer && this._rightLaserPointer.isVisible && (<any>this.currentVRCamera).rightController) {
  1005. ray = (<any>this.currentVRCamera).rightController.getForwardRay(this._rayLength);
  1006. } else {
  1007. ray = this.currentVRCamera.getForwardRay(this._rayLength);
  1008. }
  1009. var hit = this._scene.pickWithRay(ray, this._raySelectionPredicate);
  1010. // Moving the gazeTracker on the mesh face targetted
  1011. if (hit && hit.pickedPoint) {
  1012. if (this._displayGaze) {
  1013. let multiplier = 1;
  1014. this._gazeTracker.isVisible = true;
  1015. if (this._isActionableMesh) {
  1016. multiplier = 3;
  1017. }
  1018. this._gazeTracker.scaling.x = hit.distance * multiplier;
  1019. this._gazeTracker.scaling.y = hit.distance * multiplier;
  1020. this._gazeTracker.scaling.z = hit.distance * multiplier;
  1021. var pickNormal = hit.getNormal();
  1022. // To avoid z-fighting
  1023. let deltaFighting = 0.002;
  1024. if (pickNormal) {
  1025. var axis1 = BABYLON.Vector3.Cross(BABYLON.Axis.Y, pickNormal);
  1026. var axis2 = BABYLON.Vector3.Cross(pickNormal, axis1);
  1027. BABYLON.Vector3.RotationFromAxisToRef(axis2, pickNormal, axis1, this._gazeTracker.rotation);
  1028. }
  1029. this._gazeTracker.position.copyFrom(hit.pickedPoint);
  1030. if (this._gazeTracker.position.x < 0) {
  1031. this._gazeTracker.position.x += deltaFighting;
  1032. }
  1033. else {
  1034. this._gazeTracker.position.x -= deltaFighting;
  1035. }
  1036. if (this._gazeTracker.position.y < 0) {
  1037. this._gazeTracker.position.y += deltaFighting;
  1038. }
  1039. else {
  1040. this._gazeTracker.position.y -= deltaFighting;
  1041. }
  1042. if (this._gazeTracker.position.z < 0) {
  1043. this._gazeTracker.position.z += deltaFighting;
  1044. }
  1045. else {
  1046. this._gazeTracker.position.z -= deltaFighting;
  1047. }
  1048. }
  1049. // Changing the size of the laser pointer based on the distance from the targetted point
  1050. if (this._rightLaserPointer && this._rightLaserPointer.isVisible) {
  1051. this._rightLaserPointer.scaling.y = hit.distance;
  1052. this._rightLaserPointer.position.z = -hit.distance / 2;
  1053. }
  1054. if (this._leftLaserPointer && this._leftLaserPointer.isVisible) {
  1055. this._leftLaserPointer.scaling.y = hit.distance;
  1056. this._leftLaserPointer.position.z = -hit.distance / 2;
  1057. }
  1058. }
  1059. else {
  1060. this._gazeTracker.isVisible = false;
  1061. }
  1062. if (hit && hit.pickedMesh) {
  1063. this._currentHit = hit;
  1064. if (this._pointerDownOnMeshAsked) {
  1065. this._scene.simulatePointerMove(this._currentHit);
  1066. }
  1067. // The object selected is the floor, we're in a teleportation scenario
  1068. if (this._teleportationEnabled && this._isTeleportationFloor(hit.pickedMesh) && hit.pickedPoint) {
  1069. // Moving the teleportation area to this targetted point
  1070. this._moveTeleportationSelectorTo(hit);
  1071. return;
  1072. }
  1073. // If not, we're in a selection scenario
  1074. this._hideTeleportationCircle();
  1075. this._teleportationAllowed = false;
  1076. if (hit.pickedMesh !== this._currentMeshSelected) {
  1077. if (this.meshSelectionPredicate(hit.pickedMesh)) {
  1078. this._currentMeshSelected = hit.pickedMesh;
  1079. if (hit.pickedMesh.isPickable && hit.pickedMesh.actionManager) {
  1080. this.changeGazeColor(new BABYLON.Color3(0, 0, 1));
  1081. this.changeLaserColor(new BABYLON.Color3(0.2, 0.2, 1));
  1082. this._isActionableMesh = true;
  1083. }
  1084. else {
  1085. this.changeGazeColor(new BABYLON.Color3(0.7, 0.7, 0.7));
  1086. this.changeLaserColor(new BABYLON.Color3(0.7, 0.7, 0.7));
  1087. this._isActionableMesh = false;
  1088. }
  1089. try {
  1090. this.onNewMeshSelected.notifyObservers(this._currentMeshSelected);
  1091. }
  1092. catch (err) {
  1093. Tools.Warn("Error in your custom logic onNewMeshSelected: " + err);
  1094. }
  1095. }
  1096. else {
  1097. this._currentMeshSelected = null;
  1098. this.changeGazeColor(new BABYLON.Color3(0.7, 0.7, 0.7));
  1099. this.changeLaserColor(new BABYLON.Color3(0.7, 0.7, 0.7));
  1100. }
  1101. }
  1102. }
  1103. else {
  1104. this._currentHit = null;
  1105. this._currentMeshSelected = null;
  1106. this._teleportationAllowed = false;
  1107. this._hideTeleportationCircle();
  1108. this.changeGazeColor(new BABYLON.Color3(0.7, 0.7, 0.7));
  1109. this.changeLaserColor(new BABYLON.Color3(0.7, 0.7, 0.7));
  1110. }
  1111. }
  1112. public changeLaserColor(color: Color3) {
  1113. if (this._leftLaserPointer && this._leftLaserPointer.material) {
  1114. (<StandardMaterial>this._leftLaserPointer.material).emissiveColor = color;
  1115. }
  1116. if (this._rightLaserPointer && this._rightLaserPointer.material) {
  1117. (<StandardMaterial>this._rightLaserPointer.material).emissiveColor = color;
  1118. }
  1119. }
  1120. public changeGazeColor(color: Color3) {
  1121. if (this._gazeTracker.material) {
  1122. (<StandardMaterial>this._gazeTracker.material).emissiveColor = color;
  1123. }
  1124. }
  1125. public dispose() {
  1126. if (this.isInVRMode) {
  1127. this.exitVR();
  1128. }
  1129. if (this._deviceOrientationCamera) {
  1130. this._deviceOrientationCamera.dispose();
  1131. }
  1132. if (this._passProcessMove) {
  1133. this._passProcessMove.dispose();
  1134. }
  1135. if (this._postProcessMove) {
  1136. this._postProcessMove.dispose();
  1137. }
  1138. if (this._webVRCamera) {
  1139. this._webVRCamera.dispose();
  1140. }
  1141. if (this._vrDeviceOrientationCamera) {
  1142. this._vrDeviceOrientationCamera.dispose();
  1143. }
  1144. if (!this._useCustomVRButton) {
  1145. document.body.removeChild(this._btnVR);
  1146. }
  1147. this._floorMeshesCollection = [];
  1148. document.removeEventListener("keydown", this._onKeyDown);
  1149. window.removeEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  1150. }
  1151. public getClassName(): string {
  1152. return "VRExperienceHelper";
  1153. }
  1154. }
  1155. }