2022年3月5日 星期六

ARKit(SpriteKit) 臉部角度追蹤


  1. 建立😘 。由於前鏡頭左右相反,所以X軸要反向。

    let labelNode = SKLabelNode(text: "😘")

    labelNode.horizontalAlignmentMode = .center

    labelNode.verticalAlignmentMode = .center

    labelNode.xScale = -1

  2. 須將😘放入SKTransformNode,做後續的3D轉換。

    let face = SKTransformNode()

    face.addChild(labelNode)

    self.face = face

  3. 更新時,SKTransformNode再依據ARFaceAnchor旋轉

    let rotate = faceAnchor.transform.rotate

    face?.setRotationMatrix(rotate)

  4. 最後,記得將node轉回來,否則貼圖會是反的

完整程式

extension matrix_float4x4 {

    var rotate: matrix_float3x3 {

        let (a,b,c,_) = columns

        return float3x3(a.xyz, b.xyz, c.xyz)

    }

}


extension SIMD4 {

    var xyz: SIMD3<Scalar> {

        return SIMD3(x, y, z)

    }

}

var face: SKTransformNode?

func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {

    // Create and configure a node for the anchor added to the view's session.

    if anchor is ARFaceAnchor {

        let labelNode = SKLabelNode(text: "😘")

        labelNode.horizontalAlignmentMode = .center

        labelNode.verticalAlignmentMode = .center

        labelNode.xScale = -3

        labelNode.yScale = 3

        

        let face = SKTransformNode()

        face.addChild(labelNode)

        self.face = face

        

        let node = SKNode()

        node.addChild(face)

        return node

    }

    return nil

}


func view(_ view: ARSKView, didUpdate node: SKNode, for anchor: ARAnchor){

    if let faceAnchor = anchor as? ARFaceAnchor  {

        let rotate = faceAnchor.transform.rotate

        face?.setRotationMatrix(rotate)

        node.xScale *= -1

    }

}

沒有留言: