RealityKit & ARKit in SwiftUI — Adding Multiple Items

DevTechie
DevTechie
Published in
5 min readMay 18, 2024

--

RealityKit & ARKit in SwiftUI — Adding Multiple Items

RealityKit places anchors based on the anchoring component’s target property. For example, you can configure an anchor entity to rest on a detected horizontal surface in an AR scene like a table or floor, and RealityKit automatically places that anchor once it detects an appropriate horizontal plane in the real world.

We position virtual items on top of the detected anchor. We’ve successfully placed a single item into the AR scene by detecting a horizontal anchor. Now, let’s enhance the experience by adding multiple items.

Next, we’ll introduce a plane, upon which the previously created box will rest. To specify the location where the plane should appear, we’ll set the position of the newly created plane.

Our ARViewController looks like this

struct ARViewContainer: UIViewRepresentable {

func makeUIView(context: Context) -> ARView {

let arView = ARView(frame: .zero)

let anchor = AnchorEntity(
plane: .horizontal
)

let material = SimpleMaterial(
color: .purple,
isMetallic: true
)

let box = ModelEntity(
mesh: .generateBox(
size: 0.2
),
materials: [material]
)

anchor.addChild(box)…

--

--