Skip to content

Commit

Permalink
Improve HQ terrain water
Browse files Browse the repository at this point in the history
Co-Authored-By: MaNGusT- <[email protected]>
  • Loading branch information
past-due and MaNGusT- committed Oct 24, 2024
1 parent f5b85c5 commit adcadc8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 53 deletions.
4 changes: 2 additions & 2 deletions data/WZPrebuiltPackages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# Prebuilt package DL info

# terrain_overrides/high.wz
set(WZ_PREBUILT_TERRAIN_HIGH_DL_URL "https://github.com/Warzone2100/data-terrain-high/releases/download/v1/high.wz")
set(WZ_PREBUILT_TERRAIN_HIGH_DL_SHA512 "44a39e585344b25ab0fa4ed769f2b48bf23a0e4fc4b23e0fab4e68643cc6e3ebeabba790e8b1e6c96493ad01b6a2f09e92b893063013077dcac2475f0b5373d6")
set(WZ_PREBUILT_TERRAIN_HIGH_DL_URL "https://github.com/Warzone2100/data-terrain-high/releases/download/v2/high.wz")
set(WZ_PREBUILT_TERRAIN_HIGH_DL_SHA512 "c96ee757c6771ed7cfd104bf4d86bf80a6ce9f1598a16bd26a711fcf2359ae7b13d3812d9cbe64ff6d1e9d55ed7f28c72a150e1c755cc595928f893029802570")
40 changes: 18 additions & 22 deletions data/base/shaders/terrain_water_high.frag
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,22 @@ vec4 main_bumpMapping()
vec2 uv1 = uv1_uv2.xy;
vec2 uv2 = uv1_uv2.zw;

vec3 N1 = texture2DArray(tex_nm, vec3(uv2, 0.f), WZ_MIP_LOAD_BIAS).xzy; // y is up in modelSpace
vec3 N2 = texture2DArray(tex_nm, vec3(uv1, 1.f), WZ_MIP_LOAD_BIAS).xzy;
//use overlay blending to mix normal maps properly
bvec3 computedN_select = lessThan(N1, vec3(0.5));
vec3 computedN_multiply = 2.f * N1 * N2;
vec3 computedN_screen = vec3(1.f) - 2.f * (vec3(1.f) - N1) * (vec3(1.f) - N2);
vec3 N = mix(computedN_screen, computedN_multiply, vec3(computedN_select));

N = mix(normalize(N * 2.f - 1.f), vec3(0.f,1.f,0.f), vec3(float(N == vec3(0.f,0.f,0.f))));

float lambertTerm = max(dot(N, lightDir), 0.0); // diffuse lighting

// Gaussian specular term computation
float gloss = texture2DArray(tex_sm, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS).r * texture2DArray(tex_sm, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS).r;
vec3 H = normalize(halfVec);
float exponent = acos(dot(H, N)) / (gloss + 0.05);
float gaussianTerm = exp(-(exponent * exponent));

vec4 fragColor = (texture2DArray(tex, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS)+texture2DArray(tex, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS)) * (gloss+vec4(0.08,0.13,0.15,1.0));
fragColor = fragColor*(ambientLight+diffuseLight*lambertTerm) + specularLight*(1.0-gloss)*gaussianTerm*vec4(1.0,0.843,0.686,1.0);
vec3 N1 = texture2DArray(tex_nm, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS).xzy*vec3(2.f, 2.f, 2.f) + vec3(-1.f, 0.f, -1.f); // y is up in modelSpace
vec3 N2 = texture2DArray(tex_nm, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS).xzy*vec3(-2.f, 2.f,-2.f) + vec3(1.f, -1.f, 1.f);

//use RNM blending to mix normal maps properly, see https://blog.selfshadow.com/publications/blending-in-detail/
vec3 N = normalize(N1 * dot(N1,N2) - N2*N1.y);

// Light
float noise = texture2DArray(tex, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS).r * texture2DArray(tex, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS).r;
float foam = texture2DArray(tex_sm, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS).r * texture2DArray(tex_sm, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS).r;

float lambertTerm = max(dot(N, lightDir), 0.0);
float blinnTerm = pow(max(dot(N, halfVec), 0.0), 32.f);

vec4 fragColor = vec4(0.16,0.26,0.3,1.0)+(noise+foam)*noise*0.5;
fragColor = fragColor*(ambientLight+diffuseLight*lambertTerm) + specularLight*blinnTerm*(foam*foam+noise);

vec4 lightmap_vec4 = texture(lightmap_tex, uvLightmap, 0.f);
vec4 color = fragColor * vec4(vec3(lightmap_vec4.a), 1.f); // ... * tile brightness / ambient occlusion (stored in lightmap.a);
color.rgb = blendAddEffectLighting(color.rgb, (lightmap_vec4.rgb / 1.5f)); // additive color (from environmental point lights / effects)
Expand All @@ -83,8 +79,8 @@ vec4 main_bumpMapping()
void main()
{
vec4 fragColor = main_bumpMapping();
fragColor = mix(fragColor, fragColor-depth*0.0007, depth*0.0009);
fragColor.a = mix(0.25, 1.0, depth2*0.005);
fragColor = mix(fragColor, fragColor-depth, depth);
fragColor.a = mix(0.25, 1.0, depth2);

if (fogEnabled > 0)
{
Expand Down
6 changes: 3 additions & 3 deletions data/base/shaders/terrain_water_high.vert
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ VERTEX_OUTPUT vec3 halfVec;
void main()
{
uvLightmap = (ModelUVLightmapMatrix * vec4(vertex.xyz, 1.f)).xy;
depth = vertex.w;
depth2 = length(vertex.y - vertex.w);
depth = vertex.w*0.0007;
depth2 = length(vertex.y - vertex.w)*0.005;

vec2 uv1 = vec2(vertex.x/4.f/128.f + timeSec/80.f, -vertex.z/4.f/128.f + timeSec/40.f); // (ModelUV1Matrix * vertex).xy;
vec2 uv2 = vec2(vertex.x/4.f/128.f, -vertex.z/4.f/128.f - timeSec/40.f); // (ModelUV2Matrix * vertex).xy;
uv1_uv2 = vec4(uv1.x, uv1.y, uv2.x, uv2.y);

vec3 eyeVec = normalize(cameraPos.xyz - vertex.xyz);
lightDir = sunPos.xyz;
halfVec = lightDir + eyeVec;
halfVec = normalize(lightDir + eyeVec);

vec4 position = ModelViewProjectionMatrix * vec4(vertex.xyz, 1.f);
vertexDistance = position.z;
Expand Down
40 changes: 18 additions & 22 deletions data/base/shaders/vk/terrain_water_high.frag
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,22 @@ vec4 main_bumpMapping()
vec2 uv1 = uv1_uv2.xy;
vec2 uv2 = uv1_uv2.zw;

vec3 N1 = texture(tex_nm, vec3(uv2, 0.f), WZ_MIP_LOAD_BIAS).xzy; // y is up in modelSpace
vec3 N2 = texture(tex_nm, vec3(uv1, 1.f), WZ_MIP_LOAD_BIAS).xzy;
//use overlay blending to mix normal maps properly
bvec3 computedN_select = lessThan(N1, vec3(0.5));
vec3 computedN_multiply = 2.f * N1 * N2;
vec3 computedN_screen = vec3(1.f) - 2.f * (vec3(1.f) - N1) * (vec3(1.f) - N2);
vec3 N = mix(computedN_screen, computedN_multiply, vec3(computedN_select));

N = mix(normalize(N * 2.f - 1.f), vec3(0.f,1.f,0.f), vec3(float(N == vec3(0.f,0.f,0.f))));

float lambertTerm = max(dot(N, lightDir), 0.0); // diffuse lighting

// Gaussian specular term computation
float gloss = texture(tex_sm, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS).r * texture(tex_sm, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS).r;
vec3 H = normalize(halfVec);
float exponent = acos(dot(H, N)) / (gloss + 0.05);
float gaussianTerm = exp(-(exponent * exponent));

vec4 fragColor = (texture(tex, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS)+texture(tex, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS)) * (gloss+vec4(0.08,0.13,0.15,1.0));
fragColor = fragColor*(ambientLight+diffuseLight*lambertTerm) + specularLight*(1.0-gloss)*gaussianTerm*vec4(1.0,0.843,0.686,1.0);
vec3 N1 = texture(tex_nm, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS).xzy*vec3(2.f, 2.f, 2.f) + vec3(-1.f, 0.f, -1.f); // y is up in modelSpace
vec3 N2 = texture(tex_nm, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS).xzy*vec3(-2.f, 2.f,-2.f) + vec3(1.f, -1.f, 1.f);

//use RNM blending to mix normal maps properly, see https://blog.selfshadow.com/publications/blending-in-detail/
vec3 N = normalize(N1 * dot(N1,N2) - N2*N1.y);

// Light
float noise = texture(tex, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS).r * texture(tex, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS).r;
float foam = texture(tex_sm, vec3(uv1, 0.f), WZ_MIP_LOAD_BIAS).r * texture(tex_sm, vec3(uv2, 1.f), WZ_MIP_LOAD_BIAS).r;

float lambertTerm = max(dot(N, lightDir), 0.0);
float blinnTerm = pow(max(dot(N, halfVec), 0.0), 32.f);

vec4 fragColor = vec4(0.16,0.26,0.3,1.0)+(noise+foam)*noise*0.5;
fragColor = fragColor*(ambientLight+diffuseLight*lambertTerm) + specularLight*blinnTerm*(foam*foam+noise);

vec4 lightmap_vec4 = texture(lightmap_tex, uvLightmap, 0.f);
vec4 color = fragColor * vec4(vec3(lightmap_vec4.a), 1.f); // ... * tile brightness / ambient occlusion (stored in lightmap.a);
color.rgb = blendAddEffectLighting(color.rgb, (lightmap_vec4.rgb / 1.5f)); // additive color (from environmental point lights / effects)
Expand All @@ -73,8 +69,8 @@ vec4 main_bumpMapping()
void main()
{
vec4 fragColor = main_bumpMapping();
fragColor = mix(fragColor, fragColor-depth*0.0007, depth*0.0009);
fragColor.a = mix(0.25, 1.0, depth2*0.005);
fragColor = mix(fragColor, fragColor-depth, depth);
fragColor.a = mix(0.25, 1.0, depth2);

if (fogEnabled > 0)
{
Expand Down
6 changes: 3 additions & 3 deletions data/base/shaders/vk/terrain_water_high.vert
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ layout(location = 7) out float depth2;
void main()
{
uvLightmap = (ModelUVLightmapMatrix * vec4(vertex.xyz, 1.f)).xy;
depth = vertex.w;
depth2 = length(vertex.y - vertex.w);
depth = vertex.w*0.0007;
depth2 = length(vertex.y - vertex.w)*0.005;

vec2 uv1 = vec2(vertex.x/4.f/128.f + timeSec/80.f, -vertex.z/4.f/128.f + timeSec/40.f); // (ModelUV1Matrix * vertex).xy;
vec2 uv2 = vec2(vertex.x/4.f/128.f, -vertex.z/4.f/128.f - timeSec/40.f); // (ModelUV2Matrix * vertex).xy;
uv1_uv2 = vec4(uv1.x, uv1.y, uv2.x, uv2.y);

vec3 eyeVec = normalize(cameraPos.xyz - vertex.xyz);
lightDir = sunPos.xyz;
halfVec = lightDir + eyeVec;
halfVec = normalize(lightDir + eyeVec);

vec4 position = ModelViewProjectionMatrix * vec4(vertex.xyz, 1.f);
vertexDistance = position.z;
Expand Down

0 comments on commit adcadc8

Please sign in to comment.