PlanetEditor/grk/cw 6/shaders/shader_tex.frag

158 lines
3.7 KiB
GLSL
Raw Normal View History

2023-12-18 16:15:08 +01:00
#version 430 core
2024-02-07 20:16:03 +01:00
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 BrightColor;
float AMBIENT = 0.05;
2024-02-04 13:31:51 +01:00
float PI = 3.14159;
uniform sampler2D colorTexture;
uniform sampler2D textureNormal;
2023-12-18 16:15:08 +01:00
2024-01-15 16:36:57 +01:00
uniform vec3 color;
2023-12-18 16:15:08 +01:00
uniform vec3 lightPos;
2024-01-15 16:36:57 +01:00
uniform vec3 lightColor;
2023-12-18 16:15:08 +01:00
uniform vec3 cameraPos;
uniform bool atmosphereCheck;
2024-02-08 02:16:04 +01:00
uniform bool toneMappingCheck;
uniform float time;
uniform float cloudIntensity;
uniform float cloudMotion;
uniform float cloudBrightness;
2024-02-04 13:31:51 +01:00
2023-12-18 16:15:08 +01:00
in vec3 vecNormal;
in vec3 worldPos;
2024-01-14 19:42:19 +01:00
in vec2 vtc;
2023-12-18 16:15:08 +01:00
in vec3 viewDirTS;
in vec3 lightDirTS;
in vec3 sunDirTS;
vec3 toneMapping(vec3 color)
{
2024-01-15 16:36:57 +01:00
float exposure = 0.06;
vec3 mapped = 1 - exp(-color * exposure);
2024-01-15 16:36:57 +01:00
return mapped;
}
2024-02-04 13:31:51 +01:00
float random (in vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
float noise (in vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
// Four corners in 2D of a tile
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
#define OCTAVES 6
float fbm (in vec2 st) {
// Initial values
float value = 0.0;
float amplitude = .5;
float frequency = 0.;
2024-02-08 11:11:26 +01:00
2024-02-04 13:31:51 +01:00
// Loop of octaves
for (int i = 0; i < OCTAVES; i++) {
value += amplitude * noise(st);
st *= 2.;
amplitude *= .5;
}
return value;
}
vec3 noiseColor() {
2024-02-04 13:31:51 +01:00
vec2 st = vtc.xy;
vec2 mirroredSt = vec2(1.0 - st.x, st.y);
float timeOffset = time * cloudMotion;
2024-02-04 13:31:51 +01:00
st.x -= timeOffset;
mirroredSt.x += timeOffset; // Inverse direction for mirrored effect
st.x = fract(st.x);
mirroredSt.x = fract(mirroredSt.x);
vec3 color = vec3(fbm(st * cloudIntensity));
vec3 mirroredColor = vec3(fbm(mirroredSt * cloudIntensity));
float blend = smoothstep(0.45, 0.55, st.x);
vec3 noiseColor = mix(color, mirroredColor, blend);
2024-02-04 13:31:51 +01:00
return noiseColor;
}
2023-12-18 16:15:08 +01:00
void main()
{
vec3 normal = normalize(vecNormal);
vec3 viewDir = normalize(cameraPos - worldPos);
vec3 lightDir = normalize(lightPos - worldPos);
2024-01-14 19:42:19 +01:00
float diffuse = max(0, dot(normal, lightDir));
vec3 textureColor = texture2D(colorTexture, vtc).rgb;
vec3 N = texture2D(textureNormal, vtc).rgb;
vec3 normalTexture = normalize((N * 2.0 - 1.0));
float diffuseNormal = max(0, dot(normalTexture, lightDir));
if (atmosphereCheck)
{
float atmosphereDot = dot(normal, viewDir);
vec3 atmosphereColor = vec3(0.0, 0.44, 1.0);
textureColor = mix(textureColor, atmosphereColor, pow(1 - atmosphereDot, 3));
}
2024-01-18 16:50:20 +01:00
vec3 diffuseColor = textureColor * min(1, AMBIENT + diffuse) * min(1, AMBIENT + diffuseNormal);
2024-02-08 02:16:04 +01:00
vec3 toneMappedColor;
2024-02-08 02:16:04 +01:00
if (toneMappingCheck)
{
vec3 distance = lightColor / pow(length(lightPos - worldPos), 2.0) * 1000;
toneMappedColor = toneMapping(diffuseColor * distance);
2024-02-08 11:11:26 +01:00
2024-02-08 02:16:04 +01:00
//gamma correction
toneMappedColor = pow(toneMappedColor, vec3(1.0/2.2));
}
else
toneMappedColor = diffuseColor;
2023-12-18 16:15:08 +01:00
2024-02-08 02:16:04 +01:00
vec3 finalColor;
2024-02-04 13:31:51 +01:00
if (atmosphereCheck)
{
vec3 noiseColor = noiseColor() * min(1, 20.0 * max(0.02, dot(normal, lightDir))) * lightColor * cloudBrightness;
finalColor = mix(toneMappedColor, noiseColor, noiseColor.r);
}
2024-02-08 02:16:04 +01:00
else
finalColor = toneMappedColor;
2024-02-07 20:16:03 +01:00
float brightness = dot(finalColor, vec3(0.2126, 0.7152, 0.0722));
if(brightness > 0.2)
BrightColor = vec4(finalColor, 1.0);
else
BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
2024-02-04 13:31:51 +01:00
outColor = vec4(finalColor, 1.0);
2024-01-15 16:36:57 +01:00
}