gpu plugin: add fog

Co-authored-by: logarithm <allan.palmu@gmail.com>
This commit is contained in:
Adam
2019-01-07 13:40:37 -05:00
parent a342a1f220
commit 85460f16c3
5 changed files with 72 additions and 4 deletions

View File

@@ -28,10 +28,12 @@ uniform sampler2DArray textures;
uniform vec2 textureOffsets[64];
uniform float brightness;
uniform float smoothBanding;
uniform vec4 fogColor;
in vec4 Color;
in float fHsl;
in vec4 fUv;
in float fogAmount;
out vec4 FragColor;
@@ -54,8 +56,9 @@ void main() {
vec4 textureColor = texture(textures, vec3(animatedUv, n));
vec4 textureColorBrightness = pow(textureColor, vec4(brightness, brightness, brightness, 1.0f));
FragColor = textureColorBrightness * smoothColor;
} else {
FragColor = smoothColor;
smoothColor = textureColorBrightness * smoothColor;
}
vec3 mixedColor = mix(smoothColor.rgb, fogColor.rgb, fogAmount);
FragColor = vec4(mixedColor, smoothColor.a);
}

View File

@@ -49,10 +49,12 @@ in ivec3 vPosition[];
in vec4 vColor[];
in float vHsl[];
in vec4 vUv[];
in float vFogAmount[];
out vec4 Color;
out float fHsl;
out vec4 fUv;
out float fogAmount;
#include to_screen.glsl
@@ -71,6 +73,7 @@ void main() {
Color = vColor[0];
fHsl = vHsl[0];
fUv = vUv[0];
fogAmount = vFogAmount[0];
gl_Position = projectionMatrix * tmp;
EmitVertex();
@@ -78,6 +81,7 @@ void main() {
Color = vColor[1];
fHsl = vHsl[1];
fUv = vUv[1];
fogAmount = vFogAmount[1];
gl_Position = projectionMatrix * tmp;
EmitVertex();
@@ -85,6 +89,7 @@ void main() {
Color = vColor[2];
fHsl = vHsl[2];
fUv = vUv[2];
fogAmount = vFogAmount[2];
gl_Position = projectionMatrix * tmp;
EmitVertex();

View File

@@ -25,18 +25,43 @@
#version 330
#define TILE_SIZE 128
#define FOG_SCENE_EDGE_MIN TILE_SIZE
#define FOG_SCENE_EDGE_MAX (103 * TILE_SIZE)
layout (location = 0) in ivec4 VertexPosition;
layout (location = 1) in vec4 uv;
layout(std140) uniform uniforms {
int cameraYaw;
int cameraPitch;
int centerX;
int centerY;
int zoom;
int cameraX;
int cameraY;
int cameraZ;
ivec2 sinCosTable[2048];
};
uniform float brightness;
uniform int useFog;
uniform int fogDepth;
uniform int drawDistance;
out ivec3 vPosition;
out vec4 vColor;
out float vHsl;
out vec4 vUv;
out float vFogAmount;
#include hsl_to_rgb.glsl
float fogFactorLinear(const float dist, const float start, const float end) {
return 1.0 - clamp((dist - start) / (end - start), 0.0, 1.0);
}
void main()
{
ivec3 vertex = VertexPosition.xyz;
@@ -50,4 +75,14 @@ void main()
vColor = vec4(rgb, 1.f - a);
vHsl = float(hsl);
vUv = uv;
int fogWest = max(FOG_SCENE_EDGE_MIN, cameraX - drawDistance);
int fogEast = min(FOG_SCENE_EDGE_MAX, cameraX + drawDistance - TILE_SIZE);
int fogSouth = max(FOG_SCENE_EDGE_MIN, cameraZ - drawDistance);
int fogNorth = min(FOG_SCENE_EDGE_MAX, cameraZ + drawDistance - TILE_SIZE);
// Calculate distance from the scene edge
float fogDistance = min(min(vertex.x - fogWest, fogEast - vertex.x), min(vertex.z - fogSouth, fogNorth - vertex.z));
vFogAmount = fogFactorLinear(fogDistance, 0, fogDepth * TILE_SIZE) * useFog;
}