Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 3 additions & 89 deletions resources/shaders/110/gouraud.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ uniform vec4 uniform_color_clip_plane_1;
uniform vec4 uniform_color_clip_plane_2;
uniform SlopeDetection slope;

//BBS: add outline_color
uniform bool is_outline;
uniform sampler2D depth_tex;
uniform vec2 screen_size;


#ifdef ENABLE_ENVIRONMENT_MAP
uniform sampler2D environment_tex;
Expand All @@ -47,9 +42,6 @@ uniform vec2 screen_size;

uniform PrintVolumeDetection print_volume;

uniform float z_far;
uniform float z_near;

varying vec3 clipping_planes_dots;
varying float color_clip_plane_dot;

Expand All @@ -60,71 +52,6 @@ varying vec4 world_pos;
varying float world_normal_z;
varying vec3 eye_normal;

vec3 getBackfaceColor(vec3 fill) {
float brightness = 0.2126 * fill.r + 0.7152 * fill.g + 0.0722 * fill.b;
return (brightness > 0.75) ? vec3(0.11, 0.165, 0.208) : vec3(0.988, 0.988, 0.988);
}

// Silhouette edge detection & rendering algorithem by leoneruggiero
// https://www.shadertoy.com/view/DslXz2
#define INFLATE 1

float GetTolerance(float d, float k)
{
// -------------------------------------------
// Find a tolerance for depth that is constant
// in view space (k in view space).
//
// tol = k*ddx(ZtoDepth(z))
// -------------------------------------------

float A=- (z_far+z_near)/(z_far-z_near);
float B=-2.0*z_far*z_near /(z_far-z_near);

d = d*2.0-1.0;

return -k*(d+A)*(d+A)/B;
}

float DetectSilho(vec2 fragCoord, vec2 dir)
{
// -------------------------------------------
// x0 ___ x1----o
// :\ :
// r0 : \ : r1
// : \ :
// o---x2 ___ x3
//
// r0 and r1 are the differences between actual
// and expected (as if x0..3 where on the same
// plane) depth values.
// -------------------------------------------

float x0 = abs(texture2D(depth_tex, (fragCoord + dir*-2.0) / screen_size).r);
float x1 = abs(texture2D(depth_tex, (fragCoord + dir*-1.0) / screen_size).r);
float x2 = abs(texture2D(depth_tex, (fragCoord + dir* 0.0) / screen_size).r);
float x3 = abs(texture2D(depth_tex, (fragCoord + dir* 1.0) / screen_size).r);

float d0 = (x1-x0);
float d1 = (x2-x3);

float r0 = x1 + d0 - x2;
float r1 = x2 + d1 - x1;

float tol = GetTolerance(x2, 0.04);

return smoothstep(0.0, tol*tol, max( - r0*r1, 0.0));

}

float DetectSilho(vec2 fragCoord)
{
return max(
DetectSilho(fragCoord, vec2(1,0)), // Horizontal
DetectSilho(fragCoord, vec2(0,1)) // Vertical
);
}

void main()
{
if (any(lessThan(clipping_planes_dots, ZERO)))
Expand Down Expand Up @@ -166,23 +93,10 @@ void main()
}
color.rgb = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color.rgb, ZERO, 0.3333) : color.rgb;

//BBS: add outline_color
if (is_outline) {
color = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
vec2 fragCoord = gl_FragCoord.xy;
float s = DetectSilho(fragCoord);
// Makes silhouettes thicker.
for(int i=1;i<=INFLATE; i++)
{
s = max(s, DetectSilho(fragCoord.xy + vec2(i, 0)));
s = max(s, DetectSilho(fragCoord.xy + vec2(0, i)));
}
gl_FragColor = vec4(mix(color.rgb, getBackfaceColor(color.rgb), s), color.a);
}
#ifdef ENABLE_ENVIRONMENT_MAP
else if (use_environment_tex)
if (use_environment_tex)
gl_FragColor = vec4(0.45 * texture(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color.rgb * intensity.x, color.a);
#endif
else
#endif
gl_FragColor = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
}
}
32 changes: 32 additions & 0 deletions resources/shaders/110/selection_composite.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#version 110

uniform sampler2D mask_texture;
uniform sampler2D edge_texture;
uniform sampler2D glow_texture;
uniform vec3 outline_color;

const float fillAlpha = 0.4;
const float outlineExclusionLow = 0.25;
const float outlineExclusionHigh = 0.75;
const float edgeStrength = 3.0;
const float edgeGlow = 0.8;

varying vec2 tex_coord;

