webXRFeaturesManager.ts 13 KB

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