This tutorial covers how to add support for flat shading and showing the wireframe of a mesh. It uses advanced rendering techniques and assumes you're familiar with the material covered in the Rendering series.

This tutorial is made with Unity 2017.1.0.

Flat Shading

Meshes consist of triangles, which are flat by definition. We use surface normal vectors to add the illusion of curvature. This makes it possible to create meshes that represent seemingly smooth surfaces. However, sometimes you actually want to display flat triangles, either for style or to better see the mesh's topology.

To make the triangles appear as flat as they really are, we have to use the surface normals of the actual triangles. It will give meshes a faceted appearance, known as flat shading. This can be done by making the normal vectors of a triangle's three vertices equal to the triangle's normal vector. This makes it impossible to share vertices between triangles, because then they would share normals as well. So we end up with more mesh data. It would be convenient if we could keep sharing vertices. Also, it would be nice if we could use a flat-shading material with any mesh, overriding its original normals, if any.

Besides flat shading, it can also be useful or stylish to show a mesh's wireframe. This makes the topology of the mesh even more obvious. Ideally, we can do both flat shading and wireframe rendering with a custom material, in a single pass, for any mesh. To create such a material, we need a new shader. We'll use the final shader from part 20 of the Rendering series as our base. Duplicate My First Lighting Shader and change its name to Flat Wireframe.

Shader "Custom/Flat Wireframe" { … }

Can't we already see the wireframe in the editor? We can indeed see the wireframe in the scene view, but not in the game view, and not in builds. So if you want to see the wireframe outside the scene view, you have to use a custom solution. Also, the scene view only displays the wireframe of the original mesh, regardless whether the shader renders something else. So it doesn't work with vertex displacement of tessellation.

Derivative Instructions Because triangles are flat, their surface normal is the same at every point on their surface. Hence, each fragment rendered for a triangle should use the same normal vector. But we current do not know what this vector is. In the vertex program, we only have access to the vertex data stored in the mesh, processed in isolation. The normal vector stored here is of no use to us, unless it's designed to represent the triangle's normal. And in the fragment program, we only have access to the interpolated vertex normals. To determine the surface normal, we need to know the orientation of the triangle in world space. This can be determined via the positions of the triangle's vertices. Assuming that the triangle is not degenerate, its normal vector is equal to the normalized cross product of two of the triangle's edges. If it is degenerate, then it won't be rendered anyway. So gives a triangle's vertices `a`, `b`, and `c` in counter-clockwise order, its normal vector is `n=(c-a)xx(b-a)`. Normalizing that gives us the final unit normal vector, `hatn=n/|n|`. Deriving a triangle's normal. We don't actually need to use the triangle's vertices. Any three points that lie in the triangle's plane will do, as long as those points form a triangle too. Specifically, we only need two vectors that lie in the triangle's plane, as long as they're not parallel and are larger than zero. One possibility is to use points corresponding to the world positions of rendered fragments. For example, the world position of the fragment we're currently rendering, the position of the fragment to the right of it, and the position of the fragment above it, in screen space. Using world positions of fragments. If we could access the world positions of adjacent fragments, then this could work. There is no way to directly access the data of adjacent fragments, but we can access the screen-space derivatives of this data. This is done via special instructions, which tell us the rate of change between fragments, for any piece of data, in either the screen-space X or Y dimension. For example, our current fragment's world position is `p_0`. The position of the next fragment in the screen-space X dimension is `p_x`. The rate of change of the world position in the X dimension between these two fragments is thus `(delp)/(delx)=p_x-p_0`. This is the partial derivative of the world position, in the screen-space X dimension. We can retrieve this data in the fragment program via the ddx function, by supplying it with the world position. Let's do this at the start of the InitializeFragmentNormal function in My Lighting.cginc. void InitializeFragmentNormal(inout Interpolators i) { float3 dpdx = ddx(i.worldPos); … } We can do the same for the screen-space Y dimension, finding `(delp)/(dely)=p_y-p_0` by invoking the ddy function with the world position. float3 dpdx = ddx(i.worldPos); float3 dpdy = ddy(i.worldPos); Because these values represent the differences between the fragment world positions, they define two edges of a triangle. We don't actually know the exact shape of that triangle, but it's guaranteed to lie in the original triangle's plane, and that's all that matters. So the final normal vector is the normalized cross product of those vectors. Override the original normal with this vector. float3 dpdx = ddx(i.worldPos); float3 dpdy = ddy(i.worldPos); i.normal = normalize(cross(dpdy, dpdx)); How do ddx and ddy work? The GPU needs to know the screen-space derivatives of texture coordinates to determine which mipmap level to use, when sampling textures. It figures this out by comparing the coordinates of adjacent fragments. The screen-space derivative instructions are an extension of that, making this functionality available for all fragment programs, for any data they use. To be able to compare fragments, the GPU processes them in blocks of 2×2. Per block, it determines two derivatives in the X dimension, for the two 2×1 fragment pairs, and two derivatives in the Y dimension, for the two 1×2 fragment pairs. The two fragments of a pair use the same derivative data. This means that the derivatives only change per block, once every two pixels, instead of every pixel. As a result, these derivatives are an approximation and will appear blocky when used for data that changes nonlinearly per fragment. Because triangles are flat, this approximation doesn't affect our derived normal vector. Blocks of derivative pairs. The GPU always processes fragments in 2×2 blocks, so along the triangle's edge fragments will get processed that end up outside the triangle. These invalid fragments are discarded, but still need to be processed to determine the derivatives. Outside the triangle, the fragment's interpolation data is extrapolated beyond the range defined by the vertices. Create a new material that uses our Flat Wireframe shader. Any mesh that uses this material should be rendered using flat shading. They will appear faceted, though this might be hard to see when you're also using normal maps. I use a standard capsule mesh in the screenshots for this tutorial, with a gray material. Smooth and flat shading. From a distance, it might look like the capsule's made out of quads, but those quads are made of two triangles each. Quads made of triangles. While this works, we've actually changed the behavior of all shaders that rely on the My Lighting include file. So remove the code that we just added. // float3 dpdx = ddx(i.worldPos); // float3 dpdy = ddy(i.worldPos); // i.normal = normalize(cross(dpdy, dpdx));

