- Home /
Shaders to show damage?
I'm trying to take the pressure off of my artist a bit in designing the game. Buildings in the game take damage and I'm wondering if there is a simple method to take a sprite that would indicate damage and mask it over the buildings?
As an example, when a building is damaged, I would place a mask that has just drawings of cracks on it that would cover the sprite. This would allow me to reuse the same damage indicator for multiple buildings.
Thanks
Answer by Berenger · Jan 12, 2012 at 05:13 AM
As it happens, I just had to do the same thing. Here is what I did : In the shader below, I the is the main texture and three masks, for low / mid / high damage, controlled by a float I change in-game (material.SetFloat etc) but enough talk, here is the shader :
Shader "Custom/Breakables"
{
Properties
{
_Color ("Main Color", Color) = (1,0.5,0.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Crack1 ("Crack 1", 2D) = "white" {}
_Crack2 ("Crack 2", 2D) = "white" {}
_Crack3 ("Crack 3", 2D) = "white" {}
_CrackIndex ("Index", Float ) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
float4 _Color;
float _CrackIndex;
sampler2D _MainTex;
sampler2D _Crack1;
sampler2D _Crack2;
sampler2D _Crack3;
struct Input
{
float2 uv_MainTex;
float2 uv_Crack1;
float2 uv_Crack2;
float2 uv_Crack3;
};
void surf (Input IN, inout SurfaceOutput o)
{
half4 c = tex2D ( _MainTex, IN.uv_MainTex );
half4 cracks;
if( _CrackIndex <= 1f ) cracks = (1).rrrr;
else if( _CrackIndex <= 2f ) cracks = tex2D ( _Crack1, IN.uv_Crack1 );
else if( _CrackIndex <= 3f ) cracks = tex2D ( _Crack2, IN.uv_Crack2 );
else cracks = tex2D ( _Crack3, IN.uv_Crack3 );
o.Albedo = c.rgb * _Color * cracks.r;
//o.Albedo = cracks.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
I'm not really a pro in shaders from unity so it's the simplest possible, but I guess you works things out with that !
I can't seem to get this to work, is it outdated or something?