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