Geometry Shaders There is another way that we can determine the triangle's normal. Instead of using derivative instructions, we could use the actual triangle vertices to compute the normal vector. This requires use to do work per triangle, not per individual vertex or fragment. That's where geometry shaders come in. The geometry shader stage sits in between the vertex and the fragment stage. It is fed the output of the vertex program, grouped per primitive. A geometry program can modify this data, before it gets interpolated and used to render fragments. Processing vertices per triangle. The added value of the geometry shader is that the vertices are fed to it per primitive, so three for each triangle in our case. Whether mesh triangles share vertices doesn't matter, because the geometry program outputs new vertex data. This allows us to derive the triangle's normal vector and use it as the normal for all three vertices. Let's put the code for our geometry shader in its own include file, MyFlatWireframe.cginc. Have this file include My Lighting.cginc and define a MyGeometryProgram function. Start with an empty void function. #if !defined(FLAT_WIREFRAME_INCLUDED) #define FLAT_WIREFRAME_INCLUDED #include "My Lighting.cginc" void MyGeometryProgram () {} #endif Geometry shaders are only supported when targeting shader model 4.0 or higher. Unity will automatically increase the target to this level if it was defined lower, but let's be explicit about it. To actually use a geometry shader, we have to add the #pragma geometry directive, just like for the vertex and fragment functions. Finally, MyFlatWireframe has to be included instead of My Lighting. Apply these changes to the base, additive, and deferred passes of our Flat Wireframe shader. #pragma target 4.0 … #pragma vertex MyVertexProgram #pragma fragment MyFragmentProgram #pragma geometry MyGeometryProgram … // #include "My Lighting.cginc" #include "MyFlatWireframe.cginc" This will result in shader compiler errors, because we haven't defined our geometry function correctly yet. We have to declare how many vertices it will output. This number can vary, so we must provide a maximum. Because we're working with triangles, we'll always output three vertices per invocation. This is specified by adding the maxvertexcount attribute to our function, with 3 as an argument. [maxvertexcount(3)] void GeometryProgram () {} The next step is to define the input. As we're working with the output of the vertex program before interpolation, the data type is InterpolatorsVertex . So the type name isn't technically correct in this case, but we didn't took the geometry shader into consideration when we named it. [maxvertexcount(3)] void MyGeometryProgram ( InterpolatorsVertex i ) {} We also have to declare which type of primitive we're working on, which is triangle in our case. This has to be specified before the input type. Also, as triangles have three vertices each, we're working on an array of three structures. We have to define this explicitly. [maxvertexcount(3)] void MyGeometryProgram ( triangle InterpolatorsVertex i [3] ) {} Because the amount of vertices that a geometry shader can output varies, we don't have a singular return type. Instead, the geometry shader writes to a stream of primitives. In our case, it's a TriangleStream , which has to be specified as an inout parameter. [maxvertexcount(3)] void MyGeometryProgram ( triangle InterpolatorsVertex i[3] , inout TriangleStream stream ) {} TriangleStream works like a generic type in C#. It needs to know the type of the vertex data that we're going to give it, which is still InterpolatorsVertex . [maxvertexcount(3)] void MyGeometryProgram ( triangle InterpolatorsVertex i[3], inout TriangleStream <InterpolatorsVertex> stream ) {} Now that the function signature is correct, we have to put the vertex data into the stream. This is done by invoking the stream's Append function once per vertex, in the order that we received them. [maxvertexcount(3)] void MyGeometryProgram ( triangle InterpolatorsVertex i[3], inout TriangleStream<InterpolatorsVertex> stream ) { stream.Append(i[0]); stream.Append(i[1]); stream.Append(i[2]); } At this point our shader works again. We've added a custom geometry stage, which simply passes through the output from the vertex program, unmodified. Why does the geometry program look so different? Unity's shader syntax is a mix of CG and HLSL code. Mostly it looks like CG, but in this case it resembles HLSL.