swift 如何用RealityKit绘制标准面网格?

dwbf0jvd  于 2022-12-26  发布在  Swift
关注(0)|答案(1)|浏览(185)

我尝试用RealityKit渲染一个人脸网格,还没有成功。所以当ARKit检测到人脸时,ARSession会生成一个ARFaceAnchor,其中包含一个人脸几何网格。
但它不能作为模型图元生成。
有人能帮忙吗?

jdzmm42g

jdzmm42g1#

RealityKit中的标准面网格

要在RealityKit 2.0中以编程方式生成和渲染ARKit的canonical face mesh(由1220个顶点组成的ARFaceGeometry对象),请使用以下代码:

import ARKit
import RealityKit

class ControllerView: UIViewController {
    
    @IBOutlet var arView: ARView!
    var anchor = AnchorEntity()
    var model = ModelEntity()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        arView.automaticallyConfigureSession = false
        arView.session.delegate = self

        guard ARFaceTrackingConfiguration.isSupported
        else { 
            fatalError("We can't run face tracking config") 
        }
                
        let config = ARFaceTrackingConfiguration()
        config.maximumNumberOfTrackedFaces = 1
        arView.session.run(config)
    }
}

然后创建一个方法来转换面锚的子属性。注意,我使用了for-in循环来将索引从[Int16]转换为[UInt32]类型(类型转换在这里没有帮助)。

extension ControllerView {
    
    private func nutsAndBoltsOf(_ anchor: ARFaceAnchor) -> MeshDescriptor {
        
        let vertices: [simd_float3] = anchor.geometry.vertices
        var triangleIndices: [UInt32] = []
        let texCoords: [simd_float2] = anchor.geometry.textureCoordinates
        
        for index in anchor.geometry.triangleIndices {         // [Int16]
            triangleIndices.append(UInt32(index))
        }
        print(vertices.count)         // 1220 vertices
        
        var descriptor = MeshDescriptor(name: "canonical_face_mesh")
        descriptor.positions = MeshBuffers.Positions(vertices)
        descriptor.primitives = .triangles(triangleIndices)
        descriptor.textureCoordinates = MeshBuffers.TextureCoordinates(texCoords)
        return descriptor
    }
}

最后,让我们运行一个委托的方法来提供一个网格资源:

extension ControllerView: ARSessionDelegate {
    
    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {

        guard let faceAnchor = anchors[0] as? ARFaceAnchor else { return }
        arView.session.add(anchor: faceAnchor)
        self.anchor = AnchorEntity(anchor: faceAnchor)
        self.anchor.scale *= 1.2

        let mesh: MeshResource = try! .generate(from: [nutsAndBoltsOf(faceAnchor)])
        var material = SimpleMaterial(color: .magenta, isMetallic: true)
        self.model = ModelEntity(mesh: mesh, materials: [material])
        self.anchor.addChild(self.model)
        arView.scene.anchors.append(self.anchor)
    }
}

结果(在iPadOS 16.2中的第四代iPad Pro上测试)。

我还建议您看一下关于在RealityKit 2.0中可视化检测到的平面的帖子。
圣诞快乐!

相关问题