armory/Shaders/world_pass/world_pass.frag.glsl

173 lines
4.8 KiB
Plaintext
Raw Normal View History

2016-01-28 00:58:00 +01:00
#version 450
#include "compiled.inc"
2016-10-17 17:39:40 +02:00
#ifdef _EnvTex
2017-12-13 14:21:42 +01:00
#include "std/math.glsl"
2016-10-17 17:39:40 +02:00
#endif
2015-12-17 14:25:42 +01:00
2016-07-10 00:51:39 +02:00
#ifdef _EnvCol
uniform vec3 backgroundCol;
#endif
2016-06-30 13:22:05 +02:00
#ifdef _EnvSky
uniform vec3 A;
uniform vec3 B;
uniform vec3 C;
uniform vec3 D;
uniform vec3 E;
uniform vec3 F;
uniform vec3 G;
uniform vec3 H;
uniform vec3 I;
uniform vec3 Z;
uniform vec3 hosekSunDirection;
2016-06-30 13:22:05 +02:00
#endif
2016-07-12 00:09:02 +02:00
#ifdef _EnvClouds
2019-04-25 22:39:08 +02:00
uniform sampler3D scloudsBase;
uniform sampler3D scloudsDetail;
uniform sampler2D scloudsMap;
2016-07-12 00:09:02 +02:00
uniform float time;
#endif
2016-06-30 13:22:05 +02:00
#ifdef _EnvTex
uniform sampler2D envmap;
2016-06-03 17:18:38 +02:00
#endif
2016-11-03 19:07:16 +01:00
#ifdef _EnvImg // Static background
uniform vec2 screenSize;
uniform sampler2D envmap;
#endif
2016-07-12 00:09:02 +02:00
2017-12-20 22:56:22 +01:00
#ifdef _EnvStr
2017-12-13 00:10:30 +01:00
uniform float envmapStrength;
2017-12-20 22:56:22 +01:00
#endif
2015-12-17 14:25:42 +01:00
2016-02-08 12:03:20 +01:00
in vec3 normal;
2016-10-12 17:52:27 +02:00
out vec4 fragColor;
2015-12-17 14:25:42 +01:00
2016-06-30 13:22:05 +02:00
#ifdef _EnvSky
2016-06-03 17:18:38 +02:00
vec3 hosekWilkie(float cos_theta, float gamma, float cos_gamma) {
vec3 chi = (1 + cos_gamma * cos_gamma) / pow(1 + H * H - 2 * cos_gamma * H, vec3(1.5));
2016-10-17 17:39:40 +02:00
return (1 + A * exp(B / (cos_theta + 0.01))) * (C + D * exp(E * gamma) + F * (cos_gamma * cos_gamma) + G * chi + I * sqrt(cos_theta));
2016-06-03 17:18:38 +02:00
}
#endif
2016-07-12 00:09:02 +02:00
#ifdef _EnvClouds
2019-04-25 22:39:08 +02:00
// GPU PRO 7 - Real-time Volumetric Cloudscapes
// https://www.guerrilla-games.com/read/the-real-time-volumetric-cloudscapes-of-horizon-zero-dawn
// https://github.com/sebh/TileableVolumeNoise
float remap(float old_val, float old_min, float old_max, float new_min, float new_max) {
return new_min + (((old_val - old_min) / (old_max - old_min)) * (new_max - new_min));
2016-07-12 00:09:02 +02:00
}
2019-04-25 22:39:08 +02:00
float getDensityHeightGradientForPoint(float height, float cloud_type) {
const vec4 stratusGrad = vec4(0.02f, 0.05f, 0.09f, 0.11f);
const vec4 stratocumulusGrad = vec4(0.02f, 0.2f, 0.48f, 0.625f);
const vec4 cumulusGrad = vec4(0.01f, 0.0625f, 0.78f, 1.0f);
float stratus = 1.0f - clamp(cloud_type * 2.0f, 0, 1);
float stratocumulus = 1.0f - abs(cloud_type - 0.5f) * 2.0f;
float cumulus = clamp(cloud_type - 0.5f, 0, 1) * 2.0f;
vec4 cloudGradient = stratusGrad * stratus + stratocumulusGrad * stratocumulus + cumulusGrad * cumulus;
return smoothstep(cloudGradient.x, cloudGradient.y, height) - smoothstep(cloudGradient.z, cloudGradient.w, height);
2016-07-12 00:09:02 +02:00
}
2019-04-25 22:39:08 +02:00
float sampleCloudDensity(vec3 p) {
float cloud_base = textureLod(scloudsBase, p, 0).r * 40; // Base noise
2019-04-27 20:36:14 +02:00
vec3 weather_data = textureLod(scloudsMap, p.xy, 0).rgb; // Weather map
2019-04-25 22:39:08 +02:00
cloud_base *= getDensityHeightGradientForPoint(p.z, weather_data.b); // Cloud type
cloud_base = remap(cloud_base, weather_data.r, 1.0, 0.0, 1.0); // Coverage
cloud_base *= weather_data.r;
float cloud_detail = textureLod(scloudsDetail, p, 0).r * 2; // Detail noise
float cloud_detail_mod = mix(cloud_detail, 1.0 - cloud_detail, clamp(p.z * 10.0, 0, 1));
cloud_base = remap(cloud_base, cloud_detail_mod * 0.2, 1.0, 0.0, 1.0);
return cloud_base;
2016-07-12 00:09:02 +02:00
}
2019-04-25 22:39:08 +02:00
2019-04-27 20:36:14 +02:00
float cloudRadiance(vec3 p, vec3 dir){
#ifdef _EnvSky
vec3 sun_dir = hosekSunDirection;
#else
vec3 sun_dir = vec3(0, 0, -1);
#endif
const int steps = 8;
float step_size = 0.5 / float(steps);
float d = 0.0;
p += sun_dir * step_size;
for(int i = 0; i < steps; ++i) {
d += sampleCloudDensity(p + sun_dir * float(i) * step_size);
}
return 1.0 - d;
}
2019-04-25 22:39:08 +02:00
vec3 traceClouds(vec3 sky, vec3 dir) {
2019-04-26 09:58:20 +02:00
const float step_size = 0.5 / float(cloudsSteps);
2019-04-25 22:39:08 +02:00
float T = 1.0;
2019-04-26 09:58:20 +02:00
float C = 0.0;
vec2 uv = dir.xy / dir.z * 0.4 * cloudsLower + cloudsWind * time * 0.02;
2019-04-25 22:39:08 +02:00
2019-04-26 09:58:20 +02:00
for (int i = 0; i < cloudsSteps; ++i) {
float h = float(i) / float(cloudsSteps);
2019-04-27 20:36:14 +02:00
vec3 p = vec3(uv * 0.04, h);
float d = sampleCloudDensity(p);
2019-04-25 22:39:08 +02:00
if (d > 0) {
2019-04-27 20:36:14 +02:00
// float radiance = cloudRadiance(p, dir);
2019-04-26 09:58:20 +02:00
C += T * exp(h) * d * step_size * 0.6 * cloudsPrecipitation;
2019-04-27 20:36:14 +02:00
T *= exp(-d * step_size);
if (T < 0.01) break;
2019-04-25 22:39:08 +02:00
}
2019-04-26 09:58:20 +02:00
uv += (dir.xy / dir.z) * step_size * cloudsUpper;
2017-12-20 22:56:22 +01:00
}
2019-04-25 22:39:08 +02:00
2019-04-27 20:36:14 +02:00
return vec3(C) + sky * T;
2016-07-12 00:09:02 +02:00
}
2019-04-25 22:39:08 +02:00
#endif // _EnvClouds
2016-07-12 00:09:02 +02:00
2016-01-24 22:32:51 +01:00
void main() {
2016-07-10 00:51:39 +02:00
#ifdef _EnvCol
2017-03-12 17:29:22 +01:00
fragColor.rgb = backgroundCol;
2017-11-10 15:46:05 +01:00
#ifdef _EnvTransp
return;
#endif
2016-07-17 20:29:53 +02:00
#ifdef _EnvClouds
2016-02-08 12:03:20 +01:00
vec3 n = normalize(normal);
2016-07-10 00:51:39 +02:00
#endif
2016-07-17 20:29:53 +02:00
#endif
2016-07-10 00:51:39 +02:00
#ifndef _EnvSky // Prevent case when sky radiance is enabled
2016-06-30 13:22:05 +02:00
#ifdef _EnvTex
2016-07-17 20:29:53 +02:00
vec3 n = normalize(normal);
2017-03-12 17:29:22 +01:00
fragColor.rgb = texture(envmap, envMapEquirect(n)).rgb * envmapStrength;
2017-01-08 11:25:30 +01:00
#ifdef _EnvLDR
2017-03-12 17:29:22 +01:00
fragColor.rgb = pow(fragColor.rgb, vec3(2.2));
2017-01-08 11:25:30 +01:00
#endif
2016-06-30 13:22:05 +02:00
#endif
#endif
2016-06-03 17:18:38 +02:00
2016-11-03 19:07:16 +01:00
#ifdef _EnvImg // Static background
// Will have to get rid of gl_FragCoord, pass tc from VS
vec2 texco = gl_FragCoord.xy / screenSize;
2017-03-12 17:29:22 +01:00
fragColor.rgb = texture(envmap, vec2(texco.x, 1.0 - texco.y)).rgb * envmapStrength;
2016-11-03 19:07:16 +01:00
#endif
2016-06-30 13:22:05 +02:00
#ifdef _EnvSky
2016-07-17 20:29:53 +02:00
vec3 n = normalize(normal);
2016-06-03 17:18:38 +02:00
float phi = acos(n.z);
float theta = atan(-n.y, n.x) + PI;
2016-05-10 12:11:31 +02:00
2016-08-11 22:24:45 +02:00
float cos_theta = clamp(n.z, 0.0, 1.0);
float cos_gamma = dot(n, hosekSunDirection);
2016-06-03 17:18:38 +02:00
float gamma_val = acos(cos_gamma);
2017-03-12 17:29:22 +01:00
fragColor.rgb = Z * hosekWilkie(cos_theta, gamma_val, cos_gamma) * envmapStrength;
2016-07-17 20:29:53 +02:00
#endif
2016-07-12 00:09:02 +02:00
#ifdef _EnvClouds
2019-04-25 22:39:08 +02:00
if (n.z > 0.0) fragColor.rgb = mix(fragColor.rgb, traceClouds(fragColor.rgb, n), clamp(n.z * 5.0, 0, 1));
2016-07-12 00:09:02 +02:00
#endif
2016-08-07 01:43:21 +02:00
#ifdef _LDR
2017-03-12 17:29:22 +01:00
fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 2.2));
2016-08-07 01:43:21 +02:00
#endif
2019-04-16 13:30:35 +02:00
fragColor.a = 0.0; // Mark as non-opaque
2015-12-17 14:25:42 +01:00
}