//----------------------------------------------------------------------------------------------------------------------


uniform vec2 uStageSize;
uniform sampler2DRect uOutgoingTexture;
uniform float uOutgoingAlpha;
uniform sampler2DRect uIncomingTexture;
uniform float uOutInMix;

uniform float uInterferenceFrequency;
uniform float uInterferenceAmplitude;

uniform float uDistortionMaxY;
uniform float uDistortionRangeY;
uniform float uDistortionAmplitude;
uniform float uDistortionFrequency1;
uniform float uDistortionFrequency2;

uniform vec2 uCollapseCenter;
uniform vec2 uCollapseScale;
uniform float uCollapseBoundsX1;
uniform float uCollapseBoundsX2;
uniform float uCollapseBoundsY1;
uniform float uCollapseBoundsY2;

uniform float uGlowAlpha;
uniform float uGlowRadius;

const vec4 black = vec4(0.0,0.0,0.0,1.0);


//----------------------------------------------------------------------------------------------------------------------


float box(float x,float x1,float x2)
{
	float y1 = step(x1,x);
	float y2 = 1.0 - step(x2,x);
	return y1 * y2;
}


float smoothBox(float x,float x1,float x2,float x3,float x4)
{
	float y1 = smoothstep(x1,x2,x);
	float y2 = 1.0 - smoothstep(x3,x4,x);
	return y1 * y2;
}


//----------------------------------------------------------------------------------------------------------------------


void main()
{
	// Get the screen and texture coordinates. Also calculate the collapse effect...
	
	vec2 scrCoord = gl_TexCoord[0].xy;
	vec2 texCoord = scrCoord;
	texCoord.x = (texCoord.x - uCollapseCenter.x) / uCollapseScale.x + uCollapseCenter.x;
	texCoord.y = (texCoord.y - uCollapseCenter.y) / uCollapseScale.y + uCollapseCenter.y;
	
	// Apply the horizontal distortion to the image...
	
	float dy = texCoord.y - uDistortionMaxY;
	float amplitude = uDistortionAmplitude * pow(2.71828,-uDistortionRangeY*dy*dy);
	texCoord.x += amplitude * sin(texCoord.y * uDistortionFrequency1) * sin(texCoord.y * uDistortionFrequency2);
	
	// Get the image pixels and apply an interference wave pattern...
	
	float interference = sin(texCoord.y * uInterferenceFrequency) * uInterferenceAmplitude;
	float distortionBox = box(texCoord.x,0.0,uStageSize.x);
	float collapseBoxX = box(scrCoord.x,uCollapseBoundsX1,uCollapseBoundsX2);
	float collapseBoxY = box(scrCoord.y,uCollapseBoundsY1,uCollapseBoundsY2);

	vec4 outgoingColor = texture2DRect(uOutgoingTexture,texCoord);
	outgoingColor += interference;
	outgoingColor *= distortionBox * collapseBoxX * collapseBoxY;

	vec4 incomingColor = texture2DRect(uIncomingTexture,texCoord);
	incomingColor *= collapseBoxX * collapseBoxY;
	
	// Apply a glow effect...
	
	float glowX1 = uCollapseBoundsX1 - uGlowRadius;
	float glowX2 = uCollapseBoundsX2 + uGlowRadius;
	float glowX = smoothBox(scrCoord.x,glowX1,uCollapseBoundsX1,uCollapseBoundsX2,glowX2);

	float glowY1 = uCollapseBoundsY1 - uGlowRadius;
	float glowY2 = uCollapseBoundsY2 + uGlowRadius;
	float glowY = smoothBox(scrCoord.y,glowY1,uCollapseBoundsY1,uCollapseBoundsY2,glowY2);

	float glow = glowX * glowY;
	glow = glow * glow;
	glow *= uGlowAlpha;
	
	outgoingColor += glow; 
	outgoingColor.a = uOutgoingAlpha;
	
	incomingColor += glow; 

	// Return the pixel color...
	
	gl_FragColor = mix(outgoingColor,incomingColor,uOutInMix);
}


//----------------------------------------------------------------------------------------------------------------------
