diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2017-09-21 23:00:48 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2017-09-22 03:44:39 +0300 |
commit | 4bfa3f4f9ba13d49409e64d71d6894a3be12cf16 (patch) | |
tree | 95c301bf256f6eaac2f319f568fdc79d22b9e2de | |
parent | e4f05df206b865997f42fe928442226f948cd0e5 (diff) |
Edit Mesh Mode : Make line smoother.
This introduce some little artifacts on the border of edges because some pixel with very low opacity does not get discarded and then occlude the face rendered behind if it has not been drawn yet.
To fix this. I added an offset in the geometry shader for the edge fixup. This make the artifact only visible on the border of the object if there is a very dense wire region. It's only visible in edge select mode since vertex and face center also hides the artifacts.
We can enable this only if AA is enabled but for now it's always enabled.
3 files changed, 55 insertions, 35 deletions
diff --git a/source/blender/draw/modes/shaders/edit_mesh_overlay_frag.glsl b/source/blender/draw/modes/shaders/edit_mesh_overlay_frag.glsl index a8f74ac3e0f..8ae7a9e3ab5 100644 --- a/source/blender/draw/modes/shaders/edit_mesh_overlay_frag.glsl +++ b/source/blender/draw/modes/shaders/edit_mesh_overlay_frag.glsl @@ -88,6 +88,17 @@ float distToEdge(vec2 o, vec2 dir) return sqrt(abs(dot(af, af) - daf * daf)); } +#define ANTI_ALIASING + +#ifdef ANTI_ALIASING +void colorDistEdge(vec4 color, float dist) +{ + FragColor = mix(color, FragColor, clamp(dist, 0.0, 1.0)); +} +#else +#define colorDistEdge colorDist +#endif + void main() { vec3 e, p; @@ -143,27 +154,33 @@ void main() /* Edges */ for (int v = 0; v < 3; ++v) { if ((flag[v] & EDGE_EXISTS) != 0) { - float largeEdge = e[v] - sizeEdge * 2.0; + /* Outer large edge */ + float largeEdge = e[v] - sizeEdge * 3.0; + + vec4 large_edge_color = vec4(0.0); + large_edge_color = ((flag[v] & EDGE_SHARP) != 0) ? colorEdgeSharp : large_edge_color; + large_edge_color = (edgesCrease[v] > 0.0) ? vec4(colorEdgeCrease.rgb, edgesCrease[v]) : large_edge_color; + large_edge_color = (edgesBweight[v] > 0.0) ? vec4(colorEdgeBWeight.rgb, edgesBweight[v]) : large_edge_color; + large_edge_color = ((flag[v] & EDGE_SEAM) != 0) ? colorEdgeSeam : large_edge_color; + + if (large_edge_color != 0.0) { + colorDistEdge(large_edge_color, largeEdge); + } + + /* Inner thin edge */ float innerEdge = e[v] - sizeEdge; +#ifdef ANTI_ALIASING + innerEdge += 0.125; +#endif - if ((flag[v] & EDGE_SEAM) != 0) - colorDist(colorEdgeSeam, largeEdge); - else if (edgesBweight[v] > 0.0) - colorDist(vec4(colorEdgeBWeight.rgb, edgesBweight[v]), largeEdge); - else if (edgesCrease[v] > 0.0) - colorDist(vec4(colorEdgeCrease.rgb, edgesCrease[v]), largeEdge); - else if ((flag[v] & EDGE_SHARP) != 0) - colorDist(colorEdgeSharp, largeEdge); -#ifndef VERTEX_SELECTION - else - colorDist(colorWireEdit, innerEdge); - - if ((flag[v] & EDGE_ACTIVE) != 0) - colorDist(vec4(colorEditMeshActive.xyz, 1.0), innerEdge); - else if ((flag[v] & EDGE_SELECTED) != 0) - colorDist(colorEdgeSelect, innerEdge); +#ifdef VERTEX_SELECTION + colorDistEdge(vec4(vertexColor, 1.0), innerEdge); #else - colorDist(vec4(vertexColor, 1.0), innerEdge); + vec4 inner_edge_color = colorWireEdit; + inner_edge_color = ((flag[v] & EDGE_SELECTED) != 0) ? colorEdgeSelect : inner_edge_color; + inner_edge_color = ((flag[v] & EDGE_ACTIVE) != 0) ? vec4(colorEditMeshActive.xyz, 1.0) : inner_edge_color; + + colorDistEdge(inner_edge_color, innerEdge); #endif } } @@ -173,15 +190,14 @@ void main() for (int v = 0; v < 3; ++v) { float size = p[v] - sizeVertex; - if ((flag[v] & VERTEX_ACTIVE) != 0) - colorDist(vec4(colorEditMeshActive.xyz, 1.0), size); - else if ((flag[v] & VERTEX_SELECTED) != 0) - colorDist(colorVertexSelect, size); - else - colorDist(colorVertex, size); + vec4 point_color = colorVertex; + point_color = ((flag[v] & VERTEX_SELECTED) != 0) ? colorVertexSelect : point_color; + point_color = ((flag[v] & VERTEX_ACTIVE) != 0) ? vec4(colorEditMeshActive.xyz, 1.0) : point_color; + + colorDist(point_color, size); } #endif /* don't write depth if not opaque */ - if (FragColor.a == 0.0) discard; + if (FragColor.a == 0.0) discard; } diff --git a/source/blender/draw/modes/shaders/edit_mesh_overlay_geom_tri.glsl b/source/blender/draw/modes/shaders/edit_mesh_overlay_geom_tri.glsl index 64809a37d11..afd1e5a74a8 100644 --- a/source/blender/draw/modes/shaders/edit_mesh_overlay_geom_tri.glsl +++ b/source/blender/draw/modes/shaders/edit_mesh_overlay_geom_tri.glsl @@ -108,6 +108,12 @@ void doVertex(int v, vec4 pos) EmitVertex(); } +#ifdef ANTI_ALIASING +#define Z_OFFSET 0.008 +#else +#define Z_OFFSET 0.0 +#endif + void main() { /* First we detect which case we are in */ @@ -214,7 +220,7 @@ void main() } /* to not let face color bleed */ - faceColor = vec4(0.0); + faceColor.a = 0.0; /* we don't want other edges : make them far */ eData1 = vec4(1e10); @@ -231,7 +237,7 @@ void main() eData1.zw = pos[vbe]; doVertex(v, pPos[v]); - doVertex(v, pPos[v] + vec4(fixvec[v], 0.0, 0.0)); + doVertex(v, pPos[v] + vec4(fixvec[v], Z_OFFSET, 0.0)); /* Now one triangle only shade one edge * so we use the edge distance calculated @@ -247,19 +253,19 @@ void main() edgesBweight[2] = ebweight[vbe]; doVertex(vaf, pPos[vaf]); - doVertex(vaf, pPos[vaf] + vec4(fixvecaf[v], 0.0, 0.0)); + doVertex(vaf, pPos[vaf] + vec4(fixvecaf[v], Z_OFFSET, 0.0)); /* corner vertices should not draw edges but draw point only */ flag[2] = (vData[vbe].x << 8); #ifdef VERTEX_SELECTION doVertex(vaf, pPos[vaf]); - doVertex(vaf, pPos[vaf] + vec4(cornervec[vaf], 0.0, 0.0)); + doVertex(vaf, pPos[vaf] + vec4(cornervec[vaf], Z_OFFSET, 0.0)); #endif } /* finish the loop strip */ doVertex(2, pPos[2]); - doVertex(2, pPos[2] + vec4(fixvec[2], 0.0, 0.0)); + doVertex(2, pPos[2] + vec4(fixvec[2], Z_OFFSET, 0.0)); #endif } /* Harder case : compute visible edges vectors */ diff --git a/source/blender/draw/modes/shaders/edit_mesh_overlay_mix_frag.glsl b/source/blender/draw/modes/shaders/edit_mesh_overlay_mix_frag.glsl index f567a3a5cbd..8f94a105332 100644 --- a/source/blender/draw/modes/shaders/edit_mesh_overlay_mix_frag.glsl +++ b/source/blender/draw/modes/shaders/edit_mesh_overlay_mix_frag.glsl @@ -15,10 +15,8 @@ void main() FragColor = wire_color; - /* this works because not rendered depth is 1.0 and the - * following test is always true even when no wires */ - if ((wire_depth > scene_depth) && (wire_color.a > 0)) { - /* Note : Using wire_color.a * alpha produce unwanted result */ - FragColor.a = alpha; + /* Modulate alpha if occluded */ + if (wire_depth > scene_depth) { + FragColor.a *= alpha; } } |