Skip to content

Commit 0d07103

Browse files
committed
Fix for ui drawing not on top of everything. Less singletonmess. General cleanup
1 parent d38d837 commit 0d07103

16 files changed

+196
-164
lines changed

Assets/DebugOverlay/Console.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ void CmdHelp(string[] args)
5151

5252
public void Init()
5353
{
54-
Init(80, 25);
54+
Init(null);
5555
}
5656

57-
public void Init(int width, int height)
57+
public void Init(DebugOverlay debugOverlay)
5858
{
59-
Resize(width, height);
59+
m_DebugOverlay = debugOverlay != null ? debugOverlay : DebugOverlay.instance;
60+
Resize(m_DebugOverlay.width, m_DebugOverlay.height);
6061
Clear();
6162
}
6263

6364
public void Shutdown()
6465
{
65-
6666
}
6767

6868
public delegate void CommandDelegate(string[] args);
@@ -235,7 +235,7 @@ public void TickLateUpdate()
235235
return;
236236

237237
var yoffset = -(float)m_Height * (1.0f - m_ConsoleFoldout);
238-
DebugOverlay.DrawRect(0, 0 + yoffset, m_Width, m_Height, m_BackgroundColor);
238+
m_DebugOverlay._DrawRect(0, 0 + yoffset, m_Width, m_Height, m_BackgroundColor);
239239

240240
var line = m_LastVisibleLine;
241241
if ((m_LastVisibleLine == m_LastLine) && (m_LastColumn == 0))
@@ -260,7 +260,7 @@ public void TickLateUpdate()
260260
col.z = (float)((icol >> 8) & 0xff) / 255.0f;
261261
}
262262
if (c != '\0')
263-
DebugOverlay.instance.AddQuad(j, m_Height - 2 - i + yoffset, 1, 1, ch, col);
263+
m_DebugOverlay.AddQuad(j, m_Height - 2 - i + yoffset, 1, 1, ch, col);
264264
}
265265
}
266266

@@ -271,9 +271,9 @@ public void TickLateUpdate()
271271
{
272272
char c = m_InputFieldBuffer[i];
273273
if (c != '\0')
274-
DebugOverlay.instance.AddQuad(i - horizontalScroll, m_Height - 1 + yoffset, 1, 1, c, m_TextColor);
274+
m_DebugOverlay.AddQuad(i - horizontalScroll, m_Height - 1 + yoffset, 1, 1, c, m_TextColor);
275275
}
276-
DebugOverlay.instance.AddQuad(m_CursorPos - horizontalScroll, m_Height - 1 + yoffset, 1, 1, '\0', m_CursorCol);
276+
m_DebugOverlay.AddQuad(m_CursorPos - horizontalScroll, m_Height - 1 + yoffset, 1, 1, '\0', m_CursorCol);
277277
}
278278

279279
void NewLine()
@@ -430,4 +430,5 @@ static int CommonPrefix(string a, string b)
430430
return minl;
431431
}
432432

433+
DebugOverlay m_DebugOverlay;
433434
}

Assets/DebugOverlay/DebugOverlay.cs

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,29 @@
11
using UnityEngine;
22
using System.Collections;
33

4-
public class DebugOverlay : MonoBehaviour
4+
public class DebugOverlay
55
{
6-
[Header("Overlay size")]
7-
[SerializeField]
8-
int width = 80;
9-
[SerializeField]
10-
int height = 25;
11-
12-
[Header("Font material info")]
13-
public Material instanceMaterialProc;
14-
[Tooltip("Number of columns of glyphs on texture")]
15-
public int charCols = 30;
16-
[Tooltip("Number of rows of glyphs on texture")]
17-
public int charRows = 16;
18-
[Tooltip("Width in pixels of each glyph")]
19-
public int cellWidth = 32;
20-
[Tooltip("Height in pixels of each glyph")]
21-
public int cellHeight = 32;
22-
23-
public Shader lineShaderProc;
6+
public int width = 80;
7+
public int height = 25;
248

259
public static DebugOverlay instance;
10+
static DebugOverlayResources resources;
2611

2712
public static int Width { get { return instance.width; } }
2813
public static int Height { get { return instance.height; } }
2914

30-
void Awake()
15+
public void Init(int w, int h)
3116
{
32-
m_LineMaterial = new Material(lineShaderProc);
33-
}
17+
if(resources == null)
18+
{
19+
resources = Resources.Load<DebugOverlayResources>("DebugOverlayResources");
20+
Debug.Assert(resources != null, "Unable to load DebugOverlayResources");
21+
}
22+
if (instance == null)
23+
instance = this;
3424

35-
public void Init()
36-
{
37-
instance = this;
25+
width = w;
26+
height = h;
3827
}
3928

4029
public void Shutdown()
@@ -49,7 +38,8 @@ public void Shutdown()
4938
m_LineInstanceBuffer = null;
5039
m_LineInstanceData = null;
5140

52-
instance = null;
41+
if (instance == this)
42+
instance = null;
5343
}
5444

5545
public void TickLateUpdate()
@@ -64,7 +54,7 @@ public void TickLateUpdate()
6454
}
6555

6656
m_QuadInstanceBuffer = new ComputeBuffer(m_QuadInstanceData.Length, 16 + 16 + 16);
67-
instanceMaterialProc.SetBuffer("positionBuffer", m_QuadInstanceBuffer);
57+
resources.glyphMaterial.SetBuffer("positionBuffer", m_QuadInstanceBuffer);
6858
}
6959

7060
if (m_LineInstanceBuffer == null || m_LineInstanceBuffer.count != m_LineInstanceData.Length)
@@ -76,7 +66,7 @@ public void TickLateUpdate()
7666
}
7767

7868
m_LineInstanceBuffer = new ComputeBuffer(m_LineInstanceData.Length, 16 + 16);
79-
m_LineMaterial.SetBuffer("positionBuffer", m_LineInstanceBuffer);
69+
resources.lineMaterial.SetBuffer("positionBuffer", m_LineInstanceBuffer);
8070
}
8171

8272
m_QuadInstanceBuffer.SetData(m_QuadInstanceData, 0, 0, m_NumQuadsUsed);
@@ -85,21 +75,17 @@ public void TickLateUpdate()
8575
m_LineInstanceBuffer.SetData(m_LineInstanceData, 0, 0, m_NumLinesUsed);
8676
m_NumLinesToDraw = m_NumLinesUsed;
8777

88-
instanceMaterialProc.SetVector("scales", new Vector4(
78+
resources.glyphMaterial.SetVector("scales", new Vector4(
8979
1.0f / width,
9080
1.0f / height,
91-
(float)cellWidth / instanceMaterialProc.mainTexture.width,
92-
(float)cellHeight / instanceMaterialProc.mainTexture.height));
81+
(float)resources.cellWidth / resources.glyphMaterial.mainTexture.width,
82+
(float)resources.cellHeight / resources.glyphMaterial.mainTexture.height));
9383

94-
m_LineMaterial.SetVector("scales", new Vector4(1.0f / width, 1.0f / height, 1.0f / 1280.0f, 1.0f / 720.0f));
84+
resources.lineMaterial.SetVector("scales", new Vector4(1.0f / width, 1.0f / height, 1.0f / 1280.0f, 1.0f / 720.0f));
9585

9686
_Clear();
9787
}
9888

99-
/// <summary>
100-
/// Set color of text.
101-
/// </summary>
102-
/// <param name="col"></param>
10389
public static void SetColor(Color col)
10490
{
10591
if (instance == null)
@@ -351,7 +337,7 @@ void _DrawHist(float x, float y, float w, float h, float[][] data, int startSamp
351337
}
352338
}
353339

354-
void _DrawRect(float x, float y, float w, float h, Color col)
340+
public void _DrawRect(float x, float y, float w, float h, Color col)
355341
{
356342
AddQuad(m_OriginX + x, m_OriginY + y, w, h, '\0', col);
357343
}
@@ -370,11 +356,11 @@ void _Write<T>(float x, float y, string format, T argList) where T : IArgList
370356
_DrawText(x, y, ref _buf, num);
371357
}
372358

373-
void OnPostRender()
359+
public void Render()
374360
{
375-
m_LineMaterial.SetPass(0);
361+
resources.lineMaterial.SetPass(0);
376362
Graphics.DrawProcedural(MeshTopology.Triangles, m_NumLinesToDraw * 6, 1);
377-
instanceMaterialProc.SetPass(0);
363+
resources.glyphMaterial.SetPass(0);
378364
Graphics.DrawProcedural(MeshTopology.Triangles, m_NumQuadsToDraw * 6, 1);
379365
}
380366

