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

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

RealityKit – ARView raycasting returns 0 results

问题

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

func makeRaycastQuery(alignmentType: ARRaycastQuery.TargetAlignment) -> simd_float4x4? {
    let results = self.raycast(from: self.center,
                        // Better for pinning to planes
                        allowing: .estimatedPlane,
                        alignment: alignmentType)

    // 我们不关心射线投射时的缩放,所以保持它不变
    guard var result = results.first?.worldTransform else { return nil }
    result.scale = SCNVector3(1, 1, 1)
    return result
}

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

英文:

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

func makeRaycastQuery(alignmentType: ARRaycastQuery.TargetAlignment) -> simd_float4x4? {
    let results = self.raycast(from: self.center,
                           // Better for pinning to planes
                           allowing: .estimatedPlane,
                          alignment: alignmentType)

    // We don't care about changing scale on raycast so keep it the same
    guard var result = results.first?.worldTransform else { return nil }
    result.scale = SCNVector3(1, 1, 1)
    return result
}

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

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

尝试使用此代码(我为iPad的Playgrounds编写了它):

    import RealityKit
    import SwiftUI
    import ARKit
    import PlaygroundSupport
    
    struct ContentView : View {
        
        @State private var arView = ARView(frame: .zero)
        
        var body: some View {
            return ARContainer(arView: $arView)
                .gesture(
                    TapGesture()
                        .onEnded { _ in
                            raycasting(arView: arView)
                        }
                )
        }        
        func raycasting(arView: ARView) {
            guard let query = arView.makeRaycastQuery(from: arView.center, 
                                                  allowing: .estimatedPlane, 
                                                 alignment: .any)
            else { fatalError() }
                
            guard let result = arView.session.raycast(query).first
            else { fatalError() }
                
            let entity = ModelEntity(mesh: .generateSphere(radius: 0.1))
            let anchor = AnchorEntity(raycastResult: result)
            anchor.addChild(entity)
            arView.scene.anchors.append(anchor)
        }
    }

    struct ARContainer : UIViewRepresentable {
        
        @Binding var arView: ARView
        
        func makeUIView(context: Context) -> ARView {
            arView.cameraMode = .ar
            return arView
        }   
        func updateUIView(_ view: ARView, context: Context) { }
    }

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

P. S. 

[此版本][1] UIKit应用程序中有效

  [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):

import RealityKit
import SwiftUI
import ARKit
import PlaygroundSupport

struct ContentView : View {
    
    @State private var arView = ARView(frame: .zero)
    
    var body: some View {
        return ARContainer(arView: $arView)
            .gesture(
                TapGesture()
                    .onEnded { _ in
                        raycasting(arView: arView)
                    }
            )
    }        
    func raycasting(arView: ARView) {
        guard let query = arView.makeRaycastQuery(from: arView.center, 
                                              allowing: .estimatedPlane, 
                                             alignment: .any)
        else { fatalError() }
            
        guard let result = arView.session.raycast(query).first
        else { fatalError() }
            
        let entity = ModelEntity(mesh: .generateSphere(radius: 0.1))
        let anchor = AnchorEntity(raycastResult: result)
        anchor.addChild(entity)
        arView.scene.anchors.append(anchor)
    }
}

struct ARContainer : UIViewRepresentable {
    
    @Binding var arView: ARView
    
    func makeUIView(context: Context) -> ARView {
        arView.cameraMode = .ar
        return arView
    }   
    func updateUIView(_ view: ARView, context: Context) { }
}

PlaygroundPage.current.needsIndefiniteExecution = true
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:

确定