Canvatorium Visio Lab 5013
A simple attachment.
This lab will create a sphere called model and an attachment view called SphereLabel. SphereLabel will be attached to and positioned relative to the model.
struct Lab5013: View {
var body: some View {
RealityView { content, attachments in
// 1. Create a model entity and add it to the scene.
let model = ModelEntity(
mesh: .generateSphere(radius: 0.1),
materials: [SimpleMaterial(color: .black, isMetallic: false)])
model.position = SIMD3(x: 0.6, y: 1.5, z: -2)
content.add(model)
// 3. Get the attachment as an entity and position it relative to the model.
if let attachmentEntity = attachments.entity(for: "SphereLabel") {
attachmentEntity.position = model.position + [0, 0.16, 0]
model.addChild(attachmentEntity)
content.add(attachmentEntity)
}
} attachments: {
// 2. Create an attachment view. RealityKit will convert this into an entity.
Attachment(id: "SphereLabel") {
Text("A Sphere")
.font(.largeTitle)
.padding(18)
.background(.black)
.cornerRadius(12)
}
}
}
}