// xs_begin
// author : 'ephtracy'
// arg : {id = '0' name = 'Degree' value = '3'   range = '3 64'    step = '1'    decimal = '0' }
// xs_end

//===== user args =====
// uniform float	i_args[8];

//===== built-in args =====
// uniform vec3		i_volume_size;	// volume size [1-256]
// uniform float	i_color_index;	// color index [0-255]
// uniform vec3		i_mirror;		// mirror mode [0,1]
// uniform vec3		i_axis;			// axis mode [0,1]
// uniform float	i_iter;			// iteration index

//===== built-in functions ===== 
// float voxel( vec3 v );			// get voxel color index at position v

vec3 powern( vec3 v, float n ) {
	float r = length( v );
	float phi = atan( v.y, v.x );
	float theta = atan( length( v.xy ), v.z );

	r = pow( r, n );
	phi *= n;
	theta *= n;

	return r * vec3(
		sin( theta ) * vec2( cos( phi ), sin( phi ) ),
		cos( theta )
	);
}

// generate a new voxel color index [0, 255] at position v ( v is at the center of voxel, such as vec3( 1.5, 2.5, 4.5 ) )
float map( vec3 v ) {
	float n = max( i_args[0], 4.0 );
	
	vec3 center = i_volume_size * 0.5;
	
	float size = min( min( center.x, center.y ), center.z );

	vec3 u = ( v - center ) / size * 1.1;

	for ( int i = 0; i < 8; i++ ) {
		u = powern( u, n ) + u;
	}
	
	return i_color_index * step( length( u ), 2.0 );
}