Edit Mesh Mode: Add a facing falloff effect
authorClément Foucault <foucault.clem@gmail.com>
Fri, 22 Sep 2017 00:42:57 +0000 (02:42 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Fri, 22 Sep 2017 00:44:40 +0000 (02:44 +0200)
This let the user keep track of the shape by fading the center of the object.
An Opacity parameter is yet to be added.

source/blender/draw/modes/edit_mesh_mode.c
source/blender/draw/modes/shaders/edit_mesh_overlay_facedot_frag.glsl
source/blender/draw/modes/shaders/edit_mesh_overlay_facedot_vert.glsl
source/blender/draw/modes/shaders/edit_mesh_overlay_frag.glsl
source/blender/draw/modes/shaders/edit_mesh_overlay_geom_edge.glsl
source/blender/draw/modes/shaders/edit_mesh_overlay_geom_tri.glsl
source/blender/draw/modes/shaders/edit_mesh_overlay_vert.glsl

index c06a2976ac72535674e6f2ec2daa43e57b0ce9f2..c723d6e2d2d9a84877f7c65023e568900532c449 100644 (file)
@@ -152,14 +152,17 @@ static void EDIT_MESH_engine_init(void *vedata)
                        datatoc_edit_mesh_overlay_vert_glsl,
                        datatoc_edit_mesh_overlay_geom_tri_glsl,
                        datatoc_edit_mesh_overlay_frag_glsl,
-                       datatoc_common_globals_lib_glsl, "#define EDGE_FIX\n");
+                       datatoc_common_globals_lib_glsl,
+                       "#define EDGE_FIX\n"
+                       "#define VERTEX_FACING");
        }
        if (!e_data.overlay_tri_fast_sh) {
                e_data.overlay_tri_fast_sh = DRW_shader_create_with_lib(
                        datatoc_edit_mesh_overlay_vert_glsl,
                        datatoc_edit_mesh_overlay_geom_tri_glsl,
                        datatoc_edit_mesh_overlay_frag_glsl,
-                       datatoc_common_globals_lib_glsl, NULL);
+                       datatoc_common_globals_lib_glsl,
+                       "#define VERTEX_FACING");
        }
        if (!e_data.overlay_tri_vcol_sh) {
                e_data.overlay_tri_vcol_sh = DRW_shader_create_with_lib(
@@ -168,7 +171,8 @@ static void EDIT_MESH_engine_init(void *vedata)
                        datatoc_edit_mesh_overlay_frag_glsl,
                        datatoc_common_globals_lib_glsl,
                        "#define EDGE_FIX\n"
-                       "#define VERTEX_SELECTION\n");
+                       "#define VERTEX_SELECTION\n"
+                       "#define VERTEX_FACING");
        }
        if (!e_data.overlay_tri_vcol_fast_sh) {
                e_data.overlay_tri_vcol_fast_sh = DRW_shader_create_with_lib(
@@ -176,14 +180,16 @@ static void EDIT_MESH_engine_init(void *vedata)
                        datatoc_edit_mesh_overlay_geom_tri_glsl,
                        datatoc_edit_mesh_overlay_frag_glsl,
                        datatoc_common_globals_lib_glsl,
-                       "#define VERTEX_SELECTION\n");
+                       "#define VERTEX_SELECTION\n"
+                       "#define VERTEX_FACING");
        }
        if (!e_data.overlay_edge_sh) {
                e_data.overlay_edge_sh = DRW_shader_create_with_lib(
                        datatoc_edit_mesh_overlay_vert_glsl,
                        datatoc_edit_mesh_overlay_geom_edge_glsl,
                        datatoc_edit_mesh_overlay_frag_glsl,
-                       datatoc_common_globals_lib_glsl, NULL);
+                       datatoc_common_globals_lib_glsl,
+                       "#define VERTEX_FACING");
        }
        if (!e_data.overlay_edge_vcol_sh) {
                e_data.overlay_edge_vcol_sh = DRW_shader_create_with_lib(
@@ -191,20 +197,23 @@ static void EDIT_MESH_engine_init(void *vedata)
                        datatoc_edit_mesh_overlay_geom_edge_glsl,
                        datatoc_edit_mesh_overlay_frag_glsl,
                        datatoc_common_globals_lib_glsl,
-                       "#define VERTEX_SELECTION\n");
+                       "#define VERTEX_SELECTION\n"
+                       "#define VERTEX_FACING");
        }
        if (!e_data.overlay_vert_sh) {
                e_data.overlay_vert_sh = DRW_shader_create_with_lib(
                        datatoc_edit_mesh_overlay_loosevert_vert_glsl, NULL,
                        datatoc_edit_mesh_overlay_frag_glsl,
                        datatoc_common_globals_lib_glsl,
-                       "#define VERTEX_SELECTION\n");
+                       "#define VERTEX_SELECTION\n"
+                       "#define VERTEX_FACING");
        }
        if (!e_data.overlay_facedot_sh) {
                e_data.overlay_facedot_sh = DRW_shader_create_with_lib(
                        datatoc_edit_mesh_overlay_facedot_vert_glsl, NULL,
                        datatoc_edit_mesh_overlay_facedot_frag_glsl,
-                       datatoc_common_globals_lib_glsl, NULL);
+                       datatoc_common_globals_lib_glsl,
+                       "#define VERTEX_FACING");
        }
        if (!e_data.overlay_mix_sh) {
                e_data.overlay_mix_sh = DRW_shader_create_fullscreen(datatoc_edit_mesh_overlay_mix_frag_glsl, NULL);
index 9b3fe6286e88972ab4e365d524cc90ed8a4b00ba..6dcbc0974688767da33084859cf77bdb3859d2d6 100644 (file)
@@ -1,5 +1,8 @@
 
 flat in int isSelected;
+#ifdef VERTEX_FACING
+in float facing;
+#endif
 
 out vec4 FragColor;
 
@@ -9,4 +12,8 @@ void main()
                FragColor = colorFaceDot;
        else
                FragColor = colorWireEdit;
+
+#ifdef VERTEX_FACING
+       FragColor.a *= 1.0 - abs(facing) * 0.4;
+#endif
 }
index 3e6f0ead04c146a2a005a4794938d25388b30276..b27a4bba4caa804ae1b71f4763d34c44d38b000c 100644 (file)
@@ -6,6 +6,16 @@ in vec4 norAndFlag;
 
 flat out int isSelected;
 
+#ifdef VERTEX_FACING
+uniform mat4 ProjectionMatrix;
+uniform mat4 ModelViewMatrix;
+uniform mat3 NormalMatrix;
+uniform vec3 eye;
+
+in vec3 vnor;
+out float facing;
+#endif
+
 void main()
 {
        gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
@@ -13,4 +23,11 @@ void main()
        gl_Position.z -= 0.0002;
        gl_PointSize = sizeFaceDot;
        isSelected = int(norAndFlag.w);
+#ifdef VERTEX_FACING
+       vec3 view_normal = normalize(NormalMatrix * norAndFlag.xyz);
+       vec3 view_vec = (ProjectionMatrix[3][3] == 0.0)
+               ? normalize((ModelViewMatrix * vec4(pos, 1.0)).xyz)
+               : vec3(0.0, 0.0, 1.0);
+       facing = dot(view_vec, view_normal);
+#endif
 }
index 8ae7a9e3ab569c47fc28a2f519e2b39223660b2a..d7f96695d0e76ea8909de2ff3dcf113674fccf4d 100644 (file)
@@ -15,6 +15,9 @@ flat in int clipCase;
 #ifdef VERTEX_SELECTION
 in vec3 vertexColor;
 #endif
+#ifdef VERTEX_FACING
+in float facing;
+#endif
 
 /* We use a vec4[2] interface to pass edge data
  * (without fragmenting memory accesses)
@@ -198,6 +201,10 @@ void main()
        }
 #endif
 
+#ifdef VERTEX_FACING
+       FragColor.a *= 1.0 - abs(facing) * 0.4;
+#endif
+
        /* don't write depth if not opaque */
        if (FragColor.a == 0.0) discard;
 }
index 5e3ecd59cb477f465b9ced360099d0b1536a5098..7d71e1540d5e15423762a6058ba1d3a99551d7aa 100644 (file)
@@ -14,6 +14,9 @@ uniform vec2 viewportSize;
 in vec4 vPos[];
 in vec4 pPos[];
 in ivec4 vData[];
+#ifdef VERTEX_FACING
+in float vFacing[];
+#endif
 
 /* these are the same for all vertices
  * and does not need interpolation */
@@ -25,6 +28,9 @@ flat out int clipCase;
 #ifdef VERTEX_SELECTION
 out vec3 vertexColor;
 #endif
+#ifdef VERTEX_FACING
+out float facing;
+#endif
 
 /* See fragment shader */
 noperspective out vec4 eData1;
@@ -79,6 +85,10 @@ void doVertex(int v, vec4 pos)
        vertexColor = getVertexColor(v);
 #endif
 
+#ifdef VERTEX_FACING
+       facing = vFacing[v];
+#endif
+
        gl_Position = pos;
 
        EmitVertex();
index afd1e5a74a8f698f02343cbd9b9e90a9f19ace18..f83402e3869b1c95d7111a4b4f15b0785280e134 100644 (file)
@@ -29,6 +29,9 @@ uniform vec2 viewportSize;
 in vec4 vPos[];
 in vec4 pPos[];
 in ivec4 vData[];
+#ifdef VERTEX_FACING
+in float vFacing[];
+#endif
 
 /* these are the same for all vertices
  * and does not need interpolation */
@@ -40,6 +43,9 @@ flat out int clipCase;
 #ifdef VERTEX_SELECTION
 out vec3 vertexColor;
 #endif
+#ifdef VERTEX_FACING
+out float facing;
+#endif
 
 /* See fragment shader */
 noperspective out vec4 eData1;
@@ -103,6 +109,10 @@ void doVertex(int v, vec4 pos)
        vertexColor = getVertexColor(v);
 #endif
 
+#ifdef VERTEX_FACING
+       facing = vFacing[v];
+#endif
+
        gl_Position = pos;
 
        EmitVertex();
index 5bc9a8bef8546a6bb83854960eb04befe510b7fa..6da9b0a0e863e9193cb6315a26d533246c4bc664 100644 (file)
@@ -15,9 +15,25 @@ out vec4 vPos;
 out vec4 pPos;
 out ivec4 vData;
 
+#ifdef VERTEX_FACING
+uniform mat4 ProjectionMatrix;
+uniform mat3 NormalMatrix;
+uniform vec3 eye;
+
+in vec3 vnor;
+out float vFacing;
+#endif
+
 void main()
 {
        vPos = ModelViewMatrix * vec4(pos, 1.0);
        pPos = ModelViewProjectionMatrix * vec4(pos, 1.0);
        vData = data;
+#ifdef VERTEX_FACING
+       vec3 view_normal = normalize(NormalMatrix * vnor);
+       vec3 view_vec = (ProjectionMatrix[3][3] == 0.0)
+               ? normalize(vPos.xyz)
+               : vec3(0.0, 0.0, 1.0);
+       vFacing = dot(view_vec, view_normal);
+#endif
 }