My goal is to pin an attachment view precisely at the point where I tap on an entity using SpatialTapGesture. However, the current code doesn't pin the attachment view accurately to the tapped point. Instead, it often appears in space rather than on the entity itself. The issue might be due to an incorrect conversion of coordinates or values.
My code:
struct ImmersiveView: View {
@State private var location: GlobeLocation?
var body: some View {
RealityView { content, attachments in
guard let rootEnity = try? await Entity(named: "Scene", in: realityKitContentBundle) else { return }
content.add(rootEnity)
}update: { content, attachments in
if let earth = content.entities.first?.findEntity(named: "Earth"),let desView = attachments.entity(for: "1") {
let pinTransform = computeTransform(for: location ?? GlobeLocation(latitude: 0, longitude: 0))
earth.addChild(desView)
// desView.transform =
desView.setPosition(pinTransform, relativeTo: earth)
}
}
attachments: {
Attachment(id: "1") {
DescriptionView(location: location)
}
}
.gesture(DragGesture().targetedToAnyEntity().onChanged({ value in
value.entity.position = value.convert(value.location3D, from: .local, to: .scene)
}))
.gesture(SpatialTapGesture().targetedToAnyEntity().onEnded({ value in
}))
}
func lookUpLocation(at value: CGPoint) -> GlobeLocation? {
return GlobeLocation(latitude: value.x, longitude: value.y)
}
func computeTransform(for location: GlobeLocation) -> SIMD3<Float> {
// Constants for Earth's radius. Adjust this to match the scale of your 3D model.
let earthRadius: Float = 1.0
// Convert latitude and longitude from degrees to radians
let latitude = Float(location.latitude) * .pi / 180
let longitude = Float(location.longitude) * .pi / 180
// Calculate the position in Cartesian coordinates
let x = earthRadius * cos(latitude) * cos(longitude)
let y = earthRadius * sin(latitude)
let z = earthRadius * cos(latitude) * sin(longitude)
return position
}
}
struct GlobeLocation {
var latitude: Double
var longitude: Double
}
Post
Replies
Boosts
Views
Activity
Like title, I want to ask how to use this APIs: CameraFrameProvider
I got the warning : Cannot find 'CameraFrameProvider' in scope
Xcode 16.0 beta 4
imported ARKit
imported Vision
When I use the create a resources function to read the audio file in the immersive.usda file, it doesn’t work. The compiler reports that it cannot find the file in the project. I tried to get the URL of the immersive.usda file when creating a new default project, but I couldn’t retrieve it either. Why is that? Maybe I’m missing some configuration steps?