babylon.vrExperienceHelper.ts 73 KB

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