diff options
author | chai <215380520@qq.com> | 2024-04-02 17:19:00 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-04-02 17:19:00 +0800 |
commit | f29bf0024803264704e31ac78269f9be54b7e904 (patch) | |
tree | 7dba7a956713eeda56cc6a78dcdfc2a7f7f2f48c /ActiveRagdoll/Assets/TABG/Scripts/Debug/GLHandle.cs | |
parent | ce3211f753719e76051074ed332123b59d7e7a38 (diff) |
*debug
Diffstat (limited to 'ActiveRagdoll/Assets/TABG/Scripts/Debug/GLHandle.cs')
-rw-r--r-- | ActiveRagdoll/Assets/TABG/Scripts/Debug/GLHandle.cs | 143 |
1 files changed, 133 insertions, 10 deletions
diff --git a/ActiveRagdoll/Assets/TABG/Scripts/Debug/GLHandle.cs b/ActiveRagdoll/Assets/TABG/Scripts/Debug/GLHandle.cs index f2a6679..7f2eeae 100644 --- a/ActiveRagdoll/Assets/TABG/Scripts/Debug/GLHandle.cs +++ b/ActiveRagdoll/Assets/TABG/Scripts/Debug/GLHandle.cs @@ -1,18 +1,141 @@ -using System.Collections; -using System.Collections.Generic; +using System; using UnityEngine; -public class GLHandle : MonoBehaviour +namespace Rigging.Debugging { - // Start is called before the first frame update - void Start() + + public class GLScope : IDisposable { - + public GLScope(bool enabled) + { + GLHandle.EnterScope(enabled); + } + + public void Dispose() + { + GLHandle.EndScope(); + } } - // Update is called once per frame - void Update() + [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; + + private 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() + { + 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); + }; + } + } + } -} + +}
\ No newline at end of file |