Search code examples
unity-game-engineshaderpixel-shader

Up-Down, Left-Right facing ratio shader


A little basic question i can't get my head around. I need to create a shader that works like a facing ratio shader however also containing up-down and left-right (in screen space).

Just like this:
just like this

I want it to work without having the original geometry, only having the normals, point position and the camera position. How should i approach this?


Solution

  • Here's your shader:

    #pragma vertex vert
    #pragma fragment frag
    #pragma fragmentoption ARB_precision_hint_fastest
    #include "UnityCG.cginc"
    
    struct app2vert
    {
        float4 position: POSITION;
        float3 normal: NORMAL;
    };
    
    struct vert2frag
    {
        float4 position: POSITION;
        float3 normal: TEXCOORD0;
    };
    
    vert2frag vert(app2vert input)
    {
        vert2frag output;
        output.position = mul(UNITY_MATRIX_MVP, input.position);
        output.normal = mul((float3x3)UNITY_MATRIX_IT_MV, input.normal);
        return output;
    }
    
    float4 frag(vert2frag input) : COLOR
    {
        float3 normal = normalize(input.normal);
        float4 output = fixed4(normal.x, normal.y, normal.z, 1.0f);
    
        return output;
    }