vrExperienceHelper.ts 84 KB

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