1
0
forked from 0ad/0ad

Removes shader code duplication to calculate LOS.

Tested By: Stan
Differential Revision: https://code.wildfiregames.com/D3428
This was SVN commit r24732.
This commit is contained in:
Vladislav Belov 2021-01-20 18:46:32 +00:00
parent 71eaeb853c
commit 9df127f9d1
20 changed files with 102 additions and 130 deletions

View File

@ -0,0 +1,21 @@
#ifndef INCLUDED_LOS_FRAGMENT
#define INCLUDED_LOS_FRAGMENT
#if !IGNORE_LOS
uniform sampler2D losTex;
varying vec2 v_los;
#endif
float getLOS()
{
#if !IGNORE_LOS
float los = texture2D(losTex, v_los).a;
float threshold = 0.03;
return (los - threshold) / (1.0 - threshold);
#else
return 1.0;
#endif
}
#endif // INCLUDED_LOS_FRAGMENT

View File

@ -0,0 +1,17 @@
#ifndef INCLUDED_LOS_VERTEX
#define INCLUDED_LOS_VERTEX
#if !IGNORE_LOS
uniform vec2 losTransform;
varying vec2 v_los;
#endif
void calculateLOSCoordinates(vec2 position)
{
#if !IGNORE_LOS
v_los = position * losTransform.x + losTransform.y;
#endif
}
#endif // INCLUDED_LOS_VERTEX

View File

@ -1,10 +1,10 @@
#version 120
#include "common/fog.h"
#include "common/los_fragment.h"
#include "common/shadows_fragment.h"
uniform sampler2D baseTex;
uniform sampler2D losTex;
uniform sampler2D aoTex;
uniform sampler2D normTex;
uniform sampler2D specTex;
@ -24,7 +24,6 @@ uniform vec3 sunDir;
varying vec4 v_lighting;
varying vec2 v_tex;
varying vec2 v_los;
#if (USE_INSTANCING || USE_GPU_SKINNING) && USE_AO
varying vec2 v_tex2;
@ -171,11 +170,7 @@ void main()
color = applyFog(color);
#if !IGNORE_LOS
float los = texture2D(losTex, v_los).a;
los = los < 0.03 ? 0.0 : los;
color *= los;
#endif
color *= getLOS();
color *= shadingColor;

View File

@ -1,5 +1,6 @@
#version 120
#include "common/los_vertex.h"
#include "common/shadows_vertex.h"
uniform mat4 transform;
@ -11,7 +12,6 @@ uniform mediump vec3 sunColor;
uniform vec3 sunDir;
uniform vec3 sunColor;
#endif
uniform vec2 losTransform;
uniform mat4 instancingTransform;
#if USE_WIND
@ -21,7 +21,6 @@ uniform mat4 instancingTransform;
varying vec4 v_lighting;
varying vec2 v_tex;
varying vec2 v_los;
#if (USE_INSTANCING || USE_GPU_SKINNING) && USE_AO
varying vec2 v_tex2;
@ -61,7 +60,7 @@ attribute vec2 a_uv1;
vec4 fakeCos(vec4 x)
{
vec4 tri = abs(fract(x + 0.5) * 2.0 - 1.0);
return tri * tri *(3.0 - 2.0 * tri);
return tri * tri *(3.0 - 2.0 * tri);
}
@ -122,7 +121,7 @@ void main()
float limit = clamp((a_vertex.x * a_vertex.z * a_vertex.y) / 3000.0, 0.0, 0.2);
float diff = cosVec.x * limit;
float diff = cosVec.x * limit;
float diff2 = cosVec.y * clamp(a_vertex.y / 60.0, 0.0, 0.25);
// fluttering of model parts based on distance from model center (ie longer branches)
@ -148,7 +147,7 @@ void main()
#if USE_SPECULAR || USE_SPECULAR_MAP || USE_PARALLAX
vec3 eyeVec = cameraPos.xyz - position.xyz;
#if USE_SPECULAR || USE_SPECULAR_MAP
#if USE_SPECULAR || USE_SPECULAR_MAP
vec3 sunVec = -sunDir;
v_half = normalize(sunVec + normalize(eyeVec));
#endif
@ -157,7 +156,7 @@ void main()
#endif
#endif
#endif
v_lighting.xyz = max(0.0, dot(normal, -sunDir)) * sunColor;
v_tex = a_uv0;
@ -168,5 +167,5 @@ void main()
calculatePositionInShadowSpace(position);
v_los = position.xz * losTransform.x + losTransform.y;
calculateLOSCoordinates(position.xz);
}

View File

@ -1,9 +1,9 @@
#version 120
#include "common/los_fragment.h"
#include "common/shadows_fragment.h"
uniform sampler2D baseTex;
uniform sampler2D losTex;
uniform sampler2D aoTex;
uniform sampler2D normTex;
uniform sampler2D specTex;
@ -32,12 +32,11 @@ uniform float murkiness;
uniform vec3 reflectionTint;
uniform float reflectionTintStrength;
float waterDepth = 4.0;
float waterDepth = 4.0;
float fullDepth = 5.0; // Depth at which to use full murkiness (shallower water will be clearer)
varying vec4 worldPos;
varying vec4 v_tex;
varying vec2 v_los;
void main()
{
@ -47,7 +46,6 @@ void main()
float t; // Temporary variable
vec2 reflCoords, refrCoords;
vec3 reflColor, refrColor, specular;
float losMod;
//vec4 wtex = textureGrad(waterTex, vec3(fract(v_tex.xy), v_tex.z), dFdx(v_tex.xy), dFdy(v_tex.xy));
vec4 wtex = texture2D(waterTex, fract(v_tex.xy));
@ -56,35 +54,33 @@ void main()
l = -sunDir;
v = normalize(cameraPos - worldPos.xyz);
h = normalize(l + v);
ndotl = dot(n, l);
ndoth = dot(n, h);
ndotv = dot(n, v);
fresnel = pow(1.0 - ndotv, 0.8); // A rather random Fresnel approximation
//refrCoords = (0.5*gl_TexCoord[2].xy - 0.8*waviness*n.xz) / gl_TexCoord[2].w + 0.5; // Unbias texture coords
//reflCoords = (0.5*gl_TexCoord[1].xy + waviness*n.xz) / gl_TexCoord[1].w + 0.5; // Unbias texture coords
//vec3 dir = normalize(v + vec3(waviness*n.x, 0.0, waviness*n.z));
vec3 eye = reflect(v, n);
vec3 tex = textureCube(skyCube, eye).rgb;
reflColor = mix(tex, sunColor * reflectionTint,
reflColor = mix(tex, sunColor * reflectionTint,
reflectionTintStrength);
//waterDepth = 4.0 + 2.0 * dot(abs(v_tex.zw - 0.5), vec2(0.5));
waterDepth = 4.0;
//refrColor = (0.5 + 0.5*ndotl) * mix(texture2D(refractionMap, refrCoords).rgb, sunColor * tint,
refrColor = (0.5 + 0.5*ndotl) * mix(vec3(0.3), sunColor * waterTint,
murkiness * clamp(waterDepth / fullDepth, 0.0, 1.0)); // Murkiness and tint at this pixel (tweaked based on lighting and depth)
specular = pow(max(0.0, ndoth), 150.0f) * sunColor * specularStrength;
losMod = texture2D(losTex, v_los).a;
specular = pow(max(0.0, ndoth), 150.0f) * sunColor * specularStrength;
#if USE_SHADOW
float shadow = get_shadow();
@ -92,17 +88,13 @@ void main()
#else
float fresShadow = fresnel;
#endif
vec3 color = mix(refrColor + 0.3*specular, reflColor + specular, fresShadow);
gl_FragColor.rgb = color * losMod;
gl_FragColor.rgb = color * getLOS();
//gl_FragColor.rgb = mix(refrColor + 0.3*specular, reflColor + specular, fresnel) * losMod;
// Make alpha vary based on both depth (so it blends with the shore) and view angle (make it
// become opaque faster at lower view angles so we can't look "underneath" the water plane)
t = 18.0 * max(0.0, 0.7 - v.y);
gl_FragColor.a = 0.15 * waterDepth * (1.2 + t + fresnel);
}

View File

@ -5,13 +5,13 @@
#version 120
#endif
#include "common/los_vertex.h"
#include "common/shadows_vertex.h"
uniform mat4 transform;
uniform vec3 cameraPos;
uniform vec3 sunDir;
uniform vec3 sunColor;
uniform vec2 losTransform;
uniform mat4 instancingTransform;
uniform float sim_time;
@ -35,25 +35,24 @@ attribute vec2 a_uv1;
varying vec4 worldPos;
varying vec4 v_tex;
varying vec2 v_los;
vec4 fakeCos(vec4 x)
{
vec4 tri = abs(fract(x + 0.5) * 2.0 - 1.0);
return tri * tri *(3.0 - 2.0 * tri);
return tri * tri *(3.0 - 2.0 * tri);
}
void main()
{
worldPos = instancingTransform * vec4(a_vertex, 1.0);
v_tex.xy = (a_uv0 + worldPos.xz) / 5.0 + sim_time * translation;
v_tex.zw = a_uv0;
calculatePositionInShadowSpace(worldPos);
v_los = worldPos.xz * losTransform.x + losTransform.y;
calculateLOSCoordinates(worldPos.xz);
gl_Position = transform * worldPos;
}

View File

@ -1,9 +1,9 @@
#version 120
#include "common/los_fragment.h"
#include "common/shadows_fragment.h"
uniform sampler2D baseTex;
uniform sampler2D losTex;
uniform vec3 shadingColor;
uniform vec3 ambient;
@ -15,7 +15,6 @@ uniform float specularPower;
uniform vec3 specularColor;
varying vec4 v_tex;
varying vec2 v_los;
varying vec3 v_half;
varying vec3 v_normal;
varying float v_transp;
@ -36,11 +35,7 @@ void main()
vec3 color = (texdiffuse.rgb * v_lighting + specular) * get_shadow();
color += texdiffuse.rgb * ambient;
#if !IGNORE_LOS
float los = texture2D(losTex, v_los).a;
los = los < 0.03 ? 0.0 : los;
color *= los;
#endif
color *= getLOS();
gl_FragColor.rgb = color;
gl_FragColor.a = texdiffuse.a;

View File

@ -5,13 +5,13 @@
#version 120
#endif
#include "common/los_vertex.h"
#include "common/shadows_vertex.h"
uniform mat4 transform;
uniform vec3 cameraPos;
uniform vec3 sunDir;
uniform vec3 sunColor;
uniform vec2 losTransform;
uniform mat4 instancingTransform;
uniform float sim_time;
@ -24,7 +24,6 @@ attribute vec2 a_uv1;
varying vec4 worldPos;
varying vec4 v_tex;
varying vec2 v_los;
varying vec3 v_half;
varying vec3 v_normal;
varying float v_transp;
@ -33,17 +32,17 @@ varying vec3 v_lighting;
void main()
{
worldPos = instancingTransform * vec4(a_vertex, 1.0);
v_tex.xy = a_uv0 + sim_time * translation;
v_transp = a_uv1.x;
calculatePositionInShadowSpace(worldPos);
v_los = worldPos.xz * losTransform.x + losTransform.y;
calculateLOSCoordinates(worldPos.xz);
vec3 eyeVec = cameraPos.xyz - worldPos.xyz;
vec3 sunVec = -sunDir;
v_half = normalize(sunVec + normalize(eyeVec));
vec3 sunVec = -sunDir;
v_half = normalize(sunVec + normalize(eyeVec));
mat3 normalMatrix = mat3(instancingTransform[0].xyz, instancingTransform[1].xyz, instancingTransform[2].xyz);
v_normal = normalMatrix * a_normal;
@ -51,4 +50,3 @@ void main()
gl_Position = transform * worldPos;
}

View File

@ -1,8 +1,9 @@
#version 120
#include "common/los_fragment.h"
uniform sampler2D baseTex;
uniform sampler2D maskTex;
uniform sampler2D losTex;
#if USE_OBJECTCOLOR
uniform vec4 objectColor;
@ -12,10 +13,6 @@ varying vec4 v_color;
varying vec2 v_tex;
#if !IGNORE_LOS
varying vec2 v_los;
#endif
void main()
{
#if USE_OBJECTCOLOR
@ -30,11 +27,7 @@ void main()
vec4 mask = texture2D(maskTex, v_tex);
color = mix(base.rgb, color, mask.r);
#if !IGNORE_LOS
float los = texture2D(losTex, v_los).a;
los = los < 0.03 ? 0.0 : los;
color *= los;
#endif
color *= getLOS();
gl_FragColor = vec4(color, alpha * base.a);
}

View File

@ -1,8 +1,6 @@
#version 120
#if !IGNORE_LOS
uniform vec2 losTransform;
#endif
#include "common/los_vertex.h"
attribute vec3 a_vertex;
attribute vec2 a_uv0;
@ -14,16 +12,10 @@ varying vec4 v_color;
varying vec2 v_tex;
#if !IGNORE_LOS
varying vec2 v_los;
#endif
void main()
{
v_tex = a_uv0;
#if !IGNORE_LOS
v_los = a_vertex.xz * losTransform.x + losTransform.yy;
#endif
calculateLOSCoordinates(a_vertex.xz);
#if !USE_OBJECTCOLOR
v_color = a_color;
#endif

View File

@ -1,12 +1,11 @@
#version 110
#include "common/fog.h"
#include "common/los_fragment.h"
uniform sampler2D baseTex;
uniform sampler2D losTex;
varying vec2 v_tex;
varying vec2 v_los;
varying vec4 v_color;
uniform vec3 sunColor;
@ -14,12 +13,9 @@ uniform vec3 sunColor;
void main()
{
vec4 color = texture2D(baseTex, v_tex) * vec4((v_color.rgb + sunColor)/2.0,v_color.a);
float los = texture2D(losTex, v_los).a;
los = los < 0.03 ? 0.0 : los;
color.rgb *= los;
color.rgb = applyFog(color.rgb);
color.rgb *= getLOS();
gl_FragColor = color;
}

View File

@ -1,11 +1,11 @@
#version 110
#include "common/los_vertex.h"
uniform mat4 transform;
uniform mat4 modelViewMatrix;
uniform vec2 losTransform;
varying vec2 v_tex;
varying vec2 v_los;
varying vec4 v_color;
attribute vec3 a_vertex;
@ -20,10 +20,11 @@ void main()
vec2 offset = a_uv1;
vec3 position = axis1*offset.x + axis1*offset.y + axis2*offset.x + axis2*-offset.y + a_vertex;
gl_Position = transform * vec4(position, 1.0);
v_los = position.xz * losTransform.x + losTransform.y;
calculateLOSCoordinates(position.xz);
v_tex = a_uv0;
v_color = a_color;
}

View File

@ -1,11 +1,11 @@
#version 120
#include "common/fog.h"
#include "common/los_fragment.h"
#include "common/shadows_fragment.h"
uniform sampler2D baseTex;
uniform sampler2D blendTex;
uniform sampler2D losTex;
uniform sampler2D normTex;
uniform sampler2D specTex;
@ -18,7 +18,6 @@ uniform vec2 textureTransform;
varying vec3 v_lighting;
varying vec2 v_los;
varying vec2 v_blend;
#if USE_TRIPLANAR
@ -178,9 +177,7 @@ void main()
color = applyFog(color);
float los = texture2D(losTex, v_los).a;
los = los < 0.03 ? 0.0 : los;
color *= los;
color *= getLOS();
#if DECAL
color *= shadingColor;

View File

@ -1,5 +1,6 @@
#version 120
#include "common/los_vertex.h"
#include "common/shadows_vertex.h"
uniform mat4 transform;
@ -12,11 +13,9 @@ uniform vec3 sunDir;
uniform vec3 sunColor;
#endif
uniform vec2 textureTransform;
uniform vec2 losTransform;
varying vec3 v_lighting;
varying vec2 v_los;
varying vec2 v_blend;
#if USE_TRIPLANAR
@ -102,5 +101,5 @@ void main()
#endif
#endif
v_los = a_vertex.xz * losTransform.x + losTransform.yy;
calculateLOSCoordinates(a_vertex.xz);
}

View File

@ -1,7 +1,7 @@
#version 110
#include "common/fog.h"
#include "common/los_fragment.h"
#include "common/shadows_fragment.h"
// Environment settings
@ -12,8 +12,6 @@ uniform mat4 skyBoxRot;
uniform vec3 cameraPos;
uniform sampler2D losTex;
uniform float waviness; // "Wildness" of the reflections and refractions; choose based on texture
uniform vec3 color; // color of the water
uniform vec3 tint; // Tint for refraction (used to simulate particles in water)
@ -38,7 +36,6 @@ varying vec3 reflectionCoords;
#if USE_REFRACTION
varying vec3 refractionCoords;
#endif
varying vec2 losCoords;
varying float fwaviness;
@ -67,20 +64,6 @@ uniform vec4 waveParams2; // Smallintensity, Smallbase, Bigmovement, Smallmoveme
#endif
#endif
// TODO: convert this to something not only for AABBs
struct Ray {
vec3 Origin;
vec3 Direction;
};
float IntersectBox (in Ray ray, in vec3 minimum, in vec3 maximum)
{
vec3 OMIN = ( minimum - ray.Origin ) / ray.Direction;
vec3 OMAX = ( maximum - ray.Origin ) / ray.Direction;
vec3 MAX = max ( OMAX, OMIN );
return min ( MAX.x, min ( MAX.y, MAX.z ) );
}
vec3 getNormal(vec4 fancyeffects)
{
float wavyEffect = waveParams1.r;
@ -314,9 +297,11 @@ void main()
#if USE_SHADOW
float shadow = get_shadow();
float fresShadow = mix(fresnel, fresnel * shadow, 0.05 + murkiness * 0.2);
float fresShadow = mix(fresnel, fresnel * shadow, 0.05 + 10.0 * murkiness * 0.2);
fresShadow = fresnel;
vec3 color = mix(refrColor.rgb, reflColor.rgb, fresShadow * reflColor.a);
color += shadow * specular;
//color = vec3(fresShadow);
vec4 foam = getFoam(fancyeffects, shadow);
#else
vec3 color = mix(refrColor.rgb, reflColor.rgb, fresnel * reflColor.a);
@ -328,8 +313,5 @@ void main()
color = applyFog(color);
float alpha = refrColor.a;
float losMod = texture2D(losTex, losCoords.st).a;
losMod = losMod < 0.03 ? 0.0 : losMod;
gl_FragColor = vec4(color * losMod, alpha);
gl_FragColor = vec4(color * getLOS(), refrColor.a);
}

View File

@ -1,10 +1,10 @@
#version 110
#include "common/los_vertex.h"
#include "common/shadows_vertex.h"
uniform mat4 reflectionMatrix;
uniform mat4 refractionMatrix;
uniform mat4 losMatrix;
uniform float repeatScale;
uniform float windAngle;
// "Wildness" of the reflections and refractions; choose based on texture
@ -31,7 +31,6 @@ varying vec3 reflectionCoords;
#if USE_REFRACTION
varying vec3 refractionCoords;
#endif
varying vec2 losCoords;
varying float fwaviness;
varying vec2 WindCosSin;
@ -60,7 +59,7 @@ void main()
#if USE_REFRACTION
refractionCoords = (refractionMatrix * vec4(a_vertex, 1.0)).rga;
#endif
losCoords = (losMatrix * vec4(a_vertex, 1.0)).rg;
calculateLOSCoordinates(a_vertex.xz);
calculatePositionInShadowSpace(vec4(a_vertex, 1.0));

View File

@ -1,15 +1,13 @@
#version 110
#include "common/los_fragment.h"
uniform sampler2D baseTex;
uniform sampler2D losTex;
uniform vec3 color;
varying vec2 v_coords;
varying vec2 v_losCoords;
void main()
{
float losMod = texture2D(losTex, v_losCoords.st).a;
losMod = losMod < 0.03 ? 0.0 : losMod;
gl_FragColor = vec4(texture2D(baseTex, v_coords).rgb * color * losMod, 1.0);
gl_FragColor = vec4(texture2D(baseTex, v_coords).rgb * color * getLOS(), 1.0);
}

View File

@ -1,13 +1,13 @@
#version 110
#include "common/los_vertex.h"
attribute vec3 a_vertex;
uniform mat4 transform;
uniform mat4 losMatrix;
uniform float time;
varying vec2 v_coords;
varying vec2 v_losCoords;
void main()
{
@ -17,6 +17,6 @@ void main()
float repeatPeriod = 16.0;
v_coords = a_vertex.xz / repeatPeriod + vec2(tx, tz);
v_losCoords = (losMatrix * vec4(a_vertex, 1.0)).rg;
calculateLOSCoordinates(a_vertex.xz);
gl_Position = transform * vec4(a_vertex, 1.0);
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -102,7 +102,6 @@ X(gui_text)
X(hdr)
X(height)
X(instancingTransform)
X(losMatrix)
X(losTex)
X(losTex1)
X(losTex2)

View File

@ -567,7 +567,7 @@ bool TerrainRenderer::RenderFancyWater(const CShaderDefines& context, int cullGr
m->fancyWaterShader->Uniform(str_murkiness, WaterMgr->m_Murkiness);
m->fancyWaterShader->Uniform(str_windAngle, WaterMgr->m_WindAngle);
m->fancyWaterShader->Uniform(str_repeatScale, 1.0f / repeatPeriod);
m->fancyWaterShader->Uniform(str_losMatrix, losTexture.GetTextureMatrix());
m->fancyWaterShader->Uniform(str_losTransform, losTexture.GetTextureMatrix()[0], losTexture.GetTextureMatrix()[12], 0.f, 0.f);
m->fancyWaterShader->Uniform(str_cameraPos, camera.GetOrientation().GetTranslation());
@ -635,7 +635,7 @@ void TerrainRenderer::RenderSimpleWater(int cullGroup)
waterSimpleShader->BindTexture(str_baseTex, WaterMgr->m_WaterTexture[curTex]);
waterSimpleShader->BindTexture(str_losTex, losTexture.GetTextureSmooth());
waterSimpleShader->Uniform(str_transform, g_Renderer.GetViewCamera().GetViewProjection());
waterSimpleShader->Uniform(str_losMatrix, losTexture.GetTextureMatrix());
waterSimpleShader->Uniform(str_losTransform, losTexture.GetTextureMatrix()[0], losTexture.GetTextureMatrix()[12], 0.f, 0.f);
waterSimpleShader->Uniform(str_time, static_cast<float>(time));
waterSimpleShader->Uniform(str_color, WaterMgr->m_WaterColor);