webXRFeaturesManager.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import { Tools } from "../Misc/tools.js";
  2. /**
  3. * A list of the currently available features without referencing them
  4. */
  5. export class WebXRFeatureName {
  6. }
  7. /**
  8. * The name of the anchor system feature
  9. */
  10. WebXRFeatureName.ANCHOR_SYSTEM = "xr-anchor-system";
  11. /**
  12. * The name of the background remover feature
  13. */
  14. WebXRFeatureName.BACKGROUND_REMOVER = "xr-background-remover";
  15. /**
  16. * The name of the hit test feature
  17. */
  18. WebXRFeatureName.HIT_TEST = "xr-hit-test";
  19. /**
  20. * The name of the mesh detection feature
  21. */
  22. WebXRFeatureName.MESH_DETECTION = "xr-mesh-detection";
  23. /**
  24. * physics impostors for xr controllers feature
  25. */
  26. WebXRFeatureName.PHYSICS_CONTROLLERS = "xr-physics-controller";
  27. /**
  28. * The name of the plane detection feature
  29. */
  30. WebXRFeatureName.PLANE_DETECTION = "xr-plane-detection";
  31. /**
  32. * The name of the pointer selection feature
  33. */
  34. WebXRFeatureName.POINTER_SELECTION = "xr-controller-pointer-selection";
  35. /**
  36. * The name of the teleportation feature
  37. */
  38. WebXRFeatureName.TELEPORTATION = "xr-controller-teleportation";
  39. /**
  40. * The name of the feature points feature.
  41. */
  42. WebXRFeatureName.FEATURE_POINTS = "xr-feature-points";
  43. /**
  44. * The name of the hand tracking feature.
  45. */
  46. WebXRFeatureName.HAND_TRACKING = "xr-hand-tracking";
  47. /**
  48. * The name of the image tracking feature
  49. */
  50. WebXRFeatureName.IMAGE_TRACKING = "xr-image-tracking";
  51. /**
  52. * The name of the near interaction feature
  53. */
  54. WebXRFeatureName.NEAR_INTERACTION = "xr-near-interaction";
  55. /**
  56. * The name of the DOM overlay feature
  57. */
  58. WebXRFeatureName.DOM_OVERLAY = "xr-dom-overlay";
  59. /**
  60. * The name of the movement feature
  61. */
  62. WebXRFeatureName.MOVEMENT = "xr-controller-movement";
  63. /**
  64. * The name of the light estimation feature
  65. */
  66. WebXRFeatureName.LIGHT_ESTIMATION = "xr-light-estimation";
  67. /**
  68. * The name of the eye tracking feature
  69. */
  70. WebXRFeatureName.EYE_TRACKING = "xr-eye-tracking";
  71. /**
  72. * The name of the walking locomotion feature
  73. */
  74. WebXRFeatureName.WALKING_LOCOMOTION = "xr-walking-locomotion";
  75. /**
  76. * The name of the composition layers feature
  77. */
  78. WebXRFeatureName.LAYERS = "xr-layers";
  79. /**
  80. * The name of the depth sensing feature
  81. */
  82. WebXRFeatureName.DEPTH_SENSING = "xr-depth-sensing";
  83. /**
  84. * The name of the WebXR Space Warp feature
  85. */
  86. WebXRFeatureName.SPACE_WARP = "xr-space-warp";
  87. /**
  88. * The name of the WebXR Raw Camera Access feature
  89. */
  90. WebXRFeatureName.RAW_CAMERA_ACCESS = "xr-raw-camera-access";
  91. /**
  92. * The WebXR features manager is responsible of enabling or disabling features required for the current XR session.
  93. * It is mainly used in AR sessions.
  94. *
  95. * A feature can have a version that is defined by Babylon (and does not correspond with the webxr version).
  96. */
  97. export class WebXRFeaturesManager {
  98. /**
  99. * constructs a new features manages.
  100. *
  101. * @param _xrSessionManager an instance of WebXRSessionManager
  102. */
  103. constructor(_xrSessionManager) {
  104. this._xrSessionManager = _xrSessionManager;
  105. this._features = {};
  106. // when session starts / initialized - attach
  107. this._xrSessionManager.onXRSessionInit.add(() => {
  108. this.getEnabledFeatures().forEach((featureName) => {
  109. const feature = this._features[featureName];
  110. if (feature.enabled && !feature.featureImplementation.attached && !feature.featureImplementation.disableAutoAttach) {
  111. this.attachFeature(featureName);
  112. }
  113. });
  114. });
  115. // when session ends - detach
  116. this._xrSessionManager.onXRSessionEnded.add(() => {
  117. this.getEnabledFeatures().forEach((featureName) => {
  118. const feature = this._features[featureName];
  119. if (feature.enabled && feature.featureImplementation.attached) {
  120. // detach, but don't disable!
  121. this.detachFeature(featureName);
  122. }
  123. });
  124. });
  125. }
  126. /**
  127. * Used to register a module. After calling this function a developer can use this feature in the scene.
  128. * Mainly used internally.
  129. *
  130. * @param featureName the name of the feature to register
  131. * @param constructorFunction the function used to construct the module
  132. * @param version the (babylon) version of the module
  133. * @param stable is that a stable version of this module
  134. */
  135. static AddWebXRFeature(featureName, constructorFunction, version = 1, stable = false) {
  136. this._AvailableFeatures[featureName] = this._AvailableFeatures[featureName] || { latest: version };
  137. if (version > this._AvailableFeatures[featureName].latest) {
  138. this._AvailableFeatures[featureName].latest = version;
  139. }
  140. if (stable) {
  141. this._AvailableFeatures[featureName].stable = version;
  142. }
  143. this._AvailableFeatures[featureName][version] = constructorFunction;
  144. }
  145. /**
  146. * Returns a constructor of a specific feature.
  147. *
  148. * @param featureName the name of the feature to construct
  149. * @param version the version of the feature to load
  150. * @param xrSessionManager the xrSessionManager. Used to construct the module
  151. * @param options optional options provided to the module.
  152. * @returns a function that, when called, will return a new instance of this feature
  153. */
  154. static ConstructFeature(featureName, version = 1, xrSessionManager, options) {
  155. const constructorFunction = this._AvailableFeatures[featureName][version];
  156. if (!constructorFunction) {
  157. // throw an error? return nothing?
  158. throw new Error("feature not found");
  159. }
  160. return constructorFunction(xrSessionManager, options);
  161. }
  162. /**
  163. * Can be used to return the list of features currently registered
  164. *
  165. * @returns an Array of available features
  166. */
  167. static GetAvailableFeatures() {
  168. return Object.keys(this._AvailableFeatures);
  169. }
  170. /**
  171. * Gets the versions available for a specific feature
  172. * @param featureName the name of the feature
  173. * @returns an array with the available versions
  174. */
  175. static GetAvailableVersions(featureName) {
  176. return Object.keys(this._AvailableFeatures[featureName]);
  177. }
  178. /**
  179. * Return the latest unstable version of this feature
  180. * @param featureName the name of the feature to search
  181. * @returns the version number. if not found will return -1
  182. */
  183. static GetLatestVersionOfFeature(featureName) {
  184. return (this._AvailableFeatures[featureName] && this._AvailableFeatures[featureName].latest) || -1;
  185. }
  186. /**
  187. * Return the latest stable version of this feature
  188. * @param featureName the name of the feature to search
  189. * @returns the version number. if not found will return -1
  190. */
  191. static GetStableVersionOfFeature(featureName) {
  192. return (this._AvailableFeatures[featureName] && this._AvailableFeatures[featureName].stable) || -1;
  193. }
  194. /**
  195. * Attach a feature to the current session. Mainly used when session started to start the feature effect.
  196. * Can be used during a session to start a feature
  197. * @param featureName the name of feature to attach
  198. */
  199. attachFeature(featureName) {
  200. const feature = this._features[featureName];
  201. if (feature && feature.enabled && !feature.featureImplementation.attached) {
  202. const attached = feature.featureImplementation.attach();
  203. if (!attached) {
  204. Tools.Warn(`Feature ${featureName} failed to attach`);
  205. }
  206. }
  207. }
  208. /**
  209. * Can be used inside a session or when the session ends to detach a specific feature
  210. * @param featureName the name of the feature to detach
  211. */
  212. detachFeature(featureName) {
  213. const feature = this._features[featureName];
  214. if (feature && feature.featureImplementation.attached) {
  215. const detached = feature.featureImplementation.detach();
  216. if (!detached) {
  217. Tools.Warn(`Feature ${featureName} failed to detach`);
  218. }
  219. }
  220. }
  221. /**
  222. * Used to disable an already-enabled feature
  223. * The feature will be disposed and will be recreated once enabled.
  224. * @param featureName the feature to disable
  225. * @returns true if disable was successful
  226. */
  227. // eslint-disable-next-line @typescript-eslint/naming-convention
  228. disableFeature(featureName) {
  229. const name = typeof featureName === "string" ? featureName : featureName.Name;
  230. const feature = this._features[name];
  231. if (feature && feature.enabled) {
  232. feature.enabled = false;
  233. this.detachFeature(name);
  234. feature.featureImplementation.dispose();
  235. delete this._features[name];
  236. return true;
  237. }
  238. return false;
  239. }
  240. /**
  241. * dispose this features manager
  242. */
  243. dispose() {
  244. this.getEnabledFeatures().forEach((feature) => {
  245. this.disableFeature(feature);
  246. });
  247. }
  248. /**
  249. * 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.
  250. * 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.
  251. *
  252. * @param featureName the name of the feature to load or the class of the feature
  253. * @param version optional version to load. if not provided the latest version will be enabled
  254. * @param moduleOptions options provided to the module. Ses the module documentation / constructor
  255. * @param attachIfPossible if set to true (default) the feature will be automatically attached, if it is currently possible
  256. * @param required is this feature required to the app. If set to true the session init will fail if the feature is not available.
  257. * @returns a new constructed feature or throws an error if feature not found or conflicts with another enabled feature.
  258. */
  259. enableFeature(
  260. // eslint-disable-next-line @typescript-eslint/naming-convention
  261. featureName, version = "latest", moduleOptions = {}, attachIfPossible = true, required = true) {
  262. const name = typeof featureName === "string" ? featureName : featureName.Name;
  263. let versionToLoad = 0;
  264. if (typeof version === "string") {
  265. if (!version) {
  266. throw new Error(`Error in provided version - ${name} (${version})`);
  267. }
  268. if (version === "stable") {
  269. versionToLoad = WebXRFeaturesManager.GetStableVersionOfFeature(name);
  270. }
  271. else if (version === "latest") {
  272. versionToLoad = WebXRFeaturesManager.GetLatestVersionOfFeature(name);
  273. }
  274. else {
  275. // try loading the number the string represents
  276. versionToLoad = +version;
  277. }
  278. if (versionToLoad === -1 || isNaN(versionToLoad)) {
  279. throw new Error(`feature not found - ${name} (${version})`);
  280. }
  281. }
  282. else {
  283. versionToLoad = version;
  284. }
  285. // check if there is a feature conflict
  286. const conflictingFeature = WebXRFeaturesManager._ConflictingFeatures[name];
  287. if (conflictingFeature !== undefined && this.getEnabledFeatures().indexOf(conflictingFeature) !== -1) {
  288. throw new Error(`Feature ${name} cannot be enabled while ${conflictingFeature} is enabled.`);
  289. }
  290. // check if already initialized
  291. const feature = this._features[name];
  292. const constructFunction = WebXRFeaturesManager.ConstructFeature(name, versionToLoad, this._xrSessionManager, moduleOptions);
  293. if (!constructFunction) {
  294. // report error?
  295. throw new Error(`feature not found - ${name}`);
  296. }
  297. /* If the feature is already enabled, detach and dispose it, and create a new one */
  298. if (feature) {
  299. this.disableFeature(name);
  300. }
  301. const constructed = constructFunction();
  302. if (constructed.dependsOn) {
  303. const dependentsFound = constructed.dependsOn.every((featureName) => !!this._features[featureName]);
  304. if (!dependentsFound) {
  305. throw new Error(`Dependant features missing. Make sure the following features are enabled - ${constructed.dependsOn.join(", ")}`);
  306. }
  307. }
  308. if (constructed.isCompatible()) {
  309. this._features[name] = {
  310. featureImplementation: constructed,
  311. enabled: true,
  312. version: versionToLoad,
  313. required,
  314. };
  315. if (attachIfPossible) {
  316. // if session started already, request and enable
  317. if (this._xrSessionManager.session && !this._features[name].featureImplementation.attached) {
  318. // enable feature
  319. this.attachFeature(name);
  320. }
  321. }
  322. else {
  323. // disable auto-attach when session starts
  324. this._features[name].featureImplementation.disableAutoAttach = true;
  325. }
  326. return this._features[name].featureImplementation;
  327. }
  328. else {
  329. if (required) {
  330. throw new Error("required feature not compatible");
  331. }
  332. else {
  333. Tools.Warn(`Feature ${name} not compatible with the current environment/browser and was not enabled.`);
  334. return constructed;
  335. }
  336. }
  337. }
  338. /**
  339. * get the implementation of an enabled feature.
  340. * @param featureName the name of the feature to load
  341. * @returns the feature class, if found
  342. */
  343. getEnabledFeature(featureName) {
  344. return this._features[featureName] && this._features[featureName].featureImplementation;
  345. }
  346. /**
  347. * Get the list of enabled features
  348. * @returns an array of enabled features
  349. */
  350. getEnabledFeatures() {
  351. return Object.keys(this._features);
  352. }
  353. /**
  354. * This function will extend the session creation configuration object with enabled features.
  355. * If, for example, the anchors feature is enabled, it will be automatically added to the optional or required features list,
  356. * according to the defined "required" variable, provided during enableFeature call
  357. * @param xrSessionInit the xr Session init object to extend
  358. *
  359. * @returns an extended XRSessionInit object
  360. */
  361. async _extendXRSessionInitObject(xrSessionInit) {
  362. const enabledFeatures = this.getEnabledFeatures();
  363. for (const featureName of enabledFeatures) {
  364. const feature = this._features[featureName];
  365. const nativeName = feature.featureImplementation.xrNativeFeatureName;
  366. if (nativeName) {
  367. if (feature.required) {
  368. xrSessionInit.requiredFeatures = xrSessionInit.requiredFeatures || [];
  369. if (xrSessionInit.requiredFeatures.indexOf(nativeName) === -1) {
  370. xrSessionInit.requiredFeatures.push(nativeName);
  371. }
  372. }
  373. else {
  374. xrSessionInit.optionalFeatures = xrSessionInit.optionalFeatures || [];
  375. if (xrSessionInit.optionalFeatures.indexOf(nativeName) === -1) {
  376. xrSessionInit.optionalFeatures.push(nativeName);
  377. }
  378. }
  379. }
  380. if (feature.featureImplementation.getXRSessionInitExtension) {
  381. const extended = await feature.featureImplementation.getXRSessionInitExtension();
  382. xrSessionInit = {
  383. ...xrSessionInit,
  384. ...extended,
  385. };
  386. }
  387. }
  388. return xrSessionInit;
  389. }
  390. }
  391. WebXRFeaturesManager._AvailableFeatures = {};
  392. /**
  393. * The key is the feature to check and the value is the feature that conflicts.
  394. */
  395. WebXRFeaturesManager._ConflictingFeatures = {
  396. [WebXRFeatureName.TELEPORTATION]: WebXRFeatureName.MOVEMENT,
  397. [WebXRFeatureName.MOVEMENT]: WebXRFeatureName.TELEPORTATION,
  398. };
  399. //# sourceMappingURL=webXRFeaturesManager.js.map