Last active
February 26, 2019 20:51
-
-
Save HurricanKai/d2bb591d314a12c3cba0f40ab8d373b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DebugRenderer : MonoBehaviour | |
{ | |
private bool _enabled = true; | |
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; | |
// set color | |
lineMaterial.SetColor("_Color", Color.white); | |
// 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); | |
} | |
} | |
public void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.F11)) | |
_enabled = !_enabled; | |
} | |
public void OnRenderObject() | |
{ | |
if (!_enabled || Camera.current != Camera.main) | |
return; | |
CreateLineMaterial(); | |
// Apply the line material | |
lineMaterial.SetPass(0); | |
GL.PushMatrix(); | |
// Set transformation matrix for drawing to | |
// match our transform | |
// GL.MultMatrix(transform.localToWorldMatrix); | |
var collisionGroup = Contexts.sharedInstance.logic.GetGroup(LogicMatcher.AllOf(LogicMatcher.Collider)); | |
var collisionEntities = collisionGroup.GetEntities(); | |
GL.Begin(GL.LINES); | |
GL.Color(Color.red); | |
Vector3 ConvertToVector(FixedVector2 vector) | |
=> new Vector3(vector.x.ToPreciseFloat(), vector.y.ToPreciseFloat(), 0f); | |
/* | |
* version that uses the bounds, not rendering floors. not sure what's preferable | |
void Vertex3(FixedVector2 vector) | |
=> GL.Vertex3(vector.x.ToPreciseFloat(), vector.y.ToPreciseFloat(), 0f); | |
foreach (var entity in collisionEntities) | |
{ | |
var bounds = entity.collider.value.bounds; | |
Vertex3(bounds.bottomRight); | |
Vertex3(bounds.topRight); | |
Vertex3(bounds.topRight); | |
Vertex3(bounds.topLeft); | |
Vertex3(bounds.topLeft); | |
Vertex3(bounds.bottomLeft); | |
Vertex3(bounds.bottomLeft); | |
Vertex3(bounds.bottomRight); | |
} | |
*/ | |
foreach (var entity in collisionEntities) | |
{ | |
var edges = entity.collider.value.edges; | |
foreach (var edge in edges) | |
{ | |
GL.Vertex(ConvertToVector(edge.start)); | |
GL.Vertex(ConvertToVector(edge.end)); | |
} | |
} | |
GL.End(); | |
var velocityGroup = Contexts.sharedInstance.logic.GetGroup(LogicMatcher.AllOf(LogicMatcher.Position, LogicMatcher.Velocity)); | |
var velocityEntities = velocityGroup.GetEntities(); | |
GL.Begin(GL.LINES); | |
GL.Color(Color.green); | |
foreach (var entity in velocityEntities) | |
{ | |
var v = ConvertToVector(entity.position.value); | |
GL.Vertex(v); | |
GL.Vertex(v + (ConvertToVector(entity.velocity.value) / 3)); | |
} | |
GL.End(); | |
GL.PopMatrix(); | |
} | |
} |
Updated to Show Hitboxes of Floor (old version kept as a comment, not sure what's better) & removed weird Artifacts in the background (caused by the class rendering to the Background Paralax Cam)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use different Colors