26
Feb
08

SuperShape 2D CIFilter Sourcecode

//*
Based on supershape formula
http://local.wasp.uwa.edu.au/~pbourke/surfaces_curves/supershape/

CIFilter conversion by alx at toneburst, 2008
https://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;
}
Advertisements

0 Responses to “SuperShape 2D CIFilter Sourcecode”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Twitter

February 2008
M T W T F S S
« Jan   Mar »
 123
45678910
11121314151617
18192021222324
2526272829  

Links

Blog Stats

  • 473,930 hits

%d bloggers like this: