webXRFeaturesManager.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import { WebXRSessionManager } from './webXRSessionManager';
  2. import { IDisposable } from '../../scene';
  3. /**
  4. * Defining the interface required for a (webxr) feature
  5. */
  6. export interface IWebXRFeature extends IDisposable {
  7. /**
  8. * Is this feature attached
  9. */
  10. attached: boolean;
  11. /**
  12. * Should auto-attach be disabled?
  13. */
  14. disableAutoAttach: boolean;
  15. /**
  16. * Attach the feature to the session
  17. * Will usually be called by the features manager
  18. *
  19. * @param force should attachment be forced (even when already attached)
  20. * @returns true if successful.
  21. */
  22. attach(force?: boolean): boolean;
  23. /**
  24. * Detach the feature from the session
  25. * Will usually be called by the features manager
  26. *
  27. * @returns true if successful.
  28. */
  29. detach(): boolean;
  30. }
  31. /**
  32. * A list of the currently available features without referencing them
  33. */
  34. export class WebXRFeatureName {
  35. /**
  36. * The name of the hit test feature
  37. */
  38. public static HIT_TEST = "xr-hit-test";
  39. /**
  40. * The name of the anchor system feature
  41. */
  42. public static ANCHOR_SYSTEM = "xr-anchor-system";
  43. /**
  44. * The name of the background remover feature
  45. */
  46. public static BACKGROUND_REMOVER = "xr-background-remover";
  47. /**
  48. * The name of the pointer selection feature
  49. */
  50. public static POINTER_SELECTION = "xr-controller-pointer-selection";
  51. /**
  52. * The name of the teleportation feature
  53. */
  54. public static TELEPORTATION = "xr-controller-teleportation";
  55. /**
  56. * The name of the plane detection feature
  57. */
  58. public static PLANE_DETECTION = "xr-plane-detection";
  59. /**
  60. * physics impostors for xr controllers feature
  61. */
  62. public static PHYSICS_CONTROLLERS = "xr-physics-controller";
  63. }
  64. /**
  65. * Defining the constructor of a feature. Used to register the modules.
  66. */
  67. export type WebXRFeatureConstructor = (xrSessionManager: WebXRSessionManager, options?: any) => (() => IWebXRFeature);
  68. /**
  69. * The WebXR features manager is responsible of enabling or disabling features required for the current XR session.
  70. * It is mainly used in AR sessions.
  71. *
  72. * A feature can have a version that is defined by Babylon (and does not correspond with the webxr version).
  73. */
  74. export class WebXRFeaturesManager implements IDisposable {
  75. private static readonly _AvailableFeatures: {
  76. [name: string]: {
  77. stable: number;
  78. latest: number;
  79. [version: number]: WebXRFeatureConstructor;
  80. }
  81. } = {};
  82. /**
  83. * Used to register a module. After calling this function a developer can use this feature in the scene.
  84. * Mainly used internally.
  85. *
  86. * @param featureName the name of the feature to register
  87. * @param constructorFunction the function used to construct the module
  88. * @param version the (babylon) version of the module
  89. * @param stable is that a stable version of this module
  90. */
  91. public static AddWebXRFeature(featureName: string, constructorFunction: WebXRFeatureConstructor, version: number = 1, stable: boolean = false) {
  92. this._AvailableFeatures[featureName] = this._AvailableFeatures[featureName] || { latest: version };
  93. if (version > this._AvailableFeatures[featureName].latest) {
  94. this._AvailableFeatures[featureName].latest = version;
  95. }
  96. if (stable) {
  97. this._AvailableFeatures[featureName].stable = version;
  98. }
  99. this._AvailableFeatures[featureName][version] = constructorFunction;
  100. }
  101. /**
  102. * Returns a constructor of a specific feature.
  103. *
  104. * @param featureName the name of the feature to construct
  105. * @param version the version of the feature to load
  106. * @param xrSessionManager the xrSessionManager. Used to construct the module
  107. * @param options optional options provided to the module.
  108. * @returns a function that, when called, will return a new instance of this feature
  109. */
  110. public static ConstructFeature(featureName: string, version: number = 1, xrSessionManager: WebXRSessionManager, options?: any): (() => IWebXRFeature) {
  111. const constructorFunction = this._AvailableFeatures[featureName][version];
  112. if (!constructorFunction) {
  113. // throw an error? return nothing?
  114. throw new Error('feature not found');
  115. }
  116. return constructorFunction(xrSessionManager, options);
  117. }
  118. /**
  119. * Return the latest unstable version of this feature
  120. * @param featureName the name of the feature to search
  121. * @returns the version number. if not found will return -1
  122. */
  123. public static GetLatestVersionOfFeature(featureName: string): number {
  124. return (this._AvailableFeatures[featureName] && this._AvailableFeatures[featureName].latest) || -1;
  125. }
  126. /**
  127. * Return the latest stable version of this feature
  128. * @param featureName the name of the feature to search
  129. * @returns the version number. if not found will return -1
  130. */
  131. public static GetStableVersionOfFeature(featureName: string): number {
  132. return (this._AvailableFeatures[featureName] && this._AvailableFeatures[featureName].stable) || -1;
  133. }
  134. /**
  135. * Can be used to return the list of features currently registered
  136. *
  137. * @returns an Array of available features
  138. */
  139. public static GetAvailableFeatures() {
  140. return Object.keys(this._AvailableFeatures);
  141. }
  142. /**
  143. * Gets the versions available for a specific feature
  144. * @param featureName the name of the feature
  145. * @returns an array with the available versions
  146. */
  147. public static GetAvailableVersions(featureName: string) {
  148. return Object.keys(this._AvailableFeatures[featureName]);
  149. }
  150. private _features: {
  151. [name: string]: {
  152. featureImplementation: IWebXRFeature,
  153. version: number,
  154. enabled: boolean
  155. }
  156. } = {};
  157. /**
  158. * constructs a new features manages.
  159. *
  160. * @param _xrSessionManager an instance of WebXRSessionManager
  161. */
  162. constructor(private _xrSessionManager: WebXRSessionManager) {
  163. // when session starts / initialized - attach
  164. this._xrSessionManager.onXRSessionInit.add(() => {
  165. this.getEnabledFeatures().forEach((featureName) => {
  166. const feature = this._features[featureName];
  167. if (feature.enabled && !feature.featureImplementation.attached && !feature.featureImplementation.disableAutoAttach) {
  168. this.attachFeature(featureName);
  169. }
  170. });
  171. });
  172. // when session ends - detach
  173. this._xrSessionManager.onXRSessionEnded.add(() => {
  174. this.getEnabledFeatures().forEach((featureName) => {
  175. const feature = this._features[featureName];
  176. if (feature.enabled && feature.featureImplementation.attached) {
  177. // detach, but don't disable!
  178. this.detachFeature(featureName);
  179. }
  180. });
  181. });
  182. }
  183. /**
  184. * Enable a feature using its name and a version. This will enable it in the scene, and will be responsible to attach it when the session starts.
  185. * If used twice, the old version will be disposed and a new one will be constructed. This way you can re-enable with different configuration.
  186. *
  187. * @param featureName the name of the feature to load or the class of the feature
  188. * @param version optional version to load. if not provided the latest version will be enabled
  189. * @param moduleOptions options provided to the module. Ses the module documentation / constructor
  190. * @param attachIfPossible if set to true (default) the feature will be automatically attached, if it is currently possible
  191. * @returns a new constructed feature or throws an error if feature not found.
  192. */
  193. public enableFeature(featureName: string | { Name: string }, version: number | string = 'latest', moduleOptions: any = {}, attachIfPossible: boolean = true): IWebXRFeature {
  194. const name = typeof featureName === 'string' ? featureName : featureName.Name;
  195. let versionToLoad = 0;
  196. if (typeof version === 'string') {
  197. if (!version) {
  198. throw new Error(`Error in provided version - ${name} (${version})`);
  199. }
  200. if (version === 'stable') {
  201. versionToLoad = WebXRFeaturesManager.GetStableVersionOfFeature(name);
  202. } else if (version === 'latest') {
  203. versionToLoad = WebXRFeaturesManager.GetLatestVersionOfFeature(name);
  204. } else {
  205. // try loading the number the string represents
  206. versionToLoad = +version;
  207. }
  208. if (versionToLoad === -1 || isNaN(versionToLoad)) {
  209. throw new Error(`feature not found - ${name} (${version})`);
  210. }
  211. } else {
  212. versionToLoad = version;
  213. }
  214. // check if already initialized
  215. const feature = this._features[name];
  216. const constructFunction = WebXRFeaturesManager.ConstructFeature(name, versionToLoad, this._xrSessionManager, moduleOptions);
  217. if (!constructFunction) {
  218. // report error?
  219. throw new Error(`feature not found - ${name}`);
  220. }
  221. /* If the feature is already enabled, detach and dispose it, and create a new one */
  222. if (feature) {
  223. this.disableFeature(name);
  224. }
  225. this._features[name] = {
  226. featureImplementation: constructFunction(),
  227. enabled: true,
  228. version: versionToLoad
  229. };
  230. if (attachIfPossible) {
  231. // if session started already, request and enable
  232. if (this._xrSessionManager.session && !feature.featureImplementation.attached) {
  233. // enable feature
  234. this.attachFeature(name);
  235. }
  236. } else {
  237. // disable auto-attach when session starts
  238. this._features[name].featureImplementation.disableAutoAttach = true;
  239. }
  240. return this._features[name].featureImplementation;
  241. }
  242. /**
  243. * Used to disable an already-enabled feature
  244. * The feature will be disposed and will be recreated once enabled.
  245. * @param featureName the feature to disable
  246. * @returns true if disable was successful
  247. */
  248. public disableFeature(featureName: string | { Name: string }): boolean {
  249. const name = typeof featureName === 'string' ? featureName : featureName.Name;
  250. const feature = this._features[name];
  251. if (feature && feature.enabled) {
  252. feature.enabled = false;
  253. this.detachFeature(name);
  254. feature.featureImplementation.dispose();
  255. return true;
  256. }
  257. return false;
  258. }
  259. /**
  260. * Attach a feature to the current session. Mainly used when session started to start the feature effect.
  261. * Can be used during a session to start a feature
  262. * @param featureName the name of feature to attach
  263. */
  264. public attachFeature(featureName: string) {
  265. const feature = this._features[featureName];
  266. if (feature && feature.enabled && !feature.featureImplementation.attached) {
  267. feature.featureImplementation.attach();
  268. }
  269. }
  270. /**
  271. * Can be used inside a session or when the session ends to detach a specific feature
  272. * @param featureName the name of the feature to detach
  273. */
  274. public detachFeature(featureName: string) {
  275. const feature = this._features[featureName];
  276. if (feature && feature.featureImplementation.attached) {
  277. feature.featureImplementation.detach();
  278. }
  279. }
  280. /**
  281. * Get the list of enabled features
  282. * @returns an array of enabled features
  283. */
  284. public getEnabledFeatures() {
  285. return Object.keys(this._features);
  286. }
  287. /**
  288. * get the implementation of an enabled feature.
  289. * @param featureName the name of the feature to load
  290. * @returns the feature class, if found
  291. */
  292. public getEnabledFeature(featureName: string): IWebXRFeature {
  293. return this._features[featureName] && this._features[featureName].featureImplementation;
  294. }
  295. /**
  296. * dispose this features manager
  297. */
  298. dispose(): void {
  299. this.getEnabledFeatures().forEach((feature) => {
  300. this.disableFeature(feature);
  301. this._features[feature].featureImplementation.dispose();
  302. });
  303. }
  304. }