Skip to content

Commit

Permalink
improve albedo heuristics and ambient color
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Feb 29, 2024
1 parent b4ee93d commit 5b8e4c3
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion game/graphics/sky/sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void Sky::updateLight(const int64_t now) {

Vec3 direct;
direct = Vec3(1.0f) * DirectSunLux;
ambient = groundAlbedo * DirectSunLux * aDirect * sunOcclude * 0.05f;// * scale;
ambient = groundAlbedo * DirectSunLux * aDirect * sunOcclude * 0.03f;// * scale;

sun.setColor(direct*sunMul);
ambient = ambient*ambMul;
Expand Down
3 changes: 2 additions & 1 deletion shader/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const float Fd_LambertInv = (M_PI);
/* Color value is multiplied to achive adequate albedo and correctness in GI.
* Need to scale it back, when material receives direct light.
*/
const float GColorScale = (1.0/M_PI);
const float GColorScaleInv = (6);
const float GColorScale = (1.0/GColorScaleInv);

float linearDepth(float d, vec3 clipInfo) {
// z_n * z_f, z_n - z_f, z_f
Expand Down
1 change: 1 addition & 0 deletions shader/lighting/ambient_light.frag
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ void main() {

outColor = vec4(color, 1);
// outColor = vec4(vec3(1-ao), 0);
// outColor = vec4(linear, 0);
}
2 changes: 1 addition & 1 deletion shader/lighting/irradiance.comp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void storeSample(vec3 dir, vec3 val) {

vec3 resolveMiss(const vec3 rayOrigin, const vec3 rayDirection) {
// PERF: need to preload sky lut early in shader
return textureSkyLUT(skyLUT, vec3(0,RPlanet,0), rayDirection, scene.sunDir) * scene.GSunIntensity;
return textureSkyLUT(skyLUT, vec3(0,RPlanet,0), rayDirection, scene.sunDir) * scene.GSunIntensity * GColorScale;
}

void gather() {
Expand Down
2 changes: 1 addition & 1 deletion shader/lighting/light.frag
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void main(void) {
const vec3 d = texelFetch(gbufDiffuse, ivec2(gl_FragCoord.xy), 0).xyz;
const vec3 linear = textureLinear(d.rgb);

vec3 color = linear*color*Fd_Lambert*light*0.2;
vec3 color = linear*color*light*Fd_Lambert*GColorScale*0.5;
//color *= scene.exposure;

outColor = vec4(color,0.0);
Expand Down
26 changes: 16 additions & 10 deletions shader/lighting/rt/probe_lighting.comp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const uint samplesTotal = gl_WorkGroupSize.x*samplesCount;
shared vec4 samples[gl_WorkGroupSize.x][3][2]; // 6 Kb
shared uint shProbeId;

layout(binding = 0, std140) readonly buffer UboScene {
layout(binding = 0, std140) readonly buffer UboScene { // ubo is broken on NVidia?
SceneDesc scene;
};
layout(binding = 1) uniform writeonly image2D probesLighting;
Expand Down Expand Up @@ -68,7 +68,9 @@ void storeSample(vec3 dir, vec4 val) {
}
}

vec3 resolveHit(const vec3 rayOrigin, const vec3 rayDirection, float rayT, vec3 albedo, vec3 normal, bool opaque, float shMap) {
vec3 resolveHit(const vec3 rayOrigin, const vec3 rayDirection, float rayT, vec3 normal, bool opaque, float shMap) {
// return 1.0 * scene.ambient;

float dt = dot(normal, scene.sunDir);
if(!opaque) {
// atificial lit on vegetation, as RT can't capture small bright light leaks from the trees
Expand All @@ -78,9 +80,9 @@ vec3 resolveHit(const vec3 rayOrigin, const vec3 rayDirection, float rayT, vec3

const float lambert = max(0, dt); //max(opaque ? 0 : 0.2, dt);
const float shadow = max(0, shMap);
const vec3 lcolor = scene.sunColor * Fd_Lambert * lambert * shadow;
const vec3 lcolor = scene.sunColor * Fd_Lambert * lambert * shadow * GColorScale;

return albedo*lcolor;
return lcolor;
//return scene.sunColor * scene.ambient;
}

Expand All @@ -90,7 +92,7 @@ vec3 resolveMiss(const vec3 rayOrigin, const vec3 rayDirection) {
}

vec3 resolveSecondBounce(vec3 pos, vec3 norm, uint selfProbeId) {
return 4.0 * scene.ambient * Fd_LambertInv;
return scene.ambient;

// doesn't look coherent yet - need to keep offscreen data for long term
const ivec3 gridPos = ivec3(round(pos/(probeGridStep*1)));
Expand Down Expand Up @@ -140,20 +142,24 @@ vec4 processSample(const vec3 rayOrigin, const vec3 rayDirection, uint probeId,
const bool opaque = (bits & 0x1)!=0;
const bool semiSky = (bits & 0x8)!=0;

const vec3 albedo = textureLinear(diff.rgb) * Fd_Lambert;
const vec3 albedo = textureLinear(diff.rgb);
const vec3 norm = normalize(nr.xyz*2.0-vec3(1));

const float rayT = rt * probeRayDistance;
const vec3 pos = rayOrigin + rayT*rayDirection;

const vec3 bounce2 = resolveSecondBounce(pos, -rayDirection, probeId);

// TODO: rt-shadow lighting ?
vec4 shCoord = scene.viewShadow[1]*vec4(pos,1);
float shadow = shadowFactor(shCoord);

vec3 hit = resolveHit(rayOrigin, rayDirection, rayT, albedo, norm, opaque, shadow);
hit += albedo*bounce2;
vec3 hit = vec3(0);
hit += albedo * resolveHit(rayOrigin, rayDirection, rayT, norm, opaque, shadow);
hit += albedo * resolveSecondBounce(pos, -rayDirection, probeId);
// hit += albedo * resolveHit(rayOrigin, rayDirection, rayT, norm, opaque, shadow);
// hit += albedo * resolveSecondBounce(pos, -rayDirection, probeId) / max(1.0 - albedo, 0.1);

// hit = resolveSecondBounce(pos, -rayDirection, probeId);

if(semiSky) {
// avoid pitch-black lighting in forrest
hit = (hit*0.25 + resolveMiss(rayOrigin, rayDirection)*0.75);
Expand Down
2 changes: 1 addition & 1 deletion shader/lighting/sky_exposure.comp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void main() {
const vec3 sunColor = scene.sunColor * tr * clouds;
const vec3 ambient = scene.ambient * tr;// * clouds;

vec3 light = sunColor*GColorScale*1.5 + ambient + skyIrradiance();
vec3 light = sunColor*Fd_Lambert*1.5 + ambient + skyIrradiance();
float lum = (vmax+vmin) * 0.5 * scene.GSunIntensity;
//lum = dot(light, vec3(1.0/3.0));
lum = dot(light, vec3(0.2125, 0.7154, 0.0721))*0.6 + lum*0.4;
Expand Down
2 changes: 1 addition & 1 deletion shader/lighting/tonemapping.frag
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void main() {

vec3 color = textureLod(textureD, uv, 0).rgb;
{
// outColor = vec4(color, 1.0);
// outColor = vec4(srgbEncode(color), 1);
// return;
}

Expand Down
2 changes: 1 addition & 1 deletion shader/lighting/tonemapping.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ vec3 textureLinear(vec3 rgb) {
// return vec3(0.33, 0.34, 0.18); // leaves
// return vec3(0.9);
// return acesTonemapInv(linear*0.8);
return acesTonemapInv(linear*0.78+0.001)*Fd_LambertInv; // adjusted to have 'realistic' albedo values
return acesTonemapInv(linear*0.78+0.001)*GColorScaleInv; // adjusted to have 'realistic' albedo values
#endif
}

Expand Down

0 comments on commit 5b8e4c3

Please sign in to comment.