2024-01-26 19:41:55 +01:00
|
|
|
|
#version 430 core
|
|
|
|
|
|
2024-01-26 22:40:06 +01:00
|
|
|
|
float AMBIENT = 0.25f;
|
2024-01-26 19:41:55 +01:00
|
|
|
|
float PI = 3.14159f;
|
|
|
|
|
|
|
|
|
|
uniform sampler2D depthMap;
|
|
|
|
|
|
|
|
|
|
uniform vec3 cameraPos;
|
|
|
|
|
|
2024-02-02 19:56:51 +01:00
|
|
|
|
uniform sampler2D colorTexture; //planet texture
|
2024-01-26 19:41:55 +01:00
|
|
|
|
|
|
|
|
|
uniform vec3 sunDir;
|
|
|
|
|
uniform vec3 sunColor;
|
|
|
|
|
|
|
|
|
|
uniform vec3 lightPos;
|
|
|
|
|
uniform vec3 lightColor;
|
|
|
|
|
|
2024-02-02 19:56:51 +01:00
|
|
|
|
uniform float metallic; //pbr
|
|
|
|
|
uniform float roughness; //pbr
|
|
|
|
|
uniform float exposition; //pbr
|
2024-01-26 19:41:55 +01:00
|
|
|
|
|
2024-02-02 19:56:51 +01:00
|
|
|
|
uniform float u_time;
|
2024-01-26 19:41:55 +01:00
|
|
|
|
|
|
|
|
|
in vec3 vecNormal;
|
|
|
|
|
in vec3 worldPos;
|
|
|
|
|
in vec2 vtc;
|
|
|
|
|
|
|
|
|
|
out vec4 outColor;
|
|
|
|
|
|
|
|
|
|
in vec3 viewDirTS;
|
|
|
|
|
in vec3 lightDirTS;
|
|
|
|
|
in vec3 sunDirTS;
|
|
|
|
|
|
2024-02-02 19:56:51 +01:00
|
|
|
|
vec4 textureColor;
|
|
|
|
|
|
2024-01-26 19:41:55 +01:00
|
|
|
|
float DistributionGGX(vec3 normal, vec3 H, float roughness)
|
|
|
|
|
{
|
|
|
|
|
float a = roughness * roughness;
|
|
|
|
|
float a2 = a * a;
|
|
|
|
|
float NdotH = max(dot(normal, H), 0.0);
|
|
|
|
|
float NdotH2 = NdotH * NdotH;
|
|
|
|
|
|
|
|
|
|
float num = a2;
|
|
|
|
|
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
|
|
|
|
denom = PI * denom * denom;
|
|
|
|
|
|
|
|
|
|
return num / denom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float GeometrySchlickGGX(float NdotV, float roughness)
|
|
|
|
|
{
|
|
|
|
|
float r = (roughness + 1.0);
|
|
|
|
|
float k = (r * r) / 8.0;
|
|
|
|
|
|
2024-01-26 22:40:06 +01:00
|
|
|
|
float num = NdotV;
|
2024-01-26 19:41:55 +01:00
|
|
|
|
float denom = NdotV * (1.0 - k) + k;
|
|
|
|
|
|
|
|
|
|
return num / denom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float GeometrySmith(vec3 normal, vec3 V, vec3 lightDir, float roughness)
|
|
|
|
|
{
|
|
|
|
|
float NdotV = max(dot(normal, V), 0.0);
|
|
|
|
|
float NdotL = max(dot(normal, lightDir), 0.0);
|
2024-01-26 22:40:06 +01:00
|
|
|
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
|
|
|
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
2024-01-26 19:41:55 +01:00
|
|
|
|
|
|
|
|
|
return ggx1 * ggx2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec3 fresnelSchlick(float cosTheta, vec3 F0)
|
|
|
|
|
{
|
|
|
|
|
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V, vec3 color)
|
|
|
|
|
{
|
|
|
|
|
float diffuse = max(0, dot(normal, lightDir));
|
|
|
|
|
|
|
|
|
|
vec3 F0 = vec3(0.04);
|
|
|
|
|
F0 = mix(F0, color, metallic);
|
|
|
|
|
|
|
|
|
|
vec3 H = normalize(V + lightDir);
|
|
|
|
|
|
|
|
|
|
// cook-torrance brdf
|
|
|
|
|
float NDF = DistributionGGX(normal, H, roughness);
|
2024-01-26 22:40:06 +01:00
|
|
|
|
float G = GeometrySmith(normal, V, lightDir, roughness);
|
|
|
|
|
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
|
2024-01-26 19:41:55 +01:00
|
|
|
|
|
|
|
|
|
vec3 kS = F;
|
|
|
|
|
vec3 kD = vec3(1.0) - kS;
|
|
|
|
|
kD *= 1.0 - metallic;
|
|
|
|
|
|
2024-01-26 22:40:06 +01:00
|
|
|
|
vec3 numerator = NDF * G * F;
|
2024-01-26 19:41:55 +01:00
|
|
|
|
float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, lightDir), 0.0) + 0.0001;
|
2024-01-26 22:40:06 +01:00
|
|
|
|
vec3 specular = numerator / denominator;
|
2024-01-26 19:41:55 +01:00
|
|
|
|
|
|
|
|
|
// add to outgoing radiance Lo
|
|
|
|
|
float NdotL = max(dot(normal, lightDir), 0.0);
|
|
|
|
|
return (kD * color / PI + specular) * radiance * NdotL;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 22:40:06 +01:00
|
|
|
|
vec3 toneMapping(vec3 color)
|
|
|
|
|
{
|
|
|
|
|
float exposure = 0.03;
|
|
|
|
|
vec3 mapped = 1 - exp(-color * exposure);
|
|
|
|
|
return mapped;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 19:56:51 +01:00
|
|
|
|
float random (in vec2 st) {
|
|
|
|
|
return fract(sin(dot(st.xy,
|
|
|
|
|
vec2(12.9898,78.233)))*
|
|
|
|
|
43758.5453123);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Based on Morgan McGuire @morgan3d
|
|
|
|
|
// https://www.shadertoy.com/view/4dS3Wd
|
|
|
|
|
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.;
|
|
|
|
|
//
|
|
|
|
|
// Loop of octaves
|
|
|
|
|
for (int i = 0; i < OCTAVES; i++) {
|
|
|
|
|
value += amplitude * noise(st);
|
|
|
|
|
st *= 2.;
|
|
|
|
|
amplitude *= .5;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec4 noiseColor() {
|
|
|
|
|
vec2 st = vtc;
|
|
|
|
|
float cloudIntensity = 15.0;
|
|
|
|
|
|
|
|
|
|
// Efekt lustrzanego odbicia
|
|
|
|
|
vec2 mirroredSt = vec2(1.0 - st.x, st.y);
|
|
|
|
|
vec3 mirroredColor = vec3(0.0);
|
|
|
|
|
mirroredColor += fbm(mirroredSt * cloudIntensity);
|
|
|
|
|
|
|
|
|
|
// Efekt oryginalny
|
|
|
|
|
vec3 color = vec3(0.0);
|
|
|
|
|
color += fbm(st * cloudIntensity);
|
|
|
|
|
|
|
|
|
|
// Dodaj gradientowe mieszanie mi<6D>dzy orygina<6E>em a lustrzanym odbiciem
|
|
|
|
|
float gradient = smoothstep(0.45, 0.55, st.x);
|
|
|
|
|
vec3 finalColor = mix(color, mirroredColor, gradient);
|
|
|
|
|
|
|
|
|
|
vec4 noiseColor = vec4(finalColor, 1.0);
|
|
|
|
|
|
|
|
|
|
return noiseColor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void main() {
|
2024-01-26 19:41:55 +01:00
|
|
|
|
vec3 normal = normalize(vecNormal);
|
|
|
|
|
|
|
|
|
|
vec3 viewDir = normalize(cameraPos - worldPos);
|
|
|
|
|
|
|
|
|
|
vec3 lightDir = normalize(lightPos - worldPos);
|
|
|
|
|
|
|
|
|
|
vec4 textureColor = texture2D(colorTexture, vtc);
|
|
|
|
|
|
2024-01-26 22:40:06 +01:00
|
|
|
|
float diffuse = max(0, dot(normal, lightDir));
|
|
|
|
|
vec3 distance = lightColor / pow(length(lightPos - worldPos), 2.0) * 300;
|
|
|
|
|
vec3 toneMappedColor = toneMapping(vec3(textureColor) * min(1, AMBIENT + diffuse) * distance);
|
|
|
|
|
//gamma correction
|
|
|
|
|
//toneMappedColor = pow(toneMappedColor, vec3(1.0/2.2));
|
|
|
|
|
|
|
|
|
|
vec3 ambient = AMBIENT * toneMappedColor;
|
2024-01-26 19:41:55 +01:00
|
|
|
|
vec3 attenuatedLightColor = lightColor / pow(length(lightPos - worldPos), 2);
|
|
|
|
|
vec3 illumination;
|
2024-01-26 22:40:06 +01:00
|
|
|
|
illumination = ambient + PBRLight(lightDir, attenuatedLightColor, normal, viewDir, toneMappedColor);
|
2024-01-26 19:41:55 +01:00
|
|
|
|
|
|
|
|
|
//sun
|
2024-01-26 22:40:06 +01:00
|
|
|
|
illumination = illumination + PBRLight(sunDir, sunColor, normal, viewDir, toneMappedColor);
|
2024-01-26 19:41:55 +01:00
|
|
|
|
|
2024-02-02 19:56:51 +01:00
|
|
|
|
textureColor = vec4(vec3(1.0) - exp(-illumination * exposition), 1);
|
|
|
|
|
|
|
|
|
|
vec4 noiseColor = noiseColor();
|
|
|
|
|
|
|
|
|
|
vec3 mixedColor = mix(textureColor.rgb, noiseColor.rgb, noiseColor.r);
|
|
|
|
|
|
|
|
|
|
outColor = vec4(mixedColor * min(1, AMBIENT + diffuse), 1.0);
|
2024-01-26 19:41:55 +01:00
|
|
|
|
}
|