将图像映射到3D面部网格上

coc*_*oco 19 3d augmented-reality ios arkit iphone-x

我正在使用iPhone X并ARFaceKit捕捉用户的脸部.目标是使用用户的图像纹理面部网格.

我只是ARFrameAR会话中看一帧(一).从ARFaceGeometry,我有一组描述脸部的顶点.我制作了当前帧的jpeg表示capturedImage.

然后我想找到将创建的jpeg映射到网格顶点的纹理坐标.我想:1.将顶点从模型空间映射到世界空间; 2.将顶点从世界空间映射到摄像机空间; 3.除以图像尺寸以获得纹理的像素坐标.

let geometry: ARFaceGeometry = contentUpdater.faceGeometry!
let theCamera = session.currentFrame?.camera

let theFaceAnchor:SCNNode = contentUpdater.faceNode
let anchorTransform = float4x4((theFaceAnchor?.transform)!)

for index in 0..<totalVertices {
    let vertex = geometry.vertices[index]

    // Step 1: Model space to world space, using the anchor's transform
    let vertex4 = float4(vertex.x, vertex.y, vertex.z, 1.0)
    let worldSpace = anchorTransform * vertex4

    // Step 2: World space to camera space
    let world3 = float3(worldSpace.x, worldSpace.y, worldSpace.z)
    let projectedPt = theCamera?.projectPoint(world3, orientation: .landscapeRight, viewportSize: (theCamera?.imageResolution)!)

    // Step 3: Divide by image width/height to get pixel coordinates
    if (projectedPt != nil) {
        let vtx = projectedPt!.x / (theCamera?.imageResolution.width)!
        let vty = projectedPt!.y / (theCamera?.imageResolution.height)!
        textureVs += "vt \(vtx) \(vty)\n"
    }
}
Run Code Online (Sandbox Code Playgroud)

这不起作用,但反而给我一个非常时髦的脸!我哪里错了?

oli*_*ver 6

现在, Apple 发布的基于面部的示例代码中提供了使用用户图像对面部网格进行纹理化的功能(将相机视频映射到 3D 面部几何图形部分)。

人们可以使用以下着色器修改器将摄像机视频映射到 3D 面部几何图形上。

// Transform the vertex to the camera coordinate system.
float4 vertexCamera = scn_node.modelViewTransform * _geometry.position;

// Camera projection and perspective divide to get normalized viewport coordinates (clip space).
float4 vertexClipSpace = scn_frame.projectionTransform * vertexCamera;
vertexClipSpace /= vertexClipSpace.w;

// XY in clip space is [-1,1]x[-1,1], so adjust to UV texture coordinates: [0,1]x[0,1].
// Image coordinates are Y-flipped (upper-left origin).
float4 vertexImageSpace = float4(vertexClipSpace.xy * 0.5 + 0.5, 0.0, 1.0);
vertexImageSpace.y = 1.0 - vertexImageSpace.y;

// Apply ARKit's display transform (device orientation * front-facing camera flip).
float4 transformedVertex = displayTransform * vertexImageSpace;

// Output as texture coordinates for use in later rendering stages.
_geometry.texcoords[0] = transformedVertex.xy;
Run Code Online (Sandbox Code Playgroud)


小智 0

出发点不同:

起点不同

对您的代码应用以下更改:

//let vty = projectedPt!.y / (theCamera?.imageResolution.height)!
let vty = ((theCamera?.imageResolution.height)! - projectedPt!.y) / (theCamera?.imageResolution.height)!
Run Code Online (Sandbox Code Playgroud)

你可以获得普通脸。