/*
	{
	"DESCRIPTION": "RGB Laser Globe_xL",
	"CATEGORIES": 
		[
		"generator",
		"Optimized for xLights"
		],
	"ISFVSN": "2",
	"CREDIT": "original by @paulofalcao, modified by Old Salt",
	"VSN": "1.0",
	"INPUTS": 
		[
			{
			"LABEL": "Center: ",
			"NAME": "offset",
			"TYPE": "point2D",
			"MAX":
				[
				1.0,
				1.0
				],
			"MIN":
				[
				0.0,
				0.0
				],
			"DEFAULT": 
				[
				0.5,
				0.5
				]
			},
			{
			"LABEL": "Color Mode: ",
			"LABELS":
				[
				"Shader Default                             ",
				"xLights Color Pallete (3 used - blended)   ",
				"xLights Color Pallete (3 used - Max Color) "
				],
			"NAME": "colorMode",
			"TYPE": "long",
			"VALUES":
				[
				0,
				1,
				2
				],
			"DEFAULT": 0
			},
			{
			"NAME": "color1",
			"TYPE": "color",
			"DEFAULT":
				[
				1.0,
				0.0,
				0.0,
				1.0
				]
			},
			{
			"NAME": "color2",
			"TYPE": "color",
			"DEFAULT": 
				[
				0.0,
				1.0,
				0.0,
				1.0
				]
			},
			{
			"NAME": "color3",
			"TYPE": "color",
			"DEFAULT": 
				[
				0.0,
				0.0,
				1.0,
				1.0
				]
			},			
			{
			"LABEL": "Intensity: ",
			"NAME": "intensity",
			"TYPE": "float",
			"MAX": 1,
			"MIN": 0,
			"DEFAULT": 0.5
			},
			{
			"LABEL": "Dispersion: ",
			"NAME": "dispersion",
			"TYPE": "float",
			"MAX": 1,
			"MIN": 0,
			"DEFAULT": 0.5
			}
    ]
	}
*/

// original shader: https://editor.isf.video/shaders/5e7a804a7c113618206dee9b

#ifdef GL_ES
	precision highp float;
#endif

// float makePoint(float x,float y,float fx,float fy,float sx,float sy,float t){
// "float sx,float sy," removed as they were not used

float makePoint(float x,float y,float fx,float fy,float t)
	{
	float xx=x*sin(t*fx);
	float yy=y*cos(t*fy);

	//   return 1./ (sqrt(length(xx+yy) + length(xx*yy)));
	// add dispersion control
	return 1.0/ (sqrt(length(xx+yy) + length(xx*yy)*dispersion*2.0));
	}

void main( void ) 
	{
	//	vec2 p=(gl_FragCoord.xy/RENDERSIZE.x)*2.0-vec2(1.0,RENDERSIZE.y/RENDERSIZE.x);
	// modification to allow positioning
	vec2	centerPt = 2.0 * offset * RENDERSIZE;
	vec2 p=(gl_FragCoord.xy/centerPt.x)*2.0-vec2(1.0,centerPt.y/centerPt.x);
	float x=p.x;
	float y=p.y;

	// two values before TIME removed as they were not used	 
	float a=
		makePoint(x,y,3.3,2.9,TIME);
	a=a+makePoint(x,y,1.9,2.0,TIME);
	a=a+makePoint(x,y,0.8,0.7,TIME);
	a=a+makePoint(x,y,2.3,0.1,TIME);
	a=a+makePoint(x,y,0.8,1.7,TIME);
	a=a+makePoint(x,y,0.3,1.0,TIME);
	a=a+makePoint(x,y,1.4,1.7,TIME);
	a=a+makePoint(x,y,1.3,2.1,TIME);
	a=a+makePoint(x,y,1.8,1.7,TIME);

	float b=
		makePoint(x,y,1.2,1.9,TIME);
	b=b+makePoint(x,y,0.7,2.7,TIME);
	b=b+makePoint(x,y,1.4,0.6,TIME);
	b=b+makePoint(x,y,2.6,0.9,TIME);
	b=b+makePoint(x,y,0.7,1.4,TIME);
	b=b+makePoint(x,y,0.7,1.7,TIME);
	b=b+makePoint(x,y,0.8,0.5,TIME);
	b=b+makePoint(x,y,1.4,0.7,TIME);
	b=b+makePoint(x,y,0.7,1.3,TIME);

	float c=
		makePoint(x,y,3.7,0.3,TIME);
	c=c+makePoint(x,y,1.9,1.3,TIME);
	c=c+makePoint(x,y,0.8,0.9,TIME);
	c=c+makePoint(x,y,1.2,1.7,TIME);
	c=c+makePoint(x,y,0.3,0.6,TIME);
	c=c+makePoint(x,y,0.3,0.3,TIME);
	c=c+makePoint(x,y,1.4,0.8,TIME);
	c=c+makePoint(x,y,0.2,0.6,TIME);
	c=c+makePoint(x,y,1.3,0.5,TIME);

	//	vec3 d=vec3(a,b,c)*intensity/20.0 * (1.0- gl_FragCoord.x/RENDERSIZE.x);
	// modified to incorporate intensity control, and correct intensity along the x-axis
	// further modifications to use either colors from xLights or the shader defaults
	vec3 d;
	if (colorMode == 0) 
		{
		// Shader Default
		d=vec3(a,b,c) * intensity / 20.0  * (1.0- gl_FragCoord.x/RENDERSIZE.x);
		}
	else if (colorMode == 1)
		{
		// xLights Color Pallete (Blended)
		d = color1.rgb * a;
		d = d + color2.rgb * b;
		d = d + color3.rgb * c;
		d = d * intensity / 40.0  * (1.0- gl_FragCoord.x/RENDERSIZE.x);
		}
	else if (colorMode == 2)
		{
		// xLights Color Pallete (Max Color)
		d = color1.rgb * a;
		if (length(d) < (length(color2.rgb) * b)) d = color2.rgb * b;
		if (length(d) < (length(color3.rgb) * c)) d = color3.rgb * c;
		d = d * intensity / 40.0  * (1.0- gl_FragCoord.x/RENDERSIZE.x);
		}

	gl_FragColor = vec4(d.x,d.y,d.z,1.0);
	}