- Home /
Debug Rendering using the depth buffer
How do you make some debug rendering in editormode (game not playing) which takes the depth buffer into account? I need a couple of transparent triangles to visualize a tool for the level designers.
I consider using GameObjects with MeshFilter, MeshRenderer and HideFlags.HideAndDontSave. I think this solution will work though it is slightly akward. Is it possible to avoid using GameObjects?
Answer by Kuba · Nov 03, 2010 at 01:30 PM
The approach with Graphics.DrawMesh() is the preferred one, since it will make the mesh fit in naturally into the rendering queue. Thanks to that it can be per pixel lit, cast and receive shadows, and, of course, can be Z tested. Use that method in the Update callback on a MonoBehaviour with the ExecuteInEditMode attribute set.
NOTE: in edit mode Update is only called when the scene is updated, i.e. when a property of any component is changed via inspector or when objects are manipulated in the scene view.
Here's a package showing mesh being modified in Update and submitted to rendering via Grahics.DrawMesh(): DrawMeshInEditMode.unitypackage
[ExecuteInEditMode] public class DrawMeshInEditMode : MonoBehaviour { public Material m_Material; private Mesh m_Mesh; private Matrix4x4 m_Trs; [...]
void Update()
{
[...]
Graphics.DrawMesh(m_Mesh, m_Trs, m_Material, 0);
}
}
Thanks a lot! did not knew about the ExecuteInEdit$$anonymous$$ode feature. Was messing around with UnityEditor.EditorApplication.update and SceneView.onSceneGUIDelegate callbacks - ouch.
Answer by Thomas Krog · Oct 25, 2010 at 08:56 PM
I found Graphics.DrawMesh which solves the problem without using GameObjects in playmode. However in editormode (!Application.isPlaying) the mesh no longer looks transparent and the performance keeps decreasing as if the render queue slowly grows towards infinity.
Any ideas how to get a stable debugrendering in editormode?
Answer by thorbrian · Dec 03, 2010 at 03:21 AM
What works stably and well for me is using Graphics.DrawMeshNow() from the OnRenderObject() function of the Monobehavior script with ExecuteInEditMode set on an object and using Application.IsPlaying to skip the drawing when the game is playing
(using DrawMesh() had problems for me - if you need to change the material, try calling SetPass on the material before DrawMeshNow)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Is there a way to write to Deferred Depth from a command buffer in CameraEvent.AfterImageEffects? 1 Answer
Get distance to camera from CameraDepthTexture 1 Answer
Draw the bounding box for a selected game object, outside of editor? 3 Answers
Not a question - just some performance metrics for rendering from Unity scripts 0 Answers