Skip to content

Instantly share code, notes, and snippets.

@belzecue
Created July 5, 2021 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belzecue/96dad64d48389eda4900cf773135ea0e to your computer and use it in GitHub Desktop.
Save belzecue/96dad64d48389eda4900cf773135ea0e to your computer and use it in GitHub Desktop.
Unity - UV animation/distortion shader
Shader "Uber Animation Shader" {
Properties {
[Header(Main)]
_MainTex("Albedo (RGB)", 2D) = "white" {}
_MainBumpMap("Normal", 2D) = "bump" {}
[ToggleOff] _StaticBump("Static Bump Map", Float) = 1.0
_MainTexScale("MainTex Scale", float) = 0.0
_TexMColor("Color", Color) = (1,1,1,1)
_TexMColorBoost("Color Boost", Range(-1,1)) = 0.0
[Header(Texture 2)]
_Tex2("Albedo (RGB)", 2D) = "white" {}
_Tex2Scale("Tex2 Scale", float) = 0.0
_Tex2Color("Color", Color) = (1,1,1,0)
_Tex2ColorBoost("Color Boost", Range(-1,1)) = 0.0
[Header(Texture Blending)]
_ColorBlend("Color Blend [0.5]", Range(0,1)) = 0.5
_AlphaBlend("Alpha Blend [0.5]", Range(0,1)) = 0.5
_ColorStrength("Color Strength [0.5]", Range(0,4)) = 0.5
_AlphaStrength("Alpha Strength [0.5]", Range(0,1)) = 0.5
[Header(Distortion)]
_MasterDispl("Master Distortion [1.0]", Range(0,1)) = 1.0
_DisplMainScale("MainTex Scale", range(0,8)) = 1
_DisplMainAmount("MainTex Amount", range(0,1)) = 0.5
_DisplTex2Scale("Tex2 Scale", range(0,8)) = 1
_DisplTex2Amount("Tex2 Amount", range(0,1)) = 0.5
[Header(Speed)]
_MasterSpeed("Master Speed [1.0]", Range(0,8)) = 1.0
_MainScrollSpeed ("Tex Scrolling (XY,ZW)", vector) = (0,0,0,0)
[Header(Material)]
_Glossiness("Smoothness [0.5]", Range(0,1)) = 0.5
_Metallic ("Metallic [0]", Range(0,1)) = 0.0
}
SubShader {
Tags { "Queue"="Transparent" "RenderType"="Transparent" } // <= #transparent
//Tags { "RenderType"="Opaque" } // <= #opaque
LOD 200
CGPROGRAM
#define IF(a, b, c) lerp(b, c, step((fixed) (a), 0));
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows alpha:fade // <= #transparent
//#pragma surface surf Standard fullforwardshadows // <= #opaque
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex, _MainBumpMap, _Tex2;
struct Input {
float2 uv_MainTex;
float2 uv_MainBumpMap;
float2 uv_Tex2;
};
float
_Glossiness, _Metallic,
_ColorBlend, _AlphaBlend, _ColorStrength, _AlphaStrength,
_Tex2ColorBoost, _TexMColorBoost,
_MasterDispl, _MasterSpeed, _MainTexScale, _Tex2Scale,
_DisplMainScale, _DisplTex2Scale,
_DisplMainAmount, _DisplTex2Amount,
_DisplMainSin, _DisplTex2Sin, _DisplMainSinSpeed, _DisplTex2SinSpeed;
fixed4 _TexMColor, _Tex2Color;
fixed _StaticBump;
half4 _MainScrollSpeed;
float time;
float2 scaleCenter = float2(0.5f, 0.5f);
// 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)
void surf (Input IN, inout SurfaceOutputStandard o) {
time = _Time.x * _MasterSpeed;
// Displacement.
float displMain = tex2D(
_Tex2,
(IN.uv_Tex2 + _MainScrollSpeed.zw * time) * _DisplTex2Scale
).a;
// Displacement (scale to -1:1) multiplier.
displMain = ((displMain * 2) - 1) * _DisplMainAmount * _MasterDispl;
// MainTex color.
fixed4 colMain = tex2D (
_MainTex,
(IN.uv_MainTex + _MainScrollSpeed.xy * time)
+ ((IN.uv_MainTex - scaleCenter) * _MainTexScale + scaleCenter)
+ displMain
) * _TexMColor * (1 + _TexMColorBoost);
// Displacement.
float displTex2 = tex2D(
_MainTex,
(IN.uv_MainTex + _MainScrollSpeed.xy * time) * _DisplMainScale
).a;
// Displacement (scale to -1:1) multiplier.
displTex2 = ((displTex2 * 2) - 1) * _DisplTex2Amount * _MasterDispl;
// Tex2 color.
fixed4 colTex2 = tex2D (
_Tex2,
(IN.uv_Tex2 + _MainScrollSpeed.zw * time)
+ ((IN.uv_Tex2 - scaleCenter) * _Tex2Scale + scaleCenter)
+ displTex2
) * _Tex2Color * (1 + _Tex2ColorBoost);
// Color blend.
o.Albedo = lerp(
colMain.rgb,
colTex2.rgb,
_ColorBlend
);
o.Albedo = o.Albedo + ((_ColorStrength * 2.0 - 1.0) * o.Albedo);
// Alpha blend.
float alpha = lerp(
colMain.a,
colTex2.a,
_AlphaBlend
);
o.Alpha = clamp(alpha + ((_AlphaStrength * 2.0 - 1.0) * alpha), 0, 1);
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
// Normal mapping.
o.Normal = IF(
_StaticBump > 0,
UnpackNormal (tex2D(_MainBumpMap, IN.uv_MainBumpMap)),
UnpackNormal (
tex2D
(
_MainBumpMap,
//IN.uv_MainBumpMap
(IN.uv_MainBumpMap + _MainScrollSpeed.zw * time)
+ ((IN.uv_MainBumpMap - scaleCenter) * _Tex2Scale + scaleCenter)
+ displTex2
)
)
);
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment