Calculate interpolated vertex normals in vertex shader

I would like to calculate the interpolated Vertex Normals so I could use that data in a Vertex Shader for an outline effect, but I don’t know how.

This is the material I have:

But this is how it looks like on a non-smooth surface mesh:

And this is on a smooth surface (the look I am after):

I don’t really have the option to create duplicates with smooth surfaces, and I therefore want to create a workaround where I calculate the interpolated vertex normals so I could use them in the vertex shader.

I appreciate any help I could get!

quick thought… you could use smooth shading and bake the flat normals into the vertex color. or if there are, use additional vertex attributes. i don’t think you can do this in unreal tho. external program required.

1 Like

I am trying to find a way to not have to do this :frowning:

google result for ddx and ddy math. https://www.youtube.com/watch?v=u0QpgHECXv0

i’m checking right now if this works for any given shape, not just cubes.

edit: seems to work. you gotta disable tangent space normals in the material to avoid the xtra transform required.

1 Like

That video was great, but I tested it and it works on cubes and basic-ish meshes, but it doesn’t work on anything in the form of a stick or for example a fire extinguisher.

works for me. i don’t have a fire extinguisher, but i imported a smooth shaded random stick. using the flat shading shader and has a continous outline.

you can check parameters and the shader is copied from the video.

1 Like

Thank you, but I have a flat shaded mesh that I need to have smooth shaded vertices.

Or a way to make the edges stick together so it doesnt look like this:

If you make the parameter Param have a higher value, the outline wont be continous

What about using post-process effects?

1 Like

I have tried using postprocess outlines with custom depth and it looks great. The problem with this is that it´s for a VR project. Adding a postprocess with an outline material adds +0.8 in average in the GPU stats. That´s why I am trying to find another way.

1 Like

you want it to look flat and you need the smooth shading for the vertex displacment. that’s exactly what the shader does. you export your meshes smooth shaded so the vertex normals are displacing correctly. the shader does the flat shading. it’s the other way around than you think you should do it. but that’s how that works.

you can’t compute smooth normals in a shader. you don’t have information about neighbouring vertices. that doesn’t work. do the flat shaded “hack”. it works.

look… 5 minute blender and copy pasta.

1 Like