0
Not a bug

Procedural mesh renders black

Lars Islander 8 years ago updated 8 years ago 3

Hi,


I have a script which procedurally generates a terrain mesh at run-time. I've used shader forge to create a shader that blends between two different defuse and normal textures based on the world-space height of the terrain.

When applying this shader to a primitive like a sphere, it works fine. However, when applying it to the run-time generated mesh, I get a totally black mesh.

I suspect that this is todo with the lighting, because if I set the ambient lighting to a high value, or of i set the shader to an unlit shader, I can see the texture is applied to the mesh as expected, however none of the lights in the scene are picked up.

I expect that I'm doing something dumb, but after an hour of googling and tweaking random settings in the shader forge editor, I cannot get the mesh to pick up the lights.

What info do you need to help me debug this?

Thanks,

Lars

Answer

Answer
Not a bug

You're likely just missing normals and/or tangents on the mesh

Here's the results I'm seeing, the sphere is a unity primitive object, and the terrain is the mesh generated by the script.

Setting the ambient lighting high shows that the shader is working as expected (as far as blending the different textures by height), but the lights in the scene do not get picked up by the terrain mesh.

The lights are picked up by the sphere as expected.

Answer
Not a bug

You're likely just missing normals and/or tangents on the mesh

Awesome, setting the mesh tangents fixes the problem!

I followed this thread here for the code to do this at run-time:

http://answers.unity3d.com/questions/7789/calculating-tangents-vector4.html