【问题标题】:Map screen coordinates to texture coordinates of a face mesh using ARKit使用 ARKit 将屏幕坐标映射到面部网格的纹理坐标
【发布时间】:2019-12-29 16:43:27
【问题描述】:

我正在尝试使用 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()
}

【问题讨论】:

    标签: ios swift arkit


    【解决方案1】:

    其实超级简单。不需要转换矩阵。命中测试提供 0 到 1 范围内的纹理坐标 u,v。所以乘以贴图的宽高,就可以得到贴图上的像素坐标

    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)
    }
    

    【讨论】:

    • 嗨!你有发布结果吗?
    • 嗨!你使用的纹理图像是什么?我正在尝试执行类似的操作;从 3d 到 2d 你能帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    相关资源
    最近更新 更多