engine.webVR.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { Nullable } from "../../types";
  2. import { Engine, IDisplayChangedEventArgs } from "../../Engines/engine";
  3. import { _TimeToken } from "../../Instrumentation/timeToken";
  4. import { Size } from '../../Maths/math.size';
  5. import { Observable } from '../../Misc/observable';
  6. import { Tools } from '../../Misc/tools';
  7. import { DomManagement } from '../../Misc/domManagement';
  8. declare module "../../Engines/engine" {
  9. export interface Engine {
  10. /** @hidden */
  11. _vrDisplay: any;
  12. /** @hidden */
  13. _vrSupported: boolean;
  14. /** @hidden */
  15. _oldSize: Size;
  16. /** @hidden */
  17. _oldHardwareScaleFactor: number;
  18. /** @hidden */
  19. _vrExclusivePointerMode: boolean;
  20. /** @hidden */
  21. _webVRInitPromise: Promise<IDisplayChangedEventArgs>;
  22. /** @hidden */
  23. _onVRDisplayPointerRestricted: () => void;
  24. /** @hidden */
  25. _onVRDisplayPointerUnrestricted: () => void;
  26. /** @hidden */
  27. _onVrDisplayConnect: Nullable<(display: any) => void>;
  28. /** @hidden */
  29. _onVrDisplayDisconnect: Nullable<() => void>;
  30. /** @hidden */
  31. _onVrDisplayPresentChange: Nullable<() => void>;
  32. /**
  33. * Observable signaled when VR display mode changes
  34. */
  35. onVRDisplayChangedObservable: Observable<IDisplayChangedEventArgs>;
  36. /**
  37. * Observable signaled when VR request present is complete
  38. */
  39. onVRRequestPresentComplete: Observable<boolean>;
  40. /**
  41. * Observable signaled when VR request present starts
  42. */
  43. onVRRequestPresentStart: Observable<Engine>;
  44. /**
  45. * Gets a boolean indicating that the engine is currently in VR exclusive mode for the pointers
  46. * @see https://docs.microsoft.com/en-us/microsoft-edge/webvr/essentials#mouse-input
  47. */
  48. isInVRExclusivePointerMode: boolean;
  49. /**
  50. * Gets a boolean indicating if a webVR device was detected
  51. * @returns true if a webVR device was detected
  52. */
  53. isVRDevicePresent(): boolean;
  54. /**
  55. * Gets the current webVR device
  56. * @returns the current webVR device (or null)
  57. */
  58. getVRDevice(): any;
  59. /**
  60. * Initializes a webVR display and starts listening to display change events
  61. * The onVRDisplayChangedObservable will be notified upon these changes
  62. * @returns A promise containing a VRDisplay and if vr is supported
  63. */
  64. initWebVRAsync(): Promise<IDisplayChangedEventArgs>;
  65. /** @hidden */
  66. _getVRDisplaysAsync(): Promise<IDisplayChangedEventArgs>;
  67. /**
  68. * Call this function to switch to webVR mode
  69. * Will do nothing if webVR is not supported or if there is no webVR device
  70. * @see http://doc.babylonjs.com/how_to/webvr_camera
  71. */
  72. enableVR(): void;
  73. /** @hidden */
  74. _onVRFullScreenTriggered(): void;
  75. }
  76. }
  77. Object.defineProperty(Engine.prototype, "isInVRExclusivePointerMode", {
  78. get: function(this: Engine) {
  79. return this._vrExclusivePointerMode;
  80. },
  81. enumerable: true,
  82. configurable: true
  83. });
  84. Engine.prototype._prepareVRComponent = function() {
  85. this._vrSupported = false;
  86. this._vrExclusivePointerMode = false;
  87. this.onVRDisplayChangedObservable = new Observable<IDisplayChangedEventArgs>();
  88. this.onVRRequestPresentComplete = new Observable<boolean>();
  89. this.onVRRequestPresentStart = new Observable<Engine>();
  90. };
  91. Engine.prototype.isVRDevicePresent = function() {
  92. return !!this._vrDisplay;
  93. };
  94. Engine.prototype.getVRDevice = function(): any {
  95. return this._vrDisplay;
  96. };
  97. Engine.prototype.initWebVR = function(): Observable<IDisplayChangedEventArgs> {
  98. this.initWebVRAsync();
  99. return this.onVRDisplayChangedObservable;
  100. };
  101. Engine.prototype.initWebVRAsync = function(): Promise<IDisplayChangedEventArgs> {
  102. var notifyObservers = () => {
  103. var eventArgs = {
  104. vrDisplay: this._vrDisplay,
  105. vrSupported: this._vrSupported
  106. };
  107. this.onVRDisplayChangedObservable.notifyObservers(eventArgs);
  108. this._webVRInitPromise = new Promise((res) => { res(eventArgs); });
  109. };
  110. if (!this._onVrDisplayConnect) {
  111. this._onVrDisplayConnect = (event) => {
  112. this._vrDisplay = event.display;
  113. notifyObservers();
  114. };
  115. this._onVrDisplayDisconnect = () => {
  116. this._vrDisplay.cancelAnimationFrame(this._frameHandler);
  117. this._vrDisplay = undefined;
  118. this._frameHandler = Engine.QueueNewFrame(this._bindedRenderFunction);
  119. notifyObservers();
  120. };
  121. this._onVrDisplayPresentChange = () => {
  122. this._vrExclusivePointerMode = this._vrDisplay && this._vrDisplay.isPresenting;
  123. };
  124. let hostWindow = this.getHostWindow();
  125. if (hostWindow) {
  126. hostWindow.addEventListener('vrdisplayconnect', this._onVrDisplayConnect);
  127. hostWindow.addEventListener('vrdisplaydisconnect', this._onVrDisplayDisconnect);
  128. hostWindow.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  129. }
  130. }
  131. this._webVRInitPromise = this._webVRInitPromise || this._getVRDisplaysAsync();
  132. this._webVRInitPromise.then(notifyObservers);
  133. return this._webVRInitPromise;
  134. };
  135. Engine.prototype._getVRDisplaysAsync = function(): Promise<IDisplayChangedEventArgs> {
  136. return new Promise((res) => {
  137. if (navigator.getVRDisplays) {
  138. navigator.getVRDisplays().then((devices: Array<any>) => {
  139. this._vrSupported = true;
  140. // note that devices may actually be an empty array. This is fine;
  141. // we expect this._vrDisplay to be undefined in this case.
  142. this._vrDisplay = devices[0];
  143. res({
  144. vrDisplay: this._vrDisplay,
  145. vrSupported: this._vrSupported
  146. });
  147. });
  148. } else {
  149. this._vrDisplay = undefined;
  150. this._vrSupported = false;
  151. res({
  152. vrDisplay: this._vrDisplay,
  153. vrSupported: this._vrSupported
  154. });
  155. }
  156. });
  157. };
  158. Engine.prototype.enableVR = function() {
  159. if (this._vrDisplay && !this._vrDisplay.isPresenting) {
  160. var onResolved = () => {
  161. this.onVRRequestPresentComplete.notifyObservers(true);
  162. this._onVRFullScreenTriggered();
  163. };
  164. var onRejected = () => {
  165. this.onVRRequestPresentComplete.notifyObservers(false);
  166. };
  167. this.onVRRequestPresentStart.notifyObservers(this);
  168. this._vrDisplay.requestPresent([{ source: this.getRenderingCanvas() }]).then(onResolved).catch(onRejected);
  169. }
  170. };
  171. Engine.prototype._onVRFullScreenTriggered = function() {
  172. if (this._vrDisplay && this._vrDisplay.isPresenting) {
  173. //get the old size before we change
  174. this._oldSize = new Size(this.getRenderWidth(), this.getRenderHeight());
  175. this._oldHardwareScaleFactor = this.getHardwareScalingLevel();
  176. //get the width and height, change the render size
  177. var leftEye = this._vrDisplay.getEyeParameters('left');
  178. this.setHardwareScalingLevel(1);
  179. this.setSize(leftEye.renderWidth * 2, leftEye.renderHeight);
  180. } else {
  181. this.setHardwareScalingLevel(this._oldHardwareScaleFactor);
  182. this.setSize(this._oldSize.width, this._oldSize.height);
  183. }
  184. };
  185. Engine.prototype.disableVR = function() {
  186. if (this._vrDisplay && this._vrDisplay.isPresenting) {
  187. this._vrDisplay.exitPresent()
  188. .then(() => this._onVRFullScreenTriggered())
  189. .catch(() => this._onVRFullScreenTriggered());
  190. }
  191. if (DomManagement.IsWindowObjectExist()) {
  192. window.removeEventListener('vrdisplaypointerrestricted', this._onVRDisplayPointerRestricted);
  193. window.removeEventListener('vrdisplaypointerunrestricted', this._onVRDisplayPointerUnrestricted);
  194. if (this._onVrDisplayConnect) {
  195. window.removeEventListener('vrdisplayconnect', this._onVrDisplayConnect);
  196. if (this._onVrDisplayDisconnect) {
  197. window.removeEventListener('vrdisplaydisconnect', this._onVrDisplayDisconnect);
  198. }
  199. if (this._onVrDisplayPresentChange) {
  200. window.removeEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  201. }
  202. this._onVrDisplayConnect = null;
  203. this._onVrDisplayDisconnect = null;
  204. }
  205. }
  206. };
  207. Engine.prototype._connectVREvents = function(canvas?: HTMLCanvasElement, document?: any) {
  208. this._onVRDisplayPointerRestricted = () => {
  209. if (canvas) {
  210. canvas.requestPointerLock();
  211. }
  212. };
  213. this._onVRDisplayPointerUnrestricted = () => {
  214. if (!document.exitPointerLock) {
  215. return;
  216. }
  217. document.exitPointerLock();
  218. };
  219. if (DomManagement.IsWindowObjectExist()) {
  220. let hostWindow = this.getHostWindow()!;
  221. hostWindow.addEventListener('vrdisplaypointerrestricted', this._onVRDisplayPointerRestricted, false);
  222. hostWindow.addEventListener('vrdisplaypointerunrestricted', this._onVRDisplayPointerUnrestricted, false);
  223. }
  224. };
  225. Engine.prototype._submitVRFrame = function() {
  226. // Submit frame to the vr device, if enabled
  227. if (this._vrDisplay && this._vrDisplay.isPresenting) {
  228. // TODO: We should only submit the frame if we read frameData successfully.
  229. try {
  230. this._vrDisplay.submitFrame();
  231. } catch (e) {
  232. Tools.Warn("webVR submitFrame has had an unexpected failure: " + e);
  233. }
  234. }
  235. };
  236. Engine.prototype.isVRPresenting = function() {
  237. return this._vrDisplay && this._vrDisplay.isPresenting;
  238. };
  239. Engine.prototype._requestVRFrame = function() {
  240. this._frameHandler = Engine.QueueNewFrame(this._bindedRenderFunction, this._vrDisplay);
  241. };