Jua*_*ero 5 ios scenekit swift arkit
snapshot我正在 ARKit 中使用 SceneKit 运行面部跟踪配置,在每个帧中我可以通过属性或作为缓冲区访问相机源capturedImage,我还能够将每个面部顶点映射到图像坐标空间并添加一些UIView助手( 1点平方)在屏幕上实时显示所有面顶点,如下所示:
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard let faceGeometry = node.geometry as? ARSCNFaceGeometry,
let anchorFace = anchor as? ARFaceAnchor,
anchorFace.isTracked
else { return }
let vertices = anchorFace.geometry.vertices
for (index, vertex) in vertices.enumerated() {
let vertex = sceneView.projectPoint(node.convertPosition(SCNVector3(vertex), to: nil))
let xVertex = CGFloat(vertex.x)
let yVertex = CGFloat(vertex.y)
let newPosition = CGPoint(x: xVertex, y: yVertex)
// Here i update the position of each UIView in the screen with the calculated vertex new position, i have an array of views that matches the vertex count that is consistent across sessions.
}
}
Run Code Online (Sandbox Code Playgroud)
由于 UV 坐标在会话中也是恒定的,因此我尝试为面部网格上的每个像素绘制 UV 纹理中的相应位置,以便在经过一些迭代后,我可以将人脸纹理保存到文件中。
我得出了一些理论解决方案,例如CGPaths为每个三角形创建,并询问每个像素是否包含在该三角形中,如果包含,则创建一个三角形图像,裁剪一个矩形,然后应用从投影点获得的三角形蒙版通过图像坐标中的三角形顶点,因此以这种方式我可以获得一个三角形图像,该图像必须转换为基础三角形变换(就像将其倾斜到位),然后在(UIView1024x1024)中将每个三角形图像添加为UIImageView作为子视图,最后将 UIView 编码为 PNG,这听起来需要大量工作,特别是将裁剪后的三角形与 UV 纹理对应的三角形进行匹配的部分。
在Apple演示项目中,有一个图像显示了UV纹理的样子,如果您编辑该图像并添加一些颜色,它就会显示在脸上,但我需要相反的方式,从我所看到的相机馈送,创建脸部纹理,在同一个演示项目中,有一个示例可以完全满足我的需要,但是使用着色器,并且没有关于如何将纹理提取到文件的线索,着色器代码看起来像这:
/*
<samplecode>
<abstract>
SceneKit shader (geometry) modifier for texture mapping ARKit camera video onto the face.
</abstract>
</samplecode>
*/
#pragma arguments
float4x4 displayTransform // from ARFrame.displayTransform(for:viewportSize:)
#pragma body
// 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;
/**
* MARK: Post-process special effects
*/
Run Code Online (Sandbox Code Playgroud)
老实说,我在着色器方面没有太多经验,所以在翻译着色器信息时,如果有任何帮助,了解如何翻译为更多的 Cocoa Touch Swift 代码,我将不胜感激,现在我还没有考虑性能,所以如果必须这样做在CPU中,比如在后台线程中或离线都可以,无论如何,我必须选择正确的帧以避免倾斜的样本,或者具有非常好的信息的三角形,以及其他一些几乎没有拉伸的像素(例如检查是否正常)三角形指向相机,对其进行采样)或其他 UI 帮助器,以使用户转动面部以正确采样所有面部。
这个应用程序正是我所需要的,但它们看起来不像使用 ARKit。
谢谢。
| 归档时间: |
|
| 查看次数: |
1734 次 |
| 最近记录: |