babylon.vrExperienceHelper.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. module BABYLON {
  2. export interface VRTeleportationOptions {
  3. floorMeshName?: string; // If you'd like to provide a mesh acting as the floor
  4. }
  5. export class VRExperienceHelper {
  6. private _scene: BABYLON.Scene;
  7. private _position: Vector3;
  8. private _btnVR: HTMLButtonElement;
  9. // Can the system support WebVR, even if a headset isn't plugged in?
  10. private _webVRsupported = false;
  11. // If WebVR is supported, is a headset plugged in and are we ready to present?
  12. private _webVRready = false;
  13. // Are we waiting for the requestPresent callback to complete?
  14. private _webVRrequesting = false;
  15. // Are we presenting to the headset right now?
  16. private _webVRpresenting = false;
  17. // Are we presenting in the fullscreen fallback?
  18. private _fullscreenVRpresenting = false;
  19. private _canvas: Nullable<HTMLCanvasElement>;
  20. private _webVRCamera: WebVRFreeCamera;
  21. private _vrDeviceOrientationCamera: VRDeviceOrientationFreeCamera;
  22. private _deviceOrientationCamera: DeviceOrientationCamera;
  23. private _onKeyDown: (event: KeyboardEvent) => void;
  24. private _onVrDisplayPresentChange: any;
  25. private _onVRDisplayChanged: (eventArgs:IDisplayChangedEventArgs) => void;
  26. private _onVRRequestPresentStart: () => void;
  27. private _onVRRequestPresentComplete: (success: boolean) => void;
  28. public onEnteringVR: () => void;
  29. public onExitingVR: () => void;
  30. public onControllerMeshLoaded: (controller: WebVRController) => void;
  31. private _useCustomVRButton: boolean = false;
  32. private _teleportationRequested: boolean = false;
  33. private _teleportationEnabledOnLeftController: boolean = false;
  34. private _teleportationEnabledOnRightController: boolean = false;
  35. private _leftControllerReady: boolean = false;
  36. private _rightControllerReady: boolean = false;
  37. private _floorMeshName: string;
  38. private _teleportationAllowed: boolean = false;
  39. private _rotationAllowed: boolean = true;
  40. private _teleportationRequestInitiated = false;
  41. private _xboxGamepadTeleportationRequestInitiated = false;
  42. private _rotationRightAsked = false;
  43. private _rotationLeftAsked = false;
  44. private _teleportationCircle: BABYLON.Mesh;
  45. private _postProcessMove: ImageProcessingPostProcess;
  46. private _teleportationFillColor: string = "#444444";
  47. private _teleportationBorderColor: string = "#FFFFFF";
  48. private _rotationAngle: number = 0;
  49. private _haloCenter = new BABYLON.Vector3(0, 0, 0);
  50. private _rayHelper: RayHelper;
  51. public meshSelectionPredicate: (mesh: BABYLON.Mesh) => boolean;
  52. public get deviceOrientationCamera(): DeviceOrientationCamera {
  53. return this._deviceOrientationCamera;
  54. }
  55. // Based on the current WebVR support, returns the current VR camera used
  56. public get currentVRCamera(): FreeCamera {
  57. if (this._webVRready) {
  58. return this._webVRCamera;
  59. }
  60. else {
  61. return this._vrDeviceOrientationCamera;
  62. }
  63. }
  64. public get webVRCamera(): WebVRFreeCamera {
  65. return this._webVRCamera;
  66. }
  67. public get vrDeviceOrientationCamera(): VRDeviceOrientationFreeCamera {
  68. return this._vrDeviceOrientationCamera;
  69. }
  70. constructor(scene: Scene, public webVROptions: WebVROptions = {}) {
  71. this._scene = scene;
  72. if (!this._scene.activeCamera || isNaN(this._scene.activeCamera.position.x)) {
  73. this._position = new BABYLON.Vector3(0, 2, 0);
  74. this._deviceOrientationCamera = new BABYLON.DeviceOrientationCamera("deviceOrientationVRHelper", new BABYLON.Vector3(0, 2, 0), scene);
  75. }
  76. else {
  77. this._position = this._scene.activeCamera.position.clone();
  78. this._deviceOrientationCamera = new BABYLON.DeviceOrientationCamera("deviceOrientationVRHelper", this._position, scene);
  79. this._deviceOrientationCamera.minZ = this._scene.activeCamera.minZ;
  80. this._deviceOrientationCamera.maxZ = this._scene.activeCamera.maxZ;
  81. }
  82. this._scene.activeCamera = this._deviceOrientationCamera;
  83. this._canvas = scene.getEngine().getRenderingCanvas();
  84. if (this._canvas) {
  85. this._scene.activeCamera.attachControl(this._canvas);
  86. }
  87. if (webVROptions) {
  88. if (webVROptions.useCustomVRButton) {
  89. this._useCustomVRButton = true;
  90. if (webVROptions.customVRButton) {
  91. this._btnVR = webVROptions.customVRButton;
  92. }
  93. }
  94. }
  95. if (!this._useCustomVRButton) {
  96. this._btnVR = <HTMLButtonElement>document.createElement("BUTTON");
  97. this._btnVR.className = "babylonVRicon";
  98. this._btnVR.id = "babylonVRiconbtn";
  99. this._btnVR.title = "Click to switch to VR";
  100. 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) }";
  101. css += ".babylonVRicon.vrdisplaypresenting { display: none; }";
  102. // TODO: Add user feedback so that they know what state the VRDisplay is in (disconnected, connected, entering-VR)
  103. // css += ".babylonVRicon.vrdisplaysupported { }";
  104. // css += ".babylonVRicon.vrdisplayready { }";
  105. // css += ".babylonVRicon.vrdisplayrequesting { }";
  106. var style = document.createElement('style');
  107. style.appendChild(document.createTextNode(css));
  108. document.getElementsByTagName('head')[0].appendChild(style);
  109. }
  110. if (this._canvas) {
  111. if (!this._useCustomVRButton) {
  112. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  113. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  114. }
  115. if (this._btnVR) {
  116. this._btnVR.addEventListener("click", () => {
  117. this.enterVR();
  118. });
  119. }
  120. window.addEventListener("resize", () => {
  121. if (this._canvas && !this._useCustomVRButton) {
  122. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  123. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  124. }
  125. if (this._fullscreenVRpresenting && this._webVRready) {
  126. this.exitVR();
  127. }
  128. });
  129. }
  130. document.addEventListener("fullscreenchange", () => { this._onFullscreenChange() }, false);
  131. document.addEventListener("mozfullscreenchange", () => { this._onFullscreenChange() }, false);
  132. document.addEventListener("webkitfullscreenchange", () => { this._onFullscreenChange() }, false);
  133. document.addEventListener("msfullscreenchange", () => { this._onFullscreenChange() }, false);
  134. if (!this._useCustomVRButton) {
  135. document.body.appendChild(this._btnVR);
  136. }
  137. // Exiting VR mode using 'ESC' key on desktop
  138. this._onKeyDown = (event: KeyboardEvent) => {
  139. if (event.keyCode === 27 && this.isInVRMode()) {
  140. this.exitVR();
  141. }
  142. };
  143. document.addEventListener("keydown", this._onKeyDown);
  144. // Exiting VR mode double tapping the touch screen
  145. this._scene.onPrePointerObservable.add( (pointerInfo, eventState) => {
  146. if (this.isInVRMode()) {
  147. this.exitVR();
  148. if (this._fullscreenVRpresenting) {
  149. this._scene.getEngine().switchFullscreen(true);
  150. }
  151. }
  152. }, BABYLON.PointerEventTypes.POINTERDOUBLETAP, false);
  153. // Listen for WebVR display changes
  154. this._onVRDisplayChanged = (eventArgs:IDisplayChangedEventArgs) => this.onVRDisplayChanged(eventArgs);
  155. this._onVrDisplayPresentChange = () => this.onVrDisplayPresentChange();
  156. this._onVRRequestPresentStart = () => {
  157. this._webVRrequesting = true;
  158. this.updateButtonVisibility();
  159. }
  160. this._onVRRequestPresentComplete = (success: boolean) => {
  161. this._webVRrequesting = false;
  162. this.updateButtonVisibility();
  163. };
  164. scene.getEngine().onVRDisplayChangedObservable.add(this._onVRDisplayChanged);
  165. scene.getEngine().onVRRequestPresentStart.add(this._onVRRequestPresentStart);
  166. scene.getEngine().onVRRequestPresentComplete.add(this._onVRRequestPresentComplete);
  167. window.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  168. // Create the cameras
  169. this._vrDeviceOrientationCamera = new BABYLON.VRDeviceOrientationFreeCamera("VRDeviceOrientationVRHelper", this._position, this._scene);
  170. this._webVRCamera = new BABYLON.WebVRFreeCamera("WebVRHelper", this._position, this._scene, webVROptions);
  171. this._webVRCamera.onControllerMeshLoadedObservable.add((webVRController) => this._onDefaultMeshLoaded(webVRController));
  172. this.updateButtonVisibility();
  173. }
  174. // Raised when one of the controller has loaded successfully its associated default mesh
  175. private _onDefaultMeshLoaded(webVRController: WebVRController) {
  176. if (webVRController.hand === "left") {
  177. this._leftControllerReady = true;
  178. if (this._teleportationRequested && !this._teleportationEnabledOnLeftController) {
  179. this._enableTeleportationOnController(webVRController);
  180. }
  181. }
  182. if (webVRController.hand === "right") {
  183. this._rightControllerReady = true;
  184. if (this._teleportationRequested && !this._teleportationEnabledOnRightController) {
  185. this._enableTeleportationOnController(webVRController);
  186. }
  187. }
  188. if (this.onControllerMeshLoaded) {
  189. this.onControllerMeshLoaded(webVRController);
  190. }
  191. }
  192. private _onFullscreenChange() {
  193. if (document.fullscreen !== undefined) {
  194. this._fullscreenVRpresenting = document.fullscreen;
  195. } else if (document.mozFullScreen !== undefined) {
  196. this._fullscreenVRpresenting = document.mozFullScreen;
  197. } else if (document.webkitIsFullScreen !== undefined) {
  198. this._fullscreenVRpresenting = document.webkitIsFullScreen;
  199. } else if (document.msIsFullScreen !== undefined) {
  200. this._fullscreenVRpresenting = document.msIsFullScreen;
  201. }
  202. if (!this._fullscreenVRpresenting && this._canvas) {
  203. this.exitVR();
  204. if (!this._useCustomVRButton) {
  205. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  206. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  207. }
  208. }
  209. }
  210. private isInVRMode() {
  211. return this._webVRpresenting || this._fullscreenVRpresenting;
  212. }
  213. private onVrDisplayPresentChange() {
  214. var vrDisplay = this._scene.getEngine().getVRDevice();
  215. if (vrDisplay) {
  216. var wasPresenting = this._webVRpresenting;
  217. // A VR display is connected
  218. this._webVRpresenting = vrDisplay.isPresenting;
  219. if (wasPresenting && !this._webVRpresenting)
  220. this.exitVR();
  221. } else {
  222. Tools.Warn('Detected VRDisplayPresentChange on an unknown VRDisplay. Did you can enterVR on the vrExperienceHelper?');
  223. }
  224. this.updateButtonVisibility();
  225. }
  226. private onVRDisplayChanged(eventArgs:IDisplayChangedEventArgs) {
  227. this._webVRsupported = eventArgs.vrSupported;
  228. this._webVRready = !!eventArgs.vrDisplay;
  229. this._webVRpresenting = eventArgs.vrDisplay && eventArgs.vrDisplay.isPresenting;
  230. this.updateButtonVisibility();
  231. }
  232. private updateButtonVisibility() {
  233. if (!this._btnVR) {
  234. return;
  235. }
  236. this._btnVR.className = "babylonVRicon";
  237. if (this.isInVRMode()) {
  238. this._btnVR.className += " vrdisplaypresenting";
  239. } else {
  240. if (this._webVRready) this._btnVR.className += " vrdisplayready";
  241. if (this._webVRsupported) this._btnVR.className += " vrdisplaysupported";
  242. if (this._webVRrequesting) this._btnVR.className += " vrdisplayrequesting";
  243. }
  244. }
  245. /**
  246. * Attempt to enter VR. If a headset is connected and ready, will request present on that.
  247. * Otherwise, will use the fullscreen API.
  248. */
  249. public enterVR() {
  250. if (this._scene.activeCamera) {
  251. this._position = this._scene.activeCamera.position.clone();
  252. }
  253. if (this.onEnteringVR) {
  254. this.onEnteringVR();
  255. }
  256. if (this._webVRrequesting)
  257. return;
  258. // If WebVR is supported and a headset is connected
  259. if (this._webVRready) {
  260. if (!this._webVRpresenting) {
  261. this._webVRCamera.position = this._position;
  262. this._scene.activeCamera = this._webVRCamera;
  263. }
  264. }
  265. else {
  266. this._vrDeviceOrientationCamera.position = this._position;
  267. this._scene.activeCamera = this._vrDeviceOrientationCamera;
  268. this._scene.getEngine().switchFullscreen(true);
  269. this.updateButtonVisibility();
  270. }
  271. if (this._scene.activeCamera && this._canvas) {
  272. this._scene.activeCamera.attachControl(this._canvas);
  273. }
  274. }
  275. /**
  276. * Attempt to exit VR, or fullscreen.
  277. */
  278. public exitVR() {
  279. if (this.onExitingVR) {
  280. this.onExitingVR();
  281. }
  282. if (this._webVRpresenting) {
  283. this._scene.getEngine().disableVR();
  284. }
  285. if (this._scene.activeCamera) {
  286. this._position = this._scene.activeCamera.position.clone();
  287. }
  288. this._deviceOrientationCamera.position = this._position;
  289. this._scene.activeCamera = this._deviceOrientationCamera;
  290. if (this._canvas) {
  291. this._scene.activeCamera.attachControl(this._canvas);
  292. }
  293. this.updateButtonVisibility();
  294. }
  295. public get position(): Vector3 {
  296. return this._position;
  297. }
  298. public set position(value: Vector3) {
  299. this._position = value;
  300. if (this._scene.activeCamera) {
  301. this._scene.activeCamera.position = value;
  302. }
  303. }
  304. public enableTeleportation(vrTeleportationOptions: VRTeleportationOptions = {}) {
  305. this._teleportationRequested = true;
  306. if (vrTeleportationOptions) {
  307. if (vrTeleportationOptions.floorMeshName) {
  308. this._floorMeshName = vrTeleportationOptions.floorMeshName;
  309. }
  310. }
  311. if (this._leftControllerReady && this._webVRCamera.leftController) {
  312. this._enableTeleportationOnController(this._webVRCamera.leftController)
  313. }
  314. if (this._rightControllerReady && this._webVRCamera.rightController) {
  315. this._enableTeleportationOnController(this._webVRCamera.rightController)
  316. }
  317. this._postProcessMove = new BABYLON.ImageProcessingPostProcess("postProcessMove", 1.0, this._webVRCamera);
  318. this._postProcessMove.vignetteWeight = 0;
  319. this._postProcessMove.vignetteStretch = 0;
  320. this._postProcessMove.vignetteColor = new BABYLON.Color4(0, 0, 0, 0);
  321. this._postProcessMove.vignetteEnabled = false;
  322. new BABYLON.PassPostProcess("pass", 1.0, this._webVRCamera);
  323. this._createTeleportationCircles();
  324. this.meshSelectionPredicate = (mesh) => {
  325. if (mesh.name.indexOf(this._floorMeshName) !== -1) {
  326. return true;
  327. }
  328. return false;
  329. }
  330. this._scene.registerBeforeRender(() => {
  331. this._castRayAndSelectObject();
  332. });
  333. }
  334. private _enableTeleportationOnController(webVRController: WebVRController) {
  335. var controllerMesh = webVRController.mesh;
  336. if (controllerMesh) {
  337. var childMeshes = controllerMesh.getChildMeshes();
  338. for (var i = 0; i < childMeshes.length; i++) {
  339. if (childMeshes[i].name === "POINTING_POSE") {
  340. controllerMesh = childMeshes[i];
  341. break;
  342. }
  343. }
  344. var laserPointer = BABYLON.Mesh.CreateCylinder("laserPointer", 3, 0.004, 0.0001, 20, 1, this._scene, false);
  345. var laserPointerMaterial = new BABYLON.StandardMaterial("laserPointerMat", this._scene);
  346. laserPointerMaterial.emissiveColor = new BABYLON.Color3(0.7, 0.7, 0.7);
  347. laserPointerMaterial.alpha = 0.6;
  348. laserPointer.material = laserPointerMaterial;
  349. laserPointer.rotation.x = Math.PI / 2;
  350. laserPointer.parent = controllerMesh;
  351. laserPointer.position.z = -1.5;
  352. laserPointer.position.y = 0;
  353. laserPointer.isVisible = false;
  354. webVRController.onMainButtonStateChangedObservable.add((stateObject) => {
  355. // Enabling / disabling laserPointer
  356. if (stateObject.value === 1) {
  357. laserPointer.isVisible = !laserPointer.isVisible;
  358. }
  359. });
  360. webVRController.onPadValuesChangedObservable.add((stateObject) => {
  361. // on pressed
  362. if (!this._teleportationRequestInitiated) {
  363. if (stateObject.y < -0.6) {
  364. laserPointer.isVisible = true;
  365. this._teleportationRequestInitiated = true;
  366. }
  367. }
  368. else {
  369. if (stateObject.y > -0.4) {
  370. if (this._teleportationAllowed) {
  371. this._teleportCamera();
  372. }
  373. this._teleportationRequestInitiated = false;
  374. laserPointer.isVisible = false;
  375. }
  376. }
  377. if (!this._rotationLeftAsked) {
  378. if (stateObject.x < -0.6) {
  379. this._rotationLeftAsked = true;
  380. if (this._rotationAllowed) {
  381. this._rotateCamera(false);
  382. }
  383. }
  384. }
  385. else {
  386. if (stateObject.x > -0.4) {
  387. this._rotationLeftAsked = false;
  388. }
  389. }
  390. if (!this._rotationRightAsked) {
  391. if (stateObject.x > 0.6) {
  392. this._rotationRightAsked = true;
  393. if (this._rotationAllowed) {
  394. this._rotateCamera(true);
  395. }
  396. }
  397. }
  398. else {
  399. if (stateObject.x < 0.4) {
  400. this._rotationRightAsked = false;
  401. }
  402. }
  403. });
  404. }
  405. }
  406. private _createTeleportationCircles() {
  407. this._teleportationCircle = BABYLON.Mesh.CreateGround("teleportationCircle", 2, 2, 2, this._scene);
  408. var length = 512;
  409. var dynamicTexture = new BABYLON.DynamicTexture("DynamicTexture", length, this._scene, true);
  410. dynamicTexture.hasAlpha = true;
  411. var context = dynamicTexture.getContext();
  412. var centerX = length / 2;
  413. var centerY = length / 2;
  414. var radius = 200;
  415. context.beginPath();
  416. context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  417. context.fillStyle = this._teleportationFillColor;
  418. context.fill();
  419. context.lineWidth = 10;
  420. context.strokeStyle = this._teleportationBorderColor;
  421. context.stroke();
  422. context.closePath();
  423. dynamicTexture.update();
  424. var teleportationCircleMaterial = new BABYLON.StandardMaterial("TextPlaneMaterial", this._scene);
  425. teleportationCircleMaterial.diffuseTexture = dynamicTexture;
  426. this._teleportationCircle.material = teleportationCircleMaterial;
  427. var torus = BABYLON.Mesh.CreateTorus("torus", 0.75, 0.1, 25, this._scene, false);
  428. torus.parent = this._teleportationCircle;
  429. var animationInnerCircle = new BABYLON.Animation("animationInnerCircle", "position.y", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  430. var keys = [];
  431. keys.push({
  432. frame: 0,
  433. value: 0
  434. });
  435. keys.push({
  436. frame: 30,
  437. value: 0.4
  438. });
  439. keys.push({
  440. frame: 60,
  441. value: 0
  442. });
  443. animationInnerCircle.setKeys(keys);
  444. var easingFunction = new BABYLON.SineEase();
  445. easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT);
  446. animationInnerCircle.setEasingFunction(easingFunction);
  447. torus.animations = [];
  448. torus.animations.push(animationInnerCircle);
  449. this._scene.beginAnimation(torus, 0, 60, true);
  450. this._hideTeleportationCircle();
  451. }
  452. private _displayTeleportationCircle() {
  453. this._teleportationCircle.isVisible = true;
  454. (<Mesh>this._teleportationCircle.getChildren()[0]).isVisible = true;
  455. }
  456. private _hideTeleportationCircle() {
  457. this._teleportationCircle.isVisible = false;
  458. (<Mesh>this._teleportationCircle.getChildren()[0]).isVisible = false;
  459. }
  460. private _rotateCamera(right: boolean) {
  461. if (right) {
  462. this._rotationAngle++;
  463. }
  464. else {
  465. this._rotationAngle--;
  466. }
  467. this.currentVRCamera.animations = [];
  468. var target = BABYLON.Quaternion.FromRotationMatrix(BABYLON.Matrix.RotationY(Math.PI / 4 * this._rotationAngle));
  469. var animationRotation = new BABYLON.Animation("animationRotation", "rotationQuaternion", 90, BABYLON.Animation.ANIMATIONTYPE_QUATERNION,
  470. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  471. var animationRotationKeys = [];
  472. animationRotationKeys.push({
  473. frame: 0,
  474. value: this.currentVRCamera.rotationQuaternion
  475. });
  476. animationRotationKeys.push({
  477. frame: 6,
  478. value: target
  479. });
  480. animationRotation.setKeys(animationRotationKeys);
  481. var easingFunction = new BABYLON.CircleEase();
  482. easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT);
  483. animationRotation.setEasingFunction(easingFunction);
  484. this.currentVRCamera.animations.push(animationRotation);
  485. (<any>this._postProcessMove).animations = [];
  486. var animationPP = new BABYLON.Animation("animationPP", "vignetteWeight", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  487. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  488. var vignetteWeightKeys = [];
  489. vignetteWeightKeys.push({
  490. frame: 0,
  491. value: 0
  492. });
  493. vignetteWeightKeys.push({
  494. frame: 3,
  495. value: 4
  496. });
  497. vignetteWeightKeys.push({
  498. frame: 6,
  499. value: 0
  500. });
  501. animationPP.setKeys(vignetteWeightKeys);
  502. animationPP.setEasingFunction(easingFunction);
  503. (<any>this._postProcessMove).animations.push(animationPP);
  504. var animationPP2 = new BABYLON.Animation("animationPP2", "vignetteStretch", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  505. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  506. var vignetteStretchKeys = [];
  507. vignetteStretchKeys.push({
  508. frame: 0,
  509. value: 0
  510. });
  511. vignetteStretchKeys.push({
  512. frame: 3,
  513. value: 10
  514. });
  515. vignetteStretchKeys.push({
  516. frame: 6,
  517. value: 0
  518. });
  519. animationPP2.setKeys(vignetteStretchKeys);
  520. animationPP2.setEasingFunction(easingFunction);
  521. (<any>this._postProcessMove).animations.push(animationPP2);
  522. this._postProcessMove.vignetteWeight = 0;
  523. this._postProcessMove.vignetteStretch = 0;
  524. this._postProcessMove.vignetteEnabled = true;
  525. this._scene.beginAnimation(this._postProcessMove, 0, 6, false, 1, () => {
  526. this._postProcessMove.vignetteEnabled = false;
  527. });
  528. this._scene.beginAnimation(this.currentVRCamera, 0, 6, false, 1);
  529. }
  530. private _moveTeleportationSelectorTo(coordinates: Vector3) {
  531. this._teleportationAllowed = true;
  532. if (this._teleportationRequestInitiated || this._xboxGamepadTeleportationRequestInitiated) {
  533. this._displayTeleportationCircle();
  534. }
  535. else {
  536. this._hideTeleportationCircle();
  537. }
  538. this._haloCenter.copyFrom(coordinates);
  539. this._teleportationCircle.position = coordinates;
  540. this._teleportationCircle.position.y += 0.001;
  541. }
  542. private _teleportCamera() {
  543. this.currentVRCamera.animations = [];
  544. var animationCameraTeleportation = new BABYLON.Animation("animationCameraTeleportation", "position.x", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  545. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  546. var animationCameraTeleportationKeys = [];
  547. animationCameraTeleportationKeys.push({
  548. frame: 0,
  549. value: this.currentVRCamera.position.x
  550. });
  551. animationCameraTeleportationKeys.push({
  552. frame: 11,
  553. value: this._haloCenter.x
  554. });
  555. animationCameraTeleportation.setKeys(animationCameraTeleportationKeys);
  556. var easingFunction = new BABYLON.CircleEase();
  557. easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT);
  558. animationCameraTeleportation.setEasingFunction(easingFunction);
  559. this.currentVRCamera.animations.push(animationCameraTeleportation);
  560. var animationZoomIn2 = new BABYLON.Animation("animationZoomIn", "position.z", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  561. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  562. var keys2 = [];
  563. keys2.push({
  564. frame: 0,
  565. value: this.currentVRCamera.position.z
  566. });
  567. keys2.push({
  568. frame: 11,
  569. value: this._haloCenter.z
  570. });
  571. animationZoomIn2.setKeys(keys2);
  572. animationZoomIn2.setEasingFunction(easingFunction);
  573. this.currentVRCamera.animations.push(animationZoomIn2);
  574. (<any>this._postProcessMove).animations = [];
  575. var animationPP = new BABYLON.Animation("animationPP", "vignetteWeight", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  576. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  577. var vignetteWeightKeys = [];
  578. vignetteWeightKeys.push({
  579. frame: 0,
  580. value: 0
  581. });
  582. vignetteWeightKeys.push({
  583. frame: 5,
  584. value: 8
  585. });
  586. vignetteWeightKeys.push({
  587. frame: 11,
  588. value: 0
  589. });
  590. animationPP.setKeys(vignetteWeightKeys);
  591. (<any>this._postProcessMove).animations.push(animationPP);
  592. var animationPP2 = new BABYLON.Animation("animationPP2", "vignetteStretch", 90, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  593. BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
  594. var vignetteStretchKeys = [];
  595. vignetteStretchKeys.push({
  596. frame: 0,
  597. value: 0
  598. });
  599. vignetteStretchKeys.push({
  600. frame: 5,
  601. value: 10
  602. });
  603. vignetteStretchKeys.push({
  604. frame: 11,
  605. value: 0
  606. });
  607. animationPP2.setKeys(vignetteStretchKeys);
  608. (<any>this._postProcessMove).animations.push(animationPP2);
  609. this._postProcessMove.vignetteWeight = 8;
  610. this._postProcessMove.vignetteStretch = 10;
  611. this._postProcessMove.vignetteEnabled = true;
  612. this._scene.beginAnimation(this._postProcessMove, 0, 11, false, 1, () => {
  613. this._postProcessMove.vignetteEnabled = false;
  614. });
  615. this._scene.beginAnimation(this.currentVRCamera, 0, 11, false, 1);
  616. }
  617. private _castRayAndSelectObject () {
  618. var ray;
  619. if (!(<WebVRFreeCamera>this.currentVRCamera).rightController) {
  620. ray = this.currentVRCamera.getForwardRay();
  621. } else {
  622. ray = (<any>this.currentVRCamera).rightController.getForwardRay();
  623. }
  624. var hit = this._scene.pickWithRay(ray, this.meshSelectionPredicate);
  625. if (this._rayHelper) {
  626. this._rayHelper.dispose();
  627. }
  628. if ((<WebVRFreeCamera>this.currentVRCamera).rightController) {
  629. //if (target) target.isVisible = false;
  630. this._rayHelper = BABYLON.RayHelper.CreateAndShow(ray, this._scene, new BABYLON.Color3(0.7, 0.7, 0.7));
  631. }
  632. if (hit && hit.pickedMesh) {
  633. // The object selected is the floor, we're in a teleportation scenario
  634. if (hit.pickedMesh.name.indexOf(this._floorMeshName) !== -1 && hit.pickedPoint) {
  635. this._moveTeleportationSelectorTo(hit.pickedPoint)
  636. return;
  637. }
  638. // If not, we're in a selection scenario
  639. this._hideTeleportationCircle();
  640. this._teleportationAllowed = false;
  641. //currentMeshSelected = hit.pickedMesh;
  642. }
  643. else {
  644. this._teleportationAllowed = false;
  645. this._hideTeleportationCircle();
  646. }
  647. }
  648. public dispose() {
  649. if (this.isInVRMode()) {
  650. this.exitVR();
  651. }
  652. this._deviceOrientationCamera.dispose();
  653. if (this._webVRCamera) {
  654. this._webVRCamera.dispose();
  655. }
  656. if (this._vrDeviceOrientationCamera) {
  657. this._vrDeviceOrientationCamera.dispose();
  658. }
  659. if (!this._useCustomVRButton) {
  660. document.body.removeChild(this._btnVR);
  661. }
  662. document.removeEventListener("keydown", this._onKeyDown);
  663. window.removeEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  664. }
  665. public getClassName(): string {
  666. return "VRExperienceHelper";
  667. }
  668. }
  669. }