diff options
author | chai <215380520@qq.com> | 2024-03-14 11:43:40 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-14 11:43:40 +0800 |
commit | cc55520a19043a7b4870858e962fa3e20c46bc39 (patch) | |
tree | b437f788e506a48ec16a215c6965b8170f15d5f6 /_Debug/DebugRigidBody.cs | |
parent | 54c872fa42b1ba0fdbcfe812b80bb8eb0cfe108f (diff) |
*misc
Diffstat (limited to '_Debug/DebugRigidBody.cs')
-rw-r--r-- | _Debug/DebugRigidBody.cs | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/_Debug/DebugRigidBody.cs b/_Debug/DebugRigidBody.cs new file mode 100644 index 0000000..8267f50 --- /dev/null +++ b/_Debug/DebugRigidBody.cs @@ -0,0 +1,140 @@ + +using UnityEngine; + +public class DebugRigidBody : MonoBehaviour +{ + + public enum EMode + { + Axis, + Cube, + Axis_Cube, + } + 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; + + 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; + + 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(); + } + } + +} |