2y ago · May 18, 2024 5:00 PM
When rendering dozens of snaplines, skeleton bones, and bounding boxes in Unity, can generate significant CPU overhead.
Using Unity's low-level pipeline for maximum FPS:
Call this inside or your render hook for zero-overhead GPU line drawing!
CSHARP
GUI.DrawTextureUsing Unity's low-level
CODE
GL.LINES CSHARP
using UnityEngine;
public class GLESPRenderer : MonoBehaviour
{
private static Material _lineMaterial;
private static void CreateLineMaterial()
{
if (_lineMaterial == null)
{
Shader shader = Shader.Find("Hidden/Internal-Colored");
_lineMaterial = new Material(shader) { hideFlags = HideFlags.HideAndDontSave };
_lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
_lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
_lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
_lineMaterial.SetInt("_ZWrite", 0);
_lineMaterial.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
}
}
public static void RenderSnapline(Vector3 screenFrom, Vector3 screenTo, Color color)
{
CreateLineMaterial();
_lineMaterial.SetPass(0);
GL.PushMatrix();
GL.LoadPixelMatrix();
GL.Begin(GL.LINES);
GL.Color(color);
GL.Vertex3(screenFrom.x, screenFrom.y, 0);
GL.Vertex3(screenTo.x, screenTo.y, 0);
GL.End();
GL.PopMatrix();
}
}Call this inside
CODE
Camera.onPostRender
DirectXRay · DirectX 12 & Vulkan Command Lists
The following users thanked DirectXRay for this post: