Files
ogre-prototype/water/water.frag
2025-09-08 09:46:16 +03:00

133 lines
5.3 KiB
GLSL

OGRE_NATIVE_GLSL_VERSION_DIRECTIVE
#include <OgreUnifiedShader.h>
SAMPLER2D(reflectMap, 0);
SAMPLER2D(noiseMap, 1);
// SAMPLER2D(refractMap, 1);
#if NEW_WATER_SHADER
MAIN_PARAMETERS
IN(vec4 clipSpace, TEXCOORD0)
IN(vec2 textureCoords, TEXCOORD1)
MAIN_DECLARATION
{
vec2 ndc = clipSpace.xy / clipSpace.w / 2.0 + vec2(0.5, 0.5);
vec2 distortion1 = texture2D(noiseMap, textureCoords).rg * 2.0 - vec2(1.0, 1.0);
vec2 distortion2 = texture2D(noiseMap, textureCoords * 5.0 + vec2(10202.0, 221.0)).rg * 2.0 - vec2(1.0, 1.0);
vec2 distortion3 = texture2D(noiseMap, textureCoords * 10.0 + vec2(1302.0, 721.0)).rg * 2.0 - vec2(1.0, 1.0);
vec2 reflectionUV = (vec2(ndc.x, 1.0 - ndc.y) + distortion1 * 0.035 + distortion2 * 0.02 + distortion3 * 0.01);
vec2 refractionUV = (vec2(ndc.x, 1.0 - ndc.y) + distortion1 * 0.035 + distortion2 * 0.02 + distortion3 * 0.01);
reflectionUV.x = clamp(reflectionUV.x, 0.001, 0.999);
reflectionUV.y = clamp(reflectionUV.y, 0.001, 0.999);
refractionUV.x = clamp(refractionUV.x, 0.001, 0.999);
refractionUV.y = clamp(refractionUV.y, 0.001, 0.999);
vec4 reflectionColour = texture2D(reflectMap, reflectionUV * 0.5);
vec4 refractionColour = texture2D(reflectMap, refractionUV * 0.5 + vec2(0.5, 0.0));
gl_FragColor = mix(reflectionColour, refractionColour, 0.5);
}
#else
OGRE_UNIFORMS(
uniform float renderTargetFlipping;
uniform vec4 viewportSize;
uniform f32vec4 cameraPosition;
uniform mat4 viewProj2;
uniform float time2;
uniform vec4 materialVariables;
)
float rand(float n){return fract(sin(n) * 43758.5453123);}
float rand2(vec2 n) {
return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}
float noise(vec2 n) {
const vec2 d = vec2(0.0, 1.0);
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
float fx = f.x;
vec2 e = b + d.yx;
float m = mix(rand2(b), rand2(e), fx);
vec2 p = b + d.xy;
vec2 q = b + d.yy;
float r = mix(rand2(p), rand2(q), f.y);
return mix(m, r, fx);
}
MAIN_PARAMETERS
IN(f32vec3 positionWS, TEXCOORD0)
IN(f32vec3 vnormal, TEXCOORD1)
IN(f32vec3 viewDirection, TEXCOORD2)
IN(f32vec3 viewDirectionTS, TEXCOORD3)
IN(f32vec3 olightDirection, TEXCOORD4)
IN(f32vec3 lightDirectionTS, TEXCOORD5)
IN(vec2 bumpCoord0, TEXCOORD6)
MAIN_DECLARATION
{
float flip = -renderTargetFlipping;
vec2 screenUV = gl_FragCoord.xy / viewportSize.xy;
#if !defined(OGRE_HLSL) && !defined(VULKAN)
screenUV.y = 1.0 - screenUV.y * 0.6 - 0.2;
#else
screenUV.y = screenUV.y * 0.6 + 0.2;
#endif
vec2 texCoord = 0.001 * positionWS.xz;
vec3 normal = vec3_splat(0.0);
float time = time2 * 0.01;
f32vec2 muv = screenUV * 1.1 + vec2(-0.1, -0.1);
normal += normalize(2.0 * texture2D(noiseMap, vec2(texCoord.x, texCoord.y - 5.0 * time)).rgb - 1.0);
normal += normalize(2.0 * texture2D(noiseMap, vec2(texCoord.y, 1.0 - texCoord.x - 5.0 * time)).rgb - 1.0);
normal += normalize(2.0 * texture2D(noiseMap, vec2(1.0 - texCoord.x, 1.0 - texCoord.y - 5.0 * time)).rgb - 1.0);
normal += normalize(2.0 * texture2D(noiseMap, vec2(1.0 - texCoord.y, texCoord.x - 5.0 * time)).rgb - 1.0);
normal = normalize(normal);
float reflectionDepth = 0.08;
float refractionDepth = 1.05;
vec2 reflectionTexCoord = screenUV + (normal.xy * reflectionDepth) / length(positionWS - cameraPosition.xyz);
vec2 refractionTexCoord = screenUV + normal.xy * refractionDepth;
float depth = saturate(length(positionWS - cameraPosition.xyz) * 0.01);
float nx = sin(noise(vec2(1300.0 + screenUV.x * 48.11, 1100.0 + screenUV.y)));
float ny = sin(noise(vec2(100.0 + screenUV.y * 72.2, 1500.0 + screenUV.x)));
vec4 t0 = texture2D(noiseMap, bumpCoord0) * 2.0 - 1.0;
vec4 t1 = texture2D(noiseMap, bumpCoord0 + vec2(0.75, 0.02)) * 2.0 - 1.0;
vec4 t2 = texture2D(noiseMap, bumpCoord0 + vec2(0.33, 0.11)) * 2.0 - 1.0;
vec4 N = vec4(t0.xyz + t1.xyz + t2.xyz, 1);
N = normalize(viewProj2 * N) * 0.1;
vec4 mrow = vec4(screenUV.x, 0, screenUV.y, 1);
vec4 mrowt = mul(viewProj2, mrow);
f32vec2 e = vec2(0, 0);
float min_speed = 16.0;
float max_ax = 0.01;
float max_ay = 0.005;
float px = noise(vec2(screenUV.x * 13123.0, screenUV.y * 1121.0));
float py = noise(vec2(screenUV.y * 15123.0, screenUV.x * 1421.0));
float offsetx = 0.0;
float offsety = 0.0;
float l = length(positionWS - cameraPosition.xyz);
for (int i = 0; i < 4; i++) {
e.x += sin(mrow.z * min_speed + offsetx + time2 * 0.01) * max_ax * (1.0 + 1.0 / (1.0 + l * 30.0));
e.y += cos(mrow.x * min_speed + offsety + time2 * 0.01) * max_ay;
offsetx += 113.0;
offsety += 78.0;
min_speed *= 2.45;
max_ax *= 0.9;
max_ay *= 0.9;
}
f32vec2 reflectUV = /* screenUV + e */ reflectionTexCoord;
f32vec2 refractUV = /* screenUV + e */ refractionTexCoord;
reflectUV.x = (reflectUV.x < 0.05 ) ? 0.05 : reflectUV.x;
reflectUV.x = (reflectUV.x > 0.95 ) ? 0.95 : reflectUV.x;
f32vec2 uv_mul = vec2(0.5, 1.0);
f32vec2 uv_offset = vec2(0.5, 0.0);
vec4 reflectionColour = texture2D(reflectMap, reflectUV * uv_mul);
vec4 refractionColour = texture2D(reflectMap, refractUV * uv_mul + uv_offset);
if (reflectionColour.a == 0.0) {
reflectionColour = texture2D(reflectMap, screenUV * uv_mul);
}
if (refractionColour.a == 0.0) {
refractionColour = texture2D(reflectMap, screenUV * uv_mul + uv_offset);
}
vec4 result = mix(mix(reflectionColour, refractionColour, 0.5), vec4(0.0, 1.0, 1.0, 1.0), depth);
float mul = dot(vec3(0.0, 1.0, 0.0), vnormal);
result = result * mul;
result.a = 1.0;
gl_FragColor = result;
}
#endif