Unlit Scope Shader

Boddie

New member
I noticed the Overlay shader used for scopes, like what is on the assault rifle, use a surface shader. This adds shadows to the scope texture. When shooting from the dark towards a light area it was ideal in my game to be able to see the lighted area through the scope as you normally would. Shadows of the projected area are handled by the render texture. The shader code below is a lightly modded version of the prebuilt Unity "Unlit" shader that adds the overlay logic from the overlay shader.

C-like:
Shader "Unlit/UnlitScopeShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _OverlayTex("Overlay", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog
          
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            sampler2D _OverlayTex;
            float4 _MainTex_ST;
          
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }
          
            fixed4 frag (v2f i) : SV_Target
            {
                // sample both textures
                fixed4 mainTex = tex2D(_MainTex, i.uv);
                fixed4 overlayTex = tex2D(_OverlayTex, i.uv);
                // uses overlay alpha to apply the overlay to the main texture
                fixed4 color = (mainTex.rgba * (1 - overlayTex.a)) + (overlayTex.rgba * overlayTex.a);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, color);
                return color;
            }
            ENDCG
        }
    }
}

Enjoy!
 
Top