Im making a VR game and i needed a good way of showing the player health, so i decided to show it as a “tattoo” that can change like a ´health bar during runtime. After some digging it seemed like the best way of doing this was using a custom projector shader. Since i have no experience im amazed i managed to get it working, BUT i got issues with projection on the other side of the model and saw that the standard projectors solve this using a falloff texture. The issue is that i cant get it to work together with the falloff code. Either i get “show through” or the “bar adjustment” dont work.
Here is the code, first texture is a mask to project whatever shape you want, and the alpha will control the “bar adjust” thing. The commended lines are the falloff thing i got from the standard projector and that i cant seem to get to work together with the rest.
Shader "Projector/Healthbar" {
Properties {
_MaskTex ("Cookie", 2D) = "white" {}
_FalloffTex ("FallOff", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
}
Subshader {
Tags {"Queue"="Transparent"}
Pass {
ZWrite Off
ColorMask RGB
Blend DstColor Zero
Offset -1, -1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct v2f {
float4 uvMask : TEXCOORD0;
float4 uvFalloff : TEXCOORD1;
UNITY_FOG_COORDS(2)
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.uvMask = mul (unity_Projector, vertex);
o.uvFalloff = mul (unity_ProjectorClip, vertex);
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
sampler2D _MaskTex;
sampler2D _FalloffTex;
float4 _Color;
fixed4 frag (v2f i) : SV_Target
{
fixed4 texS = tex2Dproj (_MaskTex, UNITY_PROJ_COORD(i.uvMask));
texS.a = 1.0 - texS.a;
//fixed4 texF = tex2Dproj (_FalloffTex, UNITY_PROJ_COORD(i.uvFalloff));
//fixed4 res = lerp(fixed4(1,1,1,0), texS, texF.a);
fixed4 res = 1 - texS;
//res = 1 - res;
if (res.r == 1)
res = floor((1 - i.uvMask.y) + _Color.a * res);
float4 tmp = 1 - _Color;
tmp.a = 1;
UNITY_APPLY_FOG_COLOR(i.fogCoord, res, fixed4(1,1,1,1));
return 1 - (res * tmp);
}
ENDCG
}
}
}