There are many changes happening with RealityKit and Reality Composer Pro, and it’s quite confusing how to achieve this in SwiftUI with RealityKit. I need to detect an object using an .arobject file created from Apple’s sample, but there is no clear documentation on how to detect the object and obtain its coordinates to place my 3D object.
<code>struct ContentView : View {
@State private var objectAnchor: ARObjectAnchor?
var body: some View {
RealityView { content in
// Create a cube model
let model = Entity()
let mesh = MeshResource.generateBox(size: 0.1, cornerRadius: 0.005)
let material = SimpleMaterial(color: .gray, roughness: 0.15, isMetallic: true)
model.components.set(ModelComponent(mesh: mesh, materials: [material]))
model.position = [0, 0.05, 0]
// Create object detection anchor for the content
let objectSource = AnchoringComponent.ObjectAnchoringSource.init(group: "ArModel", name: "bottle")
let anchor = AnchorEntity(.referenceObject(from: objectSource))
//AnchorEntity(.object(group: "ArModel", name: "bottle"))
anchor.addChild(model)
// Add the horizontal plane anchor to the scene
content.add(anchor)
content.cameraTarget = anchor
content.camera = .spatialTracking
}
.edgesIgnoringSafeArea(.all)
}
</code>
<code>struct ContentView : View {
@State private var objectAnchor: ARObjectAnchor?
var body: some View {
RealityView { content in
// Create a cube model
let model = Entity()
let mesh = MeshResource.generateBox(size: 0.1, cornerRadius: 0.005)
let material = SimpleMaterial(color: .gray, roughness: 0.15, isMetallic: true)
model.components.set(ModelComponent(mesh: mesh, materials: [material]))
model.position = [0, 0.05, 0]
// Create object detection anchor for the content
let objectSource = AnchoringComponent.ObjectAnchoringSource.init(group: "ArModel", name: "bottle")
let anchor = AnchorEntity(.referenceObject(from: objectSource))
//AnchorEntity(.object(group: "ArModel", name: "bottle"))
anchor.addChild(model)
// Add the horizontal plane anchor to the scene
content.add(anchor)
content.cameraTarget = anchor
content.camera = .spatialTracking
}
.edgesIgnoringSafeArea(.all)
}
</code>
struct ContentView : View {
@State private var objectAnchor: ARObjectAnchor?
var body: some View {
RealityView { content in
// Create a cube model
let model = Entity()
let mesh = MeshResource.generateBox(size: 0.1, cornerRadius: 0.005)
let material = SimpleMaterial(color: .gray, roughness: 0.15, isMetallic: true)
model.components.set(ModelComponent(mesh: mesh, materials: [material]))
model.position = [0, 0.05, 0]
// Create object detection anchor for the content
let objectSource = AnchoringComponent.ObjectAnchoringSource.init(group: "ArModel", name: "bottle")
let anchor = AnchorEntity(.referenceObject(from: objectSource))
//AnchorEntity(.object(group: "ArModel", name: "bottle"))
anchor.addChild(model)
// Add the horizontal plane anchor to the scene
content.add(anchor)
content.cameraTarget = anchor
content.camera = .spatialTracking
}
.edgesIgnoringSafeArea(.all)
}
}
4