Outline Shader Problem with generated mesh
Hello,
I try to generate a wall structure from vertices. After putting the generated mesh into a meshfilter and meshrenderer, the wall is displayed correctly. However, when I try to apply an outline shader (i.e. this one: https://github.com/Shrimpey/Outlined-Diffuse-Shader-Fixed), it got some strange behaviour although it works with default meshes.
The mesh generation method works like this:
Vector3[] meshVertices = new Vector3[entity.Triangles.Count * 3];
int[] indices = new int[entity.Triangles.Count * 3];
int j = 0;
int k = 0;
for (int i = 0; i < entity.Triangles.Count; i++)
{
indices[j] = j;
meshVertices[j++] = entity.Triangles[i].A;
indices[j] = j;
meshVertices[j++] = entity.Triangles[i].C;
indices[j] = j;
meshVertices[j++] = entity.Triangles[i].B;
}
mesh.vertices = meshVertices;
mesh.triangles = indices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.RecalculateTangents();
The Triangle class contains just 3 Vector3 Objects for each edge of the triangle.
I cant figure out why the shader behaves like shown on the picture instead of just laying an outline around the object. So help would be appreciated.
Answer by callebo_FK · Jun 25, 2018 at 12:43 PM
I'm guessing your outline shader duplicates the mesh, pushes its vertices out and turns black. Your problem is that your vertices, or edges, are "hard". The vertices are not shared in the edges, so they go their seperate ways.
One way to solve this is by smoothing the meshes edges in your 3D program or inside unity, although I'm not sure how to do it in unity.
Same problem explained here: https://answers.unity.com/questions/625968/unitys-outline-shader-sharp-edges.html
Answer by Szyu · Jun 28, 2018 at 10:58 AM
Ok so the main problem stated on the other question seems to be, that multiple vertices on the same position have different normal vectors? Guess this will be a tough one, since the positions are coming from another system. I'll see what I can do to get the vertice structure of the base model, so that I can examine whether the normals are correct or not.
If there would be a simpler way to do this using unity, I'd appreciate a suggestion on how to do it.
But thanks in Advance @callebo_FK for this hint.
One idea is to ins$$anonymous$$d look for another shader that does outlines, since this is not the only way to do them. Not saying you should learn how to program your own shaders but I'm pretty sure there are out-of-the-box ready ones available with some googling.
I tried many different shaders... All with the same result... But something that came to my $$anonymous$$d: If the normals would be wrong, after letting unity recalculate them, should the objects be distorted ins$$anonymous$$d of displayed correctly?
I tried many different shaders... All with the same result... But something that came to my $$anonymous$$d: If the normals would be wrong, after letting unity recalculate them, should the objects be distorted ins$$anonymous$$d of displayed correctly?
EDIT: After mesh.RecalculateNormals(), I checked vertices and normals of the mesh and tried to visualize them using shapes in the editor (with a little offset). The result looks like this: Forum Img Upload doesnt seem to work, so I uploaded an image
So there are multiple vertices at the same position with different normals. but how could I avoid this without the model being distorted?
I'm sorry I'm not sure I understand what's going on :D I haven't worked with meshes in scripts in Unity ever, so I'm not sure what RecalculateNormals() does. I'm also not sure what you are talking about the model being distorted? You mean like what your outline shader is doing?
If you want to use that shader, the normals need to be soft. That's all I can say, how you accomplish that I do not know. The green/blue lines are the vertices normals. You want the one on the left, where in the middle the vertices share the normals. That way, your mesh won't go separate ways when distorted/inflated/displaced (which your outline shader does).
Your answer
Follow this Question
Related Questions
Glowing outline with occlusion 0 Answers
Trying outline with Shader Graph 2020 causes weird glitchy effect 0 Answers
How to add a MeshCollider to a plane that has been modified with a shader displacement map 1 Answer
why outline is drawed only on front side of skinned mesh render 1 Answer
How to correct texture mapping on arbitrary quadrilaterals ? 2 Answers