How to render a flat shading without adding extra vertices?

Hi

I’m generating a mesh procedurally. In order to render a “flat shading” (i.e. hard edges) I create doubled vertices on each edge. Is it possible to do this in a shader instead and thereby reduce the vertex count?

Carl Emil

There is an old graphics card setting for Flat Shading. It sets every single face to be flat shaded, using the normal of (I think) the 1st vert in the try. Doubt Unity (or anyone) supports it anymore (and Optimize Mesh would break it anyway.)

5 Answers

5

No that isn’t possible (except perhaps with dx11 not sure). You need the face normals to be separate at each corner for vertex/fragment shaders and surface shaders in Unity.

Good to know.

@thexdd I am using Unity 5.3.0f1 right now and it still doesn't work. Ever since I've been using Unity (since the early days of 4.x), the colliders have not update on the spot. I used to do what @ChaseKeeling did, but I, too have deemed it as "bad coding practice." It's inefficient. I am hoping there is another, more practical way to do it.

This should be possible, and be faster than smooth shading.

I’ve made a feature request, up vote it!

flat-shading-using-face-normals

Surely it is possible if you (or ideally someone else) write a simple shader which uses, say, the normal of the first vertex of each triangle as the normal for the whole triangle.

Certainly possible with a geometry shader (DX11) but I don't see how you would know which points to use, your fragment shader gets interpolated points for each pixel. I might have missed something of course...

I may be wrong but I seem to remember that in the fragment shader you can access individually the vertices which form the polygon of which you are shading a fragment. One way or another it is surely possible since I've read (regarding opengl) that all fixed pipeline features can be achieved using shaders. This was long before DX11.

What makes you think you can do it with a fixed function shader? I see no examples of any such shader available and all of my Googling on the subject indicates that you cannot access the vertex information in the fragment e.g. http://stackoverflow.com/questions/9111672/how-to-obtain-the-vertices-due-to-which-a-fragment-shader-is-called-in-opengl-es

I've used that fixed function feature with opengl. glShadeModel(GL_FLAT). That does it. I believe it uses the normal of the last vertex in each polygon.

Neat, must check that out.

I’ve done 3D for 20 years, in all software there is a smoothing angle for shading and to smooth shade by default is extra CPU / GPU workload. Usually a smoothing angle is between 20 and 89 degrees (44 is good) so can this be built into shading? If smoothing worked at all then this must be the case, else how is it working?

Here is a tutorial on flat shading on Unity: Go Beyond Retro Pixel Art With Flat Shaded 3D in Unity

This adds extra vertices. Do not use this approach.