RealityKit – ARView射线投射返回0结果

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

RealityKit – ARView raycasting returns 0 results

问题

我有一个RealityKit的ARView子类,具有以下用于进行射线投射的函数:

  1. func makeRaycastQuery(alignmentType: ARRaycastQuery.TargetAlignment) -> simd_float4x4? {
  2. let results = self.raycast(from: self.center,
  3. // Better for pinning to planes
  4. allowing: .estimatedPlane,
  5. alignment: alignmentType)
  6. // 我们不关心射线投射时的缩放,所以保持它不变
  7. guard var result = results.first?.worldTransform else { return nil }
  8. result.scale = SCNVector3(1, 1, 1)
  9. return result
  10. }

然而,我的results数组始终为空。在设置ARView以启用射线投射时,是否需要进行其他配置?

英文:

I have a subclass of RealityKit's ARView that has the following function for making a Raycast:

  1. func makeRaycastQuery(alignmentType: ARRaycastQuery.TargetAlignment) -> simd_float4x4? {
  2. let results = self.raycast(from: self.center,
  3. // Better for pinning to planes
  4. allowing: .estimatedPlane,
  5. alignment: alignmentType)
  6. // We don't care about changing scale on raycast so keep it the same
  7. guard var result = results.first?.worldTransform else { return nil }
  8. result.scale = SCNVector3(1, 1, 1)
  9. return result
  10. }

However, my results array is always empty. Is there some sort of other configuration I need to do when setting up an ARView to enable raycasting?

答案1

得分: 1

以下是代码的中文翻译部分:

  1. 尝试使用此代码(我为iPadPlaygrounds编写了它):
  2. import RealityKit
  3. import SwiftUI
  4. import ARKit
  5. import PlaygroundSupport
  6. struct ContentView : View {
  7. @State private var arView = ARView(frame: .zero)
  8. var body: some View {
  9. return ARContainer(arView: $arView)
  10. .gesture(
  11. TapGesture()
  12. .onEnded { _ in
  13. raycasting(arView: arView)
  14. }
  15. )
  16. }
  17. func raycasting(arView: ARView) {
  18. guard let query = arView.makeRaycastQuery(from: arView.center,
  19. allowing: .estimatedPlane,
  20. alignment: .any)
  21. else { fatalError() }
  22. guard let result = arView.session.raycast(query).first
  23. else { fatalError() }
  24. let entity = ModelEntity(mesh: .generateSphere(radius: 0.1))
  25. let anchor = AnchorEntity(raycastResult: result)
  26. anchor.addChild(entity)
  27. arView.scene.anchors.append(anchor)
  28. }
  29. }
  30. struct ARContainer : UIViewRepresentable {
  31. @Binding var arView: ARView
  32. func makeUIView(context: Context) -> ARView {
  33. arView.cameraMode = .ar
  34. return arView
  35. }
  36. func updateUIView(_ view: ARView, context: Context) { }
  37. }
  38. PlaygroundPage.current.needsIndefiniteExecution = true
  39. PlaygroundPage.current.setLiveView(ContentView())
  40. P. S.
  41. [此版本][1] UIKit应用程序中有效
  42. [1]: https://stackoverflow.com/questions/72232024/realitykit-why-anchorentity-position-is-always-x-0-y-0-z-0/72232793#72232793

希望这对您有所帮助。如果您需要进一步的协助,请随时提出。

英文:

Try this code (I've written it for iPad's Playgrounds):

  1. import RealityKit
  2. import SwiftUI
  3. import ARKit
  4. import PlaygroundSupport
  5. struct ContentView : View {
  6. @State private var arView = ARView(frame: .zero)
  7. var body: some View {
  8. return ARContainer(arView: $arView)
  9. .gesture(
  10. TapGesture()
  11. .onEnded { _ in
  12. raycasting(arView: arView)
  13. }
  14. )
  15. }
  16. func raycasting(arView: ARView) {
  17. guard let query = arView.makeRaycastQuery(from: arView.center,
  18. allowing: .estimatedPlane,
  19. alignment: .any)
  20. else { fatalError() }
  21. guard let result = arView.session.raycast(query).first
  22. else { fatalError() }
  23. let entity = ModelEntity(mesh: .generateSphere(radius: 0.1))
  24. let anchor = AnchorEntity(raycastResult: result)
  25. anchor.addChild(entity)
  26. arView.scene.anchors.append(anchor)
  27. }
  28. }

  1. struct ARContainer : UIViewRepresentable {
  2. @Binding var arView: ARView
  3. func makeUIView(context: Context) -> ARView {
  4. arView.cameraMode = .ar
  5. return arView
  6. }
  7. func updateUIView(_ view: ARView, context: Context) { }
  8. }

  1. PlaygroundPage.current.needsIndefiniteExecution = true
  2. PlaygroundPage.current.setLiveView(ContentView())

P. S.

This version works in UIKit app.

huangapple
  • 本文由 发表于 2023年2月10日 11:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406595.html
匿名

发表评论

匿名网友

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

确定