Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Created October 5, 2021 21:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharStiles/787c2e3e898b68f7e2ca1acbdc0262b5 to your computer and use it in GitHub Desktop.
Save CharStiles/787c2e3e898b68f7e2ca1acbdc0262b5 to your computer and use it in GitHub Desktop.
Unity shader sticker sheet
// Tips for converting GLSL functions to Unity functions
// vec turn into float (usually massive comand all replace works)
// fract -> frac
// mod -> modf
// time, iTime, u_Time -> _Time.y
float3 hsv2rgb(float3 c) {
c = float3(c.x, clamp(c.yz, 0.0, 1.0));
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
// smooth min with radius of k
float smin( float a, float b, float k )
{
float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 );
return lerp( b, a, h ) - k*h*(1.0-h);
}
// smooth mod(x,y) with radius of e
float smoothMod(float x, float y, float e){
float PI = 3.1415;
float top = cos(PI * (x/y)) * sin(PI * (x/y));
float bot = pow(sin(PI * (x/y)),2.)+ pow(e, sin(_Time.y));
float at = atan(top/bot);
return y * (1./2.) - (1./PI) * at ;
}
// for visualizing a music beat
float getBPMVis(float bpm){
// this function can be found graphed out here :https://www.desmos.com/calculator/rx86e6ymw7
float bps = 60./bpm; // beats per second
float bpmVis = tan((_Time.y*3.1415)/bps);
// multiply it by PI so that tan has a regular spike every 1 instead of PI
// divide by the beat per second so there are that many spikes per second
bpmVis = clamp(bpmVis,0.,10.);
// tan goes to infinity so lets clamp it at 10
bpmVis = abs(bpmVis)/20.;
// tan goes up and down but we only want it to go up
// (so it looks like a spike) so we take the absolute value
// dividing by 20 makes the tan function more spiking than smoothly going
// up and down, check out the desmos link to see what i mean
bpmVis = 1.+(bpmVis*0.05);
// we want to multiply by this number, but its too big
// by itself (it would be too stroby) so we want the number to multiply
// by to be between 1.0 and 1.05 so its a subtle effect
return bpmVis;
}
// The following are from http://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm:
float sdCircle( float2 p, float r )
{
return length(p) - r;
}
float sdBox( in float2 p, in float2 b )
{
float2 d = abs(p)-b;
return length(max(d,float2(0.,0.))) + min(max(d.x,d.y),0.0);
}
float sdEquilateralTriangle( in float2 p )
{
const float k = sqrt(3.0);
p.x = abs(p.x) - 1.0;
p.y = p.y + 1.0/k;
if( p.x + k*p.y > 0.0 ) p = float2( p.x - k*p.y, -k*p.x - p.y )/2.0;
p.x -= clamp( p.x, -2.0, 0.0 );
return -length(p)*sign(p.y);
}
float sdEgg( in float2 p, in float ra, in float rb )
{
const float k = sqrt(3.0);
p.x = abs(p.x);
float r = ra - rb;
return ((p.y<0.0) ? length(float2(p.x, p.y )) - r :
(k*(p.x+r)<p.y) ? length(float2(p.x, p.y-k*r)) :
length(float2(p.x+r,p.y )) - 2.0*r) - rb;
}
// The following are from http://mercury.sexy/hg_sdf/
// Rotate around a coordinate axis (i.e. in a plane perpendicular to that axis) by angle <a>.
// Read like this: R(p.xz, a) rotates "x towards z".
// This is fast if <a> is a compile-time constant and slower (but still practical) if not.
void pR(inout float2 p, float a) {
p = cos(a)*p + sin(a)*float2(p.y, -p.x);
}
// Shortcut for 45-degrees rotation
void pR45(inout float2 p) {
p = (p + float2(p.y, -p.x))*sqrt(0.5);
}
// Repeat space along one axis. Use like this to repeat along the x axis:
// <float cell = pMod1(p.x,5);> - using the return value is optional.
float pMod1(inout float p, float size) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = modf(p + halfsize, size) - halfsize;
return c;
}
// Repeat the domain only in positive direction. Everything in the negative half-space is unchanged.
float pModSingle1(inout float p, float size) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
if (p >= 0.)
p = modf(p + halfsize, size) - halfsize;
return c;
}
// Repeat around the origin by a fixed angle.
// For easier use, num of repetitions is use to specify the angle.
float pModPolar(inout float2 p, float repetitions) {
float PI = 3.14;
float angle = 2.*PI/repetitions;
float a = atan(p.y/ p.x) + angle/2.;
float r = length(p);
float c = floor(a/angle);
a = modf(a,angle) - angle/2.;
p = float2(cos(a), sin(a))*r;
// For an odd number of repetitions, fix cell index of the cell in -x direction
// (cell index would be e.g. -5 and 5 in the two halves of the cell):
if (abs(c) >= (repetitions/2.)) c = abs(c);
return c;
}
// Repeat in two dimensions
float2 pMod2(inout float2 p, float2 size) {
float2 c = floor((p + size*0.5)/size);
p = modf(p + size*0.5,size) - size*0.5;
return c;
}
//// the following are from the book of shaders
float random (in float2 _st) {
return frac(sin(dot(_st.xy,float2(12.9898,78.233)))*
43758.5453123);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment