英文:
Cannot get Camera position in RealityKit
问题
I am trying to load a 3D model "cube.usdz" onto the world position of the camera.
我正在尝试将一个3D模型 "cube.usdz" 加载到相机的世界位置上。
I've already looked at this solution: https://stackoverflow.com/a/45089244/13246089, but I have no idea how to implement it and where do I even call the function "session"? Sorry I am very new to this and don't know what I am doing.
我已经看过这个解决方案:https://stackoverflow.com/a/45089244/13246089,但我不知道如何实现它,甚至不知道在哪里调用 "session" 函数?对不起,我是新手,不知道我在做什么。
Why does my code always go to "why does nothing work"? Why can't I get the position of my camera? Any assistance or suggestions would be greatly appreciated.
为什么我的代码总是跳到 "为什么什么都不起作用"?为什么我不能获取相机的位置?任何帮助或建议将不胜感激。
import UIKit
import RealityKit
import ARKit
class Load3DModelViewController: UIViewController, ARSessionDelegate {
@IBOutlet weak var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
// 设置AR配置
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
arView.session.run(configuration)
load3DModel()
arView.session.delegate = self
}
func load3DModel() {
let modelFileName = "cube.usdz"
guard let modelEntity = try? Entity.load(named: modelFileName) else {
print("加载3D模型失败")
return
}
if let pos = arView.session.currentFrame?.camera.transform {
let xpos = pos.columns.3.x
let ypos = pos.columns.3.y
let zpos = pos.columns.3.z
let modelTranslation = SIMD3<Float>(xpos,ypos,zpos - 1)
modelEntity.setPosition(modelTranslation, relativeTo: nil)
arView.scene.addAnchor(modelEntity as! HasAnchoring)
} else {
print("\n为什么什么都不起作用\n")
}
}
}
英文:
I am trying to load a 3d model "cube.usdz" onto the world position of the camera.
I've already looked at this solution: https://stackoverflow.com/a/45089244/13246089, but I have no idea how to implement it and where do I even call the function "session"? Sorry I am very new to this and don't know what I am doing.
Why does my code always go to "why does nothing work"? Why can't I get the position of my camera? Any assistance or suggestions would be greatly appreciated.
import UIKit
import RealityKit
import ARKit
class Load3DModelViewController: UIViewController, ARSessionDelegate {
@IBOutlet weak var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
// set up AR configuration
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
arView.session.run(configuration)
load3DModel()
arView.session.delegate = self
}
func load3DModel() {
let modelFileName = "cube.usdz"
guard let modelEntity = try? Entity.load(named: modelFileName) else {
print("Failed to load the 3D model")
return
}
if let pos = arView.session.currentFrame?.camera.transform {
let xpos = pos.columns.3.x
let ypos = pos.columns.3.y
let zpos = pos.columns.3.z
let modelTranslation = SIMD3<Float>(xpos,ypos,zpos - 1)
modelEntity.setPosition(modelTranslation, relativeTo: nil)
arView.scene.addAnchor(modelEntity as! HasAnchoring)
} else {
print("\nwhy does nothing work\n")
}
}
}
答案1
得分: 1
始终跟随ARCamera的模型
第一个场景表示了RealityKit如何自动跟踪目标(即ARCamera的锚点),因此球体始终跟随ARCamera(沿Z轴偏移1米)。
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))
sphere.position.z = -1.0
let cameraAnchor = AnchorEntity(.camera)
cameraAnchor.addChild(sphere)
arView.scene.addAnchor(cameraAnchor)
}
}
锚点复制ARCamera的变换信息在会话开始时
此示例显示了如何创建一个球体,该球体将被锚点位置捆绑,该位置是会话开始时相机位置的坐标加上2秒的延迟。
退后一步以查看球体。
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let worldAnchor = AnchorEntity()
worldAnchor.transform = self.arView.cameraTransform
worldAnchor.addChild(sphere)
self.arView.scene.addAnchor(worldAnchor)
}
}
}
更多信息可以在这里找到。
英文:
Model that always follows the ARCamera
The first scenario represents how RealityKit automatically tracks the target (i.e. ARCamera's anchor), so the sphere always follows the ARCamera (with 1 meter offset along Z axis).
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))
sphere.position.z = -1.0
let cameraAnchor = AnchorEntity(.camera)
cameraAnchor.addChild(sphere)
arView.scene.addAnchor(cameraAnchor)
}
}
Anchor copying ARCamera's Transform when session started
This example shows how to create a sphere that will be tethered at the anchor's position with the coordinates of the camera location at the moment when session started + 2 seconds delay.
Take a step back to see the sphere.
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let worldAnchor = AnchorEntity()
worldAnchor.transform = self.arView.cameraTransform
worldAnchor.addChild(sphere)
self.arView.scene.addAnchor(worldAnchor)
}
}
}
More info you can find here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论