using System; using UnityEngine; namespace Rigging.Debugging { 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 System.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(); } } } } }