Screen Space Texture Shader: Part 2

Yesterday we started working on our Screen Space Texture Shader. We got the texture part working, but we also want an outline.

To get the outline rendered we will be adding vertex and fragment functions to our shader, and this needs to render underneath, or before, the surface function we did yesterday! What we are essentially going to do is scale our model up by the amount needed to get our outline width, and render that without any lighting applied, so it is all in the color we set our outline to be.

First we are going to take advantage of some of the functions Unity can provide us with, so we will include UnityCG.cginc in our shader. We add this just beneath our properties like this:

Then we need to add a new tag to our subshader that queues the rendering of our pass after other geometry, this is done with the “Queue” tag.  By setting queue Transparent-10 our shader is queued before Transparent shaders but after geometry.

Our vertex and fragment functions needs to be in it’s own pass, unlike the surface function. So we add a pass{ } and then we need to set up some parameters for this pass.

First you can set the Pass name. I name it “OUTLINE”, but this is not required. The pass has it’s own tags. The “LightMode” tag says if this should be rendered in Forward(Base or Add), Deffered or like we do Always. This pass isn’t affected by lighting so that’s ok in either.

Then we have Cull, which handles what parts of our mesh we want rendered, For my outline I’m going to not render the Front parts of the mesh with Cull Front. You could do Cull Off, then every part of the mesh is rendered, but it has some trade offs, but you can get the benefit of getting outline around intersecting meshes like in the example below.

Cull test

You can try out which you prefer for your self. If you Cull Back you will not render the rear facing triangles of your mesh this will not work for our outline. So steer away from that.

ZWrite Off means that we don’t want this pass to render to our DepthBuffer, which we wouldn’t want our outline to do. ZTest less means that this will render over objects furter away. ZTest Greater means that this will render over things in front of it. Then finally you have the pass blendmode: Blend SrcAlpha OneMinusSrcAlpha. This is actually the standard blendmode so you don’t need it. But you can experiment with different Blend paramterers and also you can do BlendOp Sub, or RevSub to get diffent effects. All about blending..

The we add the CGPROGRAM for this pass, we start with the compilation directives

#pragma vertex vert – our vertex function is called vert
#pragma fragment fra – fragment shader frag

Our vertex function needs something to process – the appdata structure, contains vertex positions and normals, the vertex fuction returns the v2f structure which is passed to our fragment function for final processing. v2f contains the POSITION in camera clip space and the color.

And we need our _Outline (width) and _OutlineColor.

Then we have our vertex function:

Return v2f , input is appdata v.
Declare “o”. “o.pos” is the vertex (position) multiplied with MVP (Model, View, Projection) Matrix. This brings our position from world position to clip space which our fragment function wants to work with.

Then we bring our normals from world space to eye space by multiplying with the UNITY_MATRIX_IT_MV. Then we normalize the resulting normal.

Our offset should be the projection space normal.x and y, this makes vertices that has normals that has high angle (90 deg max) with the “sight vector”/”eye line” move the furthest and vertices with normals pointing into or out of the screen won’t be offset.

Then we displace the position. Current position + offset multiplied with the z position ( to get wider outline for objects further away (consistency)) and our _Outline width.

Then we set our outline color in the o.color variable, and return “o”.

Next up is our fragment funtion which is the simplest kind of fragment function you can get. It’s self explanatory. Remember to end the CG program with ENDCG

That is actually the entire shader! We now have a complete shader!

ssts3

This  is the resulting output I get from this shader. If you messed up or just want to download the source for the shader. Here you go!

That is it for now! Good luck, shading your worlds!

 

Leave a Reply

Your email address will not be published. Required fields are marked *