summaryrefslogtreecommitdiff
path: root/ActiveRagdoll/Assets/TABG/Scripts/Debug/GLHandle.cs
blob: ea64cec8e84e382c714df05a22d73e8af5cafaa8 (plain)
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
using System;
using UnityEngine;

namespace Rigging.Debugging
{

    public class GLScope : IDisposable
    {
        bool bScopeBefore;

        public GLScope(bool enabled)
        {
            bScopeBefore = GLHandle.bScopeEnabled;
            GLHandle.EnterScope(enabled);
        }

        public void Dispose()
        {
            GLHandle.EndScope(bScopeBefore);
        }
    }

    [DefaultExecutionOrder(-100)]
    public class GLHandle : MonoBehaviour
    {

        private static GLHandle instance;
        private static event System.Action onGL;
        private static event System.Action onGLFixedUpdate;

        private static Material lineMaterial;

        public static bool bScopeEnabled;

        static GLHandle()
        {
            bScopeEnabled = true;
        }

        static void CreateLineMaterial()
        {
            if (!lineMaterial)
            {
                // Unity has a built-in shader that is useful for drawing
                // simple colored things.
                Shader shader = Shader.Find("Hidden/Internal-Colored");
                lineMaterial = new Material(shader);
                lineMaterial.hideFlags = HideFlags.HideAndDontSave;
                // Turn on alpha blending
                lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                // Turn backface culling off
                lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
                // Turn off depth writes
                lineMaterial.SetInt("_ZWrite", 0);
                lineMaterial.SetInt("_ZTest", 0);
            }
        }

        private void FixedUpdate()
        {
            onGLFixedUpdate = null;
        }

        public void OnRenderObject()
        {
            CreateLineMaterial();

            // Apply the line material
            lineMaterial.SetPass(0);

            onGL?.Invoke();
            onGL = null;

            onGLFixedUpdate?.Invoke();
        }

        static void Check()
        {
            if(instance == null)
            {
                GameObject gl = new GameObject();
                gl.name = "GLHandle";
                instance = gl.AddComponent<GLHandle>();
            }
        }

        public static void EnterScope(bool enabled = true)
        {
            bScopeEnabled = enabled;
        }

        public static void EndScope(bool enabled = true)
        {
            bScopeEnabled = true;
        }

        public static void DrawSphere(Vector3 pos, float radius, int verticalSegments = 1, int radialSegments = 2)
        {
            Check();

            if (!bScopeEnabled) return;

            if(Time.inFixedTimeStep)
            {
                onGLFixedUpdate += () =>
                {
                    Draw.Sphere(pos, radius, verticalSegments, radialSegments);
                };
            }
            else
            {
                onGL += () =>
                {
                    Draw.Sphere(pos, radius, verticalSegments, radialSegments);
                };
            }
        }

        public static void DrawLine(Vector3 p1, Vector3 p2)
        {
            Check();

            if (!bScopeEnabled) return;

            if (Time.inFixedTimeStep)
            {
                onGLFixedUpdate += () =>
                {
                    Draw.Line3D(p1, p2);
                };
            }
            else
            {
                onGL += () =>
                {
                    Draw.Line3D(p1, p2);
                };
            }
        }

    }

}