Created
August 20, 2015 13:18
-
-
Save MartinKnopf/4a17b63d53dd3b2e5c59 to your computer and use it in GitHub Desktop.
Unity script and shader to apply partial transparency to a sprite (based on Unitys standard sprite shader)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using Random = UnityEngine.Random; | |
public class PartiallyTransparentSprite : MonoBehaviour { | |
public SpriteRenderer renderer; | |
Material material; | |
List<Color> holes= new List<Color>(); | |
void Start() { | |
material = renderer.material; | |
} | |
void Update() { | |
int i = 0; | |
holes.ForEach((hole) => material.SetColor("_hole" + i++, hole)); | |
} | |
public void Shot() { | |
float u = Random.Range(.1f, .9f); | |
float v = Random.Range(.1f, .9f); | |
float radius = Random.Range(.1f, .2f); | |
Color hole = new Color(u, v, radius, 0f); | |
holes.Add(hole); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/PartiallyTransparentSprite" | |
{ | |
Properties | |
{ | |
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} | |
_Color ("Tint", Color) = (1,1,1,1) | |
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 | |
_Damaged ("Damaged", Float) = 0 | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"Queue"="Transparent" | |
"IgnoreProjector"="True" | |
"RenderType"="Transparent" | |
"PreviewType"="Plane" | |
"CanUseSpriteAtlas"="True" | |
} | |
Cull Off | |
Lighting Off | |
ZWrite Off | |
Fog { Mode Off } | |
Blend One OneMinusSrcAlpha | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma multi_compile DUMMY PIXELSNAP_ON | |
#include "UnityCG.cginc" | |
fixed4 _Color; | |
sampler2D _MainTex; | |
float PixelSnap; | |
float _Damaged; | |
struct appdata_t { | |
float4 vertex : POSITION; | |
float4 color : COLOR; | |
float2 texcoord : TEXCOORD0; | |
}; | |
struct v2f { | |
float4 vertex : SV_POSITION; | |
fixed4 color : COLOR; | |
half2 texcoord : TEXCOORD0; | |
}; | |
v2f vert(appdata_t IN) { | |
v2f OUT; | |
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex); | |
OUT.texcoord = IN.texcoord; | |
OUT.color = IN.color * _Color; | |
#ifdef PIXELSNAP_ON | |
OUT.vertex = UnityPixelSnap (OUT.vertex); | |
#endif | |
return OUT; | |
} | |
// shots | |
uniform float4 _hole1; | |
uniform float4 _hole2; | |
uniform float4 _hole3; | |
// checks, if x and y are inside the given hole | |
float isInsideHole(float x, float y, float4 hole) { | |
float radius = hole.z; | |
if(pow(x - hole.x, 2) + pow(y - hole.y, 2) < pow(radius, 2)) | |
return 1.0; | |
return 0.0; | |
} | |
fixed4 frag(v2f IN, v2f_img i) : SV_Target { | |
fixed4 c; | |
if(_Damaged) c = float4(1.0,1.0,1.0,1.0); | |
else c = tex2D(_MainTex, IN.texcoord) * IN.color; | |
float4 source = tex2D(_MainTex, i.uv); | |
float x = i.uv.x; | |
float y = i.uv.y; | |
// check if pixel is inside a hole and render it transparent if so | |
if(isInsideHole(x, y, _hole1)) { c.a = 0.0; c.rgb *= c.a; return c;} | |
if(isInsideHole(x, y, _hole2)) { c.a = 0.0; c.rgb *= c.a; return c;} | |
if(isInsideHole(x, y, _hole3)) { c.a = 0.0; c.rgb *= c.a; return c;} | |
c.rgb *= c.a; | |
return c; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment