|
|
@@ -0,0 +1,125 @@ |
|
|
vec3 hsv2rgb(vec3 c) |
|
|
{ |
|
|
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); |
|
|
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); |
|
|
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); |
|
|
} |
|
|
|
|
|
|
|
|
// The following are from http://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm: |
|
|
|
|
|
float sdCircle( vec2 p, float r ) |
|
|
{ |
|
|
return length(p) - r; |
|
|
} |
|
|
|
|
|
float sdBox( in vec2 p, in vec2 b ) |
|
|
{ |
|
|
vec2 d = abs(p)-b; |
|
|
return length(max(d,vec2(0))) + min(max(d.x,d.y),0.0); |
|
|
} |
|
|
|
|
|
float sdEquilateralTriangle( in vec2 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 = vec2( 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 sdPentagon( in vec2 p, in float r ) |
|
|
{ |
|
|
const vec3 k = vec3(0.809016994,0.587785252,0.726542528); |
|
|
p.x = abs(p.x); |
|
|
p -= 2.0*min(dot(vec2(-k.x,k.y),p),0.0)*vec2(-k.x,k.y); |
|
|
p -= 2.0*min(dot(vec2( k.x,k.y),p),0.0)*vec2( k.x,k.y); |
|
|
return length(p-vec2(clamp(p.x,-r*k.z,r*k.z),r))*sign(p.y-r); |
|
|
} |
|
|
|
|
|
float sdCross( in vec2 p, in vec2 b, float r ) |
|
|
{ |
|
|
p = abs(p); p = (p.y>p.x) ? p.yx : p.xy; |
|
|
|
|
|
vec2 q = p - b; |
|
|
float k = max(q.y,q.x); |
|
|
vec2 w = (k>0.0) ? q : vec2(b.y-p.x,-k); |
|
|
return sign(k)*length(max(w,0.0)) + r; |
|
|
} |
|
|
|
|
|
|
|
|
// 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 vec2 p, float a) { |
|
|
p = cos(a)*p + sin(a)*vec2(p.y, -p.x); |
|
|
} |
|
|
|
|
|
// Shortcut for 45-degrees rotation |
|
|
void pR45(inout vec2 p) { |
|
|
p = (p + vec2(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 = mod(p + halfsize, size) - halfsize; |
|
|
return c; |
|
|
} |
|
|
|
|
|
// Same, but mirror every second cell so they match at the boundaries |
|
|
float pModMirror1(inout float p, float size) { |
|
|
float halfsize = size*0.5; |
|
|
float c = floor((p + halfsize)/size); |
|
|
p = mod(p + halfsize,size) - halfsize; |
|
|
p *= mod(c, 2.0)*2 - 1; |
|
|
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 = mod(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 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; |
|
|
// 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 |
|
|
vec2 pMod2(inout vec2 p, vec2 size) { |
|
|
vec2 c = floor((p + size*0.5)/size); |
|
|
p = mod(p + size*0.5,size) - size*0.5; |
|
|
return c; |
|
|
} |
|
|
|
|
|
|
|
|
//// the following are from the book of shaders |
|
|
/// |
|
|
|
|
|
float random (in vec2 _st) { |
|
|
return fract(sin(dot(_st.xy, |
|
|
vec2(12.9898,78.233)))* |
|
|
43758.5453123); |
|
|
} |
|
|
|