Post

Replies

Boosts

Views

Activity

Reply to How to use SpatialTapGesture to pin a SwiftUI view to entity
my update code: import RealityKit import SwiftUI import RealityKitContent struct ImmersiveView: View { @State var favoritePoint = [PointOfInterest]() @State var earthEntity: Entity = Entity() var body: some View { RealityView { content, attachments in // Load the root entity from your RealityKit content bundle guard let rootEntity = try? await Entity(named: "Scene", in: realityKitContentBundle) else { return } content.add(rootEntity) earthEntity = rootEntity.findEntity(named: "Earth") ?? Entity() } update: { content, attachments in // Update logic if needed for place in favoritePoint { if let placeEntity = attachments.entity(for: place.id) { earthEntity.addChild(placeEntity) placeEntity.setPosition(place.location, relativeTo: nil) } } } attachments: { // Define attachments using the Attachment type ForEach(favoritePoint) { place in Attachment(id: place.id) { Text(place.name) .padding() .glassBackgroundEffect() .tag(place.id) } } } .gesture(SpatialTapGesture().targetedToAnyEntity().onEnded({ value in let location = value.location3D let convertLocation = 1.1 * value.convert(location, from: .local, to: .scene) favoritePoint.append(PointOfInterest(name: "test1", location: convertLocation)) })) .gesture(DragGesture().targetedToAnyEntity().onChanged({ value in value.entity.position = value.convert(value.location3D, from: .local, to: .scene) })) } } struct PointOfInterest: Identifiable { var name: String var id: UUID = UUID() var location: SIMD3 }
Aug ’24