Как добиться билборда в RealityKit? - PullRequest
0 голосов
/ 07 марта 2020

Я хочу добиться эффекта рекламного щита в RealityKit (самолет всегда смотрит на камеру), я использовал метод Entity.Look(), но результат странный, я даже не вижу плоскость, сценарии, которые я использовал как ниже, так в чем же проблема?

struct ARViewContainer: UIViewRepresentable {

    func makeUIView(context: Context) -> ARView {

        let arView = ARView(frame: .zero)
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = .horizontal
        arView.session.run(config, options:[ ])
        arView.session.delegate = arView
        arView.createPlane()
        return arView

    }

    func updateUIView(_ uiView: ARView, context: Context) {
    }
}

var planeMesh = MeshResource.generatePlane(width: 0.15, height: 0.15)
var planeMaterial = SimpleMaterial(color:.white,isMetallic: false)
var planeEntity = ModelEntity(mesh:planeMesh,materials:[planeMaterial])
var arCameraPostion : SIMD3<Float>!
var isPlaced = false

extension ARView : ARSessionDelegate{
    func createPlane(){
        let planeAnchor = AnchorEntity(plane:.horizontal)
        planeAnchor.addChild(planeEntity)
        self.scene.addAnchor(planeAnchor)
        //planeAnchor.transform.rotation = simd_quatf(angle: .pi, axis: [0,1,0])

    }

    public func session(_ session: ARSession, didUpdate frame: ARFrame){
        guard let arCamera = session.currentFrame?.camera else { return }
        if isPlaced {
            arCameraPostion = SIMD3(arCamera.transform.columns.3.x,0,arCamera.transform.columns.3.z)
            planeEntity.look(at: arCameraPostion, from: planeEntity.position, upVector: [0, 1, 0],relativeTo: nil)
        }
    }

    public func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        isPlaced = true
    }
}

...