Skip to content

Instantly share code, notes, and snippets.

@Anthelmed
Last active January 28, 2022 09:52
Show Gist options
  • Save Anthelmed/c21c1ccbcb2ba223d2ef35e166dd6a48 to your computer and use it in GitHub Desktop.
Save Anthelmed/c21c1ccbcb2ba223d2ef35e166dd6a48 to your computer and use it in GitHub Desktop.
Menu Button Custom Render Texture
Shader "CustomRenderTexture/Menu Button Custom Render Texture"
{
Properties
{
_NoiseScale("Noise Scale", float) = 1
_NoiseStrength("Noise Strength", float) = 0.1
_NoiseSpeed("Noise Speed", float) = 0.1
_NoiseOffset("Noise Offset", float) = 0
}
SubShader
{
Blend One Zero
Pass
{
Name "Menu Button Custom Render Texture"
CGPROGRAM
#include "UnityCustomRenderTexture.cginc"
#pragma vertex CustomRenderTextureVertexShader
#pragma fragment frag
#pragma target 3.0
float _NoiseScale;
float _NoiseStrength;
float _NoiseSpeed;
float _NoiseOffset;
//https://www.ronja-tutorials.com/post/047-invlerp_remap/
float2 invLerp(float2 from, float2 to, float2 value){
return (value - from) / (to - from);
}
float2 remap(float2 origFrom, float2 origTo, float2 targetFrom, float2 targetTo, float2 value){
float2 rel = invLerp(origFrom, origTo, value);
return lerp(targetFrom, targetTo, rel);
}
//https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float sdRoundedBox( in float2 p, in float2 b, in float4 r )
{
r.xy = (p.x>0.0)?r.xy : r.zw;
r.x = (p.y>0.0)?r.x : r.y;
float2 q = abs(p)-b+r.x;
return min(max(q.x,q.y),0.0) + length(max(q,0.0)) - r.x;
}
//https://docs.unity3d.com/Packages/com.unity.shadergraph@12.0/manual/Gradient-Noise-Node.html
float2 unity_gradientNoise_dir(float2 p)
{
p = p % 289;
float x = (34 * p.x + 1) * p.x % 289 + p.y;
x = (34 * x + 1) * x % 289;
x = frac(x / 41) * 2 - 1;
return normalize(float2(x - floor(x + 0.5), abs(x) - 0.5));
}
float unity_gradientNoise(float2 p)
{
float2 ip = floor(p);
float2 fp = frac(p);
float d00 = dot(unity_gradientNoise_dir(ip), fp);
float d01 = dot(unity_gradientNoise_dir(ip + float2(0, 1)), fp - float2(0, 1));
float d10 = dot(unity_gradientNoise_dir(ip + float2(1, 0)), fp - float2(1, 0));
float d11 = dot(unity_gradientNoise_dir(ip + float2(1, 1)), fp - float2(1, 1));
fp = fp * fp * fp * (fp * (fp * 6 - 15) + 10);
return lerp(lerp(d00, d01, fp.y), lerp(d10, d11, fp.y), fp.x);
}
float4 frag(v2f_customrendertexture IN) : COLOR
{
float2 uv = IN.localTexcoord.xy;
float2 centerUV = remap(0, 1, -1, 1, IN.localTexcoord.xy);
float roundedBox = sdRoundedBox(centerUV, float2(1, 0.6), float4(0.6, 0.6, 0.6, 0.6));
float4 noise = (unity_gradientNoise(uv * _NoiseScale + _NoiseOffset * _NoiseSpeed) + 0.5) * _NoiseStrength;
float4 color = 1 - smoothstep(0, 0.01, roundedBox + noise);
return color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment