1
0
Fork 0

backup commit

This commit is contained in:
Fernando García Liñán 2019-11-04 13:42:00 +01:00
parent daeea22999
commit 0cc150b4d7
6 changed files with 472 additions and 1 deletions

View file

@ -10,6 +10,13 @@
<height>screen</height>
<format>rgba8</format>
</buffer>
<buffer>
<name>final</name>
<type>2d</type>
<width>screen</width>
<height>screen</height>
<format>rgba8</format>
</buffer>
<buffer>
<name>sun-shadowmap-atlas</name>
<type>2d</type>
@ -100,12 +107,26 @@
<multisample-color-samples>4</multisample-color-samples>
</attachment>
</pass>
<pass>
<name>volumetric-clouds</name>
<type>quad</type>
<effect>Effects/ALS/volumetric-clouds</effect>
<binding>
<buffer>color</buffer>
<unit>0</unit>
</binding>
<attachment>
<buffer>final</buffer>
<component>color0</component>
</attachment>
</pass>
<pass>
<name>display</name>
<type>quad</type>
<binding>
<buffer>color</buffer>
<buffer>final</buffer>
<unit>0</unit>
</binding>
</pass>

View file

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="utf-8"?>
<PropertyList>
<name>Effects/ALS/volumetric-clouds</name>
<parameters>
<texture n="5">
<image>Textures/Sky/vol_cloud_base.png</image>
<type>2d</type>
<filter>linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="6">
<image>Textures/Sky/vol_cloud_erosion.png</image>
<type>2d</type>
<filter>linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<base_scale><use>/test/base_scale</use></base_scale>
<erosion_scale><use>/test/erosion_scal</use></erosion_scale>
<cloud_density><use>/test/cloud_density</use></cloud_density>
<erosion_strength><use>/test/erosion_strength</use></erosion_strength>
</parameters>
<technique n="10">
<pass>
<texture-unit>
<unit>5</unit>
<image>
<use>texture[5]/image</use>
</image>
<type>
<use>texture[5]/type</use>
</type>
<filter>
<use>texture[5]/filter</use>
</filter>
<wrap-s>
<use>texture[5]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[5]/wrap-t</use>
</wrap-t>
<internal-format>
<use>texture[5]/internal-format</use>
</internal-format>
</texture-unit>
<texture-unit>
<unit>6</unit>
<image>
<use>texture[6]/image</use>
</image>
<type>
<use>texture[6]/type</use>
</type>
<filter>
<use>texture[6]/filter</use>
</filter>
<wrap-s>
<use>texture[6]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[6]/wrap-t</use>
</wrap-t>
<internal-format>
<use>texture[6]/internal-format</use>
</internal-format>
</texture-unit>
<program>
<vertex-shader>Shaders/Default/trivial.vert</vertex-shader>
<fragment-shader>Shaders/ALS/volumetric-clouds.frag</fragment-shader>
<fragment-shader>Shaders/ALS/noise.frag</fragment-shader>
</program>
<uniform>
<name>color_tex</name>
<type>sampler-2d</type>
<value type="int">0</value>
</uniform>
<uniform>
<name>noise_base_tex</name>
<type>sampler-2d</type>
<value type="int">5</value>
</uniform>
<uniform>
<name>noise_erosion_tex</name>
<type>sampler-2d</type>
<value type="int">6</value>
</uniform>
<uniform>
<name>BASE_SCALE</name>
<type>float</type>
<value><use>base_scale</use></value>
</uniform>
<uniform>
<name>EROSION_SCALE</name>
<type>float</type>
<value><use>erosion_scale</use></value>
</uniform>
<uniform>
<name>CLOUD_DENSITY</name>
<type>float</type>
<value><use>cloud_density</use></value>
</uniform>
<uniform>
<name>CLOUD_EROSION_STRENGTH</name>
<type>float</type>
<value><use>erosion_strength</use></value>
</uniform>
</pass>
</technique>
</PropertyList>

View file

@ -0,0 +1,122 @@
#version 120
uniform float terminator;
uniform float altitude;
uniform float cloud_self_shading;
uniform float moonlight;
const float shade = 1.0;
const float cloud_height = 1000.0;
const float EarthRadius = 5800000.0;
vec3 moonlight_perception(in vec3 light);
float light_func(in float x, in float a, in float b, in float c, in float d, in float e)
{
x = x-0.5;
if (x > 30.0) {return e;}
if (x < -15.0) {return 0.03;}
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
}
void alsCloud(out vec4 out_color, out vec3 hazeColor, out float fogFactor)
{
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
vec3 moonLightColor = vec3(0.095, 0.095, 0.15) * moonlight;
moonLightColor = moonlight_perception(moonLightColor);
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
//gl_TexCoord[0] = gl_MultiTexCoord0 + vec4(textureIndexX, textureIndexY, 0.0, 0.0);
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
vec4 l = gl_ModelViewMatrixInverse * vec4(0.0,0.0,1.0,1.0);
vec3 u = normalize(ep.xyz - l.xyz);
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
gl_Position.x = gl_Vertex.x;
gl_Position.y += gl_Vertex.y;
gl_Position.z += gl_Vertex.z;
gl_Position.xyz += gl_Color.xyz;
// Determine a lighting normal based on the vertex position from the
// center of the cloud, so that sprite on the opposite side of the cloud to the sun are darker.
float n = dot(normalize(-gl_LightSource[0].position.xyz),
normalize(mat3x3(gl_ModelViewMatrix) * (- gl_Position.xyz)));;
// Determine the position - used for fog and shading calculations
vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Position);
float fogCoord = abs(ecPosition.z);
float fract = smoothstep(0.0, cloud_height, gl_Position.z + cloud_height);
vec3 relVector = gl_Position.xyz - ep.xyz;
gl_Position = gl_ModelViewProjectionMatrix * gl_Position;
// Light at the final position
// first obtain normal to sun position
vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
// yprime is the distance of the vertex into sun direction, corrected for altitude
//float vertex_alt = max(altitude * 0.30480 + relVector.z,100.0);
float vertex_alt = altitude + relVector.z;
float yprime = -dot(relVector, lightHorizon);
float yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
// compute the light at the position
vec4 light_diffuse;
float lightArg = (terminator-yprime_alt)/100000.0;
light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 2.227, 1.08e-05, 1.0);
light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
light_diffuse.a = 1.0;
float intensity = length(light_diffuse.rgb);
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, (1.0 - smoothstep(0.5,0.9, cloud_self_shading ))));
n = min(smoothstep(-0.5, 0.0, n), fract);
vec4 backlight = light_diffuse * shade;
out_color = mix(backlight, light_diffuse, n);
out_color += gl_FrontLightModelProduct.sceneColor;
// As we get within 100m of the sprite, it is faded out. Equally at large distances it also fades out.
out_color.a = min(smoothstep(100.0, 250.0, fogCoord), 1.0 - smoothstep(70000.0, 75000.0, fogCoord));
// Fog doesn't affect rain as much as other objects.
//fogFactor = exp( -gl_Fog.density * fogCoord * 0.4);
//fogFactor = clamp(fogFactor, 0.0, 1.0);
float fadeScale = 0.05 + 0.2 * log(fogCoord/1000.0);
if (fadeScale < 0.05) fadeScale = 0.05;
fogFactor = exp( -gl_Fog.density * fogCoord * fadeScale);
hazeColor = light_diffuse.rgb;
hazeColor.r = hazeColor.r * 0.83;
hazeColor.g = hazeColor.g * 0.9;
// in sunset or sunrise conditions, do extra shading of clouds
// two times terminator width governs how quickly light fades into shadow
float terminator_width = 200000.0;
// now dim the light
float earthShade = 0.9 * smoothstep(terminator_width+ terminator, -terminator_width + terminator, yprime_alt) + 0.1;
if (earthShade < 0.8)
{
intensity = length(light_diffuse.rgb);
out_color.rgb = intensity * normalize(mix(out_color.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.8,earthShade ) ));
}
hazeColor = hazeColor * earthShade;
out_color.rgb = out_color.rgb * earthShade;
out_color.rgb = out_color.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
hazeColor.rgb = hazeColor.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
}

View file

@ -0,0 +1,214 @@
#version 120
#define MAX_MARCHING_STEPS 256
#define CLOUD_START_ALT 1500.0
#define CLOUD_END_ALT 3000.0
uniform float BASE_SCALE;
uniform float EROSION_SCALE;
uniform float CLOUD_DENSITY; // 0.04
uniform float CLOUD_EROSION_STRENGTH; // 0.2
uniform sampler2D color_tex;
uniform sampler2D noise_base_tex;
uniform sampler2D noise_erosion_tex;
uniform vec2 fg_ViewportSize;
uniform vec3 fg_CameraPositionCart;
uniform vec3 fg_CameraPositionGeod;
uniform vec3 fg_LightDirection;
uniform mat4 fg_ViewMatrixInverse;
uniform mat4 fg_ProjectionMatrixInverse;
////////////////////////////////////////////////////////////////////////////////
float Noise2D(in vec2 coord, in float wavelength);
float Noise3D(in vec3 coord, in float wavelength);
////////////////////////////////////////////////////////////////////////////////
float map(float s, float a1, float a2, float b1, float b2)
{
return b1+(s-a1)*(b2-b1)/(a2-a1);
}
vec3 getAmbientLight(float altitude)
{
// TODO: Actually return the cloud ambient light for this altitude
return mix(vec3(39.0, 67.0, 87.0) * (1.5/255.0),
vec3(149.0, 167.0, 200.0) * (1.5/255.0),
altitude);
}
// Henyey-Greenstein phase function is used instead of the much more complicated
// Mie phase function to approximate the angular distribution of scattered light
float HenyeyGreenstein(float costheta, float g)
{
float gg = g * g;
return (1.0 - gg) * pow(1.0 + gg - 2.0 * g * costheta, -1.5) * 0.25;
}
// Placeholder function to read a tiled 2D texture as a 3D texture
float sampleNoiseTexture(sampler2D tex, vec3 p, float size)
{
float xoffset = mod(p.z, size) * size;
float x = mod(p.x, size);
float y = mod(p.y, size);
vec2 uv = vec2(xoffset + x, y);
return texture2D(tex, uv).r;
}
// Get the cloud density in a given sky position and altitude
float getDensity(vec3 pos, float alt)
{
// Get the low frequency noise that defines the base shape of the cloud
float lowFreqNoise = sampleNoiseTexture(noise_base_tex, pos / BASE_SCALE, 128.0);
// Modulate the noise by a coverage value (this could be a CPU generated
// weather texture instead)
float coverage = Noise3D(pos, 10000.0) * 0.625 +
Noise3D(pos, 5000.0) * 0.250 +
Noise3D(pos, 2500.0) * 0.125;
coverage = smoothstep(0.6, 0.7, coverage);
float baseCloud = map(lowFreqNoise, 1.0 - coverage, 1.0, 0.0, 1.0);
baseCloud *= coverage;
// Get the height signal
float heightSignal = smoothstep(0.0, 0.1, alt) * (1.0 - smoothstep(0.6, 1.0, alt));
baseCloud *= heightSignal;
// Apply some erosion to add details
float highFreqNoise = sampleNoiseTexture(noise_erosion_tex, pos / EROSION_SCALE, 32.0);
//float highFreqNoise = 0.0;
float density = map(baseCloud, highFreqNoise * CLOUD_EROSION_STRENGTH, 1.0,
0.0, 1.0);
return clamp(density * CLOUD_DENSITY, 0.0, 1.0);
}
float shadowing(vec3 pos, float alt, vec3 lightDir)
{
float stepDelta = 10.0;
float t = stepDelta;
float shadow = 1.0;
for(int i = 0; i < 6; ++i) {
vec3 samplePoint = pos + lightDir * t;
float density = getDensity(samplePoint, alt);
shadow *= exp(-density * stepDelta);
stepDelta *= 1.6;
t += stepDelta;
}
return shadow;
}
vec3 raySphereIntersection(vec3 cameraPos, vec3 direction, float altitude)
{
float radius = length(cameraPos) - fg_CameraPositionGeod.z + altitude;
float a = dot(direction, direction) * 2.0;
float b = dot(direction, cameraPos) * 2.0;
float c = dot(cameraPos, cameraPos) - radius * radius;
float discriminant = b * b - 2.0 * a * c;
float t = max(0.0, (-b + sqrt(discriminant)) / a);
vec3 intersection = cameraPos + direction * t;
return intersection;
}
vec4 rayMarch(vec3 eye, vec3 dir)
{
vec3 entry;
float dist;
// TODO: make this actually work
if (fg_CameraPositionGeod.z < CLOUD_START_ALT) {
// We are below the clouds
// Discard fragments below the horizon
if (dot(normalize(eye), dir) < 0.0)
return vec4(0.0, 0.0, 0.0, 1.0);
entry = raySphereIntersection(eye, dir, CLOUD_START_ALT);
vec3 exit = raySphereIntersection(eye, dir, CLOUD_END_ALT);
dist = distance(entry, exit);
} else if (fg_CameraPositionGeod.z > CLOUD_END_ALT) {
// We are over the clouds
entry = raySphereIntersection(eye, dir, CLOUD_END_ALT);
vec3 exit = raySphereIntersection(eye, dir, CLOUD_START_ALT);
dist = distance(entry, exit);
} else {
// We are inside the clouds
entry = eye;
vec3 exit = raySphereIntersection(eye, dir, CLOUD_END_ALT);
if (exit == eye)
exit = raySphereIntersection(eye, dir, CLOUD_START_ALT);
dist = max(distance(entry, exit), 2000.0);
}
float t = 0.0;
float stepDelta = dist / float(MAX_MARCHING_STEPS);
vec3 lightDir = fg_LightDirection;
float costheta = dot(dir, lightDir);
float phase = mix(HenyeyGreenstein(costheta, 0.8),
HenyeyGreenstein(costheta, -0.2),
0.5);
// The RGB part contains the scattered light color and the alpha value contains
// the transmittance, both along the ray
vec4 result = vec4(0.0, 0.0, 0.0, 1.0);
for (int i = 0; i < MAX_MARCHING_STEPS; ++i) {
vec3 samplePoint = entry + dir * t;
float earthRadius = length(fg_CameraPositionCart) - fg_CameraPositionGeod.z;
float altitude = length(samplePoint) - earthRadius;
float normalizedAlt = clamp((altitude - CLOUD_START_ALT) /
(CLOUD_END_ALT - CLOUD_START_ALT), 0.0, 1.0);
float density = getDensity(samplePoint, normalizedAlt);
// Only evaluate lighting for samples inside clouds
if (density > 0.0) {
vec3 ambient = getAmbientLight(normalizedAlt);
// Analytical integration of BeerLamberts law to calculate
// transmittance
vec3 S = (ambient + (phase * shadowing(samplePoint, normalizedAlt, lightDir))) * density;
//float transmittance = exp(-density * stepDelta);
float transmittance = max(exp(-density * stepDelta), (exp(-density * stepDelta * 0.25) * 0.7));
vec3 Sint = (S - S * transmittance) / density;
result.rgb += result.a * Sint;
result.a *= transmittance;
}
if (result.a <= 0.1)
break;
t += stepDelta;
}
return result;
}
void main()
{
vec2 uv = gl_FragCoord.xy / fg_ViewportSize;
vec2 rayNDS = uv * 2.0 - 1.0;
vec4 rayCS = vec4(rayNDS, -1.0, 1.0);
vec4 rayVS = fg_ProjectionMatrixInverse * rayCS;
rayVS = vec4(rayVS.xy, -1.0, 0.0);
vec3 rayWS = normalize(fg_ViewMatrixInverse * rayVS).xyz;
vec4 cloudColor = rayMarch(fg_CameraPositionCart, rayWS);
vec3 backgroundColor = texture2D(color_tex, uv).xyz;
gl_FragColor = vec4(cloudColor.rgb + backgroundColor.rgb * cloudColor.a, 1.0);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 974 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB