Skip to content

Instantly share code, notes, and snippets.

@TarasOsiris
Last active April 20, 2023 07:36
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save TarasOsiris/e0e6e6c3b8fdb0d8074b to your computer and use it in GitHub Desktop.
Save TarasOsiris/e0e6e6c3b8fdb0d8074b to your computer and use it in GitHub Desktop.
Flow Map Shader for Unity3D. Used with Sprites.
Shader "Custom/Flow Map"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
// Flow
_FlowMap ("Flow Map", 2D) = "white" {}
_FlowSpeed ("Flow Speed", float) = 0.05
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 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;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
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;
}
sampler2D _MainTex;
sampler2D _FlowMap;
float _FlowSpeed;
fixed4 frag(v2f IN) : SV_Target
{
float3 flowDir = tex2D(_FlowMap, IN.texcoord) * 2.0f - 1.0f;
flowDir *= _FlowSpeed;
float phase0 = frac(_Time[1] * 0.5f + 0.5f);
float phase1 = frac(_Time[1] * 0.5f + 1.0f);
half3 tex0 = tex2D(_MainTex, IN.texcoord + flowDir.xy * phase0);
half3 tex1 = tex2D(_MainTex, IN.texcoord + flowDir.xy * phase1);
float flowLerp = abs((0.5f - phase0) / 0.5f);
half3 finalColor = lerp(tex0, tex1, flowLerp);
fixed4 c = float4(finalColor, 1.0f) * IN.color;
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}
@xcplay
Copy link

xcplay commented Oct 28, 2021

It works, but not perfect, you can check my blog below
https://blog.csdn.net/weixinyi21cn/article/details/121017117
or you can read this paper: http://graphicsrunner.blogspot.com/2010/08/water-using-flow-maps.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment