Canvatorium Visio Lab 5015
Attaching a UI to a Mesh.
This lab uses a sidewalk sign and RealityView attachments to link an interactive SwiftUI view with a mesh.
struct Lab5015: View {
var body: some View {
RealityView { content, attachments in
if let model = try? await Entity(named: "SidewalkSign", in: realityKitContentBundle) {
model.position = SIMD3(x: 1.0, y: 0, z: -2)
content.add(model)
// Attach SignContent to the model. Position and rotate it.
if let attachmentEntity = attachments.entity(for: "SignContent") {
attachmentEntity.position = model.position + [0, 0.6, 0.09]
attachmentEntity.transform.rotation = simd_quatf(Rotation3D(angle: Angle2D(degrees: 11), axis: RotationAxis3D(x: -1, y: 0, z: 0)))
model.addChild(attachmentEntity)
content.add(attachmentEntity)
}
}
} attachments: {
// Create an attachment called SignContent
Attachment(id: "SignContent") {
let modelData = ModelData()
Directory() // The main view for the app content
.environment(modelData)
.frame(width: 620, height: 1020)
}
}
}
}
Note that when a user changes their display scale the 2D view in the example above would no scale with it.