Skip to content

Unity Surface Shader Cartoon Renderer

September 6, 2015

In the newer versions of Unity, shaders use a Surface function, as opposed to a Vert/Frag combo to render everything.  But they also allow for a vertex function to be used if it’s needed.  The problem I was finding is, in the lower level vert/frag shader format the vertex function is in view space, whereas the vertex shader in a surface shader is in model space.  This makes it difficult to do things such as moving vertices along the XY plane while keeping the Z plane static.

It took me a little while, but I eventually managed to cobble together a function that achieves the cartoon look that I was looking for.  I still don’t quite understand why the normal has to be flipped, but it appears to work.  If anyone knows why I had to flip the normal, please let me know.

        void vert (inout appdata_full v) 
             float4 eyePosition =  mul (UNITY_MATRIX_MV, v.vertex);
            float4x4 it_mv = UNITY_MATRIX_IT_MV;
            float3 norm   = normalize(mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal));
            float2 offset = -norm.xy;
            eyePosition.xy += offset * eyePosition.z * _Outline;
            v.vertex = mul(eyePosition, it_mv);

From → C#, Programming, Shaders

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: