Skip to content

Commit

Permalink
add light edge smoothing to WGLMakie
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Oct 11, 2023
1 parent 4343b62 commit 9935fb4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
12 changes: 11 additions & 1 deletion WGLMakie/assets/mesh.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ flat in int sample_frag_color;
in vec3 o_normal;
in vec3 o_camdir;

// Smoothes out edge around 0 light intensity, see GLMakie
float smooth_zero_max(float x) {
const float c = 0.00390625, xswap = 0.6406707120152759, yswap = 0.20508383900190955;
const float shift = 1.0 + xswap - yswap;
float pow8 = x + shift;
pow8 = pow8 * pow8; pow8 = pow8 * pow8; pow8 = pow8 * pow8;
return x < yswap ? c * pow8 : x;
}

vec3 blinnphong(vec3 N, vec3 V, vec3 L, vec3 color){
float backlight = get_backlight();
float diff_coeff = clamp(dot(L, -N), 0.0, 1.0); // + backlight * max(dot(L, N), 0.0);
float diff_coeff = smooth_zero_max(dot(L, -N)) +
backlight * smooth_zero_max(dot(L, N));

// specular coefficient
vec3 H = normalize(L + V);
Expand Down
12 changes: 11 additions & 1 deletion WGLMakie/assets/particles.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ in vec3 frag_normal;
in vec3 frag_position;
in vec3 o_camdir;

// Smoothes out edge around 0 light intensity, see GLMakie
float smooth_zero_max(float x) {
const float c = 0.00390625, xswap = 0.6406707120152759, yswap = 0.20508383900190955;
const float shift = 1.0 + xswap - yswap;
float pow8 = x + shift;
pow8 = pow8 * pow8; pow8 = pow8 * pow8; pow8 = pow8 * pow8;
return x < yswap ? c * pow8 : x;
}

vec3 blinnphong(vec3 N, vec3 V, vec3 L, vec3 color){
float backlight = get_backlight();
float diff_coeff = max(dot(L, -N), 0.0) + backlight * max(dot(L, N), 0.0);
float diff_coeff = smooth_zero_max(dot(L, -N)) +
backlight * smooth_zero_max(dot(L, N));

// specular coefficient
vec3 H = normalize(L + V);
Expand Down
11 changes: 10 additions & 1 deletion WGLMakie/assets/volume.frag
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@ vec3 gennormal(vec3 uvw, float d)
return normalize(a-b);
}

// Smoothes out edge around 0 light intensity, see GLMakie
float smooth_zero_max(float x) {
const float c = 0.00390625, xswap = 0.6406707120152759, yswap = 0.20508383900190955;
const float shift = 1.0 + xswap - yswap;
float pow8 = x + shift;
pow8 = pow8 * pow8; pow8 = pow8 * pow8; pow8 = pow8 * pow8;
return x < yswap ? c * pow8 : x;
}

vec3 blinnphong(vec3 N, vec3 V, vec3 L, vec3 color){
// TODO use backlight here too?
float diff_coeff = max(dot(L, -N), 0.0) + max(dot(L, N), 0.0);
float diff_coeff = smooth_zero_max(dot(L, -N)) + smooth_zero_max(dot(L, N));
// specular coefficient
vec3 H = normalize(L + V);
float spec_coeff = pow(max(dot(H, -N), 0.0) + max(dot(H, N), 0.0), shininess);
Expand Down

0 comments on commit 9935fb4

Please sign in to comment.