我正在使用 iPhone X 和 ARFaceKit
来捕捉用户的面部。目标是使用用户的图像对面部网格进行纹理处理。
我只查看来自 AR
session 的单个帧(ARFrame
)。
从 ARFaceGeometry
中,我有一组描述面部的顶点。
我制作了当前帧的 capturedImage
的 jpeg 表示。
然后我想找到将创建的 jpeg 映射到网格顶点的纹理坐标。我想:
将顶点从模型空间映射到世界空间;
将顶点从世界空间映射到相机空间;
除以图像尺寸以获得纹理的像素坐标。
让几何:ARFaceGeometry = contentUpdater.faceGeometry! 让 theCamera = session.currentFrame?.camera
让theFaceAnchor: SCNNode = contentUpdater.faceNode 让 anchorTransform = float4x4((theFaceAnchor?.transform)!)
对于索引在 0..
// 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" }
这不起作用,反而让我看起来很时髦!我哪里错了?
最佳答案
现在可以在 Face-Based sample code 中使用用户图像对面部网格进行纹理化。由 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;
关于3d - 将图像映射到 3D 面部网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47663081/