File size: 5,726 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | using UnityEngine;
namespace UnityEditor.Performance.ProfileAnalyzer
{
internal class Draw2D
{
public enum Origin
{
TopLeft,
BottomLeft
};
Origin m_Origin = Origin.TopLeft;
GUIStyle m_GLStyle;
string m_ShaderName;
Material m_Material;
Rect m_Rect;
Vector4 m_ClipRect;
bool m_ClipRectEnabled = false;
public Draw2D(string shaderName)
{
m_ShaderName = shaderName;
CheckAndSetupMaterial();
}
public bool CheckAndSetupMaterial()
{
if (m_Material == null)
{
var shader = Shader.Find(m_ShaderName);
if (shader == null)
{
Debug.LogFormat("Unable to locate shader {0}", m_ShaderName);
return false;
}
m_Material = new Material(shader);
if (m_Material == null)
{
Debug.LogFormat("Unable to create material for {0}", m_ShaderName);
return false;
}
}
return true;
}
public bool IsMaterialValid()
{
if (m_Material == null)
return false;
return true;
}
public void OnGUI()
{
if (m_GLStyle == null)
{
m_GLStyle = new GUIStyle(GUI.skin.box);
m_GLStyle.padding = new RectOffset(0, 0, 0, 0);
m_GLStyle.margin = new RectOffset(0, 0, 0, 0);
}
}
public void SetClipRect(Rect clipRect)
{
m_ClipRect = new Vector4(clipRect.x, clipRect.y, clipRect.x + clipRect.width, clipRect.y + clipRect.height);
m_ClipRectEnabled = true;
if (CheckAndSetupMaterial())
{
m_Material.SetFloat("_UseClipRect", m_ClipRectEnabled ? 1f : 0f);
m_Material.SetVector("_ClipRect", m_ClipRect);
}
}
public void ClearClipRect()
{
m_ClipRectEnabled = false;
if (CheckAndSetupMaterial())
{
m_Material.SetFloat("_UseClipRect", m_ClipRectEnabled ? 1f : 0f);
m_Material.SetVector("_ClipRect", m_ClipRect);
}
}
public Rect GetClipRect()
{
return new Rect(m_ClipRect.x, m_ClipRect.y, m_ClipRect.z - m_ClipRect.x, m_ClipRect.w - m_ClipRect.y);
}
public bool DrawStart(Rect r, Origin origin = Origin.TopLeft)
{
if (Event.current.type != EventType.Repaint)
return false;
if (!CheckAndSetupMaterial())
return false;
m_Material.SetPass(0);
m_Rect = r;
m_Origin = origin;
return true;
}
public bool DrawStart(float w, float h, Origin origin = Origin.TopLeft, GUIStyle style = null)
{
Rect r = GUILayoutUtility.GetRect(w, h, style == null ? m_GLStyle : style, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
return DrawStart(r, origin);
}
public void DrawEnd()
{
}
public void Translate(ref float x, ref float y)
{
// Translation done CPU side so we have world space coords in the shader for clipping.
if (m_Origin == Origin.BottomLeft)
{
x = m_Rect.xMin + x;
y = m_Rect.yMax - y;
}
else
{
x = m_Rect.xMin + x;
y = m_Rect.yMin + y;
}
}
public void DrawFilledBox(float x, float y, float w, float h, Color col)
{
float x2 = x + w;
float y2 = y + h;
Translate(ref x, ref y);
Translate(ref x2, ref y2);
if (m_Origin == Origin.BottomLeft)
{
GL.Begin(GL.TRIANGLE_STRIP);
GL.Color(col);
GL.Vertex3(x, y, 0);
GL.Vertex3(x, y2, 0);
GL.Vertex3(x2, y, 0);
GL.Vertex3(x2, y2, 0);
GL.End();
}
else
{
GL.Begin(GL.TRIANGLE_STRIP);
GL.Color(col);
GL.Vertex3(x, y, 0);
GL.Vertex3(x2, y, 0);
GL.Vertex3(x, y2, 0);
GL.Vertex3(x2, y2, 0);
GL.End();
}
}
public void DrawFilledBox(float x, float y, float w, float h, float r, float g, float b)
{
DrawFilledBox(x, y, w, h, new Color(r, g, b));
}
public void DrawLine(float x, float y, float x2, float y2, Color col)
{
Translate(ref x, ref y);
Translate(ref x2, ref y2);
GL.Begin(GL.LINES);
GL.Color(col);
GL.Vertex3(x, y, 0);
GL.Vertex3(x2, y2, 0);
GL.End();
}
public void DrawLine(float x, float y, float x2, float y2, float r, float g, float b)
{
DrawLine(x, y, x2, y2, new Color(r, g, b));
}
public void DrawBox(float x, float y, float w, float h, Color col)
{
float x2 = x + w;
float y2 = y + h;
Translate(ref x, ref y);
Translate(ref x2, ref y2);
GL.Begin(GL.LINE_STRIP);
GL.Color(col);
GL.Vertex3(x, y, 0);
GL.Vertex3(x2, y, 0);
GL.Vertex3(x2, y2, 0);
GL.Vertex3(x, y2, 0);
GL.Vertex3(x, y, 0);
GL.End();
}
public void DrawBox(float x, float y, float w, float h, float r, float g, float b)
{
DrawBox(x, y, w, h, new Color(r, g, b));
}
}
}
|