- Home /
Drawing a mesh with Graphics.DrawMeshNow
I'm getting a strange issue with Graphics.DrawMeshNow(). As I change the camera angle, it seems as if it switches to another material from elsewhere in my scene, instead of using the material I am setting the pass of. My code is below:
//Variables - initialisation code not shown Material mat; Mesh mesh; Vector3 pos; Quaternion rot; Vector3 scale;
private void OnRenderObject() { for (int pass = 0; pass < mat.passCount; pass++) { mat.SetPass(pass);
Graphics.DrawMeshNow(mesh, Matrix4x4.TRS(pos, rot, scale));
} }
What am I doing wrong (or not doing)?
Edit: A sample project that reproduces the issue can be downloaded here: Download
Can you create a $$anonymous$$imal project exposing the problem and post it here?
Answer by Vester · Nov 25, 2009 at 03:21 PM
After some further investigation, it seems that the problem is caused by using the Diffuse shader in the material that DrawMeshNow() uses to draw the mesh. If the shader is changed to VertexLit, the mesh renders with the correct material. I suspect that this is related to DrawMeshNow() not using pixel lighting, although I'd be happy to be corrected by someone who knows better.
I am the poster of the original question. Due to a mess up involving trying to change my anonymous account to a registered one, I can no longer login with the account that I created the question with, so I can't approve a solution or edit the question or comments.
You're correct: Draw$$anonymous$$eshNow does not integrate with lighting at all. So any shader that wants to use lighting will just use light setup of whatever was rendered before.
Even VertexLit shader should not give your correct results, but most of the time the artifacts are much smaller than from the per-pixel lit shaders.
Thanks, Aras. That makes sense. Is there a "correct" built-in shader in Unity that would work well with Draw$$anonymous$$eshNow, or does one have to write a shader that ignores lighting?