Updated: In Xcode 16.3 beta, the unprojection of a point from screen to world is already better. However, the values are still wrong (very large values). Because of this, interaction with entities using InputTargetComponent is not possible.
import SwiftUI
import RealityKit
struct ContentView: View {
@State var tapPoint: CGPoint? = nil
var body: some View {
RealityView { content in
var camera = Entity()
var component = OrthographicCameraComponent()
component.scale = 5
camera.position = [0, 0, 5]
camera.components.set(component)
content.add(camera)
let model = ModelEntity(mesh: .generateSphere(radius: 1))
model.name = "Model"
model.components.set(InputTargetComponent())
model.generateCollisionShapes(recursive: false)
content.add(model)
} update: { content in
if let p = tapPoint {
let planeMatrix = simd_float4x4(
SIMD4<Float>( 1, 0, 0, 0),
SIMD4<Float>( 0, 0, 1, 0),
SIMD4<Float>( 0, -1, 0, 0),
SIMD4<Float>( 0, 0, 0, 1)
)
if let u = content.unproject(p, from: .global, to: .scene, ontoPlane: planeMatrix) {
print("Tapped at \(u)")
}
}
}
.simultaneousGesture(
SpatialTapGesture()
.onEnded({value in
tapPoint = value.location
})
)
.simultaneousGesture(
SpatialTapGesture()
.targetedToAnyEntity()
.onEnded({value in
print(value.entity.name)
})
)
}
}
Topic:
Graphics & Games
SubTopic:
RealityKit
Tags: