namespace UnityEngine.Perception.GroundTruth { /// /// Helper class that contains common visualization methods useful to ground truth labelers. /// public static class VisualizationHelper { static Texture2D s_OnePixel = new Texture2D(1, 1); /// /// Converts a 3D world space coordinate to image pixel space. /// /// The rendering camera /// The 3D world location to convert /// The coordinate in pixel space public static Vector3 ConvertToScreenSpace(Camera camera, Vector3 worldLocation) { var pt = camera.WorldToScreenPoint(worldLocation); pt.y = Screen.height - pt.y; return pt; } static Rect ToBoxRect(float x, float y, float halfSize = 3.0f) { return new Rect(x - halfSize, y - halfSize, halfSize * 2, halfSize * 2); } /// /// Draw a point (in pixel space) on the screen /// /// The point location, in pixel space /// The color of the point /// The width of the point /// The texture to use for the point, defaults to a solid pixel public static void DrawPoint(Vector3 pt, Color color, float width = 4.0f, Texture texture = null) { DrawPoint(pt.x, pt.y, color, width, texture); } /// /// Draw a point (in pixel space) on the screen /// /// The point's x value, in pixel space /// The point's y value, in pixel space /// The color of the point /// The width of the point /// The texture to use for the point, defaults to a solid pixel public static void DrawPoint(float x, float y, Color color, float width = 4, Texture texture = null) { if (texture == null) texture = s_OnePixel; var oldColor = GUI.color; GUI.color = color; GUI.DrawTexture(ToBoxRect(x, y, width * 0.5f), texture); GUI.color = oldColor; } static float Magnitude(float p1X, float p1Y, float p2X, float p2Y) { var x = p2X - p1X; var y = p2Y - p1Y; return Mathf.Sqrt(x * x + y * y); } /// /// Draw's a texture between two locations of a passed in width. /// /// The start point in pixel space /// The end point in pixel space /// The color of the line /// The width of the line /// The texture to use, if null, will draw a solid line of passed in color public static void DrawLine(Vector2 p1, Vector2 p2, Color color, float width = 3.0f, Texture texture = null) { DrawLine(p1.x, p1.y, p2.x, p2.y, color, width, texture); } /// /// Draw's a texture between two locations of a passed in width. /// /// The start point's x coordinate in pixel space /// The start point's y coordinate in pixel space /// The end point's x coordinate in pixel space /// The end point's y coordinate in pixel space /// The color of the line /// The width of the line /// The texture to use, if null, will draw a solid line of passed in color public static void DrawLine (float p1X, float p1Y, float p2X, float p2Y, Color color, float width = 3.0f, Texture texture = null) { if (texture == null) texture = s_OnePixel; var oldColor = GUI.color; GUI.color = color; var matrixBackup = GUI.matrix; var angle = Mathf.Atan2 (p2Y - p1Y, p2X - p1X) * 180f / Mathf.PI; var length = Magnitude(p1X, p1Y, p2X, p2Y); GUIUtility.RotateAroundPivot (angle, new Vector2(p1X, p1Y)); var halfWidth = width * 0.5f; GUI.DrawTexture (new Rect (p1X - halfWidth, p1Y - halfWidth, length, width), texture); GUI.matrix = matrixBackup; GUI.color = oldColor; } } }