- Home /
Problems with Graphics.DrawMesh, where to call it and how to get material property blocks to work.
I've been trying to use Graphics.DrawMesh to improve the performance of drawing multiple instances of a mesh and have run into some issues.
Based on the documentation I first tried to call Graphics.DrawMesh in the OnRenderObject method which didn't work. Putting it in the Update method instead solved the problem but is that really the proper location of calls to Graphics.DrawMesh?
Another issue is that modifying shader parameters between Graphics.DrawMesh calls using material property blocks doesn't appear to work. It looks like the material property block of the first visible instance of the mesh will get used by all other instances of the mesh, even if each draw call uses a different material property block.
Can somebody provide an example of working code that renders two or more instances of a mesh and applies different material property blocks to each instance?
Here's a (simplified) C# version of my Update method:
Graphics.DrawMesh( mesh, matrix0, material, 0, null, 0, propertyBlock0, false, false ); Graphics.DrawMesh( mesh, matrix1, material, 0, null, 0, propertyBlock1, false, false );
I've run into the same issue. I want to draw hundreds of markers with the same texture but varying alpha. I was using $$anonymous$$aterialPropertyBlocks and Graphics.Draw$$anonymous$$esh(...). But I found as you did that it didn't work as documented. It seemed to use one variant of the material for all the markers.
Did you get any joy on this problem in the end? I had to resort to using OnPostRender on the camera. But you solve one problem and you end up with another as it then renders over the top of all transparent objects since it is rendered after everything else. It does respect Z-depth for non-transparent objects though.
Answer by Jaap Kreijkamp · Feb 02, 2010 at 12:22 AM
DrawMesh
doesn't execute the rendering immediately so it doesn't really matter where you put it (I use lateupdate for several reasons but in update is fine). As the renderering isn't executed at call time, modifying materials between calls doesn't work (it takes the material at time of rendering so probably the final state). It sounds like you need to use Graphics.DrawMeshNow
.
Note that it can matter where you put it, depending on what you are doing. I was drawing a mesh in Update, but it's position was sometimes 'laggy' when the camera was moving. I think this was because the movement was being done via HOTween, which I am guessing does its updates in 'Update'; as a result, behaviour varies for objects that were updated before the tweener and those that were updated after the tweener. $$anonymous$$oving my Draw$$anonymous$$esh to LateUpdate resolved the problem.
Answer by Aras · Feb 02, 2010 at 03:47 PM
Like Jaap said, Graphics.DrawMesh does not render the mesh immediately. So it does not really matter where you put it; it will be eventually drawn in that frame.
I just found out that indeed, using MaterialPropertyBlock works funny with per-pixel lit shaders (or perhaps all multipass shaders). If I use VertexLit shader (or any other single pass shader) on the objects, then it works correctly for me.
Answer by tony oakden 1 · Jun 23, 2010 at 04:55 AM
I'm trying to use the DrawMesh code in my game but objects which I try to render using it don't light properly. See my other thread on the topic: http://forum.unity3d.com/viewtopic.php?t=53159
Can anyone advise why this might be? To summarise objects which i try to render using hte DrawMesh code light overbright on faces which face the directional light in the scene which i am using for the sun. If I render the same object using the normal rendering code they light perfectly. I'm at a loss to understand why this might be and would like to fix it.
thank you in advance,
Tony Oakden
Your answer
Follow this Question
Related Questions
Why does my game require so much rendering power? 1 Answer
Graphics.DrawTexture vs GUI.DrawTexture vs Graphics.Blit 0 Answers
Problems with rendering Particles 0 Answers
Smoothing Directionally between three materials 1 Answer
How do I make a boat that doesn't let water enter?,How do I make a boat that water cant get in to 2 Answers