Skip to content

Commit

Permalink
Improve soft-edges handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Swordfish90 committed Jul 13, 2024
1 parent 0cac511 commit c3014fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/shaders/cut2/cut2_pass_0.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ lowp int findPattern(lowp vec4 values, lowp vec2 saddleAdjustments) {
max(patternContrasts.z, patternContrasts.w)
);

bvec4 isMax = greaterThanEqual(patternContrasts, vec4(maxContrast));
bvec4 isMax = greaterThanEqual(patternContrasts, vec4(maxContrast - EPSILON));
bool isSaddle = all(isMax);

if (maxContrast < EDGE_MIN_VALUE) {
if (maxContrast < EDGE_MIN_VALUE || isSaddle) {
return 0;
} else if (isMax.x) {
return 1;
Expand All @@ -98,8 +99,8 @@ lowp int findPattern(lowp vec4 values, lowp vec2 saddleAdjustments) {

lowp float softEdgeWeight(lowp float a, lowp float b, lowp float c, lowp float d) {
lowp float result = 0.0;
result += clamp(1.0 - 2.0 * abs((max(abs(b - c), abs(c - d))) / (abs(b - d) + EPSILON) - 0.5), 0.0, 1.0);
result -= clamp(1.0 - 2.0 * abs((max(abs(a - b), abs(b - c))) / (abs(a - c) + EPSILON) - 0.5), 0.0, 1.0);
result += clamp(abs((2.0 * b - (a + c))) / abs(a - c), 0.0, 1.0);
result -= clamp(abs((2.0 * c - (d + b))) / abs(b - d), 0.0, 1.0);
return clamp(result, -1.0, 1.0);
}

Expand Down Expand Up @@ -164,12 +165,13 @@ void main() {
);

#if SOFT_EDGES_SHARPENING
lowp vec4 softEdges = SOFT_EDGES_SHARPENING_AMOUNT * vec4(
lowp vec4 softEdges = vec4(
softEdgeWeight(l04, l05, l06, l07),
softEdgeWeight(l02, l06, l10, l14),
softEdgeWeight(l08, l09, l10, l11),
softEdgeWeight(l01, l05, l09, l13)
);
softEdges = clamp(softEdges, vec4(-SOFT_EDGES_SHARPENING_AMOUNT), vec4(SOFT_EDGES_SHARPENING_AMOUNT));

edges = clamp(edges + softEdges, min(edges, softEdges), max(edges, softEdges));
#endif
Expand Down
9 changes: 5 additions & 4 deletions src/shaders/cut3/cut3_pass_0.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ lowp int findPattern(lowp vec4 values, lowp vec2 saddleAdjustments) {
max(patternContrasts.z, patternContrasts.w)
);

bvec4 isMax = greaterThanEqual(patternContrasts, vec4(maxContrast));
bvec4 isMax = greaterThanEqual(patternContrasts, vec4(maxContrast - EPSILON));
bool isSaddle = all(isMax);

if (maxContrast < EDGE_MIN_VALUE) {
if (maxContrast < EDGE_MIN_VALUE || isSaddle) {
return 0;
} else if (isMax.x) {
return 1;
Expand All @@ -98,8 +99,8 @@ lowp int findPattern(lowp vec4 values, lowp vec2 saddleAdjustments) {

lowp float softEdgeWeight(lowp float a, lowp float b, lowp float c, lowp float d) {
lowp float result = 0.0;
result += clamp(1.0 - 2.0 * abs((max(abs(b - c), abs(c - d))) / (abs(b - d) + EPSILON) - 0.5), 0.0, 1.0);
result -= clamp(1.0 - 2.0 * abs((max(abs(a - b), abs(b - c))) / (abs(a - c) + EPSILON) - 0.5), 0.0, 1.0);
result += clamp(abs((2.0 * b - (a + c))) / abs(a - c), 0.0, 1.0);
result -= clamp(abs((2.0 * c - (d + b))) / abs(b - d), 0.0, 1.0);
return clamp(result, -1.0, 1.0);
}

Expand Down
7 changes: 4 additions & 3 deletions src/shaders/cut3/cut3_pass_1.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ precision lowp float;

#define EPSILON 0.02

const float STEP = 0.5 / float(MAX_SEARCH_DISTANCE);
const float HSTEP = (STEP * 0.5);
const lowp float STEP = 0.5 / float(MAX_SEARCH_DISTANCE);
const lowp float HSTEP = (STEP * 0.5);

uniform lowp sampler2D previousPass;

Expand Down Expand Up @@ -173,10 +173,11 @@ void main() {
);

#if SOFT_EDGES_SHARPENING
lowp vec4 softEdges = 2.0 * SOFT_EDGES_SHARPENING_AMOUNT * vec4(
lowp vec4 softEdges = 2.0 * vec4(
quickUnpackFloats2(previousPassPixel.y + 0.001953125) - vec2(0.5),
quickUnpackFloats2(previousPassPixel.z + 0.001953125) - vec2(0.5)
);
softEdges = clamp(softEdges, vec4(-SOFT_EDGES_SHARPENING_AMOUNT), vec4(SOFT_EDGES_SHARPENING_AMOUNT));

edges = clamp(edges + softEdges, min(edges, softEdges), max(edges, softEdges));
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/shaders/cut3/cut3_pass_2.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ ShapeWeights triangleWeights(lowp vec2 pxCoords, lowp vec2 edgeWeights) {
ShapeWeights result;
lowp float m = edgeWeights.x;
lowp float n = edgeWeights.y;
lowp float a = (n * m + pxCoords.y * (1.0 - m - n)) / (n * m + pxCoords.x * (1.0 - m - n));
lowp float a = (n * m + pxCoords.y * (1.0 - m - n)) / (n * m + pxCoords.x * (1.0 - m - n) + EPSILON);
lowp vec2 projections = vec2((pxCoords.y -a * pxCoords.x), (1.0 - pxCoords.y) / a + pxCoords.x);
result.weights = vec3(projections.x, projections.y, pxCoords.x / (projections.y + EPSILON));
result.midPoints = vec3(m, n, 0.5);
Expand Down

0 comments on commit c3014fb

Please sign in to comment.