Shader not working on generated meshes

Hi Everyone,

I have a rather strange behavior in my Hololens project regarding shaders and meshes generated via scripts.

The problem is the following:
I have a script that generates a mesh and configures the MeshFilter and the MeshRenderer. The mesh seems perfect in the editor in play mode apart from the coloring. I only get a completely homogeneous color no matter what material and/or shader and color I use. Even if I additionally use a Texture nothing changes. In the attached image I have a triad prefab shaded correctly seen in the green rectangle. In the yellow rectangle you can see a roof like mesh shaded just plain gray with no ambient lighting. Strangely the gray color is not even the color I assign in the script. The blueish color only shows (also incorrectly shaded) if I use rim lighting on the mesh (seen in the red rectangle).
As to the normal vectors, I double checked them and even if I don’t use my own normals but let “RecalculateNormals” figure them out, I get the same results.

At this point I’m pretty desperate since I haven’t found any clues as to what might cause this. I guess I am missing something very important/basic but I’m really out of ideas. I hope someone here can point me in the right direction. Oh, and I’m using Unit 5.6.2f1.

Thanks for any help,
Kornel

Hi @krpit ,

The Unity mesh, generated from the HoloLens mesh data, only contains vertex position data. It does not contain normals or uvs. That is why you must call recalculate normals yourself if you are using a shader that requires normals. Likewise, you need to have a solution for computing uvs if you intend on texture mapping the spatial mapped mesh. There is a shading technique called tri-planar mapping which is often used when texture mapping terrains. A tri-planar shader ultimately allows you to texture map a mesh in a smooth and typically convincing manner without the need of uvs. The reason this is possible is because the uvs are calculated on the fly in the shader based on how the normal changes.

For more information on how to implement a tri-planar shader, take a look at the following websites.

I hope that helps!