ios - Getting object at location on screen in Metal -
i'm using metal , want user able tap objects onscreen. i'm using tap gesture recognizer, , have cgpoint, don't know how find object @ point. there either way onscreen coordinates metal, or way transform cgpoint metal coordinate space?
i ended doing raytracing-type technique. used bounding sphere each object , calculated whether or not ray intersected , how far ray's origin intersection was, ray being camera through onscreen point of clicking. code follows:
var location = gesturerecognizer.locationinview(self.view) var proj_matrix = projectionmatrix.copy() var view_matrix = worldmatrix.copy() var x = (2.0 * location.x) / self.view.bounds.size.width - 1.0; var y = 1.0 - (2.0 * location.y) / self.view.bounds.size.height; var newlocation = simplevertex(x: float(x), y: float(y), z: float(-1.0)) var projspaceloc = glkmatrix4multiplyvector4(glkmatrix4invert(proj_matrix.matrix(), nil), glkvector4make(newlocation.x, newlocation.y, newlocation.z, 1.0)) projspaceloc = glkvector4make(projspaceloc.x, projspaceloc.y, -1.0, 0.0) var viewspaceloc = glkmatrix4multiplyvector4(glkmatrix4invert(view_matrix.matrix(), nil), projspaceloc) glkvector4normalize(viewspaceloc) //tappable array of nodes can tapped node in tappable { var r: float = node.r //this radius of bounding sphere node var c: glkvector4 = node.origin //this origin of bounding sphere var little_c = glkvector4dotproduct(c, c) - powf(r, 2) var b = glkvector4dotproduct(viewspaceloc, c) var discriminant = powf(b, 2) - little_c //if discriminant positive, 2 points of intersection, if 0, 1 point, , if negative, no points. if (discriminant >= 0) { var t_1 = (float(-1.0)*b) + sqrtf(discriminant) var t_2 = (float(-1.0)*b) - sqrtf(discriminant) if (intersect != nil) { if (min(t_1, t_2) < intersect.intersect_point) { intersect = (node, min(t_1, t_2)) } } else { intersect = (node, min(t_1, t_2)) } } }
Comments
Post a Comment