Skip to content

Commit

Permalink
rt: linearize alpha value for blending
Browse files Browse the repository at this point in the history
This makes transparent brushes look more correct. But also makes sprites
look a bit dull.

Fixes #528
  • Loading branch information
w23 committed Apr 28, 2023
1 parent 8ac1a76 commit 3447dfc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion ref/vk/shaders/color_spaces.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define SRGB_FAST_APPROXIMATION 0
#define SRGB_FAST_APPROXIMATION

#ifdef SRGB_FAST_APPROXIMATION
#define LINEARtoSRGB OECF_sRGBFast
Expand Down Expand Up @@ -32,6 +32,9 @@ vec4 sRGB_OECF(const vec4 sRGB)
vec3 sRGB_OECFFast(const vec3 sRGB) {
return pow(sRGB, vec3(2.2));
}
float sRGB_OECFFast(const float sRGB) {
return pow(sRGB, 2.2);
}
vec4 sRGB_OECFFast(const vec4 sRGB) {
return vec4(pow(sRGB.rgb, vec3(2.2)), sRGB.w);
}
Expand Down
8 changes: 6 additions & 2 deletions ref/vk/shaders/trace_simple_blending.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ void traceSimpleBlending(vec3 pos, vec3 dir, float L, inout vec3 emissive, inout
}
#else
const vec4 texture_color = texture(textures[nonuniformEXT(kusok.material.tex_base_color)], geom.uv);
float alpha = texture_color.a * kusok.model.color.a * geom.vertex_color.a;
vec3 color = kusok.model.color.rgb * texture_color.rgb * SRGBtoLINEAR(geom.vertex_color.rgb) * alpha;

// "Linearizing" alpha, while looking weird conceptually in the code, is necessary to make it look close to the original.
// TODO figure out whether texture alpha needs to be linearized too. Don't have good examples to look at.
// TODO this also makes sprites look dull, so likely kusok.model.color linearization should only apply to brush models, not sprites. This is better done when passing brush model to ray tracer.
float alpha = SRGBtoLINEAR(texture_color.a * kusok.model.color.a) * geom.vertex_color.a;
vec3 color = kusok.model.color.rgb * SRGBtoLINEAR(texture_color.rgb) * geom.vertex_color.rgb * alpha;

if (kusok.material.mode == MATERIAL_MODE_BLEND_GLOW) {
// Glow is additive + small overshoot
Expand Down

0 comments on commit 3447dfc

Please sign in to comment.