Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Physical sky is too dark on compatibility renderer #84441

Open
Tracked by #66458
ParsleighScumble opened this issue Nov 4, 2023 · 2 comments
Open
Tracked by #66458

Physical sky is too dark on compatibility renderer #84441

ParsleighScumble opened this issue Nov 4, 2023 · 2 comments

Comments

@ParsleighScumble
Copy link
Contributor

Godot version

4.1.1.stable

System information

Godot v4.1.1.stable - Ubuntu Core 22 - Vulkan (Compatibility) - llvmpipe (LLVM 15.0.7, 256 bits) - AMD Ryzen 7 2700X Eight-Core Processor (16 Threads) - Also works on Nvidia. All GLES3 on GL.

Issue description

Found this while shaving the gles3 fog yak. Physical sky seems dark especially compared to Vulkan. It doesn't seem to have to do with tonemapping. I'll fix this myself if I have time.

image

Steps to reproduce

Make a project with Compatibility renderer. Add WorldEnvironment. Set background mode to sky. Set sky material to PhysicalSkyMaterial. Sky should be dark like in the screenshot.

Minimal reproduction project

gles3-features-test.zip

Not exactly minimal but this should do the trick.

@Calinou
Copy link
Member

Calinou commented Nov 4, 2023

PhysicalSkyMaterial shader code is here:

// NOTE: Shader automatically converted from )" VERSION_NAME " " VERSION_FULL_CONFIG R"('s PhysicalSkyMaterial.
shader_type sky;
%s
uniform float rayleigh : hint_range(0, 64) = 2.0;
uniform vec4 rayleigh_color : source_color = vec4(0.3, 0.405, 0.6, 1.0);
uniform float mie : hint_range(0, 1) = 0.005;
uniform float mie_eccentricity : hint_range(-1, 1) = 0.8;
uniform vec4 mie_color : source_color = vec4(0.69, 0.729, 0.812, 1.0);
uniform float turbidity : hint_range(0, 1000) = 10.0;
uniform float sun_disk_scale : hint_range(0, 360) = 1.0;
uniform vec4 ground_color : source_color = vec4(0.1, 0.07, 0.034, 1.0);
uniform float exposure : hint_range(0, 128) = 1.0;
uniform sampler2D night_sky : filter_linear, source_color, hint_default_black;
const vec3 UP = vec3( 0.0, 1.0, 0.0 );
// Optical length at zenith for molecules.
const float rayleigh_zenith_size = 8.4e3;
const float mie_zenith_size = 1.25e3;
float henyey_greenstein(float cos_theta, float g) {
const float k = 0.0795774715459;
return k * (1.0 - g * g) / (pow(1.0 + g * g - 2.0 * g * cos_theta, 1.5));
}
void sky() {
if (LIGHT0_ENABLED) {
float zenith_angle = clamp( dot(UP, normalize(LIGHT0_DIRECTION)), -1.0, 1.0 );
float sun_energy = max(0.0, 1.0 - exp(-((PI * 0.5) - acos(zenith_angle)))) * LIGHT0_ENERGY;
float sun_fade = 1.0 - clamp(1.0 - exp(LIGHT0_DIRECTION.y), 0.0, 1.0);
// Rayleigh coefficients.
float rayleigh_coefficient = rayleigh - ( 1.0 * ( 1.0 - sun_fade ) );
vec3 rayleigh_beta = rayleigh_coefficient * rayleigh_color.rgb * 0.0001;
// mie coefficients from Preetham
vec3 mie_beta = turbidity * mie * mie_color.rgb * 0.000434;
// Optical length.
float zenith = acos(max(0.0, dot(UP, EYEDIR)));
float optical_mass = 1.0 / (cos(zenith) + 0.15 * pow(93.885 - degrees(zenith), -1.253));
float rayleigh_scatter = rayleigh_zenith_size * optical_mass;
float mie_scatter = mie_zenith_size * optical_mass;
// Light extinction based on thickness of atmosphere.
vec3 extinction = exp(-(rayleigh_beta * rayleigh_scatter + mie_beta * mie_scatter));
// In scattering.
float cos_theta = dot(EYEDIR, normalize(LIGHT0_DIRECTION));
float rayleigh_phase = (3.0 / (16.0 * PI)) * (1.0 + pow(cos_theta * 0.5 + 0.5, 2.0));
vec3 betaRTheta = rayleigh_beta * rayleigh_phase;
float mie_phase = henyey_greenstein(cos_theta, mie_eccentricity);
vec3 betaMTheta = mie_beta * mie_phase;
vec3 Lin = pow(sun_energy * ((betaRTheta + betaMTheta) / (rayleigh_beta + mie_beta)) * (1.0 - extinction), vec3(1.5));
// Hack from https://github.com/mrdoob/three.js/blob/master/examples/jsm/objects/Sky.js
Lin *= mix(vec3(1.0), pow(sun_energy * ((betaRTheta + betaMTheta) / (rayleigh_beta + mie_beta)) * extinction, vec3(0.5)), clamp(pow(1.0 - zenith_angle, 5.0), 0.0, 1.0));
// Hack in the ground color.
Lin *= mix(ground_color.rgb, vec3(1.0), smoothstep(-0.1, 0.1, dot(UP, EYEDIR)));
// Solar disk and out-scattering.
float sunAngularDiameterCos = cos(LIGHT0_SIZE * sun_disk_scale);
float sunAngularDiameterCos2 = cos(LIGHT0_SIZE * sun_disk_scale*0.5);
float sundisk = smoothstep(sunAngularDiameterCos, sunAngularDiameterCos2, cos_theta);
vec3 L0 = (sun_energy * extinction) * sundisk * LIGHT0_COLOR;
L0 += texture(night_sky, SKY_COORDS).xyz * extinction;
vec3 color = Lin + L0;
COLOR = pow(color, vec3(1.0 / (1.2 + (1.2 * sun_fade))));
COLOR *= exposure;
} else {
// There is no sun, so display night_sky and nothing else.
COLOR = texture(night_sky, SKY_COORDS).xyz;
COLOR *= exposure;
}
}
)",