void main()
{
float coverage = texture2D(mask_texture, tex_coord).a;
float edgeCoverage = texture2D(edge_texture, tex_coord).a;
float glowCoverage = texture2D(glow_texture, tex_coord).a;
float outlineExclusion = smoothstep(outlineExclusionLow, outlineExclusionHigh, coverage);
float outsideFill = 1.0 - outlineExclusion;
float fillCoverage = coverage * fillAlpha;
float edgeAlpha = clamp(edgeStrength * edgeCoverage * outsideFill, 0.0, 1.0);
float glowAlpha = clamp(edgeStrength * edgeGlow * glowCoverage * outsideFill, 0.0, 1.0);

// Precompose normal-alpha Fill and Edge followed by additive Glow.
float compositeAlpha = fillCoverage + edgeAlpha * (1.0 - fillCoverage);
vec3 compositeColor = vec3(fillAlpha) * fillCoverage * (1.0 - edgeAlpha) +
outline_color * (edgeAlpha + glowAlpha);
gl_FragColor = vec4(compositeColor, compositeAlpha);
}
19 changes: 19 additions & 0 deletions resources/shaders/110/selection_edge.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 110

uniform sampler2D mask_texture;
uniform vec2 inverse_texture_size;

varying vec2 tex_coord;

void main()
{
float leftCoverage = texture2D(mask_texture, tex_coord - vec2(inverse_texture_size.x, 0.0)).a;
float rightCoverage = texture2D(mask_texture, tex_coord + vec2(inverse_texture_size.x, 0.0)).a;
float bottomCoverage = texture2D(mask_texture, tex_coord - vec2(0.0, inverse_texture_size.y)).a;
float topCoverage = texture2D(mask_texture, tex_coord + vec2(0.0, inverse_texture_size.y)).a;
float horizontalEdge = (rightCoverage - leftCoverage) * 0.5;
float verticalEdge = (topCoverage - bottomCoverage) * 0.5;
float edge = length(vec2(horizontalEdge, verticalEdge));

gl_FragColor = vec4(0.0, 0.0, 0.0, edge);
}
41 changes: 41 additions & 0 deletions resources/shaders/110/selection_gaussian.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#version 110

uniform sampler2D source_texture;
uniform vec2 inverse_texture_size;
uniform vec2 blur_direction;
uniform float blur_radius;

varying vec2 tex_coord;

float gaussianPdf(float x, float sigma)
{
return 0.39894 * exp(-0.5 * x * x / (sigma * sigma)) / sigma;
}

void main()
{
if (blur_radius <= 0.0)
{
gl_FragColor = vec4(0.0, 0.0, 0.0, texture2D(source_texture, tex_coord).a);
return;
}

const int maxBlurRadius = 4;
float sigma = max(blur_radius * 0.5, 0.001);
float weightSum = gaussianPdf(0.0, sigma);
float blurred = texture2D(source_texture, tex_coord).a * weightSum;
vec2 stepUv = blur_direction * inverse_texture_size * blur_radius / float(maxBlurRadius);
vec2 sampleOffset = stepUv;

for (int i = 1; i <= maxBlurRadius; ++i)
{
float offset = blur_radius * float(i) / float(maxBlurRadius);
float weight = gaussianPdf(offset, sigma);
blurred += (texture2D(source_texture, tex_coord + sampleOffset).a +
texture2D(source_texture, tex_coord - sampleOffset).a) * weight;
weightSum += 2.0 * weight;
sampleOffset += stepUv;
}

gl_FragColor = vec4(0.0, 0.0, 0.0, blurred / weightSum);
}
8 changes: 8 additions & 0 deletions resources/shaders/110/selection_mask.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 110

uniform vec3 output_color;

void main()
{
gl_FragColor = vec4(output_color, 1.0);
}
92 changes: 3 additions & 89 deletions resources/shaders/140/gouraud.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,13 @@ uniform vec4 uniform_color_clip_plane_1;
uniform vec4 uniform_color_clip_plane_2;
uniform SlopeDetection slope;

//BBS: add outline_color
uniform bool is_outline;
uniform sampler2D depth_tex;
uniform vec2 screen_size;

#ifdef ENABLE_ENVIRONMENT_MAP
uniform sampler2D environment_tex;
uniform bool use_environment_tex;
#endif // ENABLE_ENVIRONMENT_MAP

uniform PrintVolumeDetection print_volume;

uniform float z_far;
uniform float z_near;

in vec3 clipping_planes_dots;
in float color_clip_plane_dot;

Expand All @@ -59,71 +51,6 @@ in vec4 world_pos;
in float world_normal_z;
in vec3 eye_normal;

vec3 getBackfaceColor(vec3 fill) {
float brightness = 0.2126 * fill.r + 0.7152 * fill.g + 0.0722 * fill.b;
return (brightness > 0.75) ? vec3(0.11, 0.165, 0.208) : vec3(0.988, 0.988, 0.988);
}

