ARKit Place a SCNNode facing the camera

前端 未结 6 2023
挽巷
挽巷 2021-02-02 00:15

I\'m using ARKit to display 3D objects. I managed to place the nodes in the real world in front of the user (aka the camera). But I don\'t manage to make them to fa

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 00:47

    Here's how I did it:

    func faceCamera() {
        guard constraints?.isEmpty ?? true else {
            return
        }
        SCNTransaction.begin()
        SCNTransaction.animationDuration = 5
        SCNTransaction.completionBlock = { [weak self] in
            self?.constraints = []
        }
        constraints = [billboardConstraint]
        SCNTransaction.commit()
    }
    
    private lazy var billboardConstraint: SCNBillboardConstraint = {
        let constraint = SCNBillboardConstraint()
        constraint.freeAxes = [.Y]
        return constraint
    }()
    

    As stated earlier a SCNBillboardConstraint will make the node always look at the camera. I am animating it so the node doesn't just immediately snap into place, this is optional. In the SCNTransaction.completionBlock I remove the constraint, also optional.

    Also I set the SCNBillboardConstraint's freeAxes, which customizes on what axis the node follows the camera, again optional.

提交回复
热议问题