Skip to content

Instantly share code, notes, and snippets.

@kalineh
Created September 27, 2017 00:54
Show Gist options
  • Save kalineh/d0962ba758fda6bcbee8d6235eba4817 to your computer and use it in GitHub Desktop.
Save kalineh/d0962ba758fda6bcbee8d6235eba4817 to your computer and use it in GitHub Desktop.
DebugMeshGizmo.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
// NOTES
// - DrawMesh() doesn't clear batches
// - DrawMeshNow() can have weird lighting behind some transparent things
// - EditorUtility.SetDirty() can be used to force batch clear for DrawMesh() if you have to use it
// - DrawGizmos() is called in addition to DrawGizmosSelected()
// - OnPostRender() wont show any DrawMeshNow() calls
// - no known way to check dirty state reliably (reflection extracted value seems unreliable)
// - OnValidate() not called in editor?
// - Camera.onPreCull and SceneView.onGui delegate same batching issues (unless setdirty)
[ExecuteInEditMode]
[SelectionBase]
public class Node
: MonoBehaviour
{
public GameObject renderPrefab;
#if UNITY_EDITOR
public void OnDrawGizmos()
{
if (UnityEditor.Selection.Contains(gameObject))
{
Gizmos.color = new Color(1.0f, 1.0f, 1.0f, 0.5f);
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawCube(Vector3.up * 0.5f, Vector3.one * 1.0f);
Gizmos.color = Color.white;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.up * 0.5f, Vector3.one * 1.0f);
}
else
{
Gizmos.color = new Color(0.0f, 0.0f, 0.0f, 0.0f);
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawCube(Vector3.up * 0.5f, Vector3.one * 0.5f);
}
}
public void OnRenderObject()
{
if (Application.isPlaying)
return;
if (renderPrefab == null)
return;
var meshRenderer = renderPrefab.GetComponentInChildren<MeshRenderer>();
if (meshRenderer != null)
{
var meshFilter = renderPrefab.GetComponentInChildren<MeshFilter>();
var mesh = meshFilter.sharedMesh;
var material = meshRenderer.sharedMaterial;
material.SetPass(0);
Graphics.DrawMeshNow(mesh, transform.position, transform.rotation);
}
var meshSkinnedRenderer = renderPrefab.GetComponentInChildren<SkinnedMeshRenderer>();
if (meshSkinnedRenderer != null)
{
var mesh = meshSkinnedRenderer.sharedMesh;
var material = meshSkinnedRenderer.sharedMaterial;
material.SetPass(0);
Graphics.DrawMeshNow(mesh, transform.position, transform.rotation);
}
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment