# Adding Depth to Your Project Source: https://www.nianticspatial.com/docs/nsdk/how-to/ar/depth/adding_depth/?platform=swift The Niantic Spatial SDK (NSDK) exposes its depth functionality on native iOS through the **NsdkDepthSession** API. Depth data is delivered as part of the **DepthResult** type, which includes both the image and related metadata consisting of: - **frameId:** A unique identifier for the processed frame. - **timestampMs:** The timestamp of the AR frame that this awareness result was generated from. - **intrinsics:** This 3x3 matrix contains the camera's focal length, principal point, and other intrinsic parameters needed for coordinate transformations between image and camera coordinate systems. - **pose:** This 4x4 transformation matrix represents the camera's position and orientation in the world coordinate system. This can be interpreted as the world pose of the camera associated with the image that was used to infer depth. - **orientation:** The device orientation when the depth data was captured. The **RawImage** contained within a DepthResult is a **CPU-accessible buffer** storing 32-bit floating-point (ImageType.depthRawFloat) depth values in meters. --- ## Creating and Configuring a Depth Session The **NsdkDepthSession** manages the depth feature lifecycle -- configuration, activation, and retrieval of the latest depth data. It is created from an existing NsdkSession instance and works independently of other AR features. ```swift // Assume an nsdk session is set up let nsdk = NSDKSession() // Enable depth functionality let depthSession = nsdk.acquireDepthSession() // Configure and start depth inference var config = NSDKDepthSession.Configuration() do { try depthSession.configure(with: config) depthSession.start() } catch { print("Failed to configure depth session: \(error)") } ``` The default configuration infers depth at **10 FPS**, balancing accuracy and performance. ## Retrieving Depth Data `NSDKDepthSession` exposes a `$result` publisher that emits updated depth data each frame. Subscribe to it with Combine: ```swift depthSession.$result .compactMap { state -> DepthResult? in if case .success(let result) = state { return result } else { return nil } } .receive(on: DispatchQueue.main) .sink { result in // Use result.image, result.pose, result.intrinsics, result.orientation } .store(in: &cancellables) ``` The result is returned as an `NSDKAsyncState` value: - **.success(DepthResult)** -- The most recent depth frame is available and ready to use. - **.inProgress** -- The inference module has not yet finished initializing. This typically occurs shortly after starting the session, before the first depth frame is produced. - **.failure(AwarenessError)** -- The depth feature is unavailable or encountered an error. When successful, the `DepthResult` provides: - **image** -- A `RawImage` of type `.depthRawFloat`, where each pixel is a Float32 distance value in meters. - **pose** -- A 4x4 world-space camera transform corresponding to the frame used for inference. - **intrinsics** -- A 3x3 matrix defining the camera's projection parameters. - **orientation** -- The device orientation when the depth was captured. The depth image can be accessed directly on the CPU through `RawImage`, or uploaded to the GPU for rendering and occlusion processing. ## More Information The [NSDK sample project](https://www.nianticspatial.com/docs/nsdk/sample_projects/) includes an example of running depth.