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:

CG include CGINCLUDE #include "UnityCG.cginc" ENDCG 1 2 3 CGINCLUDE #include "UnityCG.cginc" ENDCG

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.

Shader tags Tags{"RenderType" = "Opaque" "Queue" = "Transparent-10" } 1 Tags { "RenderType" = "Opaque" "Queue" = "Transparent-10" }

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.

Pass Parameters Pass{ Name "OUTLINE" Tags{ "LightMode" = "Always" } Cull Front ZWrite Off ZTest Less Blend SrcAlpha OneMinusSrcAlpha 1 2 3 4 5 6 7 Pass { Name "OUTLINE" Tags { "LightMode" = "Always" } Cull Front ZWrite Off ZTest Less Blend SrcAlpha OneMinusSrcAlpha

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.

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

The CGPROGRAM CGPROGRAM #pragma vertex vert #pragma fragment frag struct appdata { float4 vertex : POSITION; float3 normal : NORMAL; }; struct v2f { float4 pos : POSITION; float4 color : COLOR; }; float _Outline; float4 _OutlineColor; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 CGPROGRAM #pragma vertex vert #pragma fragment frag struct appdata { float4 vertex : POSITION ; float3 normal : NORMAL ; } ; struct v2f { float4 pos : POSITION ; float4 color : COLOR ; } ; float _Outline ; float4 _OutlineColor ;

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:

Vertex function v2f vert(appdata v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal); norm = normalize(norm); float2 offset = TransformViewToProjection(norm.xy); o.pos.xy += offset * o.pos.z * _Outline; o.color = _OutlineColor; return o; } 1 2 3 4 5 6 7 8 9 10 11 12 v2f vert ( appdata v ) { v2f o ; o . pos = mul ( UNITY_MATRIX_MVP , v . vertex ) ; float3 norm = mul ( ( float3x3 ) UNITY_MATRIX_IT_MV , v . normal ) ; norm = normalize ( norm ) ; float2 offset = TransformViewToProjection ( norm . xy ) ; o . pos . xy += offset * o . pos . z * _Outline ; o . color = _OutlineColor ; return o ; }

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

Fragment function half4 frag(v2f i) : COLOR{ return i.color; } ENDCG 1 2 3 4 half4 frag ( v2f i ) : COLOR { return i . color ; } ENDCG

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

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!