使用ARKit将屏幕坐标映射到面部网格的纹理坐标

3

我正在尝试使用ARkit制作一个AR应用程序,允许用户在他们的脸上涂口红。如何将屏幕坐标映射到脸部网格的纹理坐标?

func transformToTextureCoordinates(screenCoordinates: CGPoint) -> CGPoint {

     // hit test from screen to the face geometry
     let hitTestResults = sceneView.hitTest(screenCoordinates, options: nil)
     guard let result = hitTestResults.first else {
         return CGPoint(x: -1.0, y: -1.0)
     let world_coords = result.worldCoordinates

     --- HELP! ---
}

func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) {
      // transform the screen coordinates to texture coordinates
      let fromPoint_transformed = transformToTextureCoordinates(screenCoordinates: fromPoint)
      let toPoint_transformed = transformToTextureCoordinates(screenCoordinates:toPoint)

      // draw line on the texture image
      UIGraphicsBeginImageContext(view.frame.size)
          guard let context = UIGraphicsGetCurrentContext() else {
          return
      }
      textureImage?.draw(in: view.bounds)
      context.move(to: fromPoint_transformed)
      context.addLine(to: toPoint_transformed)
      context.setLineCap(.round)
      context.setBlendMode(.normal)
      context.setLineWidth(brushWidth)
      context.setStrokeColor(color.cgColor)
      context.strokePath()
      textureImage = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
}
1个回答

3

这实际上非常简单。无需转换矩阵。命中测试提供了纹理坐标u,v,其在0到1范围内。因此乘以纹理宽度和高度,您将获得纹理上的像素坐标。

func transformToTextureCoordinates(screenCoordinates: CGPoint) -> CGPoint {
    let hitTestResults = sceneView.hitTest(screenCoordinates, options: nil)
    guard let result = hitTestResults.first else {
        return CGPoint(x: -1, y: -1)
    }
    let textureCoordinates = result.textureCoordinates(withMappingChannel: 0)
    return CGPoint(x: textureCoordinates.x * textureImage.size.width, y: textureCoordinates.y * textureImage.size.height)
}

你好!你有发布的结果吗? - ina
你好!你使用的textureImage是什么?我正在尝试做类似的事情;从3D到2D,你能帮忙吗? - swiftlearneer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接