Fixing morphological shaders code

This commit is contained in:
Bruno Herbelin
2022-05-15 23:34:09 +02:00
parent 07dece9cd7
commit 062e8357fa
3 changed files with 25 additions and 18 deletions

View File

@@ -1,14 +1,15 @@
uniform float Radius; uniform float Radius;
#define MAX_SIZE 5 #define MAX_SIZE 5
vec3 dilation (sampler2D source, vec2 uv, vec2 uv_step, float R) { vec3 dilation (sampler2D source, vec2 uv, vec2 uv_step, float rad) {
vec3 maxValue = vec3(0.0); vec3 maxValue = vec3(0.0);
float D = length(vec2( R / 2.)); float R = length(vec2(rad)) ;
float D = rad;
for (float i=-R; i <= R; ++i) for (float i=-rad; i <= rad; ++i)
{ {
for (float j=-R; j <= R; ++j) for (float j=-rad; j <= rad; ++j)
{ {
vec2 delta = vec2(i, j); vec2 delta = vec2(i, j);
maxValue = max(texture(source, uv + delta * smoothstep(R, D, length(delta)) * uv_step ).rgb, maxValue); maxValue = max(texture(source, uv + delta * smoothstep(R, D, length(delta)) * uv_step ).rgb, maxValue);

View File

@@ -1,14 +1,15 @@
uniform float Radius; uniform float Radius;
#define MAX_SIZE 5 #define MAX_SIZE 5
vec3 erosion (sampler2D source, vec2 uv, vec2 uv_step, float R) vec3 erosion (sampler2D source, vec2 uv, vec2 uv_step, float rad)
{ {
vec3 minValue = vec3(1.0); vec3 minValue = vec3(1.0);
float D = length(vec2( R / 2.)); float R = length(vec2(rad)) ;
float D = rad;
for (float i=-R; i <= R; ++i) for (float i=-rad; i <= rad; ++i)
{ {
for (float j=-R; j <= R; ++j) for (float j=-rad; j <= rad; ++j)
{ {
vec2 delta = vec2(i, j); vec2 delta = vec2(i, j);
minValue = min(texture(source, uv + delta * smoothstep(R, D, length(delta)) * uv_step ).rgb, minValue); minValue = min(texture(source, uv + delta * smoothstep(R, D, length(delta)) * uv_step ).rgb, minValue);

View File

@@ -14,31 +14,31 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord )
int rad = int(Radius * 10.0); int rad = int(Radius * 10.0);
vec2 src_size = iResolution.xy; vec2 uv_step = 1.0 / iResolution.xy;
vec2 uv = fragCoord.xy / iResolution.xy; vec2 uv = fragCoord.xy / iResolution.xy;
float n = float((rad + 1) * (rad + 1)); float n = float((rad + 1) * (rad + 1));
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Bilateral difference-check, to speed up the processing of large flat areas // // Bilateral difference-check, to speed up the processing of large flat areas //
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
vec3 compare = texture(iChannel0, uv / src_size).rgb; vec3 compare = texture(iChannel0, uv).rgb;
bool diffpix = false; bool diffpix = false;
for (int j = -rad; j <= rad; ++j) { for (int j = -rad; j <= rad; ++j) {
if (texture(iChannel0, uv + vec2(0,j) / src_size).rgb != compare) { if (texture(iChannel0, uv + vec2(0,j) * uv_step).rgb != compare) {
diffpix = true; diffpix = true;
break; break;
} }
} }
if (!diffpix) { if (!diffpix) {
for (int i = -rad; i <= rad; ++i) { for (int i = -rad; i <= rad; ++i) {
if (texture(iChannel0, uv + vec2(i,0) / src_size).rgb != compare) { if (texture(iChannel0, uv + vec2(i,0) * uv_step).rgb != compare) {
diffpix = true; diffpix = true;
break; break;
} }
} }
} }
if (!diffpix) { if (!diffpix) {
outpix = vec4( texture(iChannel0, uv / src_size).rgb, 1.0); outpix = vec4( texture(iChannel0, uv).rgb, 1.0);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@@ -49,10 +49,13 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord )
m[k] = vec3(0.0); m[k] = vec3(0.0);
s[k] = vec3(0.0); s[k] = vec3(0.0);
} }
float R = 0.1;
float D = length(vec2(rad));
for (int j = -rad; j <= 0; ++j) { for (int j = -rad; j <= 0; ++j) {
for (int i = -rad; i <= 0; ++i) { for (int i = -rad; i <= 0; ++i) {
vec3 c = texture(iChannel0, uv + vec2(i,j) / src_size).rgb; vec2 delta = vec2(i, j);
vec3 c = texture(iChannel0, uv + delta * smoothstep(R, D, length(delta)) * uv_step).rgb;
m[0] += c; m[0] += c;
s[0] += c * c; s[0] += c * c;
} }
@@ -60,7 +63,8 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord )
for (int j = -rad; j <= 0; ++j) { for (int j = -rad; j <= 0; ++j) {
for (int i = 0; i <= rad; ++i) { for (int i = 0; i <= rad; ++i) {
vec3 c = texture(iChannel0, uv + vec2(i,j) / src_size).rgb; vec2 delta = vec2(i, j);
vec3 c = texture(iChannel0, uv + delta * smoothstep(R, D, length(delta)) * uv_step).rgb;
m[1] += c; m[1] += c;
s[1] += c * c; s[1] += c * c;
} }
@@ -68,7 +72,8 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord )
for (int j = 0; j <= rad; ++j) { for (int j = 0; j <= rad; ++j) {
for (int i = 0; i <= rad; ++i) { for (int i = 0; i <= rad; ++i) {
vec3 c = texture(iChannel0, uv + vec2(i,j) / src_size).rgb; vec2 delta = vec2(i, j);
vec3 c = texture(iChannel0, uv + delta * smoothstep(R, D, length(delta)) * uv_step).rgb;
m[2] += c; m[2] += c;
s[2] += c * c; s[2] += c * c;
} }
@@ -76,7 +81,8 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord )
for (int j = 0; j <= rad; ++j) { for (int j = 0; j <= rad; ++j) {
for (int i = -rad; i <= 0; ++i) { for (int i = -rad; i <= 0; ++i) {
vec3 c = texture(iChannel0, uv + vec2(i,j) / src_size).rgb; vec2 delta = vec2(i, j);
vec3 c = texture(iChannel0, uv + delta * smoothstep(R, D, length(delta)) * uv_step).rgb;
m[3] += c; m[3] += c;
s[3] += c * c; s[3] += c * c;
} }
@@ -97,4 +103,3 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord )
fragColor = outpix; fragColor = outpix;
} }