无法获取RealityKit中的相机位置。

huangapple go评论117阅读模式
英文:

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 {

  1. @IBOutlet weak var arView: ARView!
  2. override func viewDidLoad() {
  3. super.viewDidLoad()
  4. // 设置AR配置
  5. let configuration = ARWorldTrackingConfiguration()
  6. configuration.planeDetection = .horizontal
  7. arView.session.run(configuration)
  8. load3DModel()
  9. arView.session.delegate = self
  10. }
  11. func load3DModel() {
  12. let modelFileName = "cube.usdz"
  13. guard let modelEntity = try? Entity.load(named: modelFileName) else {
  14. print("加载3D模型失败")
  15. return
  16. }
  17. if let pos = arView.session.currentFrame?.camera.transform {
  18. let xpos = pos.columns.3.x
  19. let ypos = pos.columns.3.y
  20. let zpos = pos.columns.3.z
  21. let modelTranslation = SIMD3<Float>(xpos,ypos,zpos - 1)
  22. modelEntity.setPosition(modelTranslation, relativeTo: nil)
  23. arView.scene.addAnchor(modelEntity as! HasAnchoring)
  24. } else {
  25. print("\n为什么什么都不起作用\n")
  26. }
  27. }

}

英文:

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.

  1. import UIKit
  2. import RealityKit
  3. import ARKit
  4. class Load3DModelViewController: UIViewController, ARSessionDelegate {
  5. @IBOutlet weak var arView: ARView!
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8. // set up AR configuration
  9. let configuration = ARWorldTrackingConfiguration()
  10. configuration.planeDetection = .horizontal
  11. arView.session.run(configuration)
  12. load3DModel()
  13. arView.session.delegate = self
  14. }
  15. func load3DModel() {
  16. let modelFileName = &quot;cube.usdz&quot;
  17. guard let modelEntity = try? Entity.load(named: modelFileName) else {
  18. print(&quot;Failed to load the 3D model&quot;)
  19. return
  20. }
  21. if let pos = arView.session.currentFrame?.camera.transform {
  22. let xpos = pos.columns.3.x
  23. let ypos = pos.columns.3.y
  24. let zpos = pos.columns.3.z
  25. let modelTranslation = SIMD3&lt;Float&gt;(xpos,ypos,zpos - 1)
  26. modelEntity.setPosition(modelTranslation, relativeTo: nil)
  27. arView.scene.addAnchor(modelEntity as! HasAnchoring)
  28. } else {
  29. print(&quot;\nwhy does nothing work\n&quot;)
  30. }
  31. }
  32. }

答案1

得分: 1

始终跟随ARCamera的模型

第一个场景表示了RealityKit如何自动跟踪目标(即ARCamera的锚点),因此球体始终跟随ARCamera(沿Z轴偏移1米)。

  1. import UIKit
  2. import RealityKit
  3. class ViewController: UIViewController {
  4. @IBOutlet var arView: ARView!
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))
  8. sphere.position.z = -1.0
  9. let cameraAnchor = AnchorEntity(.camera)
  10. cameraAnchor.addChild(sphere)
  11. arView.scene.addAnchor(cameraAnchor)
  12. }
  13. }

锚点复制ARCamera的变换信息在会话开始时

此示例显示了如何创建一个球体,该球体将被锚点位置捆绑,该位置是会话开始时相机位置的坐标加上2秒的延迟。

退后一步以查看球体。

  1. import UIKit
  2. import RealityKit
  3. class ViewController: UIViewController {
  4. @IBOutlet var arView: ARView!
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))
  8. DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
  9. let worldAnchor = AnchorEntity()
  10. worldAnchor.transform = self.arView.cameraTransform
  11. worldAnchor.addChild(sphere)
  12. self.arView.scene.addAnchor(worldAnchor)
  13. }
  14. }
  15. }

更多信息可以在这里找到。

英文:

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).

  1. import UIKit
  2. import RealityKit
  3. class ViewController: UIViewController {
  4. @IBOutlet var arView: ARView!
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))
  8. sphere.position.z = -1.0
  9. let cameraAnchor = AnchorEntity(.camera)
  10. cameraAnchor.addChild(sphere)
  11. arView.scene.addAnchor(cameraAnchor)
  12. }
  13. }

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.

  1. import UIKit
  2. import RealityKit
  3. class ViewController: UIViewController {
  4. @IBOutlet var arView: ARView!
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))
  8. DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
  9. let worldAnchor = AnchorEntity()
  10. worldAnchor.transform = self.arView.cameraTransform
  11. worldAnchor.addChild(sphere)
  12. self.arView.scene.addAnchor(worldAnchor)
  13. }
  14. }
  15. }

More info you can find here.

huangapple
  • 本文由 发表于 2023年6月11日 21:41:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定