[Bf-blender-cvs] [a81fdefddeb] master: Overlay: Wireframe: Improve Z-fighting / missing information stippling

Clément Foucault noreply at git.blender.org
Tue Dec 3 01:53:38 CET 2019


Commit: a81fdefddebc0eec3e324cf6a9d8c51050d3e749
Author: Clément Foucault
Date:   Tue Dec 3 01:51:57 2019 +0100
Branches: master
https://developer.blender.org/rBa81fdefddebc0eec3e324cf6a9d8c51050d3e749

Overlay: Wireframe: Improve Z-fighting / missing information stippling

To do this we do a half pixel offset in the normal direction.
However, we use the shading vertex normal instead of the geometric normal
(for speed) and this leads to imprecisions.

So we try to mitigate the common case and leave the corner cases as is.

===================================================================

M	source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl

===================================================================

diff --git a/source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl b/source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl
index 16fdf2687fa..21f8bcf1791 100644
--- a/source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl
+++ b/source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl
@@ -102,8 +102,25 @@ void wire_object_color_get(out vec3 rim_col, out vec3 wire_col)
 void main()
 {
   vec3 wpos = point_object_to_world(pos);
+  vec3 wnor = normalize(normal_object_to_world(nor));
+
+  bool is_persp = (ProjectionMatrix[3][3] == 0.0);
+  vec3 V = (is_persp) ? normalize(ViewMatrixInverse[3].xyz - wpos) : ViewMatrix[2].xyz;
+
+  float facing = dot(wnor, V);
+  float facing_ratio = clamp(1.0 - facing * facing, 0.0, 1.0);
+  float flip = sign(facing);           /* Flip when not facing the normal (i.e.: backfacing). */
+  float curvature = (1.0 - wd * 0.75); /* Avoid making things worse for curvy areas. */
+  vec3 wofs = wnor * (facing_ratio * curvature * flip);
+  wofs = normal_world_to_view(wofs);
+
   gl_Position = point_world_to_ndc(wpos);
 
+  /* Push vertex half a pixel (maximum) in normal direction. */
+  gl_Position.xy += wofs.xy * sizeViewportInv.xy * gl_Position.w;
+
+  /* Push the vertex towards the camera. Helps a bit. */
+  gl_Position.z -= facing_ratio * curvature * 1e-4;
 
   /* Convert to screen position [0..sizeVp]. */
   edgeStart = ((gl_Position.xy / gl_Position.w) * 0.5 + 0.5) * sizeViewport.xy;
@@ -119,8 +136,6 @@ void main()
     wire_color_get(rim_col, wire_col);
   }
 
-  vec3 wnor = normalize(normal_object_to_world(nor));
-  float facing = dot(wnor, ViewMatrixInverse[2].xyz);
   facing = clamp(abs(facing), 0.0, 1.0);
 
   vec3 final_front_col = mix(rim_col, wire_col, 0.4);



More information about the Bf-blender-cvs mailing list