WebXRDepthSensing.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { RawTexture } from "../../Materials/Textures/rawTexture.js";
  2. import { WebXRFeatureName, WebXRFeaturesManager } from "../webXRFeaturesManager.js";
  3. import { WebXRAbstractFeature } from "./WebXRAbstractFeature.js";
  4. import { Tools } from "../../Misc/tools.js";
  5. import { Texture } from "../../Materials/Textures/texture.js";
  6. import { Engine } from "../../Engines/engine.js";
  7. import { Observable } from "../../Misc/observable.js";
  8. import { WebGLHardwareTexture } from "../../Engines/WebGL/webGLHardwareTexture.js";
  9. import { InternalTexture, InternalTextureSource } from "../../Materials/Textures/internalTexture.js";
  10. /**
  11. * WebXR Feature for WebXR Depth Sensing Module
  12. * @since 5.49.1
  13. */
  14. export class WebXRDepthSensing extends WebXRAbstractFeature {
  15. /**
  16. * Width of depth data. If depth data is not exist, returns null.
  17. */
  18. get width() {
  19. return this._width;
  20. }
  21. /**
  22. * Height of depth data. If depth data is not exist, returns null.
  23. */
  24. get height() {
  25. return this._height;
  26. }
  27. /**
  28. * Scale factor by which the raw depth values must be multiplied in order to get the depths in meters.
  29. */
  30. get rawValueToMeters() {
  31. return this._rawValueToMeters;
  32. }
  33. /**
  34. * An XRRigidTransform that needs to be applied when indexing into the depth buffer.
  35. */
  36. get normDepthBufferFromNormView() {
  37. return this._normDepthBufferFromNormView;
  38. }
  39. /**
  40. * Describes which depth-sensing usage ("cpu" or "gpu") is used.
  41. */
  42. get depthUsage() {
  43. switch (this._xrSessionManager.session.depthUsage) {
  44. case "cpu-optimized":
  45. return "cpu";
  46. case "gpu-optimized":
  47. return "gpu";
  48. }
  49. }
  50. /**
  51. * Describes which depth sensing data format ("ushort" or "float") is used.
  52. */
  53. get depthDataFormat() {
  54. switch (this._xrSessionManager.session.depthDataFormat) {
  55. case "luminance-alpha":
  56. return "ushort";
  57. case "float32":
  58. return "float";
  59. }
  60. }
  61. /**
  62. * Latest cached InternalTexture which containing depth buffer information.
  63. * This can be used when the depth usage is "gpu".
  64. */
  65. get latestInternalTexture() {
  66. if (!this._cachedWebGLTexture) {
  67. return null;
  68. }
  69. const engine = this._xrSessionManager.scene.getEngine();
  70. const internalTexture = new InternalTexture(engine, InternalTextureSource.Unknown);
  71. internalTexture.isCube = false;
  72. internalTexture.invertY = false;
  73. internalTexture._useSRGBBuffer = false;
  74. internalTexture.format = this.depthDataFormat === "ushort" ? 2 : 5;
  75. internalTexture.generateMipMaps = false;
  76. internalTexture.type = this.depthDataFormat === "ushort" ? 5 : 1;
  77. internalTexture.samplingMode = 7;
  78. internalTexture.width = this.width ?? 0;
  79. internalTexture.height = this.height ?? 0;
  80. internalTexture._cachedWrapU = 1;
  81. internalTexture._cachedWrapV = 1;
  82. internalTexture._hardwareTexture = new WebGLHardwareTexture(this._cachedWebGLTexture, engine._gl);
  83. return internalTexture;
  84. }
  85. /**
  86. * cached depth buffer
  87. */
  88. get latestDepthBuffer() {
  89. if (!this._cachedDepthBuffer) {
  90. return null;
  91. }
  92. return this.depthDataFormat === "ushort" ? new Uint16Array(this._cachedDepthBuffer) : new Float32Array(this._cachedDepthBuffer);
  93. }
  94. /**
  95. * Latest cached Texture of depth image which is made from the depth buffer data.
  96. */
  97. get latestDepthImageTexture() {
  98. return this._cachedDepthImageTexture;
  99. }
  100. /**
  101. * Creates a new instance of the depth sensing feature
  102. * @param _xrSessionManager the WebXRSessionManager
  103. * @param options options for WebXR Depth Sensing Feature
  104. */
  105. constructor(_xrSessionManager, options) {
  106. super(_xrSessionManager);
  107. this.options = options;
  108. this._width = null;
  109. this._height = null;
  110. this._rawValueToMeters = null;
  111. this._normDepthBufferFromNormView = null;
  112. this._cachedDepthBuffer = null;
  113. this._cachedWebGLTexture = null;
  114. this._cachedDepthImageTexture = null;
  115. /**
  116. * Event that notify when `DepthInformation.getDepthInMeters` is available.
  117. * `getDepthInMeters` method needs active XRFrame (not available for cached XRFrame)
  118. */
  119. this.onGetDepthInMetersAvailable = new Observable();
  120. this.xrNativeFeatureName = "depth-sensing";
  121. // https://immersive-web.github.io/depth-sensing/
  122. Tools.Warn("depth-sensing is an experimental and unstable feature.");
  123. }
  124. /**
  125. * attach this feature
  126. * Will usually be called by the features manager
  127. * @param force should attachment be forced (even when already attached)
  128. * @returns true if successful.
  129. */
  130. attach(force) {
  131. if (!super.attach(force)) {
  132. return false;
  133. }
  134. const isBothDepthUsageAndFormatNull = this._xrSessionManager.session.depthDataFormat == null || this._xrSessionManager.session.depthUsage == null;
  135. if (isBothDepthUsageAndFormatNull) {
  136. return false;
  137. }
  138. this._glBinding = new XRWebGLBinding(this._xrSessionManager.session, this._xrSessionManager.scene.getEngine()._gl);
  139. return true;
  140. }
  141. /**
  142. * Dispose this feature and all of the resources attached
  143. */
  144. dispose() {
  145. this._cachedDepthImageTexture?.dispose();
  146. }
  147. _onXRFrame(_xrFrame) {
  148. const referenceSPace = this._xrSessionManager.referenceSpace;
  149. const pose = _xrFrame.getViewerPose(referenceSPace);
  150. if (pose == null) {
  151. return;
  152. }
  153. for (const view of pose.views) {
  154. switch (this.depthUsage) {
  155. case "cpu":
  156. this._updateDepthInformationAndTextureCPUDepthUsage(_xrFrame, view, this.depthDataFormat);
  157. break;
  158. case "gpu":
  159. if (!this._glBinding) {
  160. break;
  161. }
  162. this._updateDepthInformationAndTextureWebGLDepthUsage(this._glBinding, view, this.depthDataFormat);
  163. break;
  164. default:
  165. Tools.Error("Unknown depth usage");
  166. this.detach();
  167. break;
  168. }
  169. }
  170. }
  171. _updateDepthInformationAndTextureCPUDepthUsage(frame, view, dataFormat) {
  172. const depthInfo = frame.getDepthInformation(view);
  173. if (depthInfo === null) {
  174. return;
  175. }
  176. const { data, width, height, rawValueToMeters, getDepthInMeters } = depthInfo;
  177. this._width = width;
  178. this._height = height;
  179. this._rawValueToMeters = rawValueToMeters;
  180. this._cachedDepthBuffer = data;
  181. // to avoid Illegal Invocation error, bind `this`
  182. this.onGetDepthInMetersAvailable.notifyObservers(getDepthInMeters.bind(depthInfo));
  183. if (!this._cachedDepthImageTexture) {
  184. this._cachedDepthImageTexture = RawTexture.CreateRTexture(null, width, height, this._xrSessionManager.scene, false, true, Texture.NEAREST_SAMPLINGMODE, Engine.TEXTURETYPE_FLOAT);
  185. }
  186. switch (dataFormat) {
  187. case "ushort":
  188. this._cachedDepthImageTexture.update(Float32Array.from(new Uint16Array(data)).map((value) => value * rawValueToMeters));
  189. break;
  190. case "float":
  191. this._cachedDepthImageTexture.update(new Float32Array(data).map((value) => value * rawValueToMeters));
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. _updateDepthInformationAndTextureWebGLDepthUsage(webglBinding, view, dataFormat) {
  198. const depthInfo = webglBinding.getDepthInformation(view);
  199. if (depthInfo === null) {
  200. return;
  201. }
  202. const { texture, width, height } = depthInfo;
  203. this._width = width;
  204. this._height = height;
  205. this._cachedWebGLTexture = texture;
  206. const scene = this._xrSessionManager.scene;
  207. const engine = scene.getEngine();
  208. const internalTexture = engine.wrapWebGLTexture(texture);
  209. if (!this._cachedDepthImageTexture) {
  210. this._cachedDepthImageTexture = RawTexture.CreateRTexture(null, width, height, scene, false, true, Texture.NEAREST_SAMPLINGMODE, dataFormat === "ushort" ? Engine.TEXTURETYPE_UNSIGNED_BYTE : Engine.TEXTURETYPE_FLOAT);
  211. }
  212. this._cachedDepthImageTexture._texture = internalTexture;
  213. }
  214. /**
  215. * Extends the session init object if needed
  216. * @returns augmentation object for the xr session init object.
  217. */
  218. getXRSessionInitExtension() {
  219. const isDepthUsageDeclared = this.options.usagePreference != null && this.options.usagePreference.length !== 0;
  220. const isDataFormatDeclared = this.options.dataFormatPreference != null && this.options.dataFormatPreference.length !== 0;
  221. return new Promise((resolve) => {
  222. if (isDepthUsageDeclared && isDataFormatDeclared) {
  223. const usages = this.options.usagePreference.map((usage) => {
  224. switch (usage) {
  225. case "cpu":
  226. return "cpu-optimized";
  227. case "gpu":
  228. return "gpu-optimized";
  229. }
  230. });
  231. const dataFormats = this.options.dataFormatPreference.map((format) => {
  232. switch (format) {
  233. case "ushort":
  234. return "luminance-alpha";
  235. case "float":
  236. return "float32";
  237. }
  238. });
  239. resolve({
  240. depthSensing: {
  241. usagePreference: usages,
  242. dataFormatPreference: dataFormats,
  243. },
  244. });
  245. }
  246. else {
  247. resolve({});
  248. }
  249. });
  250. }
  251. }
  252. /**
  253. * The module's name
  254. */
  255. WebXRDepthSensing.Name = WebXRFeatureName.DEPTH_SENSING;
  256. /**
  257. * The (Babylon) version of this module.
  258. * This is an integer representing the implementation version.
  259. * This number does not correspond to the WebXR specs version
  260. */
  261. WebXRDepthSensing.Version = 1;
  262. WebXRFeaturesManager.AddWebXRFeature(WebXRDepthSensing.Name, (xrSessionManager, options) => {
  263. return () => new WebXRDepthSensing(xrSessionManager, options);
  264. }, WebXRDepthSensing.Version, false);
  265. //# sourceMappingURL=WebXRDepthSensing.js.map