Quantcast
Channel: Answers for "Can you only make Raycasts visible in scene view?"
Viewing all articles
Browse latest Browse all 4

Answer by Cherno

$
0
0
Here is Drawing.cs, taken from the Unity Wiki (I think). It's used by calling Drawing.DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias) and you obviously have to convert points in 3D space to 2d Screen points first. using System.Reflection; using UnityEngine; // Line drawing routine originally courtesy of Linusmartensson: // http://forum.unity3d.com/threads/71979-Drawing-lines-in-the-editor // // Rewritten to improve performance by Yossarian King / August 2013. // // This version produces virtually identical results to the original (tested by drawing // one over the other and observing errors of one pixel or less), but for large numbers // of lines this version is more than four times faster than the original, and comes // within about 70% of the raw performance of Graphics.DrawTexture. // // Peak performance on my laptop is around 200,000 lines per second. The laptop is // Windows 7 64-bit, Intel Core2 Duo CPU 2.53GHz, 4G RAM, NVIDIA GeForce GT 220M. // Line width and anti-aliasing had negligible impact on performance. // // For a graph of benchmark results in a standalone Windows build, see this image: // https://app.box.com/s/hyuhi565dtolqdm97e00 // // For a Google spreadsheet with full benchmark results, see: // https://docs.google.com/spreadsheet/ccc?key=0AvJlJlbRO26VdHhzeHNRMVF2UHZHMXFCTVFZN011V1E&usp=sharing public static class Drawing { private static Texture2D aaLineTex = null; private static Texture2D lineTex = null; private static Material blitMaterial = null; private static Material blendMaterial = null; private static Rect lineRect = new Rect(0, 0, 1, 1); // Draw a line in screen space, suitable for use from OnGUI calls from either // MonoBehaviour or EditorWindow. Note that this should only be called during repaint // events, when (Event.current.type == EventType.Repaint). // // Works by computing a matrix that transforms a unit square -- Rect(0,0,1,1) -- into // a scaled, rotated, and offset rectangle that corresponds to the line and its width. // A DrawTexture call used to draw a line texture into the transformed rectangle. // // More specifically: // scale x by line length, y by line width // rotate around z by the angle of the line // offset by the position of the upper left corner of the target rectangle // // By working out the matrices and applying some trigonometry, the matrix calculation comes // out pretty simple. See https://app.box.com/s/xi08ow8o8ujymazg100j for a picture of my // notebook with the calculations. public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias) { // Normally the static initializer does this, but to handle texture reinitialization // after editor play mode stops we need this check in the Editor. #if UNITY_EDITOR if (!lineTex) { Initialize(); } #endif // Note that theta = atan2(dy, dx) is the angle we want to rotate by, but instead // of calculating the angle we just use the sine (dy/len) and cosine (dx/len). float dx = pointB.x - pointA.x; float dy = pointB.y - pointA.y; float len = Mathf.Sqrt(dx * dx + dy * dy); // Early out on tiny lines to avoid divide by zero. // Plus what's the point of drawing a line 1/1000th of a pixel long?? if (len

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images