- Home /
Transparent Cutout Shader?
I've been trying to make a circular health bar. Now, there's a tutorial out there that uses the cutout shader to create one.
My problem is: The health bar is supposed to be semi transparent.
So I looked for addon shaders because as far as I can see, there's no solution to that built in to Unity - needless to say, I didn't find any.
I tried writing my own, using the script reference to manage, sadly this one only works on straight bars (also, only really badly since its my first...)
So, my question is really: Can this shader somehow be salvaged to work in a circular manner? Or, even better, does anyone know a Shader with a Transparent Base Texture that also allows a seperate texture for cutouts?
Shader "Transparent/Bar" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Fill ("Fill Percentage", Range(-1.0,1.0)) = 0.5
_BarSz ("Bar Size", Float) = 3.0
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
Cull Off
CGPROGRAM
#pragma surface surf Lambert alpha
sampler2D _MainTex;
fixed4 _Color;
float _Fill;
float _BarSz;
struct Input {
float2 uv_MainTex;
float3 worldPos;
};
void surf (Input IN, inout SurfaceOutput o) {
float p = _BarSz * _Fill;
clip(IN.worldPos.x + p);
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
Thanks in advance!
Answer by ProudOne · Jul 06, 2012 at 07:46 PM
For Everyone else looking for a solution; Here it is!
Shader "Transparent/Cutout/Transparent" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_CutTex ("Cutout (A)", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha
sampler2D _MainTex;
sampler2D _CutTex;
fixed4 _Color;
float _Cutoff;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
float ca = tex2D(_CutTex, IN.uv_MainTex).a;
o.Albedo = c.rgb;
if (ca > _Cutoff)
o.Alpha = c.a;
else
o.Alpha = 0;
}
ENDCG
}
Fallback "Transparent/VertexLit"
}
This is exactly what I was looking for! So much potential for cool effects you can achieve with this shader. Thanks for this!
Hey, thanks for the shader. I was wondering..In what context is this shader used? I guess a better way to put it is: How is this shader used in order to create the "circular healthbar" effect?
Awesome shader, the semi transparency works! Only i cant seem to get any colors using graphics.drawtexture(rect,tex,mat). Im very new to shaders/coding, please help :) Thanks
more than a year later, just what I needed. Thanks for this!
Your answer
Follow this Question
Related Questions
Creating a shader that overlaps a diffuse with a TexGen ObjectLinear cutout 0 Answers
Cutout & reflective shader 0 Answers
Unlit Transparent Cutout shader that can switch between two textures 0 Answers
What is the fastest shader? 2 Answers
Transparent Cutout Shader - max transparency distance? 1 Answer