Compare it with the ProceduralSkyMaterial shader code, which doesn't exhibit this issue:

// NOTE: Shader automatically converted from )" VERSION_NAME " " VERSION_FULL_CONFIG R"('s ProceduralSkyMaterial.
shader_type sky;
%s
uniform vec4 sky_top_color : source_color = vec4(0.385, 0.454, 0.55, 1.0);
uniform vec4 sky_horizon_color : source_color = vec4(0.646, 0.656, 0.67, 1.0);
uniform float sky_curve : hint_range(0, 1) = 0.15;
uniform float sky_energy = 1.0; // In Lux.
uniform sampler2D sky_cover : filter_linear, source_color, hint_default_black;
uniform vec4 sky_cover_modulate : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform vec4 ground_bottom_color : source_color = vec4(0.2, 0.169, 0.133, 1.0);
uniform vec4 ground_horizon_color : source_color = vec4(0.646, 0.656, 0.67, 1.0);
uniform float ground_curve : hint_range(0, 1) = 0.02;
uniform float ground_energy = 1.0;
uniform float sun_angle_max = 30.0;
uniform float sun_curve : hint_range(0, 1) = 0.15;
void sky() {
float v_angle = acos(clamp(EYEDIR.y, -1.0, 1.0));
float c = (1.0 - v_angle / (PI * 0.5));
vec3 sky = mix(sky_horizon_color.rgb, sky_top_color.rgb, clamp(1.0 - pow(1.0 - c, 1.0 / sky_curve), 0.0, 1.0));
sky *= sky_energy;
if (LIGHT0_ENABLED) {
float sun_angle = acos(dot(LIGHT0_DIRECTION, EYEDIR));
if (sun_angle < LIGHT0_SIZE) {
sky = LIGHT0_COLOR * LIGHT0_ENERGY;
} else if (sun_angle < sun_angle_max) {
float c2 = (sun_angle - LIGHT0_SIZE) / (sun_angle_max - LIGHT0_SIZE);
sky = mix(LIGHT0_COLOR * LIGHT0_ENERGY, sky, clamp(1.0 - pow(1.0 - c2, 1.0 / sun_curve), 0.0, 1.0));
}
}
if (LIGHT1_ENABLED) {
float sun_angle = acos(dot(LIGHT1_DIRECTION, EYEDIR));
if (sun_angle < LIGHT1_SIZE) {
sky = LIGHT1_COLOR * LIGHT1_ENERGY;
} else if (sun_angle < sun_angle_max) {
float c2 = (sun_angle - LIGHT1_SIZE) / (sun_angle_max - LIGHT1_SIZE);
sky = mix(LIGHT1_COLOR * LIGHT1_ENERGY, sky, clamp(1.0 - pow(1.0 - c2, 1.0 / sun_curve), 0.0, 1.0));
}
}
if (LIGHT2_ENABLED) {
float sun_angle = acos(dot(LIGHT2_DIRECTION, EYEDIR));
if (sun_angle < LIGHT2_SIZE) {
sky = LIGHT2_COLOR * LIGHT2_ENERGY;
} else if (sun_angle < sun_angle_max) {
float c2 = (sun_angle - LIGHT2_SIZE) / (sun_angle_max - LIGHT2_SIZE);
sky = mix(LIGHT2_COLOR * LIGHT2_ENERGY, sky, clamp(1.0 - pow(1.0 - c2, 1.0 / sun_curve), 0.0, 1.0));
}
}
if (LIGHT3_ENABLED) {
float sun_angle = acos(dot(LIGHT3_DIRECTION, EYEDIR));
if (sun_angle < LIGHT3_SIZE) {
sky = LIGHT3_COLOR * LIGHT3_ENERGY;
} else if (sun_angle < sun_angle_max) {
float c2 = (sun_angle - LIGHT3_SIZE) / (sun_angle_max - LIGHT3_SIZE);
sky = mix(LIGHT3_COLOR * LIGHT3_ENERGY, sky, clamp(1.0 - pow(1.0 - c2, 1.0 / sun_curve), 0.0, 1.0));
}
}
vec4 sky_cover_texture = texture(sky_cover, SKY_COORDS);
sky += (sky_cover_texture.rgb * sky_cover_modulate.rgb) * sky_cover_texture.a * sky_cover_modulate.a * sky_energy;
c = (v_angle - (PI * 0.5)) / (PI * 0.5);
vec3 ground = mix(ground_horizon_color.rgb, ground_bottom_color.rgb, clamp(1.0 - pow(1.0 - c, 1.0 / ground_curve), 0.0, 1.0));
ground *= ground_energy;
COLOR = mix(ground, sky, step(0.0, EYEDIR.y));
}
)",

@ParsleighScumble
Copy link
Contributor Author

Thanks for the pointers, Calinou. I will take a look later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants