Using vision pro to detect distance to real life objects

Is it possible to detect distance from the vision pro to real live objects and people? I tried using scene.raycast to perform a raycast forward from the center of the viewport, but it doesn't seem to react to real life objects, only entities.

I see mentioned here: https://developer.apple.com/forums/thread/776807?answerId=829576022#829576022, that a raycast with scene reconstruction should allow me to measure that distance, as long as the object is non-moving. How could I accomplish that?

I think that you need to use the reconstruction of the scene to get the mesh of the surrounding

https://developer.apple.com/documentation/visionos/applying-mesh-to-real-world-surroundings

Regards

Hi @AMobileDeveloper

@igerard is correct. You need to generate collision shapes using SceneReconstructionProvider so the raycast has something to collide with.

Here's a snippet that shows how to create the collision shapes.

import SwiftUI
import RealityKit
import ARKit
struct ImmersiveView: View {
@State private var root = Entity()
@State private var meshEntities:Dictionary<UUID, ModelEntity> = [:]
@State private var arKitSession = ARKitSession()
@State private var sceneReconstruction = SceneReconstructionProvider()
var body: some View {
RealityView { content in
content.add(root)
}
.task {
try? await arKitSession.run([sceneReconstruction])
for await update in sceneReconstruction.anchorUpdates {
let meshAnchor = update.anchor
guard let shape = try? await ShapeResource.generateStaticMesh(from: meshAnchor) else { continue }
switch update.event {
case .added:
let entity = ModelEntity()
entity.transform = Transform(matrix: meshAnchor.originFromAnchorTransform)
entity.collision = CollisionComponent(shapes: [shape], isStatic: true)
meshEntities[meshAnchor.id] = entity
root.addChild(entity)
case .updated:
guard let entity = meshEntities[meshAnchor.id] else { continue }
entity.transform = Transform(matrix: meshAnchor.originFromAnchorTransform)
entity.collision?.shapes = [shape]
case .removed:
meshEntities[meshAnchor.id]?.removeFromParent()
meshEntities.removeValue(forKey: meshAnchor.id)
}
}
}
}
}

Make sure to add NSWorldSensingUsageDescription to your app’s information property list to provide a usage description that explains how your app uses the data these sensors provide. People see this description when your app prompts for access to camera data.

Please accept this response if it answers your question or followup so I can help you achieve your goal.

Using vision pro to detect distance to real life objects
 
 
Q