transfered from codeberg
This commit is contained in:
44
resources/shaders/fog.fs
Normal file
44
resources/shaders/fog.fs
Normal file
@@ -0,0 +1,44 @@
|
||||
#version 330
|
||||
|
||||
// Input vertex attributes
|
||||
in vec3 fragPosition;
|
||||
in vec2 fragTexCoord;
|
||||
in vec4 fragColor;
|
||||
in vec3 fragNormal;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
// Fog uniforms
|
||||
uniform vec3 viewPos;
|
||||
uniform float fogDensity;
|
||||
uniform float fogStart; // <--- New Uniform: Distance where fog begins
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// 1. Get the base color
|
||||
vec4 texelColor = texture(texture0, fragTexCoord);
|
||||
vec4 baseColor = texelColor * colDiffuse * fragColor;
|
||||
|
||||
// 2. Calculate distance from camera to fragment
|
||||
float dist = length(viewPos - fragPosition);
|
||||
|
||||
// 3. Calculate effective distance (distance minus the clear zone)
|
||||
// We use max() to ensure we don't get negative distance inside the fogStart radius
|
||||
float effectiveDist = max(0.0, dist - fogStart);
|
||||
|
||||
// 4. Calculate fog factor using Exponential Squared formula
|
||||
// We use effectiveDist so fog only begins accumulating after fogStart
|
||||
float fogFactor = exp(-pow(effectiveDist * fogDensity, 2.0));
|
||||
fogFactor = clamp(fogFactor, 0.0, 1.0);
|
||||
|
||||
// 5. Define fog color
|
||||
vec3 fogColor = vec3(0.47, 0.7, 0.70);
|
||||
|
||||
// 6. Final Mix
|
||||
finalColor = vec4(mix(fogColor, baseColor.rgb, fogFactor), baseColor.a);
|
||||
}
|
||||
29
resources/shaders/fog.vs
Normal file
29
resources/shaders/fog.vs
Normal file
@@ -0,0 +1,29 @@
|
||||
#version 330
|
||||
|
||||
// Input vertex attributes
|
||||
in vec3 vertexPosition;
|
||||
in vec2 vertexTexCoord;
|
||||
in vec3 vertexNormal;
|
||||
in vec4 vertexColor;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 mvp;
|
||||
uniform mat4 matModel;
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
out vec3 fragPosition;
|
||||
out vec2 fragTexCoord;
|
||||
out vec4 fragColor;
|
||||
out vec3 fragNormal;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate fragment position in world space
|
||||
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
|
||||
fragTexCoord = vertexTexCoord;
|
||||
fragColor = vertexColor;
|
||||
fragNormal = normalize(vec3(matModel * vec4(vertexNormal, 0.0)));
|
||||
// 1. Get the base color of the object
|
||||
// Calculate final vertex position
|
||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
||||
}
|
||||
Reference in New Issue
Block a user