_Distort.vsh
{
	uniform	mat4	u_ModelViewMatrixTranspose;
	uniform	mat4	u_ModelViewMatrix;
	uniform	mat4	u_ModelViewProjectionMatrix;
	uniform	mat4	u_ProjectionMatrixTranspose;
	uniform float	u_distortMagnitude;

	#if defined(CLIP)
		out	vec3	v_clipVertex;
	#endif
	out	vec4	color;
	out	vec4	texCoords;

	include deforms.inc
	include colorGen.inc
	include tcGen.inc
	void main(void)
	{
		mat2 dummy;
		vec4 glVertex = DeformGeometry(dummy);
		color = colorGen(glVertex.xyz, u_gen, u_parm0, u_parm1, u_parm2, u_parma);
		tcGen(texCoords.xy, glVertex.xyz);
		texCoords.zw = attr_texCoords;
		gl_Position = u_ModelViewProjectionMatrix * glVertex;

		// take the deform magnitude and scale it by the projection distance
		vec4 deformVec = vec4(1.0, 0.0, dot(u_ModelViewMatrixTranspose[2], glVertex), 1.0);

		float d1 = dot(u_ProjectionMatrixTranspose[0], deformVec);
		float d2 = dot(u_ProjectionMatrixTranspose[3], deformVec);

		// clamp the distance so the the deformations don't get too wacky near the view
		color.a *= u_distortMagnitude * min(d1 / max(d2, 1.0), 0.02) * 64.0;

		include clipVertex.inc
	}
}

_Distort.fsh
{
	uniform sampler2D	u_DistortMap;
	uniform float		u_DistortMapSwizzle;
	uniform sampler2D	u_DistortMask;
	uniform float		u_DistortMaskSwizzle;
	#if defined(CLIP)
		uniform vec4	u_clipPlane;
		in	vec3	v_clipVertex;
	#endif
	in	vec4		color;
	in	vec4		texCoords;
	out	vec4		fragColor;
	include Texture.inc
	void main(void)
	{
		include clipPlane.inc
		float mask = color.a * Texture(u_DistortMask, texCoords.zw, u_DistortMaskSwizzle).a;
		fragColor = vec4((mask * color.rgb * (Texture(u_DistortMap, texCoords.xy, u_DistortMapSwizzle).rgb - vec3(0.5)) + vec3(0.5)), 0.5);
	}
}
