我正在编写一个使用Metal渲染3d对象的应用程序。我想检测用户是否触摸了物体,所以我进行了相交测试来确定这一点。我需要对从touches开始的屏幕坐标获取的x和y值进行归一化,以与Metal的NDC相匹配。
我已经看到了使用OpenGL的几次尝试,但是无法在我自己的代码中实现正确的公式:
func isIntersection(location: CGPoint)->Bool{
// adjust x, y by multiplying by the projection and model matrix that have been modified previously
let v = identity * proj * model * vec4(location.x, location.y, 0, 1)
// attempt to get normalized x and y
let locx = v.x / view.bounds.size.width
let locy = v.y / view.bounds.size.height
//... test intersection
}
感谢您的帮助