OGRE_NATIVE_GLSL_VERSION_DIRECTIVE
#include <OgreUnifiedShader.h>

SAMPLER2D(sceneTex, 0);

OGRE_UNIFORMS(
	uniform float time;
)

IN(vec2 vUV, TEXCOORD0)

float hash(vec2 p)
{
	return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}

float hash(float f)
{
	return fract(sin(f * 127.1) * 43758.5453);
}

MAIN_DECLARATION
{
	// Underwater wavy distortion
	float waveX = sin(vUV.y * 40.0 + time * 2.5) * 0.003
		    + sin(vUV.y * 20.0 - time * 1.2) * 0.002;
	float waveY = cos(vUV.x * 35.0 + time * 2.0) * 0.003
		    + cos(vUV.x * 15.0 - time * 1.5) * 0.002;
	vec2 distortedUV = vUV + vec2(waveX, waveY);

	vec4 color = texture2D(sceneTex, distortedUV);

	// Blue-green underwater tint
	color.rgb = mix(color.rgb, vec3(0.0, 0.35, 0.5), 0.2);
	color.rgb *= vec3(0.9, 1.0, 1.1);

	// Bubbles - procedural circles that rise over time
	vec2 bubbleUV = vUV * 25.0;
	bubbleUV.y += time * 0.3;
	vec2 bubbleId = floor(bubbleUV);
	vec2 bubbleFract = fract(bubbleUV);
	float bubbleNoise = hash(bubbleId);
	float bubble = 0.0;
	if (bubbleNoise > 0.65) {
		vec2 bubbleCenter = vec2(
			hash(bubbleId + 1.0),
			hash(bubbleId + 2.0));
		float d = length(bubbleFract - bubbleCenter);
		bubble = smoothstep(0.12, 0.04, d);
		// Fade bubbles at top/bottom edges
		bubble *= smoothstep(0.0, 0.1, bubbleFract.y)
			* smoothstep(1.0, 0.9, bubbleFract.y);
	}
	color.rgb += vec3(0.7, 0.8, 0.9) * bubble * 0.4;

	// Occasional spark flashes
	float sparkInterval = 2.5;
	float sparkTime = floor(time / sparkInterval);
	vec2 sparkUV = vUV * 12.0;
	vec2 sparkId = floor(sparkUV);
	float sparkNoise = hash(sparkId + sparkTime);
	float spark = 0.0;
	if (sparkNoise > 0.96) {
		vec2 sparkCenter = vec2(
			hash(sparkNoise * 17.3),
			hash(sparkNoise * 53.1));
		float d = length(fract(sparkUV) - sparkCenter);
		float flash = sin(time * 15.0) * 0.5 + 0.5;
		spark = smoothstep(0.06, 0.01, d) * flash;
	}
	color.rgb += vec3(1.0, 0.95, 0.7) * spark * 0.7;

	// Simple vignette for depth feeling
	float vignette = 1.0 - length(vUV - 0.5) * 0.6;
	vignette = clamp(vignette, 0.3, 1.0);
	color.rgb *= vignette;

	gl_FragColor = color;
}
