Skip to content

Instantly share code, notes, and snippets.

@ccaner37
Created May 10, 2025 12:27
Show Gist options
  • Select an option

  • Save ccaner37/4bd2a76ddb94714968fec5b7d8432b8a to your computer and use it in GitHub Desktop.

Select an option

Save ccaner37/4bd2a76ddb94714968fec5b7d8432b8a to your computer and use it in GitHub Desktop.
2D Flag Wave Shader
Shader "Custom/UIManualWaveControl"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_WaveProgress ("Wave Progress (0-1)", Float) = 0.0
_WaveAmplitude ("Wave Amplitude", Float) = 5.0
_WaveFrequency ("Wave Frequency", Float) = 2.0
_BendAmount ("Bend Amount", Float) = 0.2
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Overlay" }
LOD 100
Pass
{
Cull Off ZWrite Off Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float _WaveProgress; // Custom wave control parameter (range: 0-1)
float _WaveAmplitude; // Amplitude of the wave
float _WaveFrequency; // Frequency of the wave
float _BendAmount; // Amount of horizontal bend
v2f vert (appdata_t v)
{
v2f o;
// Get original vertex position
float3 pos = v.vertex;
// Calculate the wave effect using the custom _WaveProgress parameter
float wave = sin(pos.x * _WaveFrequency + _WaveProgress * 3.14159) * _WaveAmplitude;
// Calculate the horizontal bend using a sine curve for smooth S-shaped flag waving
float curve = _BendAmount * sin(pos.y * 3.14159); // Create smooth horizontal curve using sine
// Modify X position to create smooth curvature along the Y-axis
pos.x += curve * pos.y;
// Apply the waving effect on Y-axis based on custom progress
pos.y += wave;
// Convert to Clip Space
o.vertex = UnityObjectToClipPos(float4(pos, 1.0));
// Pass UV coordinates to fragment shader
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// Sample the main texture
return tex2D(_MainTex, i.uv);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment