public static HitTestRay HitTestRayFromScreenPos(this ARSCNView self, CGPoint point) { if (self.Session == null || ViewController.CurrentFrame == null) { return(null); } var frame = ViewController.CurrentFrame; if (frame == null || frame.Camera == null || frame.Camera.Transform == null) { return(null); } var cameraPos = SCNVector3Extensions.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 = self.UnprojectPoint(positionVec); var rayDirection = screenPosOnFarClippingPlane.Subtract(cameraPos); rayDirection.Normalize(); return(new HitTestRay(cameraPos, rayDirection)); }
/// Reduce visual size change with distance by scaling up when close and down when far away. /// /// These adjustments result in a scale of 1.0x for a distance of 0.7 m or less /// (estimated distance when looking at a table), and a scale of 1.2x /// for a distance 1.5 m distance (estimated distance when looking at the floor). private float ScaleBasedOnDistance(ARCamera camera) { if (camera == null) { return(1.0f); } var distanceFromCamera = Position.Subtract(SCNVector3Extensions.PositionFromTransform(camera.Transform)).LengthFast; // This function reduces size changes of the focus square based on the distance by scaling it up if it far away, // and down if it is very close. // The values are adjusted such that scale will be 1 in 0.7 m distance (estimated distance when looking at a table), // and 1.2 in 1.5 m distance (estimated distance when looking at the floor). var newScale = (distanceFromCamera < 0.7f) ? (distanceFromCamera / 0.7f) : (0.25f * distanceFromCamera + 0.825f); return(newScale); }