public static Ray HitTestRayFromScreenPos(this ARSCNView sceneView, CGPoint point) { //if (sceneView.Session == null || ViewController.CurrentFrame == null) //{ // return null; //} var frame = ARGamePlay.CurrentFrame; //var frame = ViewController.CurrentFrame; if (frame == null || frame.Camera == null || frame.Camera.Transform == null) { return(null); } var cameraPos = SCNVector3Ex.PositionFromTransform(frame.Camera.Transform); // Note: z: 1.0 will unproject() the screen position to the far clipping plane. var positionVec = new SCNVector3((float)point.X, (float)point.Y, 1.0f); var screenPosOnFarClippingPlane = sceneView.UnprojectPoint(positionVec); var rayDirection = screenPosOnFarClippingPlane - cameraPos; //screenPosOnFarClippingPlane.Subtract(cameraPos); rayDirection.Normalize(); return(new Ray(cameraPos, rayDirection)); }
public static HitTestRay HitTestRayFromScreenPos(this ARSCNView self, CGPoint point) { if (self.Session == null || ViewController.CurrentFrame == null) { return(null); } var frame = ViewController.CurrentFrame; if (frame?.Camera?.Transform == null) { return(null); } SCNVector3 cameraPos; try { //TODO: why iPhone 7 plus crash on this? cameraPos = SCNVector3Extensions.PositionFromTransform(frame.Camera.Transform); } catch (Exception) { return(null); } // Note: z: 1.0 will unproject() the screen position to the far clipping plane. var positionVec = new SCNVector3((float)point.X, (float)point.Y, 1.0f); var screenPosOnFarClippingPlane = self.UnprojectPoint(positionVec); var rayDirection = screenPosOnFarClippingPlane.Subtract(cameraPos); rayDirection.Normalize(); return(new HitTestRay(cameraPos, rayDirection)); }
public static HitTestRay?HitTestRayFromScreenPosition(this ARSCNView view, CGPoint point) { HitTestRay?result = null; using (var frame = view.Session.CurrentFrame) { if (frame != null) { var cameraPosition = frame.Camera.Transform.GetTranslation(); // Note: z: 1.0 will unproject() the screen position to the far clipping plane. var positionVector = new SCNVector3((float)point.X, (float)point.Y, 1f); var screenPositionOnFarClippingPlane = view.UnprojectPoint(positionVector); var rayDirection = SCNVector3.Normalize(screenPositionOnFarClippingPlane - cameraPosition); result = new HitTestRay { Origin = cameraPosition, Direction = rayDirection }; } } return(result); }