//*
Based on supershape formula
http://local.wasp.uwa.edu.au/~pbourke/surfaces_curves/supershape/
CIFilter conversion by alx at toneburst, 2008
http://machinesdontcare.wordpress.com
http://www.toneburst.net
*/
//////////////////////////////
//// SUPERSHAPE 2D ////
//////////////////////////////
vec2 super2D(float m, float n1, float n2, float n3, float val)
{
float r;
float t1, t2;
float a = 1.0, b = 1.0;
t1 = cos(m * val / 4.0) / a;
t1 = abs(t1);
t1 = pow(t1, n2);
t2 = sin(m * val / 4.0) / b;
t2 = abs(t2);
t2 = pow(t2, n3);
r = pow(t1 + t2, 1.0 / n1);
r = 1.0 / r;
vec2 xy = (abs(r) == 0.0) ? vec2(0.0,0.0) :
vec2(r * cos(val),r * sin(val));
// Output
return xy;
}
//////////////////////////////
//// MAIN LOOP ////
//////////////////////////////
kernel vec4 superShape_2D( float M, float N1, float N2, float N3,
float Scale, float Twist, float Width,
float Zoom, vec2 Center,
float PolarMix, float CartesianMix, float PolarCartesianMix,
float ColorTable, __color Color, sampler LUT)
{
// Sampler dimensions
vec2 dims = samplerSize(LUT);
// Normalised coord of current pixel of LUT texture (0.0 > 1.0)
vec2 xy = samplerCoord(LUT) / dims;
// Centered coords (-1.0 > 1.0) + center-offset
vec2 xyCenter = ((2.0 * xy - 1.0) + Center) * Zoom;
// Polar coords (angle)
float phi = (atan(xyCenter.y,xyCenter.x));
// Distance from center (including offset)
float r = distance(xyCenter,vec2(0.0,0.0) + Center);
/* Create value to send to superShape function */
// Mix polar-coordinate values phi and r
float polar = mix(phi,r,PolarMix);
// Mix cartesian coordinate X and Y values
float cartesian = mix(xyCenter.x,xyCenter.y,CartesianMix);
// Mix the mixed values
float polarCartesian = mix(polar,cartesian,PolarCartesianMix) * Scale + Twist;
// Send value to superShape function
vec2 point = super2D(M,N1,N2,N3,polarCartesian);
// Distance between current pixel and superShape result for this pixel
float dist = distance(xyCenter,point);
// Scale distance
float distSmooth = smoothstep(Width,0.0,dist);
// Output
return sample(LUT, vec2(distSmooth,ColorTable) * dims) * Color;
}
0 Responses to “SuperShape 2D CIFilter Sourcecode”
Leave a Reply