Created
May 8, 2015 10:42
-
-
Save caiwan/0261ed2d42467d29eddb to your computer and use it in GitHub Desktop.
2D Effect library of code snippets (Shadertoy:https://www.shadertoy.com/view/ldSXzt)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// *** COLOR TRANSFORMATION *** | |
//http://beesbuzz.biz/code/hsv_color_transforms.php | |
vec3 rgb2yiq(vec3 color){return color * mat3(0.299,0.587,0.114,0.596,-0.274,-0.321,0.211,-0.523,0.311);} | |
vec3 yiq2rgb(vec3 color){return color * mat3(1.,0.956,0.621,1,-0.272,-0.647,1.,-1.107,1.705);} | |
// Direkt HSV transzformacio | |
vec3 hsvTransform(vec3 color, vec3 hsv){float _h = hsv.x, _s = hsv.y, _v = hsv.y; float VSU = _v*_s*cos(_h*PI/180.), VSW = _v*_s*sin(_h*PI/180.), rr = (.299*_v+.701*VSU+.168*VSW)*color.x + (.587*_v-.587*VSU+.330*VSW)*color.y + (.114*_v-.114*VSU-.497*VSW)*color.z, gg = (.299*_v-.299*VSU-.328*VSW)*color.x + (.587*_v+.413*VSU+.035*VSW)*color.y + (.114*_v-.114*VSU+.292*VSW)*color.z, bb = (.299*_v-.300*VSU+1.25*VSW)*color.x + (.587*_v-.588*VSU-1.05*VSW)*color.y + (.114*_v+.886*VSU-.203*VSW)*color.z; return vec3(rr,gg,bb); } | |
vec3 convertRGB4quant(vec3 color){ vec3 out0 = mod(color,1./16.); return out0;} | |
vec3 convertRGB4(vec3 color){return color-convertRGB4quant(color);} | |
vec3 convertRGB443quant(vec3 color){ vec3 out0 = mod(color,1./16.); out0.b = mod(color.b, 1./8.); return out0;} | |
vec3 convertRGB443(vec3 color){return color-convertRGB443quant(color);} | |
// Smooth HSV to RGB conversion | |
vec3 hsv2rgb_smooth( 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);} | |
vec3 hsv2rgb_trigonometric( in vec3 c ){vec3 rgb = 0.5 + 0.5*cos((c.x*6.0+vec3(0.0,4.0,2.0))*3.14159/3.0);return c.z * mix( vec3(1.0), rgb, c.y);} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// *** KALEIDOSCOPE *** | |
vec2 kaleidoscope( vec2 uv, float n, float bias ) { | |
float angle = PI / n; float r = length( uv ); | |
float a = atan( uv.y, uv.x ) / angle; a = mix( fract( a ), 1.0 - fract( a ), mod( floor( a ), 2.0 ) ) * angle; | |
return vec2( cos( a ), sin( a ) ) * r; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// *** PERLIN ZAJ *** | |
/// 2D random | |
vec2 random2(vec2 c) { float j = 4906.0*sin(dot(c,vec2(169.7, 5.8))); vec2 r; r.x = fract(512.0*j); j *= .125; r.y = fract(512.0*j);return r-0.5;} | |
/// 1D random / hash | |
float hash(float f){ return fract(56546.235423 * sin(f*235345.345345));} | |
const float F2 = 0.3660254; | |
const float G2 = -0.2113249; | |
/// simplex 2D zaj; perlin zaj egy frekvenciajahoz tartozo ertekek | |
float simplex2d(vec2 p){vec2 s = floor(p + (p.x+p.y)*F2),x = p - s - (s.x+s.y)*G2; float e = step(0.0, x.x-x.y); vec2 i1 = vec2(e, 1.0-e), x1 = x - i1 - G2, x2 = x - 1.0 - 2.0*G2; vec3 w, d; w.x = dot(x, x); w.y = dot(x1, x1); w.z = dot(x2, x2); w = max(0.5 - w, 0.0); d.x = dot(random2(s + 0.0), x); d.y = dot(random2(s + i1), x1); d.z = dot(random2(s + 1.0), x2); w *= w; w *= w; d *= w; return dot(d, vec3(70.0));} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// *** 2D TEXTURA FX *** | |
float checker(vec2 p){ return mod(floor(p.x)+floor(p.y), 2.);} | |
float stripe(float p, float h){return (abs(1.-mod(p,2.)) < h) ? 1. : 0.;} | |
float gradient(float p){return abs(1.-mod(p,2.));} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// *** TRANSZFORMACIOK *** | |
vec2 sincos( float x ){return vec2(sin(x), cos(x));} | |
vec2 rotate2d(vec2 uv, float phi){vec2 t = sincos(phi); return vec2(uv.x*t.y-uv.y*t.x, uv.x*t.x+uv.y*t.y);} | |
vec3 rotate3d(vec3 p, vec3 v, float phi){ v = normalize(v); vec2 t = sincos(-phi); float s = t.x, c = t.y, x =-v.x, y =-v.y, z =-v.z; mat4 M = mat4(x*x*(1.-c)+c,x*y*(1.-c)-z*s,x*z*(1.-c)+y*s,0.,y*x*(1.-c)+z*s,y*y*(1.-c)+c,y*z*(1.-c)-x*s,0.,z*x*(1.-c)-y*s,z*y*(1.-c)+x*s,z*z*(1.-c)+c,0.,0.,0.,0.,1.);return (vec4(p,1.)*M).xyz;} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/// *** TUNNEL *** | |
vec3 tunnel_uvw(vec2 pos){ float u = length(pos); return vec3 (u, atan(pos.y, pos.x), 1.0/u);} | |
vec3 tunnel_coord(vec2 p, float frekv, float radius, mat4 mat){ | |
vec3 o = mat[3].xzy, d = normalize(mat[2].xyz*radius + mat[0].xyz*p.x + mat[1].xyz*p.y); | |
float D=1./(d.y*d.y+d.z*d.z), a=(o.y*d.y+o.z*d.z)*D, b=(o.y*o.y+o.z*o.z-36.)*D, t=-a-sqrt(a*a-b); o+=t*d;// t = sqrt(a*a-b); | |
return vec3(o.x, atan(o.y,o.z)*(frekv/PI), t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment