I’m sorry if this has been asked before, but I’ve only been able to find similar threads thus far and nothing that gets me exactly what I need.
I need a shader that can both project onto a surface via a Unity projector and rotate UVs. The goal is a sort of radar ‘blip’ animation that gets projected onto my environment. Thus far I’ve had success either projecting the texture or rotating it, but not both (see comments in the code block below).
I’m very new to shader scripting and most of this is cobbled together from other shaders I’ve seen. I’m sure it could be optimized a bit.
Thanks in advance for any insights!
Shader "Custom/RadarBlip" {
Properties {
_Color ("Tint Color", Color) = (1,1,1,1)
_Attenuation ("Falloff", Range(0.0, 1.0)) = 1.0
_RotationTex ("Rotation Texture", 2D) = "gray" {}
_RotationSpeed("Rotation Speed", Float) = 0.0
}
Subshader {
Tags {"Queue"="Transparent"}
Pass {
ZWrite Off
ColorMask RGB
Blend SrcAlpha One
Offset -1, -1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _RotationTex;
float _RotationSpeed;
float4x4 unity_Projector;
fixed4 _Color;
float _Attenuation;
struct appdata {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float4 uvShadow : TEXCOORD0;
float2 uv : TEXCOORD1;
};
v2f vert (appdata v) {
float s = sin ( _RotationSpeed * _Time);
float c = cos ( _RotationSpeed * _Time);
float2x2 rotationMatrix = float2x2( c, -s, s, c);
float offsetX = .5;
float offsetY = .5;
float x = v.texcoord.x - offsetX;
float y = v.texcoord.y - offsetY;
v2f o;
o.uv = mul (float2(x, y), rotationMatrix ) + float2(offsetX, offsetY);
o.pos = UnityObjectToClipPos (v.vertex);
o.uvShadow = mul (unity_Projector, v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
// This returns a rotating texture, but it's not projected correcty -- it's giant and fills the entire surface of anything beneath it.
return tex2D(_RotationTex, i.uv);
// This returns a correct projection, but it doesn't rotate anymore.
// I need to merge these two commands.
fixed4 texCookie = tex2Dproj (_RotationTex, UNITY_PROJ_COORD(i.uvShadow));
fixed4 outColor = _Color * texCookie.a;
float depth = i.uvShadow.z;
return outColor * clamp(1.0 - abs(depth) + _Attenuation, 0.0, 1.0);
}
ENDCG
}
}
}