babylon.vrExperienceHelper.ts 67 KB

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