x


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

more ▼

asked Nov 25 '09 at 06:41 AM

Vesper gravatar image

Vesper
21 1 1 4

Can you create a minimal project exposing the problem and post it here?

Nov 25 '09 at 09:14 AM robert ♦

@robert - Attached. See above.

Nov 25 '09 at 12:22 PM Vesper
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Nov 25 '09 at 03:21 PM

Vester gravatar image

Vester
52 2

You're correct: DrawMeshNow 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.

Nov 26 '09 at 07:38 AM Aras ♦♦

Thanks, Aras. That makes sense. Is there a "correct" built-in shader in Unity that would work well with DrawMeshNow, or does one have to write a shader that ignores lighting?

Nov 26 '09 at 09:44 PM Vester
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5051
x1352
x491

asked: Nov 25 '09 at 06:41 AM

Seen: 2285 times

Last Updated: Nov 25 '09 at 12:21 PM