@@ -412,8 +398,8 @@ public unsafe void AddQuad(float x, float y, float w, float h, char c, Vector4 c
412398
{
413399
if (c != '\0')
414400
{
415-
d->positionAndUV.z = (c - 32) % charCols;
416-
d->positionAndUV.w = (c - 32) / charCols;
401+
d->positionAndUV.z = (c - 32) % resources.charCols;
402+
d->positionAndUV.w = (c - 32) / resources.charCols;
417403
col.w = 0.0f;
418404
}
419405
else
@@ -461,6 +447,4 @@ struct LineInstanceData
461447
int m_NumLinesToDraw = 0;
462448
QuadInstanceData[] m_QuadInstanceData = new QuadInstanceData[128];
463449
LineInstanceData[] m_LineInstanceData = new LineInstanceData[128];
464-
465-
Material m_LineMaterial;
466450
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
[CreateAssetMenu]
6+
public class DebugOverlayResources : ScriptableObject
7+
{
8+
[Tooltip("Number of columns of glyphs on texture")]
9+
public int charCols = 30;
10+
[Tooltip("Number of rows of glyphs on texture")]
11+
public int charRows = 16;
12+
[Tooltip("Width in pixels of each glyph")]
13+
public int cellWidth = 32;
14+
[Tooltip("Height in pixels of each glyph")]
15+
public int cellHeight = 32;
16+
17+
public Material lineMaterial;
18+
public Material glyphMaterial;
19+
}

Assets/DebugOverlay/DebugOverlayResources.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Material:
66
m_ObjectHideFlags: 0
77
m_PrefabParentObject: {fileID: 0}
88
m_PrefabInternal: {fileID: 0}
9-
m_Name: GlyphMatProc
9+
m_Name: GlyphMaterial
1010
m_Shader: {fileID: 4800000, guid: e77d75abe795e9040a2f1c7a49576231, type: 3}
1111
m_ShaderKeywords:
1212
m_LightmapFlags: 4

Assets/DebugOverlay/GlyphMatProc.mat.meta renamed to Assets/DebugOverlay/GlyphMaterial.mat.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/DebugOverlay/GlyphShaderProc.shader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Shader "Instanced/GlyphShaderProc" {
6363

6464
// Generate position
6565
float2 p = (v_pos*scale + pos_uv.xy) * scales.xy;
66+
p.y = 1.0 - p.y;
6667
p = float2(-1, -1) + p * 2.0;
6768

6869
v2f o;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!21 &2100000
4+
Material:
5+
serializedVersion: 6
6+
m_ObjectHideFlags: 0
7+
m_PrefabParentObject: {fileID: 0}
8+
m_PrefabInternal: {fileID: 0}
9+
m_Name: LineMaterial
10+
m_Shader: {fileID: 4800000, guid: b0127e7e38d54ef4394014ff4ccab268, type: 3}
11+
m_ShaderKeywords:
12+
m_LightmapFlags: 4
13+
m_EnableInstancingVariants: 0
14+
m_DoubleSidedGI: 0
15+
m_CustomRenderQueue: -1
16+
stringTagMap: {}
17+
disabledShaderPasses: []
18+
m_SavedProperties:
19+
serializedVersion: 3
20+
m_TexEnvs:
21+
- _BumpMap:
22+
m_Texture: {fileID: 0}
23+
m_Scale: {x: 1, y: 1}
24+
m_Offset: {x: 0, y: 0}
25+
- _DetailAlbedoMap:
26+
m_Texture: {fileID: 0}
27+
m_Scale: {x: 1, y: 1}
28+
m_Offset: {x: 0, y: 0}
29+
- _DetailMask:
30+
m_Texture: {fileID: 0}
31+
m_Scale: {x: 1, y: 1}
32+
m_Offset: {x: 0, y: 0}
33+
- _DetailNormalMap:
34+
m_Texture: {fileID: 0}
35+
m_Scale: {x: 1, y: 1}
36+
m_Offset: {x: 0, y: 0}
37+
- _EmissionMap:
38+
m_Texture: {fileID: 0}
39+
m_Scale: {x: 1, y: 1}
40+
m_Offset: {x: 0, y: 0}
41+
- _MainTex:
42+
m_Texture: {fileID: 2800000, guid: dfd2b625635c1ee439ec40cb25a86661, type: 3}
43+
m_Scale: {x: 1, y: 1}
44+
m_Offset: {x: 0, y: 0}
45+
- _MetallicGlossMap:
46+
m_Texture: {fileID: 0}
47+
m_Scale: {x: 1, y: 1}
48+
m_Offset: {x: 0, y: 0}
49+
- _OcclusionMap:
50+
m_Texture: {fileID: 0}
51+
m_Scale: {x: 1, y: 1}
52+
m_Offset: {x: 0, y: 0}
53+
- _ParallaxMap:
54+
m_Texture: {fileID: 0}
55+
m_Scale: {x: 1, y: 1}
56+
m_Offset: {x: 0, y: 0}
57+
m_Floats:
58+
- _BumpScale: 1
59+
- _Cutoff: 0.5
60+
- _DetailNormalMapScale: 1
61+
- _DstBlend: 0
62+
- _GlossMapScale: 1
63+
- _Glossiness: 0.5
64+
- _GlossyReflections: 1
65+
- _Metallic: 0
66+
- _Mode: 0
67+
- _OcclusionStrength: 1
68+
- _Parallax: 0.02
69+
- _SmoothnessTextureChannel: 0
70+
- _SpecularHighlights: 1
71+
- _SrcBlend: 1
72+
- _UVSec: 0
73+
- _ZWrite: 1
74+
m_Colors:
75+
- _Color: {r: 1, g: 1, b: 1, a: 1}
76+
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

Assets/DebugOverlay/Resources/DebugOverlay.prefab.meta renamed to Assets/DebugOverlay/LineMaterial.mat.meta

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/DebugOverlay/LineShaderProc.shader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Shader "Instanced/LineShaderProc" {
6060
float2 dir = pos.zw - pos.xy;
6161
float2 pdir = normalize(float2(-dir.y, dir.x));
6262
float2 p = (pos.xy + dir*v_pos.y)*scales.xy + pdir * 3.0 * v_pos.x * scales.zw;
63+
p.y = 1.0 - p.y;
6364
p = float2(-1, -1) + p * 2.0;
6465

6566
v2f o;

0 commit comments

Comments
 (0)