summaryrefslogtreecommitdiff
path: root/_Debug/DebugRigidBody.cs
blob: a83d1ed99c51b56c5bf42e9c08221f90a27db8a8 (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
145
146
147
148
149
150
151

using System;
using UnityEngine;

public class DebugRigidBody : MonoBehaviour
{

    public enum EMode
    {
        Axis,
        Cube,
        Axis_Cube,
        Custom,
    }
    public EMode mode = EMode.Axis;

    public Color color = Color.white;

    // When added to an object, draws colored rays from the
    // transform position.
    public int lineCount = 100;
    public float radius = 3.0f;

    public float cubeLen = 0.05f;

    public Action customDraw;

    static Material lineMaterial;
    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);
        }
    }

    private void PutVertex(Vector3 vert)
    {
        GL.Vertex3(vert.x, vert.y, vert.z);
    }

    // Will be called after all regular rendering is done
    public void OnRenderObject()
    {
        CreateLineMaterial();
        // Apply the line material
        lineMaterial.SetPass(0);

        if (mode == EMode.Axis || mode == EMode.Axis_Cube)
        {
            GL.PushMatrix();
            // Set transformation matrix for drawing to
            // match our transform
            GL.MultMatrix(transform.localToWorldMatrix);

            // Draw lines
            GL.Begin(GL.LINES);

            float len = 0.3f;

            GL.Color(Color.red);
            PutVertex(Vector3.zero);
            PutVertex(Vector3.right * len);

            GL.Color(Color.green);
            PutVertex(Vector3.zero);
            PutVertex(Vector3.up * len);

            GL.Color(Color.blue);
            PutVertex(Vector3.zero);
            PutVertex(Vector3.forward * len);

            GL.End();
            GL.PopMatrix();
        }
        if (mode == EMode.Cube || mode == EMode.Axis_Cube)
        {
            GL.PushMatrix();
            // Set transformation matrix for drawing to
            // match our transform
            GL.MultMatrix(transform.localToWorldMatrix);

            float len = cubeLen/2.0f;

            GL.wireframe = true;

            GL.Begin(GL.QUADS);

            GL.Color(color);

            GL.Vertex3(len, len, -len);
            GL.Vertex3(-len, len, -len);
            GL.Vertex3(-len, len, len);
            GL.Vertex3(len, len, len);

            // Bottom face (y = -len)
            GL.Vertex3(len, -len, len);
            GL.Vertex3(-len, -len, len);
            GL.Vertex3(-len, -len, -len);
            GL.Vertex3(len, -len, -len);

            // Front face  (z = len)
            GL.Vertex3(len, len, len);
            GL.Vertex3(-len, len, len);
            GL.Vertex3(-len, -len, len);
            GL.Vertex3(len, -len, len);

            // Back face (z = -len)
            GL.Vertex3(len, -len, -len);
            GL.Vertex3(-len, -len, -len);
            GL.Vertex3(-len, len, -len);
            GL.Vertex3(len, len, -len);

            // Left face (x = -len)
            GL.Vertex3(-len, len, len);
            GL.Vertex3(-len, len, -len);
            GL.Vertex3(-len, -len, -len);
            GL.Vertex3(-len, -len, len);

            // Right face (x = len)
            GL.Vertex3(len, len, -len);
            GL.Vertex3(len, len, len);
            GL.Vertex3(len, -len, len);
            GL.Vertex3(len, -len, -len);
            GL.End();  // End of drawing color-cube

            GL.wireframe = false;

            GL.PopMatrix();
        }
        if(mode == EMode.Custom)
        {
            if(customDraw != null)
            {
                customDraw.Invoke();
            }
        }
    }

}