如何激活RealityKit中的手势功能以控制模型实体?

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

How to activate RealityKit gestures for Model entity?

问题

在下面的代码中,我创建了一个模型实体并加载了一个名为"stone"的3D模型。然后,我将generateCollisionShapes设置为true。然后,我应该能够将手势安装到视图中,但我收到一个错误,说我的模型实体不符合类型'HasCollision'。

我需要一些帮助让这个工作起来。任何反馈都将不胜感激。

  1. import ARKit
  2. import RealityKit
  3. class Coordinator: NSObject {
  4. weak var view: ARView?
  5. @objc func handleTap(_ recognizer: UITapGestureRecognizer) {
  6. guard let view = self.view else { return }
  7. let tapLocation = recognizer.location(in: view)
  8. let results = view.raycast(from: tapLocation,
  9. allowing: .estimatedPlane,
  10. alignment: .horizontal)
  11. if let result = results.first {
  12. let anchor = AnchorEntity(raycastResult: result)
  13. guard let modelEntity = try? ModelEntity.load(named: "stone")
  14. else {
  15. print("没有找到模型")
  16. return
  17. }
  18. modelEntity.generateCollisionShapes(recursive: true)
  19. anchor.addChild(modelEntity)
  20. view.scene.addAnchor(anchor)
  21. view.installGestures(.all, for: modelEntity)
  22. }
  23. }
  24. }
英文:

In the code below, I create a model entity and load a 3d model named "stone". I then set generateCollisionShapes to true. I should then be able to install gestures to the view but I'm getting an error saying my model entity doesn't conform to the type 'HasCollision'.

I need some help getting this to work. Any feedback is appreciated.

  1. import ARKit
  2. import RealityKit
  3. class Coordinator: NSObject {
  4. weak var view: ARView?
  5. @objc func handleTap(_ recognizer: UITapGestureRecognizer) {
  6. guard let view = self.view else { return }
  7. let tapLocation = recognizer.location(in: view)
  8. let results = view.raycast(from: tapLocation,
  9. allowing: .estimatedPlane,
  10. alignment: .horizontal)
  11. if let result = results.first {
  12. let anchor = AnchorEntity(raycastResult: result)
  13. guard let modelEntity = try? ModelEntity.load(named: "stone")
  14. else {
  15. print("didn't find model")
  16. return
  17. }
  18. modelEntity.generateCollisionShapes(recursive: true)
  19. anchor.addChild(modelEntity)
  20. view.scene.addAnchor(anchor)
  21. view.installGestures(.all, for: modelEntity)
  22. }
  23. }
  24. }

答案1

得分: 1

你应该使用 Entity.loadModel(...) 方法,而不是 ModelEntity.load(...)

  1. guard let modelEntity = try? Entity.loadModel(named: "stone") else { return }
  2. modelEntity.generateCollisionShapes(recursive: true)
  3. arView.installGestures([.all], for: modelEntity as Entity & HasCollision)

附注:

  • 不建议将ARView对象声明为UIView的“view” - 声明为“arView”。
英文:

You should use Entity.loadModel(...) method instead of ModelEntity.load(...)

  1. guard let modelEntity = try? Entity.loadModel(named: "stone") else { return }
  2. modelEntity.generateCollisionShapes(recursive: true)
  3. arView.installGestures([.all], for: modelEntity as Entity & HasCollision)

P. S.

  • It's not good to declare an ARView object as UIView's view – declare it as arView.

huangapple
  • 本文由 发表于 2023年6月8日 20:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432045.html
匿名

发表评论

匿名网友

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

确定