Skip to content

Instantly share code, notes, and snippets.

@ChrisLowe-Takor
Created February 14, 2023 11:33
Show Gist options
  • Save ChrisLowe-Takor/8157be261bf0b908b73ee598d3b00c6f to your computer and use it in GitHub Desktop.
Save ChrisLowe-Takor/8157be261bf0b908b73ee598d3b00c6f to your computer and use it in GitHub Desktop.
An animated HLSL gradient shader for unity
Shader "AnimatedGradientShader"
{
Properties
{
_ColorA ("ColorA", Color ) = (0, 0, 0, 1)
_ColorB ("ColorB", Color ) = (1, 1, 1, 1)
_MainTex ("Texture", 2D) = "white" {}
_Frequency ("Frequency", Float) = 1
_Offset ("Offset", Float) = 0
_AnimationSpeed ("Animation Speed", Range(0, 255)) = 1
[ShowAsVector2] _DirectionVector("Direction", Vector) = (1, 0, 0, 0)
}
SubShader
{
Tags {
"RenderType"="Opaque"
"Queue"="Transparent"
}
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf StandardSpecular noshadow
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
#define PI 3.14159265
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
float4 _ColorA;
float4 _ColorB;
float _Frequency;
float _Offset;
float _AnimationSpeed;
float2 _DirectionVector;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
float SineWaveGenerator (float x, float y, float frequency, float offset) {
float sinValue = sin((x + y) * PI * frequency + offset);
float clampedSinWave = sinValue * 0.5 + 0.5;
return clampedSinWave;
}
fixed4 textureColor;
void surf (Input i, inout SurfaceOutputStandardSpecular o)
{
float interpolatorXValue = 0;
float interpolatorYValue = 0;
float directionX = min(1, max(-1, _DirectionVector.x));
float directionY = min(1, max(-1, _DirectionVector.y));
interpolatorXValue = i.uv_MainTex.x * clamp(-1, 1, directionX);
interpolatorYValue = i.uv_MainTex.y * clamp(-1, 1, _DirectionVector.y);
textureColor = tex2D(_MainTex, i.uv_MainTex);
float sinWaveColor = SineWaveGenerator(interpolatorXValue, interpolatorYValue, _Frequency, _Time.y * _AnimationSpeed);
float4 pixel = lerp(_ColorA, _ColorB, sinWaveColor * textureColor);
o.Albedo = pixel.xyz;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment