Need help with projector shader

Hey guys,

I’m pretty clueless when it comes to shader coding and I’m wondering if someone could help me fix what I currently have. I want to build a shader that I’ll use with my projector and it’ll just project the image exactly as it is in terms of colors and opacity, nothing more, nothing less. I’ve been searching all over the internet for some examples to see if I could find one that I could just tweak but haven’t been all that lucky. I did however find one that’s pretty close. Btw, these will be used on a mobile game so I’m trying to make them as simple as possible without any extra features to save performance. I’m trying to project a circle that indicates a “hero unit” and a shadow in the middle of it. Bellow is what it currently looks like:

90528-ss.jpg

The image in the corner is what I’d like to project. Here’s my current code:

Shader "Projector/AdditiveTint" {
	Properties {
		_Attenuation ("Falloff", Range(0.0, 1.0)) = 1.0
		_ShadowTex ("Cookie", 2D) = "gray" {}
	}
	Subshader {
		Tags {"Queue"="Transparent"}
		Pass {
			ZWrite Off
			ColorMask RGB
			Blend SrcAlpha One // Additive blending
			Offset -1, -1

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			
			struct v2f {
				float4 uvShadow : TEXCOORD0;
				float4 pos : SV_POSITION;
			};
			
			float4x4 unity_Projector;
			float4x4 unity_ProjectorClip;
			
			v2f vert (float4 vertex : POSITION)
			{
				v2f o;
				o.pos = mul (UNITY_MATRIX_MVP, vertex);
				o.uvShadow = mul (unity_Projector, vertex);
				return o;
			}
			
			sampler2D _ShadowTex;
			fixed4 _Color;
			float _Attenuation;
			
			fixed4 frag (v2f i) : SV_Target
			{
				// Apply tint & alpha mask
				fixed4 texCookie = tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
				fixed4 outColor = texCookie.a;
				// Distance attenuation
				float depth = i.uvShadow.z; // [-1(near), 1(far)]
				return outColor * clamp(1.0 - abs(depth) + _Attenuation, 0.0, 1.0);
			}
			ENDCG
		}
	}
}

I’m guessing for someone who can code shaders this isn’t anything difficult but to me it’s all Chinese. If anyone could help me, I would really appreciate it. I’d also like to remove that extra functionality at the end with the “Distance attenuation”. What it does is it makes it so based on distance the image fades out. Since I’m trying to make it as simple as possible that’s not really needed. Thanks in advance!

I think your main change is to have changed the Blending mode (Blend OneMinusSrcAlpha SrcAlpha), then you removed the distance attenuation and tint to keep the texture with the saem color as it comes.


The image you posted was white because you were using additive blending.

I ended up finding one which after some “random” edits to see what happens turned out to do what I was looking for. Unfortunately I’m not entirely sure what the code does or how I could simplify it even more, but it seems to do the job for now. If anyone knows how this code could be simplified even more, that would be awesome!

// Upgrade NOTE: replaced '_Projector' with 'unity_Projector'

Shader "Projector/TatooProjector"
{
    Properties
    {
        _ShadowTex ("Cookie", 2D) = "white" { TexGen ObjectLinear }
    }
   
    Subshader
    {
        Tags { "RenderType"="Transparent"  "Queue"="Transparent+100"}
        Pass
        {
            ZWrite Off
            Offset -1, -1
   
            Fog { Mode Off }
   
            ColorMask RGB
            Blend OneMinusSrcAlpha SrcAlpha


            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_fog_exp2
            #pragma fragmentoption ARB_precision_hint_fastest
            #include "UnityCG.cginc"
           
            struct v2f
            {
                float4 pos      : SV_POSITION;
                float4 uv       : TEXCOORD0;
            };
           
                sampler2D _ShadowTex;
                float4x4 unity_Projector;
                float4 _Color;
           
            v2f vert(appdata_tan v)
            {
                v2f o;
                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                o.uv = mul (unity_Projector, v.vertex);
                return o;
            }
           
            half4 frag (v2f i) : COLOR
            {
                half4 tex = tex2Dproj(_ShadowTex, i.uv);
                tex.a = 1-tex.a;
                if (i.uv.w < 0)
                {
                    tex = float4(0,0,0,1);
                }
                return tex;
            }
            ENDCG
       
        }
    }
}

@stefanplc I can’t thank you enough for your posted shader as I have been searching the internet and asking around and noone was able to help on that so awesome work!

Here’s an additive colored projector shader that respects it’s nearClipPlane and farClipPlane (at least in orthographic mode)

Shader "Custom/ProjectorAdditiveTint" {
	Properties{
		_Color("Tint Color", Color) = (1,1,1,1)
		_ShadowTex("Cookie", 2D) = "gray" {}
	}
	Subshader{
		Tags {"Queue" = "Transparent"}
		Pass {
			ZWrite Off
			Offset -1, -1
			ColorMask RGB
			Blend SrcAlpha One // Additive blending

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			struct v2f {
				float4 uvShadow : TEXCOORD0;
				float4 pos : SV_POSITION;
			};

			float4x4 unity_Projector;
			float4x4 unity_ProjectorClip;

			v2f vert(float4 vertex : POSITION)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(vertex);
				o.uvShadow = mul(unity_Projector, vertex);
				return o;
			}

			sampler2D _ShadowTex;
			fixed4 _Color;

			fixed4 frag(v2f i) : SV_Target
			{
				fixed4 texCookie = tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
				clip(1 + i.uvShadow.z);
				clip(1 - i.uvShadow.z);
				return _Color * texCookie.a;
			}
			ENDCG
		}
	}
}