Skip to content

Instantly share code, notes, and snippets.

@garzaa
Created January 15, 2021 06:33
Show Gist options
  • Save garzaa/718990d4cb6a835c7914da771676e9cb to your computer and use it in GitHub Desktop.
Save garzaa/718990d4cb6a835c7914da771676e9cb to your computer and use it in GitHub Desktop.
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/WindLeaves"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
[Header(Distortion)]
_NormalMap ("Normal Map", 2D) = "bump" {}
_Strength ("Distortion Strength", Range(0, 2)) = 1
_Scale ("Scale", Vector) = (1, 1, 0, 0)
_Velocity ("Movement Velocity", Vector) = (1, 1, 0, 0)
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
OUT.worldPos = mul(unity_ObjectToWorld, IN.vertex);
return OUT;
}
sampler2D _MainTex;
sampler2D _AlphaTex;
float _AlphaSplitEnabled;
fixed4 SampleSpriteTexture (float2 uv)
{
fixed4 color = tex2D (_MainTex, uv);
#if UNITY_TEXTURE_ALPHASPLIT_ALLOWED
if (_AlphaSplitEnabled)
color.a = tex2D (_AlphaTex, uv).r;
#endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED
return color;
}
sampler2D _NormalMap;
float _Strength;
float4 _Scale;
float4 _Velocity;
float4 _MainTex_TexelSize;
fixed4 frag(v2f IN) : SV_Target
{
float green_amount = SampleSpriteTexture(IN.texcoord).g;
float2 distortion_coordinates = IN.worldPos;
distortion_coordinates /= _Scale.xy;
distortion_coordinates += _Time.w * _Velocity.xy;
half2 distortion_direction = UnpackNormal(tex2D(_NormalMap, distortion_coordinates)).rg;
half2 offset = distortion_direction * _Strength * _MainTex_TexelSize.xy * green_amount;
IN.texcoord += offset;
fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
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