Canvatorium Visio Lab 5014
Basic Entity Interaction.
In order for an entity to receive input it must have an InputTargetComponent and a CollisionComponent. Then we can use regular SwiftUI gestures such as tap and drag.
struct Lab5014: View {
@State var showLabel = false
var body: some View {
RealityView { content, attachments in
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)
model.components.set(HoverEffectComponent())
// In order for an entity to receive input it must have an InputTargetComponent and a CollisionComponent
// Enable the entity for input.
model.components.set(InputTargetComponent())
// Create a collision component with an empty group and mask.
var collision = CollisionComponent(shapes: [.generateSphere(radius: 0.1)])
collision.filter = CollisionFilter(group: [], mask: [])
model.components.set(collision)
content.add(model)
if let attachmentEntity = attachments.entity(for: "SphereLabel") {
attachmentEntity.position = model.position + [0, 0.16, 0]
model.addChild(attachmentEntity)
content.add(attachmentEntity)
}
} update: { content, attachments in
if let attachmentEntity = attachments.entity(for: "SphereLabel") {
attachmentEntity.isEnabled = showLabel
}
} attachments: {
Attachment(id: "SphereLabel") {
Text("A Sphere")
.font(.largeTitle)
.padding(18)
.background(.black)
.cornerRadius(12)
}
}
.onTapGesture {
showLabel.toggle()
}
}
}