vrExperienceHelper.ts 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. import { Logger } from "../../Misc/logger";
  2. import { Observer, Observable } from "../../Misc/observable";
  3. import { Nullable } from "../../types";
  4. import { Camera } from "../../Cameras/camera";
  5. import { FreeCamera } from "../../Cameras/freeCamera";
  6. import { TargetCamera } from "../../Cameras/targetCamera";
  7. import { DeviceOrientationCamera } from "../../Cameras/deviceOrientationCamera";
  8. import { VRDeviceOrientationFreeCamera } from "../../Cameras/VR/vrDeviceOrientationFreeCamera";
  9. import { WebVROptions, WebVRFreeCamera } from "../../Cameras/VR/webVRCamera";
  10. import { PointerEventTypes } from "../../Events/pointerEvents";
  11. import { Scene, IDisposable } from "../../scene";
  12. import { Quaternion, Matrix, Vector3 } from "../../Maths/math.vector";
  13. import { Color3, Color4 } from '../../Maths/math.color';
  14. import { Gamepad, StickValues } from "../../Gamepads/gamepad";
  15. import { PoseEnabledController, PoseEnabledControllerType } from "../../Gamepads/Controllers/poseEnabledController";
  16. import { WebVRController } from "../../Gamepads/Controllers/webVRController";
  17. import { Xbox360Pad, Xbox360Button } from "../../Gamepads/xboxGamepad";
  18. import { IDisplayChangedEventArgs } from "../../Engines/engine";
  19. import { AbstractMesh } from "../../Meshes/abstractMesh";
  20. import { TransformNode } from "../../Meshes/transformNode";
  21. import { Mesh } from "../../Meshes/mesh";
  22. import { PickingInfo } from "../../Collisions/pickingInfo";
  23. import { Ray } from "../../Culling/ray";
  24. import { ImageProcessingConfiguration } from "../../Materials/imageProcessingConfiguration";
  25. import { StandardMaterial } from "../../Materials/standardMaterial";
  26. import { DynamicTexture } from "../../Materials/Textures/dynamicTexture";
  27. import { ImageProcessingPostProcess } from "../../PostProcesses/imageProcessingPostProcess";
  28. import { SineEase, EasingFunction, CircleEase } from "../../Animations/easing";
  29. import { Animation } from "../../Animations/animation";
  30. import { VRCameraMetrics } from '../../Cameras/VR/vrCameraMetrics';
  31. import "../../Meshes/Builders/groundBuilder";
  32. import "../../Meshes/Builders/torusBuilder";
  33. import "../../Meshes/Builders/cylinderBuilder";
  34. import "../../Gamepads/gamepadSceneComponent";
  35. import "../../Animations/animatable";
  36. import { Axis } from '../../Maths/math.axis';
  37. /**
  38. * Options to modify the vr teleportation behavior.
  39. */
  40. export interface VRTeleportationOptions {
  41. /**
  42. * The name of the mesh which should be used as the teleportation floor. (default: null)
  43. */
  44. floorMeshName?: string;
  45. /**
  46. * A list of meshes to be used as the teleportation floor. (default: empty)
  47. */
  48. floorMeshes?: Mesh[];
  49. }
  50. /**
  51. * Options to modify the vr experience helper's behavior.
  52. */
  53. export interface VRExperienceHelperOptions extends WebVROptions {
  54. /**
  55. * Create a DeviceOrientationCamera to be used as your out of vr camera. (default: true)
  56. */
  57. createDeviceOrientationCamera?: boolean;
  58. /**
  59. * Create a VRDeviceOrientationFreeCamera to be used for VR when no external HMD is found. (default: true)
  60. */
  61. createFallbackVRDeviceOrientationFreeCamera?: boolean;
  62. /**
  63. * Uses the main button on the controller to toggle the laser casted. (default: true)
  64. */
  65. laserToggle?: boolean;
  66. /**
  67. * A list of meshes to be used as the teleportation floor. If specified, teleportation will be enabled (default: undefined)
  68. */
  69. floorMeshes?: Mesh[];
  70. /**
  71. * Distortion metrics for the fallback vrDeviceOrientationCamera (default: VRCameraMetrics.Default)
  72. */
  73. vrDeviceOrientationCameraMetrics?: VRCameraMetrics;
  74. }
  75. class VRExperienceHelperGazer implements IDisposable {
  76. /** @hidden */
  77. public _gazeTracker: Mesh;
  78. /** @hidden */
  79. public _currentMeshSelected: Nullable<AbstractMesh>;
  80. /** @hidden */
  81. public _currentHit: Nullable<PickingInfo>;
  82. public static _idCounter = 0;
  83. /** @hidden */
  84. public _id: number;
  85. /** @hidden */
  86. public _pointerDownOnMeshAsked: boolean = false;
  87. /** @hidden */
  88. public _isActionableMesh: boolean = false;
  89. /** @hidden */
  90. public _interactionsEnabled: boolean;
  91. /** @hidden */
  92. public _teleportationEnabled: boolean;
  93. /** @hidden */
  94. public _teleportationRequestInitiated = false;
  95. /** @hidden */
  96. public _teleportationBackRequestInitiated = false;
  97. /** @hidden */
  98. public _rotationRightAsked = false;
  99. /** @hidden */
  100. public _rotationLeftAsked = false;
  101. /** @hidden */
  102. public _dpadPressed = true;
  103. /** @hidden */
  104. public _activePointer = false;
  105. constructor(public scene: Scene, gazeTrackerToClone: Nullable<Mesh> = null) {
  106. this._id = VRExperienceHelperGazer._idCounter++;
  107. // Gaze tracker
  108. if (!gazeTrackerToClone) {
  109. this._gazeTracker = Mesh.CreateTorus("gazeTracker", 0.0035, 0.0025, 20, scene, false);
  110. this._gazeTracker.bakeCurrentTransformIntoVertices();
  111. this._gazeTracker.isPickable = false;
  112. this._gazeTracker.isVisible = false;
  113. var targetMat = new StandardMaterial("targetMat", scene);
  114. targetMat.specularColor = Color3.Black();
  115. targetMat.emissiveColor = new Color3(0.7, 0.7, 0.7);
  116. targetMat.backFaceCulling = false;
  117. this._gazeTracker.material = targetMat;
  118. } else {
  119. this._gazeTracker = gazeTrackerToClone.clone("gazeTracker");
  120. }
  121. }
  122. /** @hidden */
  123. public _getForwardRay(length: number): Ray {
  124. return new Ray(Vector3.Zero(), new Vector3(0, 0, length));
  125. }
  126. /** @hidden */
  127. public _selectionPointerDown() {
  128. this._pointerDownOnMeshAsked = true;
  129. if (this._currentHit) {
  130. this.scene.simulatePointerDown(this._currentHit, { pointerId: this._id });
  131. }
  132. }
  133. /** @hidden */
  134. public _selectionPointerUp() {
  135. if (this._currentHit) {
  136. this.scene.simulatePointerUp(this._currentHit, { pointerId: this._id });
  137. }
  138. this._pointerDownOnMeshAsked = false;
  139. }
  140. /** @hidden */
  141. public _activatePointer() {
  142. this._activePointer = true;
  143. }
  144. /** @hidden */
  145. public _deactivatePointer() {
  146. this._activePointer = false;
  147. }
  148. /** @hidden */
  149. public _updatePointerDistance(distance: number = 100) {
  150. }
  151. public dispose() {
  152. this._interactionsEnabled = false;
  153. this._teleportationEnabled = false;
  154. if (this._gazeTracker) {
  155. this._gazeTracker.dispose();
  156. }
  157. }
  158. }
  159. class VRExperienceHelperControllerGazer extends VRExperienceHelperGazer {
  160. private _laserPointer: Mesh;
  161. private _meshAttachedObserver: Nullable<Observer<AbstractMesh>>;
  162. constructor(public webVRController: WebVRController, scene: Scene, gazeTrackerToClone: Mesh) {
  163. super(scene, gazeTrackerToClone);
  164. // Laser pointer
  165. this._laserPointer = Mesh.CreateCylinder("laserPointer", 1, 0.004, 0.0002, 20, 1, scene, false);
  166. var laserPointerMaterial = new StandardMaterial("laserPointerMat", scene);
  167. laserPointerMaterial.emissiveColor = new Color3(0.7, 0.7, 0.7);
  168. laserPointerMaterial.alpha = 0.6;
  169. this._laserPointer.material = laserPointerMaterial;
  170. this._laserPointer.rotation.x = Math.PI / 2;
  171. this._laserPointer.position.z = -0.5;
  172. this._laserPointer.isVisible = false;
  173. this._laserPointer.isPickable = false;
  174. if (!webVRController.mesh) {
  175. // Create an empty mesh that is used prior to loading the high quality model
  176. var preloadMesh = new Mesh("preloadControllerMesh", scene);
  177. var preloadPointerPose = new Mesh(PoseEnabledController.POINTING_POSE, scene);
  178. preloadPointerPose.rotation.x = -0.7;
  179. preloadMesh.addChild(preloadPointerPose);
  180. webVRController.attachToMesh(preloadMesh);
  181. }
  182. this._setLaserPointerParent(webVRController.mesh!);
  183. this._meshAttachedObserver = webVRController._meshAttachedObservable.add((mesh) => {
  184. this._setLaserPointerParent(mesh);
  185. });
  186. }
  187. _getForwardRay(length: number): Ray {
  188. return this.webVRController.getForwardRay(length);
  189. }
  190. /** @hidden */
  191. public _activatePointer() {
  192. super._activatePointer();
  193. this._laserPointer.isVisible = true;
  194. }
  195. /** @hidden */
  196. public _deactivatePointer() {
  197. super._deactivatePointer();
  198. this._laserPointer.isVisible = false;
  199. }
  200. /** @hidden */
  201. public _setLaserPointerColor(color: Color3) {
  202. (<StandardMaterial>this._laserPointer.material).emissiveColor = color;
  203. }
  204. /** @hidden */
  205. public _setLaserPointerParent(mesh: AbstractMesh) {
  206. var makeNotPick = (root: AbstractMesh) => {
  207. root.isPickable = false;
  208. root.getChildMeshes().forEach((c) => {
  209. makeNotPick(c);
  210. });
  211. };
  212. makeNotPick(mesh);
  213. var meshChildren = mesh.getChildren(undefined, false);
  214. let laserParent: TransformNode = mesh;
  215. this.webVRController._pointingPoseNode = null;
  216. for (var i = 0; i < meshChildren.length; i++) {
  217. if (meshChildren[i].name && meshChildren[i].name.indexOf(PoseEnabledController.POINTING_POSE) >= 0) {
  218. laserParent = <TransformNode>meshChildren[i];
  219. this.webVRController._pointingPoseNode = laserParent;
  220. break;
  221. }
  222. }
  223. this._laserPointer.parent = laserParent;
  224. }
  225. public _updatePointerDistance(distance: number = 100) {
  226. this._laserPointer.scaling.y = distance;
  227. this._laserPointer.position.z = -distance / 2;
  228. }
  229. dispose() {
  230. super.dispose();
  231. this._laserPointer.dispose();
  232. if (this._meshAttachedObserver) {
  233. this.webVRController._meshAttachedObservable.remove(this._meshAttachedObserver);
  234. }
  235. }
  236. }
  237. class VRExperienceHelperCameraGazer extends VRExperienceHelperGazer {
  238. constructor(private getCamera: () => Nullable<Camera>, scene: Scene) {
  239. super(scene);
  240. }
  241. _getForwardRay(length: number): Ray {
  242. var camera = this.getCamera();
  243. if (camera) {
  244. return camera.getForwardRay(length);
  245. } else {
  246. return new Ray(Vector3.Zero(), Vector3.Forward());
  247. }
  248. }
  249. }
  250. /**
  251. * Event containing information after VR has been entered
  252. */
  253. export class OnAfterEnteringVRObservableEvent {
  254. /**
  255. * If entering vr was successful
  256. */
  257. public success: boolean;
  258. }
  259. /**
  260. * Helps to quickly add VR support to an existing scene.
  261. * See http://doc.babylonjs.com/how_to/webvr_helper
  262. */
  263. export class VRExperienceHelper {
  264. private _scene: Scene;
  265. private _position: Vector3;
  266. private _btnVR: Nullable<HTMLButtonElement>;
  267. private _btnVRDisplayed: boolean;
  268. // Can the system support WebVR, even if a headset isn't plugged in?
  269. private _webVRsupported = false;
  270. // If WebVR is supported, is a headset plugged in and are we ready to present?
  271. private _webVRready = false;
  272. // Are we waiting for the requestPresent callback to complete?
  273. private _webVRrequesting = false;
  274. // Are we presenting to the headset right now? (this is the vrDevice state)
  275. private _webVRpresenting = false;
  276. // Have we entered VR? (this is the VRExperienceHelper state)
  277. private _hasEnteredVR: boolean;
  278. // Are we presenting in the fullscreen fallback?
  279. private _fullscreenVRpresenting = false;
  280. private _canvas: Nullable<HTMLCanvasElement>;
  281. private _webVRCamera: WebVRFreeCamera;
  282. private _vrDeviceOrientationCamera: Nullable<VRDeviceOrientationFreeCamera>;
  283. private _deviceOrientationCamera: Nullable<DeviceOrientationCamera>;
  284. private _existingCamera: Camera;
  285. private _onKeyDown: (event: KeyboardEvent) => void;
  286. private _onVrDisplayPresentChange: any;
  287. private _onVRDisplayChanged: (eventArgs: IDisplayChangedEventArgs) => void;
  288. private _onVRRequestPresentStart: () => void;
  289. private _onVRRequestPresentComplete: (success: boolean) => void;
  290. /**
  291. * Gets or sets a boolean indicating that the VREXperienceHelper will exit VR if double tap is detected
  292. */
  293. public exitVROnDoubleTap = true;
  294. /**
  295. * Observable raised right before entering VR.
  296. */
  297. public onEnteringVRObservable = new Observable<VRExperienceHelper>();
  298. /**
  299. * Observable raised when entering VR has completed.
  300. */
  301. public onAfterEnteringVRObservable = new Observable<OnAfterEnteringVRObservableEvent>();
  302. /**
  303. * Observable raised when exiting VR.
  304. */
  305. public onExitingVRObservable = new Observable<VRExperienceHelper>();
  306. /**
  307. * Observable raised when controller mesh is loaded.
  308. */
  309. public onControllerMeshLoadedObservable = new Observable<WebVRController>();
  310. /** Return this.onEnteringVRObservable
  311. * Note: This one is for backward compatibility. Please use onEnteringVRObservable directly
  312. */
  313. public get onEnteringVR(): Observable<VRExperienceHelper> {
  314. return this.onEnteringVRObservable;
  315. }
  316. /** Return this.onExitingVRObservable
  317. * Note: This one is for backward compatibility. Please use onExitingVRObservable directly
  318. */
  319. public get onExitingVR(): Observable<VRExperienceHelper> {
  320. return this.onExitingVRObservable;
  321. }
  322. /** Return this.onControllerMeshLoadedObservable
  323. * Note: This one is for backward compatibility. Please use onControllerMeshLoadedObservable directly
  324. */
  325. public get onControllerMeshLoaded(): Observable<WebVRController> {
  326. return this.onControllerMeshLoadedObservable;
  327. }
  328. private _rayLength: number;
  329. private _useCustomVRButton: boolean = false;
  330. private _teleportationRequested: boolean = false;
  331. private _teleportActive = false;
  332. private _floorMeshName: string;
  333. private _floorMeshesCollection: Mesh[] = [];
  334. private _rotationAllowed: boolean = true;
  335. private _teleportBackwardsVector = new Vector3(0, -1, -1);
  336. private _teleportationTarget: Mesh;
  337. private _isDefaultTeleportationTarget = true;
  338. private _postProcessMove: ImageProcessingPostProcess;
  339. private _teleportationFillColor: string = "#444444";
  340. private _teleportationBorderColor: string = "#FFFFFF";
  341. private _rotationAngle: number = 0;
  342. private _haloCenter = new Vector3(0, 0, 0);
  343. private _cameraGazer: VRExperienceHelperCameraGazer;
  344. private _padSensibilityUp = 0.65;
  345. private _padSensibilityDown = 0.35;
  346. private _leftController: Nullable<VRExperienceHelperControllerGazer> = null;
  347. private _rightController: Nullable<VRExperienceHelperControllerGazer> = null;
  348. /**
  349. * Observable raised when a new mesh is selected based on meshSelectionPredicate
  350. */
  351. public onNewMeshSelected = new Observable<AbstractMesh>();
  352. /**
  353. * Observable raised when a new mesh is picked based on meshSelectionPredicate
  354. */
  355. public onNewMeshPicked = new Observable<PickingInfo>();
  356. private _circleEase: CircleEase;
  357. /**
  358. * Observable raised before camera teleportation
  359. */
  360. public onBeforeCameraTeleport = new Observable<Vector3>();
  361. /**
  362. * Observable raised after camera teleportation
  363. */
  364. public onAfterCameraTeleport = new Observable<Vector3>();
  365. /**
  366. * Observable raised when current selected mesh gets unselected
  367. */
  368. public onSelectedMeshUnselected = new Observable<AbstractMesh>();
  369. private _raySelectionPredicate: (mesh: AbstractMesh) => boolean;
  370. /**
  371. * To be optionaly changed by user to define custom ray selection
  372. */
  373. public raySelectionPredicate: (mesh: AbstractMesh) => boolean;
  374. /**
  375. * To be optionaly changed by user to define custom selection logic (after ray selection)
  376. */
  377. public meshSelectionPredicate: (mesh: AbstractMesh) => boolean;
  378. /**
  379. * Set teleportation enabled. If set to false camera teleportation will be disabled but camera rotation will be kept.
  380. */
  381. public teleportationEnabled: boolean = true;
  382. private _defaultHeight: number;
  383. private _teleportationInitialized = false;
  384. private _interactionsEnabled = false;
  385. private _interactionsRequested = false;
  386. private _displayGaze = true;
  387. private _displayLaserPointer = true;
  388. /**
  389. * The mesh used to display where the user is going to teleport.
  390. */
  391. public get teleportationTarget(): Mesh {
  392. return this._teleportationTarget;
  393. }
  394. /**
  395. * Sets the mesh to be used to display where the user is going to teleport.
  396. */
  397. public set teleportationTarget(value: Mesh) {
  398. if (value) {
  399. value.name = "teleportationTarget";
  400. this._isDefaultTeleportationTarget = false;
  401. this._teleportationTarget = value;
  402. }
  403. }
  404. /**
  405. * The mesh used to display where the user is selecting, this mesh will be cloned and set as the gazeTracker for the left and right controller
  406. * when set bakeCurrentTransformIntoVertices will be called on the mesh.
  407. * See http://doc.babylonjs.com/resources/baking_transformations
  408. */
  409. public get gazeTrackerMesh(): Mesh {
  410. return this._cameraGazer._gazeTracker;
  411. }
  412. public set gazeTrackerMesh(value: Mesh) {
  413. if (value) {
  414. // Dispose of existing meshes
  415. if (this._cameraGazer._gazeTracker) {
  416. this._cameraGazer._gazeTracker.dispose();
  417. }
  418. if (this._leftController && this._leftController._gazeTracker) {
  419. this._leftController._gazeTracker.dispose();
  420. }
  421. if (this._rightController && this._rightController._gazeTracker) {
  422. this._rightController._gazeTracker.dispose();
  423. }
  424. // Set and create gaze trackers on head and controllers
  425. this._cameraGazer._gazeTracker = value;
  426. this._cameraGazer._gazeTracker.bakeCurrentTransformIntoVertices();
  427. this._cameraGazer._gazeTracker.isPickable = false;
  428. this._cameraGazer._gazeTracker.isVisible = false;
  429. this._cameraGazer._gazeTracker.name = "gazeTracker";
  430. if (this._leftController) {
  431. this._leftController._gazeTracker = this._cameraGazer._gazeTracker.clone("gazeTracker");
  432. }
  433. if (this._rightController) {
  434. this._rightController._gazeTracker = this._cameraGazer._gazeTracker.clone("gazeTracker");
  435. }
  436. }
  437. }
  438. /**
  439. * If the gaze trackers scale should be updated to be constant size when pointing at near/far meshes
  440. */
  441. public updateGazeTrackerScale = true;
  442. /**
  443. * If the gaze trackers color should be updated when selecting meshes
  444. */
  445. public updateGazeTrackerColor = true;
  446. /**
  447. * The gaze tracking mesh corresponding to the left controller
  448. */
  449. public get leftControllerGazeTrackerMesh(): Nullable<Mesh> {
  450. if (this._leftController) {
  451. return this._leftController._gazeTracker;
  452. }
  453. return null;
  454. }
  455. /**
  456. * The gaze tracking mesh corresponding to the right controller
  457. */
  458. public get rightControllerGazeTrackerMesh(): Nullable<Mesh> {
  459. if (this._rightController) {
  460. return this._rightController._gazeTracker;
  461. }
  462. return null;
  463. }
  464. /**
  465. * If the ray of the gaze should be displayed.
  466. */
  467. public get displayGaze(): boolean {
  468. return this._displayGaze;
  469. }
  470. /**
  471. * Sets if the ray of the gaze should be displayed.
  472. */
  473. public set displayGaze(value: boolean) {
  474. this._displayGaze = value;
  475. if (!value) {
  476. this._cameraGazer._gazeTracker.isVisible = false;
  477. if (this._leftController) {
  478. this._leftController._gazeTracker.isVisible = false;
  479. }
  480. if (this._rightController) {
  481. this._rightController._gazeTracker.isVisible = false;
  482. }
  483. }
  484. }
  485. /**
  486. * If the ray of the LaserPointer should be displayed.
  487. */
  488. public get displayLaserPointer(): boolean {
  489. return this._displayLaserPointer;
  490. }
  491. /**
  492. * Sets if the ray of the LaserPointer should be displayed.
  493. */
  494. public set displayLaserPointer(value: boolean) {
  495. this._displayLaserPointer = value;
  496. if (!value) {
  497. if (this._rightController) {
  498. this._rightController._deactivatePointer();
  499. this._rightController._gazeTracker.isVisible = false;
  500. }
  501. if (this._leftController) {
  502. this._leftController._deactivatePointer();
  503. this._leftController._gazeTracker.isVisible = false;
  504. }
  505. }
  506. else {
  507. if (this._rightController) {
  508. this._rightController._activatePointer();
  509. }
  510. if (this._leftController) {
  511. this._leftController._activatePointer();
  512. }
  513. }
  514. }
  515. /**
  516. * The deviceOrientationCamera used as the camera when not in VR.
  517. */
  518. public get deviceOrientationCamera(): Nullable<DeviceOrientationCamera> {
  519. return this._deviceOrientationCamera;
  520. }
  521. /**
  522. * Based on the current WebVR support, returns the current VR camera used.
  523. */
  524. public get currentVRCamera(): Nullable<Camera> {
  525. if (this._webVRready) {
  526. return this._webVRCamera;
  527. }
  528. else {
  529. return this._scene.activeCamera;
  530. }
  531. }
  532. /**
  533. * The webVRCamera which is used when in VR.
  534. */
  535. public get webVRCamera(): WebVRFreeCamera {
  536. return this._webVRCamera;
  537. }
  538. /**
  539. * The deviceOrientationCamera that is used as a fallback when vr device is not connected.
  540. */
  541. public get vrDeviceOrientationCamera(): Nullable<VRDeviceOrientationFreeCamera> {
  542. return this._vrDeviceOrientationCamera;
  543. }
  544. /**
  545. * The html button that is used to trigger entering into VR.
  546. */
  547. public get vrButton(): Nullable<HTMLButtonElement> {
  548. return this._btnVR;
  549. }
  550. private get _teleportationRequestInitiated(): boolean {
  551. var result = this._cameraGazer._teleportationRequestInitiated
  552. || (this._leftController !== null && this._leftController._teleportationRequestInitiated)
  553. || (this._rightController !== null && this._rightController._teleportationRequestInitiated);
  554. return result;
  555. }
  556. /**
  557. * Defines wether or not Pointer lock should be requested when switching to
  558. * full screen.
  559. */
  560. public requestPointerLockOnFullScreen = true;
  561. /**
  562. * Instantiates a VRExperienceHelper.
  563. * Helps to quickly add VR support to an existing scene.
  564. * @param scene The scene the VRExperienceHelper belongs to.
  565. * @param webVROptions Options to modify the vr experience helper's behavior.
  566. */
  567. constructor(scene: Scene,
  568. /** Options to modify the vr experience helper's behavior. */
  569. public webVROptions: VRExperienceHelperOptions = {}) {
  570. this._scene = scene;
  571. this._canvas = scene.getEngine().getRenderingCanvas();
  572. // Parse options
  573. if (webVROptions.createFallbackVRDeviceOrientationFreeCamera === undefined) {
  574. webVROptions.createFallbackVRDeviceOrientationFreeCamera = true;
  575. }
  576. if (webVROptions.createDeviceOrientationCamera === undefined) {
  577. webVROptions.createDeviceOrientationCamera = true;
  578. }
  579. if (webVROptions.laserToggle === undefined) {
  580. webVROptions.laserToggle = true;
  581. }
  582. if (webVROptions.defaultHeight === undefined) {
  583. webVROptions.defaultHeight = 1.7;
  584. }
  585. if (webVROptions.useCustomVRButton) {
  586. this._useCustomVRButton = true;
  587. if (webVROptions.customVRButton) {
  588. this._btnVR = webVROptions.customVRButton;
  589. }
  590. }
  591. if (webVROptions.rayLength) {
  592. this._rayLength = webVROptions.rayLength;
  593. }
  594. this._defaultHeight = webVROptions.defaultHeight;
  595. if (webVROptions.positionScale) {
  596. this._rayLength *= webVROptions.positionScale;
  597. this._defaultHeight *= webVROptions.positionScale;
  598. }
  599. this._hasEnteredVR = false;
  600. // Set position
  601. if (this._scene.activeCamera) {
  602. this._position = this._scene.activeCamera.position.clone();
  603. } else {
  604. this._position = new Vector3(0, this._defaultHeight, 0);
  605. }
  606. // Set non-vr camera
  607. if (webVROptions.createDeviceOrientationCamera || !this._scene.activeCamera) {
  608. this._deviceOrientationCamera = new DeviceOrientationCamera("deviceOrientationVRHelper", this._position.clone(), scene);
  609. // Copy data from existing camera
  610. if (this._scene.activeCamera) {
  611. this._deviceOrientationCamera.minZ = this._scene.activeCamera.minZ;
  612. this._deviceOrientationCamera.maxZ = this._scene.activeCamera.maxZ;
  613. // Set rotation from previous camera
  614. if (this._scene.activeCamera instanceof TargetCamera && this._scene.activeCamera.rotation) {
  615. var targetCamera = this._scene.activeCamera;
  616. if (targetCamera.rotationQuaternion) {
  617. this._deviceOrientationCamera.rotationQuaternion.copyFrom(targetCamera.rotationQuaternion);
  618. } else {
  619. this._deviceOrientationCamera.rotationQuaternion.copyFrom(Quaternion.RotationYawPitchRoll(targetCamera.rotation.y, targetCamera.rotation.x, targetCamera.rotation.z));
  620. }
  621. this._deviceOrientationCamera.rotation = targetCamera.rotation.clone();
  622. }
  623. }
  624. this._scene.activeCamera = this._deviceOrientationCamera;
  625. if (this._canvas) {
  626. this._scene.activeCamera.attachControl(this._canvas);
  627. }
  628. } else {
  629. this._existingCamera = this._scene.activeCamera;
  630. }
  631. // Create VR cameras
  632. if (webVROptions.createFallbackVRDeviceOrientationFreeCamera) {
  633. if (webVROptions.useMultiview) {
  634. if (!webVROptions.vrDeviceOrientationCameraMetrics) {
  635. webVROptions.vrDeviceOrientationCameraMetrics = VRCameraMetrics.GetDefault();
  636. }
  637. webVROptions.vrDeviceOrientationCameraMetrics.multiviewEnabled = true;
  638. }
  639. this._vrDeviceOrientationCamera = new VRDeviceOrientationFreeCamera("VRDeviceOrientationVRHelper", this._position, this._scene, true, webVROptions.vrDeviceOrientationCameraMetrics);
  640. this._vrDeviceOrientationCamera.angularSensibility = Number.MAX_VALUE;
  641. }
  642. this._webVRCamera = new WebVRFreeCamera("WebVRHelper", this._position, this._scene, webVROptions);
  643. this._webVRCamera.useStandingMatrix();
  644. this._cameraGazer = new VRExperienceHelperCameraGazer(() => { return this.currentVRCamera; }, scene);
  645. // Create default button
  646. if (!this._useCustomVRButton) {
  647. this._btnVR = <HTMLButtonElement>document.createElement("BUTTON");
  648. this._btnVR.className = "babylonVRicon";
  649. this._btnVR.id = "babylonVRiconbtn";
  650. this._btnVR.title = "Click to switch to VR";
  651. 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) }";
  652. css += ".babylonVRicon.vrdisplaypresenting { display: none; }";
  653. // TODO: Add user feedback so that they know what state the VRDisplay is in (disconnected, connected, entering-VR)
  654. // css += ".babylonVRicon.vrdisplaysupported { }";
  655. // css += ".babylonVRicon.vrdisplayready { }";
  656. // css += ".babylonVRicon.vrdisplayrequesting { }";
  657. var style = document.createElement('style');
  658. style.appendChild(document.createTextNode(css));
  659. document.getElementsByTagName('head')[0].appendChild(style);
  660. this.moveButtonToBottomRight();
  661. }
  662. // VR button click event
  663. if (this._btnVR) {
  664. this._btnVR.addEventListener("click", () => {
  665. if (!this.isInVRMode) {
  666. this.enterVR();
  667. } else {
  668. this.exitVR();
  669. }
  670. });
  671. }
  672. // Window events
  673. window.addEventListener("resize", this._onResize);
  674. document.addEventListener("fullscreenchange", this._onFullscreenChange, false);
  675. document.addEventListener("mozfullscreenchange", this._onFullscreenChange, false);
  676. document.addEventListener("webkitfullscreenchange", this._onFullscreenChange, false);
  677. document.addEventListener("msfullscreenchange", this._onFullscreenChange, false);
  678. (<any>document).onmsfullscreenchange = this._onFullscreenChange;
  679. // Display vr button when headset is connected
  680. if (webVROptions.createFallbackVRDeviceOrientationFreeCamera) {
  681. this.displayVRButton();
  682. } else {
  683. this._scene.getEngine().onVRDisplayChangedObservable.add((e) => {
  684. if (e.vrDisplay) {
  685. this.displayVRButton();
  686. }
  687. });
  688. }
  689. // Exiting VR mode using 'ESC' key on desktop
  690. this._onKeyDown = (event: KeyboardEvent) => {
  691. if (event.keyCode === 27 && this.isInVRMode) {
  692. this.exitVR();
  693. }
  694. };
  695. document.addEventListener("keydown", this._onKeyDown);
  696. // Exiting VR mode double tapping the touch screen
  697. this._scene.onPrePointerObservable.add(() => {
  698. if (this.isInVRMode && this.exitVROnDoubleTap) {
  699. this.exitVR();
  700. if (this._fullscreenVRpresenting) {
  701. this._scene.getEngine().exitFullscreen();
  702. }
  703. }
  704. }, PointerEventTypes.POINTERDOUBLETAP, false);
  705. // Listen for WebVR display changes
  706. this._onVRDisplayChanged = (eventArgs: IDisplayChangedEventArgs) => this.onVRDisplayChanged(eventArgs);
  707. this._onVrDisplayPresentChange = () => this.onVrDisplayPresentChange();
  708. this._onVRRequestPresentStart = () => {
  709. this._webVRrequesting = true;
  710. this.updateButtonVisibility();
  711. };
  712. this._onVRRequestPresentComplete = () => {
  713. this._webVRrequesting = false;
  714. this.updateButtonVisibility();
  715. };
  716. scene.getEngine().onVRDisplayChangedObservable.add(this._onVRDisplayChanged);
  717. scene.getEngine().onVRRequestPresentStart.add(this._onVRRequestPresentStart);
  718. scene.getEngine().onVRRequestPresentComplete.add(this._onVRRequestPresentComplete);
  719. window.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  720. scene.onDisposeObservable.add(() => {
  721. this.dispose();
  722. });
  723. // Gamepad connection events
  724. this._webVRCamera.onControllerMeshLoadedObservable.add((webVRController) => this._onDefaultMeshLoaded(webVRController));
  725. this._scene.gamepadManager.onGamepadConnectedObservable.add(this._onNewGamepadConnected);
  726. this._scene.gamepadManager.onGamepadDisconnectedObservable.add(this._onNewGamepadDisconnected);
  727. this.updateButtonVisibility();
  728. //create easing functions
  729. this._circleEase = new CircleEase();
  730. this._circleEase.setEasingMode(EasingFunction.EASINGMODE_EASEINOUT);
  731. // Allow clicking in the vrDeviceOrientationCamera
  732. scene.onPointerObservable.add((e) => {
  733. if (this._interactionsEnabled) {
  734. if (scene.activeCamera === this.vrDeviceOrientationCamera && (e.event as PointerEvent).pointerType === "mouse") {
  735. if (e.type === PointerEventTypes.POINTERDOWN) {
  736. this._cameraGazer._selectionPointerDown();
  737. } else if (e.type === PointerEventTypes.POINTERUP) {
  738. this._cameraGazer._selectionPointerUp();
  739. }
  740. }
  741. }
  742. });
  743. if (this.webVROptions.floorMeshes) {
  744. this.enableTeleportation({ floorMeshes: this.webVROptions.floorMeshes });
  745. }
  746. }
  747. // Raised when one of the controller has loaded successfully its associated default mesh
  748. private _onDefaultMeshLoaded(webVRController: WebVRController) {
  749. if (this._leftController && this._leftController.webVRController == webVRController) {
  750. if (webVRController.mesh) {
  751. this._leftController._setLaserPointerParent(webVRController.mesh);
  752. }
  753. }
  754. if (this._rightController && this._rightController.webVRController == webVRController) {
  755. if (webVRController.mesh) {
  756. this._rightController._setLaserPointerParent(webVRController.mesh);
  757. }
  758. }
  759. try {
  760. this.onControllerMeshLoadedObservable.notifyObservers(webVRController);
  761. }
  762. catch (err) {
  763. Logger.Warn("Error in your custom logic onControllerMeshLoaded: " + err);
  764. }
  765. }
  766. private _onResize = () => {
  767. this.moveButtonToBottomRight();
  768. if (this._fullscreenVRpresenting && this._webVRready) {
  769. this.exitVR();
  770. }
  771. }
  772. private _onFullscreenChange = () => {
  773. let anyDoc = document as any;
  774. if (anyDoc.fullscreen !== undefined) {
  775. this._fullscreenVRpresenting = (<any>document).fullscreen;
  776. } else if (anyDoc.mozFullScreen !== undefined) {
  777. this._fullscreenVRpresenting = anyDoc.mozFullScreen;
  778. } else if (anyDoc.webkitIsFullScreen !== undefined) {
  779. this._fullscreenVRpresenting = anyDoc.webkitIsFullScreen;
  780. } else if (anyDoc.msIsFullScreen !== undefined) {
  781. this._fullscreenVRpresenting = anyDoc.msIsFullScreen;
  782. } else if ((<any>document).msFullscreenElement !== undefined) {
  783. this._fullscreenVRpresenting = (<any>document).msFullscreenElement;
  784. }
  785. if (!this._fullscreenVRpresenting && this._canvas) {
  786. this.exitVR();
  787. if (!this._useCustomVRButton && this._btnVR) {
  788. this._btnVR.style.top = this._canvas.offsetTop + this._canvas.offsetHeight - 70 + "px";
  789. this._btnVR.style.left = this._canvas.offsetLeft + this._canvas.offsetWidth - 100 + "px";
  790. }
  791. }
  792. }
  793. /**
  794. * Gets a value indicating if we are currently in VR mode.
  795. */
  796. public get isInVRMode(): boolean {
  797. return this._webVRpresenting || this._fullscreenVRpresenting;
  798. }
  799. private onVrDisplayPresentChange() {
  800. var vrDisplay = this._scene.getEngine().getVRDevice();
  801. if (vrDisplay) {
  802. var wasPresenting = this._webVRpresenting;
  803. this._webVRpresenting = vrDisplay.isPresenting;
  804. if (wasPresenting && !this._webVRpresenting) {
  805. this.exitVR();
  806. }
  807. } else {
  808. Logger.Warn('Detected VRDisplayPresentChange on an unknown VRDisplay. Did you can enterVR on the vrExperienceHelper?');
  809. }
  810. this.updateButtonVisibility();
  811. }
  812. private onVRDisplayChanged(eventArgs: IDisplayChangedEventArgs) {
  813. this._webVRsupported = eventArgs.vrSupported;
  814. this._webVRready = !!eventArgs.vrDisplay;
  815. this._webVRpresenting = eventArgs.vrDisplay && eventArgs.vrDisplay.isPresenting;
  816. this.updateButtonVisibility();
  817. }
  818. private moveButtonToBottomRight() {
  819. if (this._canvas && !this._useCustomVRButton && this._btnVR) {
  820. const rect: ClientRect = this._canvas.getBoundingClientRect();
  821. this._btnVR.style.top = rect.top + rect.height - 70 + "px";
  822. this._btnVR.style.left = rect.left + rect.width - 100 + "px";
  823. }
  824. }
  825. private displayVRButton() {
  826. if (!this._useCustomVRButton && !this._btnVRDisplayed && this._btnVR) {
  827. document.body.appendChild(this._btnVR);
  828. this._btnVRDisplayed = true;
  829. }
  830. }
  831. private updateButtonVisibility() {
  832. if (!this._btnVR || this._useCustomVRButton) {
  833. return;
  834. }
  835. this._btnVR.className = "babylonVRicon";
  836. if (this.isInVRMode) {
  837. this._btnVR.className += " vrdisplaypresenting";
  838. } else {
  839. if (this._webVRready) { this._btnVR.className += " vrdisplayready"; }
  840. if (this._webVRsupported) { this._btnVR.className += " vrdisplaysupported"; }
  841. if (this._webVRrequesting) { this._btnVR.className += " vrdisplayrequesting"; }
  842. }
  843. }
  844. private _cachedAngularSensibility = { angularSensibilityX: null, angularSensibilityY: null, angularSensibility: null };
  845. /**
  846. * Attempt to enter VR. If a headset is connected and ready, will request present on that.
  847. * Otherwise, will use the fullscreen API.
  848. */
  849. public enterVR() {
  850. if (this.onEnteringVRObservable) {
  851. try {
  852. this.onEnteringVRObservable.notifyObservers(this);
  853. }
  854. catch (err) {
  855. Logger.Warn("Error in your custom logic onEnteringVR: " + err);
  856. }
  857. }
  858. if (this._scene.activeCamera) {
  859. this._position = this._scene.activeCamera.position.clone();
  860. if (this.vrDeviceOrientationCamera) {
  861. this.vrDeviceOrientationCamera.rotation = Quaternion.FromRotationMatrix(this._scene.activeCamera.getWorldMatrix().getRotationMatrix()).toEulerAngles();
  862. this.vrDeviceOrientationCamera.angularSensibility = 2000;
  863. }
  864. if (this.webVRCamera) {
  865. var currentYRotation = this.webVRCamera.deviceRotationQuaternion.toEulerAngles().y;
  866. var desiredYRotation = Quaternion.FromRotationMatrix(this._scene.activeCamera.getWorldMatrix().getRotationMatrix()).toEulerAngles().y;
  867. var delta = desiredYRotation - currentYRotation;
  868. var currentGlobalRotation = this.webVRCamera.rotationQuaternion.toEulerAngles().y;
  869. this.webVRCamera.rotationQuaternion = Quaternion.FromEulerAngles(0, currentGlobalRotation + delta, 0);
  870. }
  871. // make sure that we return to the last active camera
  872. this._existingCamera = this._scene.activeCamera;
  873. // Remove and cache angular sensability to avoid camera rotation when in VR
  874. if ((<any>this._existingCamera).angularSensibilityX) {
  875. this._cachedAngularSensibility.angularSensibilityX = (<any>this._existingCamera).angularSensibilityX;
  876. (<any>this._existingCamera).angularSensibilityX = Number.MAX_VALUE;
  877. }
  878. if ((<any>this._existingCamera).angularSensibilityY) {
  879. this._cachedAngularSensibility.angularSensibilityY = (<any>this._existingCamera).angularSensibilityY;
  880. (<any>this._existingCamera).angularSensibilityY = Number.MAX_VALUE;
  881. }
  882. if ((<any>this._existingCamera).angularSensibility) {
  883. this._cachedAngularSensibility.angularSensibility = (<any>this._existingCamera).angularSensibility;
  884. (<any>this._existingCamera).angularSensibility = Number.MAX_VALUE;
  885. }
  886. }
  887. if (this._webVRrequesting) {
  888. return;
  889. }
  890. // If WebVR is supported and a headset is connected
  891. if (this._webVRready) {
  892. if (!this._webVRpresenting) {
  893. this._scene.getEngine().onVRRequestPresentComplete.addOnce((result) => {
  894. this.onAfterEnteringVRObservable.notifyObservers({ success: result });
  895. });
  896. this._webVRCamera.position = this._position;
  897. this._scene.activeCamera = this._webVRCamera;
  898. }
  899. }
  900. else if (this._vrDeviceOrientationCamera) {
  901. this._vrDeviceOrientationCamera.position = this._position;
  902. if (this._scene.activeCamera) {
  903. this._vrDeviceOrientationCamera.minZ = this._scene.activeCamera.minZ;
  904. }
  905. this._scene.activeCamera = this._vrDeviceOrientationCamera;
  906. this._scene.getEngine().enterFullscreen(this.requestPointerLockOnFullScreen);
  907. this.updateButtonVisibility();
  908. this._vrDeviceOrientationCamera.onViewMatrixChangedObservable.addOnce(() => {
  909. this.onAfterEnteringVRObservable.notifyObservers({ success: true });
  910. });
  911. }
  912. if (this._scene.activeCamera && this._canvas) {
  913. this._scene.activeCamera.attachControl(this._canvas);
  914. }
  915. if (this._interactionsEnabled) {
  916. this._scene.registerBeforeRender(this.beforeRender);
  917. }
  918. if (this._displayLaserPointer) {
  919. [this._leftController, this._rightController].forEach((controller) => {
  920. if (controller) {
  921. controller._activatePointer();
  922. }
  923. });
  924. }
  925. this._hasEnteredVR = true;
  926. }
  927. /**
  928. * Attempt to exit VR, or fullscreen.
  929. */
  930. public exitVR() {
  931. if (this._hasEnteredVR) {
  932. if (this.onExitingVRObservable) {
  933. try {
  934. this.onExitingVRObservable.notifyObservers(this);
  935. }
  936. catch (err) {
  937. Logger.Warn("Error in your custom logic onExitingVR: " + err);
  938. }
  939. }
  940. if (this._webVRpresenting) {
  941. this._scene.getEngine().disableVR();
  942. }
  943. if (this._scene.activeCamera) {
  944. this._position = this._scene.activeCamera.position.clone();
  945. }
  946. if (this.vrDeviceOrientationCamera) {
  947. this.vrDeviceOrientationCamera.angularSensibility = Number.MAX_VALUE;
  948. }
  949. if (this._deviceOrientationCamera) {
  950. this._deviceOrientationCamera.position = this._position;
  951. this._scene.activeCamera = this._deviceOrientationCamera;
  952. if (this._canvas) {
  953. this._scene.activeCamera.attachControl(this._canvas);
  954. }
  955. // Restore angular sensibility
  956. if (this._cachedAngularSensibility.angularSensibilityX) {
  957. (<any>this._deviceOrientationCamera).angularSensibilityX = this._cachedAngularSensibility.angularSensibilityX;
  958. this._cachedAngularSensibility.angularSensibilityX = null;
  959. }
  960. if (this._cachedAngularSensibility.angularSensibilityY) {
  961. (<any>this._deviceOrientationCamera).angularSensibilityY = this._cachedAngularSensibility.angularSensibilityY;
  962. this._cachedAngularSensibility.angularSensibilityY = null;
  963. }
  964. if (this._cachedAngularSensibility.angularSensibility) {
  965. (<any>this._deviceOrientationCamera).angularSensibility = this._cachedAngularSensibility.angularSensibility;
  966. this._cachedAngularSensibility.angularSensibility = null;
  967. }
  968. } else if (this._existingCamera) {
  969. this._existingCamera.position = this._position;
  970. this._scene.activeCamera = this._existingCamera;
  971. // Restore angular sensibility
  972. if (this._cachedAngularSensibility.angularSensibilityX) {
  973. (<any>this._existingCamera).angularSensibilityX = this._cachedAngularSensibility.angularSensibilityX;
  974. this._cachedAngularSensibility.angularSensibilityX = null;
  975. }
  976. if (this._cachedAngularSensibility.angularSensibilityY) {
  977. (<any>this._existingCamera).angularSensibilityY = this._cachedAngularSensibility.angularSensibilityY;
  978. this._cachedAngularSensibility.angularSensibilityY = null;
  979. }
  980. if (this._cachedAngularSensibility.angularSensibility) {
  981. (<any>this._existingCamera).angularSensibility = this._cachedAngularSensibility.angularSensibility;
  982. this._cachedAngularSensibility.angularSensibility = null;
  983. }
  984. }
  985. this.updateButtonVisibility();
  986. if (this._interactionsEnabled) {
  987. this._scene.unregisterBeforeRender(this.beforeRender);
  988. this._cameraGazer._gazeTracker.isVisible = false;
  989. if (this._leftController) {
  990. this._leftController._gazeTracker.isVisible = false;
  991. }
  992. if (this._rightController) {
  993. this._rightController._gazeTracker.isVisible = false;
  994. }
  995. }
  996. // resize to update width and height when exiting vr exits fullscreen
  997. this._scene.getEngine().resize();
  998. [this._leftController, this._rightController].forEach((controller) => {
  999. if (controller) {
  1000. controller._deactivatePointer();
  1001. }
  1002. });
  1003. this._hasEnteredVR = false;
  1004. // Update engine state to re enable non-vr camera input
  1005. var engine = this._scene.getEngine();
  1006. if (engine._onVrDisplayPresentChange) {
  1007. engine._onVrDisplayPresentChange();
  1008. }
  1009. }
  1010. }
  1011. /**
  1012. * The position of the vr experience helper.
  1013. */
  1014. public get position(): Vector3 {
  1015. return this._position;
  1016. }
  1017. /**
  1018. * Sets the position of the vr experience helper.
  1019. */
  1020. public set position(value: Vector3) {
  1021. this._position = value;
  1022. if (this._scene.activeCamera) {
  1023. this._scene.activeCamera.position = value;
  1024. }
  1025. }
  1026. /**
  1027. * Enables controllers and user interactions such as selecting and object or clicking on an object.
  1028. */
  1029. public enableInteractions() {
  1030. if (!this._interactionsEnabled) {
  1031. this._interactionsRequested = true;
  1032. if (this._leftController) {
  1033. this._enableInteractionOnController(this._leftController);
  1034. }
  1035. if (this._rightController) {
  1036. this._enableInteractionOnController(this._rightController);
  1037. }
  1038. this.raySelectionPredicate = (mesh) => {
  1039. return mesh.isVisible && (mesh.isPickable || mesh.name === this._floorMeshName);
  1040. };
  1041. this.meshSelectionPredicate = () => {
  1042. return true;
  1043. };
  1044. this._raySelectionPredicate = (mesh) => {
  1045. if (this._isTeleportationFloor(mesh) || (mesh.name.indexOf("gazeTracker") === -1
  1046. && mesh.name.indexOf("teleportationTarget") === -1
  1047. && mesh.name.indexOf("torusTeleportation") === -1)) {
  1048. return this.raySelectionPredicate(mesh);
  1049. }
  1050. return false;
  1051. };
  1052. this._interactionsEnabled = true;
  1053. }
  1054. }
  1055. private get _noControllerIsActive() {
  1056. return !(this._leftController && this._leftController._activePointer) && !(this._rightController && this._rightController._activePointer);
  1057. }
  1058. private beforeRender = () => {
  1059. if (this._leftController && this._leftController._activePointer) {
  1060. this._castRayAndSelectObject(this._leftController);
  1061. }
  1062. if (this._rightController && this._rightController._activePointer) {
  1063. this._castRayAndSelectObject(this._rightController);
  1064. }
  1065. if (this._noControllerIsActive && this._scene.getEngine().isPointerLock) {
  1066. this._castRayAndSelectObject(this._cameraGazer);
  1067. } else {
  1068. this._cameraGazer._gazeTracker.isVisible = false;
  1069. }
  1070. }
  1071. private _isTeleportationFloor(mesh: AbstractMesh): boolean {
  1072. for (var i = 0; i < this._floorMeshesCollection.length; i++) {
  1073. if (this._floorMeshesCollection[i].id === mesh.id) {
  1074. return true;
  1075. }
  1076. }
  1077. if (this._floorMeshName && mesh.name === this._floorMeshName) {
  1078. return true;
  1079. }
  1080. return false;
  1081. }
  1082. /**
  1083. * Adds a floor mesh to be used for teleportation.
  1084. * @param floorMesh the mesh to be used for teleportation.
  1085. */
  1086. public addFloorMesh(floorMesh: Mesh): void {
  1087. if (!this._floorMeshesCollection) {
  1088. return;
  1089. }
  1090. if (this._floorMeshesCollection.indexOf(floorMesh) > -1) {
  1091. return;
  1092. }
  1093. this._floorMeshesCollection.push(floorMesh);
  1094. }
  1095. /**
  1096. * Removes a floor mesh from being used for teleportation.
  1097. * @param floorMesh the mesh to be removed.
  1098. */
  1099. public removeFloorMesh(floorMesh: Mesh): void {
  1100. if (!this._floorMeshesCollection) {
  1101. return;
  1102. }
  1103. const meshIndex = this._floorMeshesCollection.indexOf(floorMesh);
  1104. if (meshIndex !== -1) {
  1105. this._floorMeshesCollection.splice(meshIndex, 1);
  1106. }
  1107. }
  1108. /**
  1109. * Enables interactions and teleportation using the VR controllers and gaze.
  1110. * @param vrTeleportationOptions options to modify teleportation behavior.
  1111. */
  1112. public enableTeleportation(vrTeleportationOptions: VRTeleportationOptions = {}) {
  1113. if (!this._teleportationInitialized) {
  1114. this._teleportationRequested = true;
  1115. this.enableInteractions();
  1116. if (vrTeleportationOptions.floorMeshName) {
  1117. this._floorMeshName = vrTeleportationOptions.floorMeshName;
  1118. }
  1119. if (vrTeleportationOptions.floorMeshes) {
  1120. this._floorMeshesCollection = vrTeleportationOptions.floorMeshes;
  1121. }
  1122. if (this._leftController != null) {
  1123. this._enableTeleportationOnController(this._leftController);
  1124. }
  1125. if (this._rightController != null) {
  1126. this._enableTeleportationOnController(this._rightController);
  1127. }
  1128. // Creates an image processing post process for the vignette not relying
  1129. // on the main scene configuration for image processing to reduce setup and spaces
  1130. // (gamma/linear) conflicts.
  1131. const imageProcessingConfiguration = new ImageProcessingConfiguration();
  1132. imageProcessingConfiguration.vignetteColor = new Color4(0, 0, 0, 0);
  1133. imageProcessingConfiguration.vignetteEnabled = true;
  1134. this._postProcessMove = new ImageProcessingPostProcess("postProcessMove",
  1135. 1.0,
  1136. this._webVRCamera,
  1137. undefined,
  1138. undefined,
  1139. undefined,
  1140. undefined,
  1141. imageProcessingConfiguration);
  1142. this._webVRCamera.detachPostProcess(this._postProcessMove);
  1143. this._teleportationInitialized = true;
  1144. if (this._isDefaultTeleportationTarget) {
  1145. this._createTeleportationCircles();
  1146. this._teleportationTarget.scaling.scaleInPlace(this._webVRCamera.deviceScaleFactor);
  1147. }
  1148. }
  1149. }
  1150. private _onNewGamepadConnected = (gamepad: Gamepad) => {
  1151. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  1152. if (gamepad.leftStick) {
  1153. gamepad.onleftstickchanged((stickValues) => {
  1154. if (this._teleportationInitialized && this.teleportationEnabled) {
  1155. // Listening to classic/xbox gamepad only if no VR controller is active
  1156. if ((!this._leftController && !this._rightController) ||
  1157. ((this._leftController && !this._leftController._activePointer) &&
  1158. (this._rightController && !this._rightController._activePointer))) {
  1159. this._checkTeleportWithRay(stickValues, this._cameraGazer);
  1160. this._checkTeleportBackwards(stickValues, this._cameraGazer);
  1161. }
  1162. }
  1163. });
  1164. }
  1165. if (gamepad.rightStick) {
  1166. gamepad.onrightstickchanged((stickValues) => {
  1167. if (this._teleportationInitialized) {
  1168. this._checkRotate(stickValues, this._cameraGazer);
  1169. }
  1170. });
  1171. }
  1172. if (gamepad.type === Gamepad.XBOX) {
  1173. (<Xbox360Pad>gamepad).onbuttondown((buttonPressed: Xbox360Button) => {
  1174. if (this._interactionsEnabled && buttonPressed === Xbox360Button.A) {
  1175. this._cameraGazer._selectionPointerDown();
  1176. }
  1177. });
  1178. (<Xbox360Pad>gamepad).onbuttonup((buttonPressed: Xbox360Button) => {
  1179. if (this._interactionsEnabled && buttonPressed === Xbox360Button.A) {
  1180. this._cameraGazer._selectionPointerUp();
  1181. }
  1182. });
  1183. }
  1184. } else {
  1185. var webVRController = <WebVRController>gamepad;
  1186. var controller = new VRExperienceHelperControllerGazer(webVRController, this._scene, this._cameraGazer._gazeTracker);
  1187. if (webVRController.hand === "right" || (this._leftController && this._leftController.webVRController != webVRController)) {
  1188. this._rightController = controller;
  1189. } else {
  1190. this._leftController = controller;
  1191. }
  1192. this._tryEnableInteractionOnController(controller);
  1193. }
  1194. }
  1195. // 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
  1196. private _tryEnableInteractionOnController = (controller: VRExperienceHelperControllerGazer) => {
  1197. if (this._interactionsRequested && !controller._interactionsEnabled) {
  1198. this._enableInteractionOnController(controller);
  1199. }
  1200. if (this._teleportationRequested && !controller._teleportationEnabled) {
  1201. this._enableTeleportationOnController(controller);
  1202. }
  1203. }
  1204. private _onNewGamepadDisconnected = (gamepad: Gamepad) => {
  1205. if (gamepad instanceof WebVRController) {
  1206. if (gamepad.hand === "left" && this._leftController != null) {
  1207. this._leftController.dispose();
  1208. this._leftController = null;
  1209. }
  1210. if (gamepad.hand === "right" && this._rightController != null) {
  1211. this._rightController.dispose();
  1212. this._rightController = null;
  1213. }
  1214. }
  1215. }
  1216. private _enableInteractionOnController(controller: VRExperienceHelperControllerGazer) {
  1217. var controllerMesh = controller.webVRController.mesh;
  1218. if (controllerMesh) {
  1219. controller._interactionsEnabled = true;
  1220. if (this.isInVRMode && this._displayLaserPointer) {
  1221. controller._activatePointer();
  1222. }
  1223. if (this.webVROptions.laserToggle) {
  1224. controller.webVRController.onMainButtonStateChangedObservable.add((stateObject) => {
  1225. // Enabling / disabling laserPointer
  1226. if (this._displayLaserPointer && stateObject.value === 1) {
  1227. if (controller._activePointer) {
  1228. controller._deactivatePointer();
  1229. } else {
  1230. controller._activatePointer();
  1231. }
  1232. if (this.displayGaze) {
  1233. controller._gazeTracker.isVisible = controller._activePointer;
  1234. }
  1235. }
  1236. });
  1237. }
  1238. controller.webVRController.onTriggerStateChangedObservable.add((stateObject) => {
  1239. var gazer: VRExperienceHelperGazer = controller;
  1240. if (this._noControllerIsActive) {
  1241. gazer = this._cameraGazer;
  1242. }
  1243. if (!gazer._pointerDownOnMeshAsked) {
  1244. if (stateObject.value > this._padSensibilityUp) {
  1245. gazer._selectionPointerDown();
  1246. }
  1247. } else if (stateObject.value < this._padSensibilityDown) {
  1248. gazer._selectionPointerUp();
  1249. }
  1250. });
  1251. }
  1252. }
  1253. private _checkTeleportWithRay(stateObject: StickValues, gazer: VRExperienceHelperGazer) {
  1254. // Dont teleport if another gaze already requested teleportation
  1255. if (this._teleportationRequestInitiated && !gazer._teleportationRequestInitiated) {
  1256. return;
  1257. }
  1258. if (!gazer._teleportationRequestInitiated) {
  1259. if (stateObject.y < -this._padSensibilityUp && gazer._dpadPressed) {
  1260. gazer._activatePointer();
  1261. gazer._teleportationRequestInitiated = true;
  1262. }
  1263. } else {
  1264. // Listening to the proper controller values changes to confirm teleportation
  1265. if (Math.sqrt(stateObject.y * stateObject.y + stateObject.x * stateObject.x) < this._padSensibilityDown) {
  1266. if (this._teleportActive) {
  1267. this.teleportCamera(this._haloCenter);
  1268. }
  1269. gazer._teleportationRequestInitiated = false;
  1270. }
  1271. }
  1272. }
  1273. private _checkRotate(stateObject: StickValues, gazer: VRExperienceHelperGazer) {
  1274. // Only rotate when user is not currently selecting a teleportation location
  1275. if (gazer._teleportationRequestInitiated) {
  1276. return;
  1277. }
  1278. if (!gazer._rotationLeftAsked) {
  1279. if (stateObject.x < -this._padSensibilityUp && gazer._dpadPressed) {
  1280. gazer._rotationLeftAsked = true;
  1281. if (this._rotationAllowed) {
  1282. this._rotateCamera(false);
  1283. }
  1284. }
  1285. } else {
  1286. if (stateObject.x > -this._padSensibilityDown) {
  1287. gazer._rotationLeftAsked = false;
  1288. }
  1289. }
  1290. if (!gazer._rotationRightAsked) {
  1291. if (stateObject.x > this._padSensibilityUp && gazer._dpadPressed) {
  1292. gazer._rotationRightAsked = true;
  1293. if (this._rotationAllowed) {
  1294. this._rotateCamera(true);
  1295. }
  1296. }
  1297. } else {
  1298. if (stateObject.x < this._padSensibilityDown) {
  1299. gazer._rotationRightAsked = false;
  1300. }
  1301. }
  1302. }
  1303. private _checkTeleportBackwards(stateObject: StickValues, gazer: VRExperienceHelperGazer) {
  1304. // Only teleport backwards when user is not currently selecting a teleportation location
  1305. if (gazer._teleportationRequestInitiated) {
  1306. return;
  1307. }
  1308. // Teleport backwards
  1309. if (stateObject.y > this._padSensibilityUp && gazer._dpadPressed) {
  1310. if (!gazer._teleportationBackRequestInitiated) {
  1311. if (!this.currentVRCamera) {
  1312. return;
  1313. }
  1314. // Get rotation and position of the current camera
  1315. var rotation = Quaternion.FromRotationMatrix(this.currentVRCamera.getWorldMatrix().getRotationMatrix());
  1316. var position = this.currentVRCamera.position;
  1317. // If the camera has device position, use that instead
  1318. if ((<WebVRFreeCamera>this.currentVRCamera).devicePosition && (<WebVRFreeCamera>this.currentVRCamera).deviceRotationQuaternion) {
  1319. rotation = (<WebVRFreeCamera>this.currentVRCamera).deviceRotationQuaternion;
  1320. position = (<WebVRFreeCamera>this.currentVRCamera).devicePosition;
  1321. }
  1322. // Get matrix with only the y rotation of the device rotation
  1323. rotation.toEulerAnglesToRef(this._workingVector);
  1324. this._workingVector.z = 0;
  1325. this._workingVector.x = 0;
  1326. Quaternion.RotationYawPitchRollToRef(this._workingVector.y, this._workingVector.x, this._workingVector.z, this._workingQuaternion);
  1327. this._workingQuaternion.toRotationMatrix(this._workingMatrix);
  1328. // Rotate backwards ray by device rotation to cast at the ground behind the user
  1329. Vector3.TransformCoordinatesToRef(this._teleportBackwardsVector, this._workingMatrix, this._workingVector);
  1330. // Teleport if ray hit the ground and is not to far away eg. backwards off a cliff
  1331. var ray = new Ray(position, this._workingVector);
  1332. var hit = this._scene.pickWithRay(ray, this._raySelectionPredicate);
  1333. if (hit && hit.pickedPoint && hit.pickedMesh && this._isTeleportationFloor(hit.pickedMesh) && hit.distance < 5) {
  1334. this.teleportCamera(hit.pickedPoint);
  1335. }
  1336. gazer._teleportationBackRequestInitiated = true;
  1337. }
  1338. } else {
  1339. gazer._teleportationBackRequestInitiated = false;
  1340. }
  1341. }
  1342. private _enableTeleportationOnController(controller: VRExperienceHelperControllerGazer) {
  1343. var controllerMesh = controller.webVRController.mesh;
  1344. if (controllerMesh) {
  1345. if (!controller._interactionsEnabled) {
  1346. this._enableInteractionOnController(controller);
  1347. }
  1348. controller._interactionsEnabled = true;
  1349. controller._teleportationEnabled = true;
  1350. if (controller.webVRController.controllerType === PoseEnabledControllerType.VIVE) {
  1351. controller._dpadPressed = false;
  1352. controller.webVRController.onPadStateChangedObservable.add((stateObject) => {
  1353. controller._dpadPressed = stateObject.pressed;
  1354. if (!controller._dpadPressed) {
  1355. controller._rotationLeftAsked = false;
  1356. controller._rotationRightAsked = false;
  1357. controller._teleportationBackRequestInitiated = false;
  1358. }
  1359. });
  1360. }
  1361. controller.webVRController.onPadValuesChangedObservable.add((stateObject) => {
  1362. if (this.teleportationEnabled) {
  1363. this._checkTeleportBackwards(stateObject, controller);
  1364. this._checkTeleportWithRay(stateObject, controller);
  1365. }
  1366. this._checkRotate(stateObject, controller);
  1367. });
  1368. }
  1369. }
  1370. private _createTeleportationCircles() {
  1371. this._teleportationTarget = Mesh.CreateGround("teleportationTarget", 2, 2, 2, this._scene);
  1372. this._teleportationTarget.isPickable = false;
  1373. var length = 512;
  1374. var dynamicTexture = new DynamicTexture("DynamicTexture", length, this._scene, true);
  1375. dynamicTexture.hasAlpha = true;
  1376. var context = dynamicTexture.getContext();
  1377. var centerX = length / 2;
  1378. var centerY = length / 2;
  1379. var radius = 200;
  1380. context.beginPath();
  1381. context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  1382. context.fillStyle = this._teleportationFillColor;
  1383. context.fill();
  1384. context.lineWidth = 10;
  1385. context.strokeStyle = this._teleportationBorderColor;
  1386. context.stroke();
  1387. context.closePath();
  1388. dynamicTexture.update();
  1389. var teleportationCircleMaterial = new StandardMaterial("TextPlaneMaterial", this._scene);
  1390. teleportationCircleMaterial.diffuseTexture = dynamicTexture;
  1391. this._teleportationTarget.material = teleportationCircleMaterial;
  1392. var torus = Mesh.CreateTorus("torusTeleportation", 0.75, 0.1, 25, this._scene, false);
  1393. torus.isPickable = false;
  1394. torus.parent = this._teleportationTarget;
  1395. var animationInnerCircle = new Animation("animationInnerCircle", "position.y", 30, Animation.ANIMATIONTYPE_FLOAT, Animation.ANIMATIONLOOPMODE_CYCLE);
  1396. var keys = [];
  1397. keys.push({
  1398. frame: 0,
  1399. value: 0
  1400. });
  1401. keys.push({
  1402. frame: 30,
  1403. value: 0.4
  1404. });
  1405. keys.push({
  1406. frame: 60,
  1407. value: 0
  1408. });
  1409. animationInnerCircle.setKeys(keys);
  1410. var easingFunction = new SineEase();
  1411. easingFunction.setEasingMode(EasingFunction.EASINGMODE_EASEINOUT);
  1412. animationInnerCircle.setEasingFunction(easingFunction);
  1413. torus.animations = [];
  1414. torus.animations.push(animationInnerCircle);
  1415. this._scene.beginAnimation(torus, 0, 60, true);
  1416. this._hideTeleportationTarget();
  1417. }
  1418. private _displayTeleportationTarget() {
  1419. this._teleportActive = true;
  1420. if (this._teleportationInitialized) {
  1421. this._teleportationTarget.isVisible = true;
  1422. if (this._isDefaultTeleportationTarget) {
  1423. (<Mesh>this._teleportationTarget.getChildren()[0]).isVisible = true;
  1424. }
  1425. }
  1426. }
  1427. private _hideTeleportationTarget() {
  1428. this._teleportActive = false;
  1429. if (this._teleportationInitialized) {
  1430. this._teleportationTarget.isVisible = false;
  1431. if (this._isDefaultTeleportationTarget) {
  1432. (<Mesh>this._teleportationTarget.getChildren()[0]).isVisible = false;
  1433. }
  1434. }
  1435. }
  1436. private _rotateCamera(right: boolean) {
  1437. if (!(this.currentVRCamera instanceof FreeCamera)) {
  1438. return;
  1439. }
  1440. if (right) {
  1441. this._rotationAngle++;
  1442. }
  1443. else {
  1444. this._rotationAngle--;
  1445. }
  1446. this.currentVRCamera.animations = [];
  1447. var target = Quaternion.FromRotationMatrix(Matrix.RotationY(Math.PI / 4 * this._rotationAngle));
  1448. var animationRotation = new Animation("animationRotation", "rotationQuaternion", 90, Animation.ANIMATIONTYPE_QUATERNION,
  1449. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1450. var animationRotationKeys = [];
  1451. animationRotationKeys.push({
  1452. frame: 0,
  1453. value: this.currentVRCamera.rotationQuaternion
  1454. });
  1455. animationRotationKeys.push({
  1456. frame: 6,
  1457. value: target
  1458. });
  1459. animationRotation.setKeys(animationRotationKeys);
  1460. animationRotation.setEasingFunction(this._circleEase);
  1461. this.currentVRCamera.animations.push(animationRotation);
  1462. this._postProcessMove.animations = [];
  1463. var animationPP = new Animation("animationPP", "vignetteWeight", 90, Animation.ANIMATIONTYPE_FLOAT,
  1464. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1465. var vignetteWeightKeys = [];
  1466. vignetteWeightKeys.push({
  1467. frame: 0,
  1468. value: 0
  1469. });
  1470. vignetteWeightKeys.push({
  1471. frame: 3,
  1472. value: 4
  1473. });
  1474. vignetteWeightKeys.push({
  1475. frame: 6,
  1476. value: 0
  1477. });
  1478. animationPP.setKeys(vignetteWeightKeys);
  1479. animationPP.setEasingFunction(this._circleEase);
  1480. this._postProcessMove.animations.push(animationPP);
  1481. var animationPP2 = new Animation("animationPP2", "vignetteStretch", 90, Animation.ANIMATIONTYPE_FLOAT,
  1482. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1483. var vignetteStretchKeys = [];
  1484. vignetteStretchKeys.push({
  1485. frame: 0,
  1486. value: 0
  1487. });
  1488. vignetteStretchKeys.push({
  1489. frame: 3,
  1490. value: 10
  1491. });
  1492. vignetteStretchKeys.push({
  1493. frame: 6,
  1494. value: 0
  1495. });
  1496. animationPP2.setKeys(vignetteStretchKeys);
  1497. animationPP2.setEasingFunction(this._circleEase);
  1498. this._postProcessMove.animations.push(animationPP2);
  1499. this._postProcessMove.imageProcessingConfiguration.vignetteWeight = 0;
  1500. this._postProcessMove.imageProcessingConfiguration.vignetteStretch = 0;
  1501. this._postProcessMove.samples = 4;
  1502. this._webVRCamera.attachPostProcess(this._postProcessMove);
  1503. this._scene.beginAnimation(this._postProcessMove, 0, 6, false, 1, () => {
  1504. this._webVRCamera.detachPostProcess(this._postProcessMove);
  1505. });
  1506. this._scene.beginAnimation(this.currentVRCamera, 0, 6, false, 1);
  1507. }
  1508. private _moveTeleportationSelectorTo(hit: PickingInfo, gazer: VRExperienceHelperGazer, ray: Ray) {
  1509. if (hit.pickedPoint) {
  1510. if (gazer._teleportationRequestInitiated) {
  1511. this._displayTeleportationTarget();
  1512. this._haloCenter.copyFrom(hit.pickedPoint);
  1513. this._teleportationTarget.position.copyFrom(hit.pickedPoint);
  1514. }
  1515. var pickNormal = this._convertNormalToDirectionOfRay(hit.getNormal(true, false), ray);
  1516. if (pickNormal) {
  1517. var axis1 = Vector3.Cross(Axis.Y, pickNormal);
  1518. var axis2 = Vector3.Cross(pickNormal, axis1);
  1519. Vector3.RotationFromAxisToRef(axis2, pickNormal, axis1, this._teleportationTarget.rotation);
  1520. }
  1521. this._teleportationTarget.position.y += 0.1;
  1522. }
  1523. }
  1524. private _workingVector = Vector3.Zero();
  1525. private _workingQuaternion = Quaternion.Identity();
  1526. private _workingMatrix = Matrix.Identity();
  1527. /**
  1528. * Teleports the users feet to the desired location
  1529. * @param location The location where the user's feet should be placed
  1530. */
  1531. public teleportCamera(location: Vector3) {
  1532. if (!(this.currentVRCamera instanceof FreeCamera)) {
  1533. return;
  1534. }
  1535. // Teleport the hmd to where the user is looking by moving the anchor to where they are looking minus the
  1536. // offset of the headset from the anchor.
  1537. if (this.webVRCamera.leftCamera) {
  1538. this._workingVector.copyFrom(this.webVRCamera.leftCamera.globalPosition);
  1539. this._workingVector.subtractInPlace(this.webVRCamera.position);
  1540. location.subtractToRef(this._workingVector, this._workingVector);
  1541. } else {
  1542. this._workingVector.copyFrom(location);
  1543. }
  1544. // Add height to account for user's height offset
  1545. if (this.isInVRMode) {
  1546. this._workingVector.y += this.webVRCamera.deviceDistanceToRoomGround() * this._webVRCamera.deviceScaleFactor;
  1547. } else {
  1548. this._workingVector.y += this._defaultHeight;
  1549. }
  1550. this.onBeforeCameraTeleport.notifyObservers(this._workingVector);
  1551. // Create animation from the camera's position to the new location
  1552. this.currentVRCamera.animations = [];
  1553. var animationCameraTeleportation = new Animation("animationCameraTeleportation", "position", 90, Animation.ANIMATIONTYPE_VECTOR3, Animation.ANIMATIONLOOPMODE_CONSTANT);
  1554. var animationCameraTeleportationKeys = [{
  1555. frame: 0,
  1556. value: this.currentVRCamera.position
  1557. },
  1558. {
  1559. frame: 11,
  1560. value: this._workingVector
  1561. }
  1562. ];
  1563. animationCameraTeleportation.setKeys(animationCameraTeleportationKeys);
  1564. animationCameraTeleportation.setEasingFunction(this._circleEase);
  1565. this.currentVRCamera.animations.push(animationCameraTeleportation);
  1566. this._postProcessMove.animations = [];
  1567. var animationPP = new Animation("animationPP", "vignetteWeight", 90, Animation.ANIMATIONTYPE_FLOAT,
  1568. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1569. var vignetteWeightKeys = [];
  1570. vignetteWeightKeys.push({
  1571. frame: 0,
  1572. value: 0
  1573. });
  1574. vignetteWeightKeys.push({
  1575. frame: 5,
  1576. value: 8
  1577. });
  1578. vignetteWeightKeys.push({
  1579. frame: 11,
  1580. value: 0
  1581. });
  1582. animationPP.setKeys(vignetteWeightKeys);
  1583. this._postProcessMove.animations.push(animationPP);
  1584. var animationPP2 = new Animation("animationPP2", "vignetteStretch", 90, Animation.ANIMATIONTYPE_FLOAT,
  1585. Animation.ANIMATIONLOOPMODE_CONSTANT);
  1586. var vignetteStretchKeys = [];
  1587. vignetteStretchKeys.push({
  1588. frame: 0,
  1589. value: 0
  1590. });
  1591. vignetteStretchKeys.push({
  1592. frame: 5,
  1593. value: 10
  1594. });
  1595. vignetteStretchKeys.push({
  1596. frame: 11,
  1597. value: 0
  1598. });
  1599. animationPP2.setKeys(vignetteStretchKeys);
  1600. this._postProcessMove.animations.push(animationPP2);
  1601. this._postProcessMove.imageProcessingConfiguration.vignetteWeight = 0;
  1602. this._postProcessMove.imageProcessingConfiguration.vignetteStretch = 0;
  1603. this._webVRCamera.attachPostProcess(this._postProcessMove);
  1604. this._scene.beginAnimation(this._postProcessMove, 0, 11, false, 1, () => {
  1605. this._webVRCamera.detachPostProcess(this._postProcessMove);
  1606. });
  1607. this._scene.beginAnimation(this.currentVRCamera, 0, 11, false, 1, () => {
  1608. this.onAfterCameraTeleport.notifyObservers(this._workingVector);
  1609. });
  1610. this._hideTeleportationTarget();
  1611. }
  1612. private _convertNormalToDirectionOfRay(normal: Nullable<Vector3>, ray: Ray) {
  1613. if (normal) {
  1614. var angle = Math.acos(Vector3.Dot(normal, ray.direction));
  1615. if (angle < Math.PI / 2) {
  1616. normal.scaleInPlace(-1);
  1617. }
  1618. }
  1619. return normal;
  1620. }
  1621. private _castRayAndSelectObject(gazer: VRExperienceHelperGazer) {
  1622. if (!(this.currentVRCamera instanceof FreeCamera)) {
  1623. return;
  1624. }
  1625. var ray = gazer._getForwardRay(this._rayLength);
  1626. var hit = this._scene.pickWithRay(ray, this._raySelectionPredicate);
  1627. if (hit) {
  1628. // Populate the contrllers mesh that can be used for drag/drop
  1629. if ((<any>gazer)._laserPointer) {
  1630. hit.originMesh = (<any>gazer)._laserPointer.parent;
  1631. }
  1632. this._scene.simulatePointerMove(hit, { pointerId: gazer._id });
  1633. }
  1634. gazer._currentHit = hit;
  1635. // Moving the gazeTracker on the mesh face targetted
  1636. if (hit && hit.pickedPoint) {
  1637. if (this._displayGaze) {
  1638. let multiplier = 1;
  1639. gazer._gazeTracker.isVisible = true;
  1640. if (gazer._isActionableMesh) {
  1641. multiplier = 3;
  1642. }
  1643. if (this.updateGazeTrackerScale) {
  1644. gazer._gazeTracker.scaling.x = hit.distance * multiplier;
  1645. gazer._gazeTracker.scaling.y = hit.distance * multiplier;
  1646. gazer._gazeTracker.scaling.z = hit.distance * multiplier;
  1647. }
  1648. var pickNormal = this._convertNormalToDirectionOfRay(hit.getNormal(), ray);
  1649. // To avoid z-fighting
  1650. let deltaFighting = 0.002;
  1651. if (pickNormal) {
  1652. var axis1 = Vector3.Cross(Axis.Y, pickNormal);
  1653. var axis2 = Vector3.Cross(pickNormal, axis1);
  1654. Vector3.RotationFromAxisToRef(axis2, pickNormal, axis1, gazer._gazeTracker.rotation);
  1655. }
  1656. gazer._gazeTracker.position.copyFrom(hit.pickedPoint);
  1657. if (gazer._gazeTracker.position.x < 0) {
  1658. gazer._gazeTracker.position.x += deltaFighting;
  1659. }
  1660. else {
  1661. gazer._gazeTracker.position.x -= deltaFighting;
  1662. }
  1663. if (gazer._gazeTracker.position.y < 0) {
  1664. gazer._gazeTracker.position.y += deltaFighting;
  1665. }
  1666. else {
  1667. gazer._gazeTracker.position.y -= deltaFighting;
  1668. }
  1669. if (gazer._gazeTracker.position.z < 0) {
  1670. gazer._gazeTracker.position.z += deltaFighting;
  1671. }
  1672. else {
  1673. gazer._gazeTracker.position.z -= deltaFighting;
  1674. }
  1675. }
  1676. // Changing the size of the laser pointer based on the distance from the targetted point
  1677. gazer._updatePointerDistance(hit.distance);
  1678. }
  1679. else {
  1680. gazer._updatePointerDistance();
  1681. gazer._gazeTracker.isVisible = false;
  1682. }
  1683. if (hit && hit.pickedMesh) {
  1684. // The object selected is the floor, we're in a teleportation scenario
  1685. if (this._teleportationInitialized && this._isTeleportationFloor(hit.pickedMesh) && hit.pickedPoint) {
  1686. // Moving the teleportation area to this targetted point
  1687. //Raise onSelectedMeshUnselected observable if ray collided floor mesh/meshes and a non floor mesh was previously selected
  1688. if (gazer._currentMeshSelected && !this._isTeleportationFloor(gazer._currentMeshSelected)) {
  1689. this._notifySelectedMeshUnselected(gazer._currentMeshSelected);
  1690. }
  1691. gazer._currentMeshSelected = null;
  1692. if (gazer._teleportationRequestInitiated) {
  1693. this._moveTeleportationSelectorTo(hit, gazer, ray);
  1694. }
  1695. return;
  1696. }
  1697. // If not, we're in a selection scenario
  1698. //this._teleportationAllowed = false;
  1699. if (hit.pickedMesh !== gazer._currentMeshSelected) {
  1700. if (this.meshSelectionPredicate(hit.pickedMesh)) {
  1701. this.onNewMeshPicked.notifyObservers(hit);
  1702. gazer._currentMeshSelected = hit.pickedMesh;
  1703. if (hit.pickedMesh.isPickable && hit.pickedMesh.actionManager) {
  1704. this.changeGazeColor(new Color3(0, 0, 1));
  1705. this.changeLaserColor(new Color3(0.2, 0.2, 1));
  1706. gazer._isActionableMesh = true;
  1707. }
  1708. else {
  1709. this.changeGazeColor(new Color3(0.7, 0.7, 0.7));
  1710. this.changeLaserColor(new Color3(0.7, 0.7, 0.7));
  1711. gazer._isActionableMesh = false;
  1712. }
  1713. try {
  1714. this.onNewMeshSelected.notifyObservers(hit.pickedMesh);
  1715. }
  1716. catch (err) {
  1717. Logger.Warn("Error in your custom logic onNewMeshSelected: " + err);
  1718. }
  1719. }
  1720. else {
  1721. this._notifySelectedMeshUnselected(gazer._currentMeshSelected);
  1722. gazer._currentMeshSelected = null;
  1723. this.changeGazeColor(new Color3(0.7, 0.7, 0.7));
  1724. this.changeLaserColor(new Color3(0.7, 0.7, 0.7));
  1725. }
  1726. }
  1727. }
  1728. else {
  1729. this._notifySelectedMeshUnselected(gazer._currentMeshSelected);
  1730. gazer._currentMeshSelected = null;
  1731. //this._teleportationAllowed = false;
  1732. this.changeGazeColor(new Color3(0.7, 0.7, 0.7));
  1733. this.changeLaserColor(new Color3(0.7, 0.7, 0.7));
  1734. }
  1735. }
  1736. private _notifySelectedMeshUnselected(mesh: Nullable<AbstractMesh>) {
  1737. if (mesh) {
  1738. this.onSelectedMeshUnselected.notifyObservers(mesh);
  1739. }
  1740. }
  1741. /**
  1742. * Sets the color of the laser ray from the vr controllers.
  1743. * @param color new color for the ray.
  1744. */
  1745. public changeLaserColor(color: Color3) {
  1746. if (this._leftController) {
  1747. this._leftController._setLaserPointerColor(color);
  1748. }
  1749. if (this._rightController) {
  1750. this._rightController._setLaserPointerColor(color);
  1751. }
  1752. }
  1753. /**
  1754. * Sets the color of the ray from the vr headsets gaze.
  1755. * @param color new color for the ray.
  1756. */
  1757. public changeGazeColor(color: Color3) {
  1758. if (!this.updateGazeTrackerColor) {
  1759. return;
  1760. }
  1761. if (!(<StandardMaterial>this._cameraGazer._gazeTracker.material)) {
  1762. return;
  1763. }
  1764. (<StandardMaterial>this._cameraGazer._gazeTracker.material).emissiveColor = color;
  1765. if (this._leftController) {
  1766. (<StandardMaterial>this._leftController._gazeTracker.material).emissiveColor = color;
  1767. }
  1768. if (this._rightController) {
  1769. (<StandardMaterial>this._rightController._gazeTracker.material).emissiveColor = color;
  1770. }
  1771. }
  1772. /**
  1773. * Exits VR and disposes of the vr experience helper
  1774. */
  1775. public dispose() {
  1776. if (this.isInVRMode) {
  1777. this.exitVR();
  1778. }
  1779. if (this._postProcessMove) {
  1780. this._postProcessMove.dispose();
  1781. }
  1782. if (this._webVRCamera) {
  1783. this._webVRCamera.dispose();
  1784. }
  1785. if (this._vrDeviceOrientationCamera) {
  1786. this._vrDeviceOrientationCamera.dispose();
  1787. }
  1788. if (!this._useCustomVRButton && this._btnVR && this._btnVR.parentNode) {
  1789. document.body.removeChild(this._btnVR);
  1790. }
  1791. if (this._deviceOrientationCamera && (this._scene.activeCamera != this._deviceOrientationCamera)) {
  1792. this._deviceOrientationCamera.dispose();
  1793. }
  1794. if (this._cameraGazer) {
  1795. this._cameraGazer.dispose();
  1796. }
  1797. if (this._leftController) {
  1798. this._leftController.dispose();
  1799. }
  1800. if (this._rightController) {
  1801. this._rightController.dispose();
  1802. }
  1803. if (this._teleportationTarget) {
  1804. this._teleportationTarget.dispose();
  1805. }
  1806. this._floorMeshesCollection = [];
  1807. document.removeEventListener("keydown", this._onKeyDown);
  1808. window.removeEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  1809. window.removeEventListener("resize", this._onResize);
  1810. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  1811. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  1812. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  1813. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  1814. (<any>document).onmsfullscreenchange = null;
  1815. this._scene.getEngine().onVRDisplayChangedObservable.removeCallback(this._onVRDisplayChanged);
  1816. this._scene.getEngine().onVRRequestPresentStart.removeCallback(this._onVRRequestPresentStart);
  1817. this._scene.getEngine().onVRRequestPresentComplete.removeCallback(this._onVRRequestPresentComplete);
  1818. window.removeEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
  1819. this._scene.gamepadManager.onGamepadConnectedObservable.removeCallback(this._onNewGamepadConnected);
  1820. this._scene.gamepadManager.onGamepadDisconnectedObservable.removeCallback(this._onNewGamepadDisconnected);
  1821. this._scene.unregisterBeforeRender(this.beforeRender);
  1822. }
  1823. /**
  1824. * Gets the name of the VRExperienceHelper class
  1825. * @returns "VRExperienceHelper"
  1826. */
  1827. public getClassName(): string {
  1828. return "VRExperienceHelper";
  1829. }
  1830. }