// Silhouette edge detection & rendering algorithem by leoneruggiero
// https://www.shadertoy.com/view/DslXz2
#define INFLATE 1

float GetTolerance(float d, float k)
{
// -------------------------------------------
// Find a tolerance for depth that is constant
// in view space (k in view space).
//
// tol = k*ddx(ZtoDepth(z))
// -------------------------------------------

float A=- (z_far+z_near)/(z_far-z_near);
float B=-2.0*z_far*z_near /(z_far-z_near);

d = d*2.0-1.0;

return -k*(d+A)*(d+A)/B;
}

float DetectSilho(vec2 fragCoord, vec2 dir)
{
// -------------------------------------------
// x0 ___ x1----o
// :\ :
// r0 : \ : r1
// : \ :
// o---x2 ___ x3
//
// r0 and r1 are the differences between actual
// and expected (as if x0..3 where on the same
// plane) depth values.
// -------------------------------------------

float x0 = abs(texture(depth_tex, (fragCoord + dir*-2.0) / screen_size).r);
float x1 = abs(texture(depth_tex, (fragCoord + dir*-1.0) / screen_size).r);
float x2 = abs(texture(depth_tex, (fragCoord + dir* 0.0) / screen_size).r);
float x3 = abs(texture(depth_tex, (fragCoord + dir* 1.0) / screen_size).r);

float d0 = (x1-x0);
float d1 = (x2-x3);

float r0 = x1 + d0 - x2;
float r1 = x2 + d1 - x1;

float tol = GetTolerance(x2, 0.04);

return smoothstep(0.0, tol*tol, max( - r0*r1, 0.0));

}

float DetectSilho(vec2 fragCoord)
{
return max(
DetectSilho(fragCoord, vec2(1,0)), // Horizontal
DetectSilho(fragCoord, vec2(0,1)) // Vertical
);
}

out vec4 out_color;

void main()
Expand Down Expand Up @@ -167,23 +94,10 @@ void main()
}
color.rgb = (any(lessThan(pv_check_min, ZERO)) || any(greaterThan(pv_check_max, ZERO))) ? mix(color.rgb, ZERO, 0.3333) : color.rgb;

//BBS: add outline_color
if (is_outline) {
color = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
vec2 fragCoord = gl_FragCoord.xy;
float s = DetectSilho(fragCoord);
// Makes silhouettes thicker.
for(int i=1;i<=INFLATE; i++)
{
s = max(s, DetectSilho(fragCoord.xy + vec2(i, 0)));
s = max(s, DetectSilho(fragCoord.xy + vec2(0, i)));
}
out_color = vec4(mix(color.rgb, getBackfaceColor(color.rgb), s), color.a);
}
#ifdef ENABLE_ENVIRONMENT_MAP
else if (use_environment_tex)
if (use_environment_tex)
out_color = vec4(0.45 * texture(environment_tex, normalize(eye_normal).xy * 0.5 + 0.5).xyz + 0.8 * color.rgb * intensity.x, color.a);
#endif
else
#endif
out_color = vec4(vec3(intensity.y) + color.rgb * intensity.x, color.a);
}
}
32 changes: 32 additions & 0 deletions resources/shaders/140/selection_composite.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#version 140

uniform sampler2D mask_texture;
uniform sampler2D edge_texture;
uniform sampler2D glow_texture;
uniform vec3 outline_color;

const float fillAlpha = 0.4;
const float outlineExclusionLow = 0.25;
const float outlineExclusionHigh = 0.75;
const float edgeStrength = 3.0;
const float edgeGlow = 0.8;

in vec2 tex_coord;
out vec4 frag_color;

void main()
{
float coverage = texture(mask_texture, tex_coord).a;
float edgeCoverage = texture(edge_texture, tex_coord).a;
float glowCoverage = texture(glow_texture, tex_coord).a;
float outlineExclusion = smoothstep(outlineExclusionLow, outlineExclusionHigh, coverage);
float outsideFill = 1.0 - outlineExclusion;
float fillCoverage = coverage * fillAlpha;
float edgeAlpha = clamp(edgeStrength * edgeCoverage * outsideFill, 0.0, 1.0);
float glowAlpha = clamp(edgeStrength * edgeGlow * glowCoverage * outsideFill, 0.0, 1.0);

// Precompose normal-alpha Fill and Edge followed by additive Glow.
float compositeAlpha = fillCoverage + edgeAlpha * (1.0 - fillCoverage);
vec3 compositeColor = vec3(fillAlpha) * fillCoverage * (1.0 - edgeAlpha) + outline_color * (edgeAlpha + glowAlpha);
frag_color = vec4(compositeColor, compositeAlpha);
}
Loading