Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Last active July 12, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharStiles/783675b1229221be108cfbb1c430e094 to your computer and use it in GitHub Desktop.
Save CharStiles/783675b1229221be108cfbb1c430e094 to your computer and use it in GitHub Desktop.
// these are color palettes, basically you have a variable in and it changes color smoothly based on that variable
// Function from Iñigo Quiles
// https://www.shadertoy.com/view/MsS3Wc
// NOTE this is already built into the force
vec3 hsb2rgb( in vec3 c ){
vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),
6.0)-3.0)-1.0,
0.0,
1.0 );
rgb = rgb*rgb*(3.0-2.0*rgb);
return c.z * mix( vec3(1.0), rgb, c.y);
}
// to see this function graphed out go to: https://www.desmos.com/calculator/rz7abjujdj
vec3 cosPalette( float t )
{
// please play around with these numbers to get a better palette
vec3 brightness = vec3(0.3);
vec3 contrast = vec3(0.5);
vec3 osc = vec3(0.5,1.0,0.0);
vec3 phase = vec3(0.4,0.9,0.2);
return brightness + contrast*cos( 6.28318*(osc*t+phase) );
}
// this is the same thing but you pass in all the variables yourself
// As t runs from 0 to 1 (our normalized palette index or domain),
//the cosine oscilates c times with a phase of d.
//The result is scaled and biased by a and b to meet the desired constrast and brightness.
// http://www.iquilezles.org/www/articles/palettes/palettes.htm
vec3 cosPalette2( float t, vec3 a, vec3 b, vec3 c, vec3 d )
{
return a + b*cos( 6.28318*(c*t+d) );
}
// to read more about turbo: https://ai.googleblog.com/2019/08/turbo-improved-rainbow-colormap-for.html
// in short, its good for colorblindness, getting more deatil (based on human perception), high contrast
// but still smooth interpretation of the in-value
vec3 turboPalette(float x){ // x can be any floating point number that changes, like time or pos.x;
float m = 1.0; // it repeats every 1 unit, because thats the turbo function's range
float t = m - abs(mod(x*20. , (2.*m)) - m);
// this line turns an increasing value into a triangle wave, linear interpolation of length 1 up
// linear interpolation of length 1 down. like this: /\/\/\/\/\/\/\/\/\/\/\/\/\/
// you use const whenever the varible is just using plain old data that wont change
// its more efficient
const vec3 c0 = vec3(0.1140890109226559, 0.06288340699912215, 0.2248337216805064);
const vec3 c1 = vec3(6.716419496985708, 3.182286745507602, 7.571581586103393);
const vec3 c2 = vec3(-66.09402360453038, -4.9279827041226, -10.09439367561635);
const vec3 c3 = vec3(228.7660791526501, 25.04986699771073, -91.54105330182436);
const vec3 c4 = vec3(-334.8351565777451, -69.31749712757485, 288.5858850615712);
const vec3 c5 = vec3(218.7637218434795, 67.52150567819112, -305.2045772184957);
const vec3 c6 = vec3(-52.88903478218835, -21.54527364654712, 110.5174647748972);
return c0+t*(c1+t*(c2+t*(c3+t*(c4+t*(c5+t*c6))))); // this is magic, dont worry about it.
}
// change space functions
// The following are from http://mercury.sexy/hg_sdf/ (modified a lil by char)
// 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".
vec2 rotate(vec2 p, float a) {
return p = cos(a)*p + sin(a)*vec2(p.y, -p.x);
}
// Repeat around the origin by a fixed angle.
// For easier use, num of repetitions is use to specify the angle.
vec2 modPolar(vec2 p, float repetitions) {
float angle = 2.*PI/repetitions;
float a = atan(p.y, p.x) + angle/2.;
float r = length(p);
float c = floor(a/angle);
a = mod(a,angle) - angle/2.;
p = vec2(cos(a), sin(a))*r;
return p;
}
// some other functions
vec4 invert(vec4 col ){
return 1.0-col; // shorthand for vec4(1.0) - col
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment