babylon.vrExperienceHelper.ts 66 KB

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