diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2017-10-12 05:03:42 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2017-10-12 05:03:42 +0300 |
commit | 5f025a7e5e043a2dbdebf66668217fbae591ef89 (patch) | |
tree | f59755507998ba44bafbefd14dee69cc01597a09 /source/blender/draw/modes/shaders | |
parent | dc95c79971d48dae9418e0047f14c01e6dcab13c (diff) |
Object Mode: Grid: Add a non-hard depth test.
This adds a custom depth test that have the benefits to glitch less and be more visually pleasing.
Downside is that it let the grid pass trough the objects a little.
This effect is done in NDC space so that it counteract the logarithmic depth distribution imprecision (read as it's less visible near the camera but more present far away).
This patch also includes some cleanups.
Diffstat (limited to 'source/blender/draw/modes/shaders')
-rw-r--r-- | source/blender/draw/modes/shaders/object_grid_frag.glsl | 18 | ||||
-rw-r--r-- | source/blender/draw/modes/shaders/object_grid_vert.glsl | 4 |
2 files changed, 18 insertions, 4 deletions
diff --git a/source/blender/draw/modes/shaders/object_grid_frag.glsl b/source/blender/draw/modes/shaders/object_grid_frag.glsl index 61bd4d58a4a..eab4de72115 100644 --- a/source/blender/draw/modes/shaders/object_grid_frag.glsl +++ b/source/blender/draw/modes/shaders/object_grid_frag.glsl @@ -2,7 +2,6 @@ /* Infinite grid * Clément Foucault */ - out vec4 FragColor; uniform mat4 ProjectionMatrix; @@ -14,6 +13,7 @@ uniform vec4 gridSettings; uniform vec2 viewportSize; uniform vec4 screenvecs[3]; uniform float gridOneOverLogSubdiv; +uniform sampler2D depthBuffer; #define gridDistance gridSettings.x #define gridResolution gridSettings.y @@ -120,6 +120,7 @@ void main() angle = viewvec.z; angle = 1.0 - abs(angle); + angle *= angle; fade = 1.0 - angle * angle; fade *= 1.0 - smoothstep(0.0, gridDistance, dist - gridDistance); } @@ -130,11 +131,24 @@ void main() if ((gridFlag & PLANE_XY) > 0) { float angle = 1.0 - abs(eye.z); - fade *= 1.0 - angle * angle * angle; dist = 1.0 + angle * 2.0; + angle *= angle; + fade *= 1.0 - angle * angle; } } + /* Manual, non hard, depth test: + * Progressively fade the grid below occluders + * (avoids poping visuals due to depth buffer precision) */ + float scene_depth = texture(depthBuffer, sPos).r; + /* Add a small bias so the grid will always + * be on top of a mesh with the same depth. */ + float grid_depth = gl_FragCoord.z - 1e-8; + /* Harder settings tend to flicker more, + * but have less "see through" appearance. */ + const float test_hardness = 1e4; + fade *= 1.0 - clamp((grid_depth - scene_depth) * test_hardness, 0.0, 1.0); + if ((gridFlag & GRID) > 0) { float grid_res = log(dist * gridResolution) * gridOneOverLogSubdiv; diff --git a/source/blender/draw/modes/shaders/object_grid_vert.glsl b/source/blender/draw/modes/shaders/object_grid_vert.glsl index 2ecbfa8e11f..3f99d8b5d0a 100644 --- a/source/blender/draw/modes/shaders/object_grid_vert.glsl +++ b/source/blender/draw/modes/shaders/object_grid_vert.glsl @@ -2,7 +2,7 @@ /* Infinite grid * Clément Foucault */ -uniform mat4 ViewProjectionOffsetMatrix; +uniform mat4 ViewProjectionMatrix; uniform mat4 ProjectionMatrix; uniform vec3 cameraPos; uniform vec4 gridSettings; @@ -59,5 +59,5 @@ void main() realPos.z = min(-realPos.z, 0.0); } - gl_Position = ViewProjectionOffsetMatrix * vec4(realPos, 1.0); + gl_Position = ViewProjectionMatrix * vec4(realPos, 1.0); } |