// xs_begin
// author : 'ephtracy'
// arg : {id = '0' name = 'Side'  value = '3'   range = '3 64'    step = '1'    decimal = '0' }
// arg : {id = '1' name = 'Angle' value = '0'   range = '0 360'   step = '5'    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

// 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 ) {
	const float PI = 3.1415926;

	// model center
	vec3 center = i_volume_size * 0.5;
	
	// 2D pos
	vec2 u = ( v - center ).xy;

	// poly
	float angle = PI * 2.0 / max( floor( i_args[0] ), 3.0 );
	float t = min( center.x, center.y );

	// polar coord
	float r = length( u );
	float theta = atan( u.y, u.x ) + PI + i_args[1] / 180.0 * PI;

	r *= cos( angle * abs( fract( theta / angle ) - 0.5 ) );
	t *= cos( angle * 0.5 );

	r = max( r, 0.0 );
	t = max( t, 0.0 );

	return i_color_index * step( r, t );
}
