GP: Define new Draw Mode
authorAntonioya <blendergit@gmail.com>
Sat, 23 Feb 2019 10:34:23 +0000 (11:34 +0100)
committerAntonioya <blendergit@gmail.com>
Sat, 23 Feb 2019 10:46:24 +0000 (11:46 +0100)
Add a new Draw Mode to display panel in order to define the z-.depth order of the strokes using the real 3D position and not the 2D layer position.

This change makes possible to use VR with grease pencil drawings because the depth of the strokes change with camera position. Also, provide an alternative solution to tasks: T57859, T60325,

The parameter only works with 3D space depth ordering. The Back and Front depths are incompatible with 3D Space mode.

Options are:
- Back
- Front
- 3D Space->2D Layers (default)
-3D Space->3D Location (new mode)

release/scripts/startup/bl_ui/properties_data_gpencil.py
source/blender/draw/engines/gpencil/gpencil_draw_utils.c
source/blender/draw/engines/gpencil/gpencil_engine.c
source/blender/draw/engines/gpencil/gpencil_engine.h
source/blender/draw/engines/gpencil/shaders/gpencil_fill_frag.glsl
source/blender/makesdna/DNA_gpencil_types.h
source/blender/makesrna/intern/rna_gpencil.c

index d6633c9..a910cbe 100644 (file)
@@ -347,6 +347,9 @@ class DATA_PT_gpencil_display(DataButtonsPanel, Panel):
         gpl = context.active_gpencil_layer
 
         layout.prop(gpd, "xray_mode", text="Depth Ordering")
+        if gpd.xray_mode == '3DSPACE':
+            layout.prop(gpd, "draw_mode", text="Mode")
+
         layout.prop(ob, "empty_display_size", text="Marker Size")
 
         col = layout.column(align=True)
index 795bc6b..2e9a3cf 100644 (file)
@@ -302,6 +302,8 @@ static DRWShadingGroup *DRW_gpencil_shgroup_fill_create(
        DRW_shgroup_uniform_int(grp, "texture_flip", &stl->shgroups[id].texture_flip, 1);
 
        DRW_shgroup_uniform_int(grp, "xraymode", (const int *) &gpd->xray_mode, 1);
+       DRW_shgroup_uniform_int(grp, "drawmode", (const int *) &gpd->draw_mode, 1);
+
        /* image texture */
        if ((gp_style->flag & GP_STYLE_COLOR_TEX_MIX) ||
            (gp_style->fill_style & GP_STYLE_FILL_STYLE_TEXTURE))
@@ -1306,6 +1308,7 @@ static void DRW_gpencil_shgroups_create(
        GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
        GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
        bGPdata *gpd = (bGPdata *)ob->data;
+       DRWPass *stroke_pass = GPENCIL_3D_DRAWMODE(gpd) ? psl->stroke_pass_3d : psl->stroke_pass_2d;
 
        GpencilBatchGroup *elm = NULL;
        DRWShadingGroup *shgrp = NULL;
@@ -1364,7 +1367,7 @@ static void DRW_gpencil_shgroups_create(
                                const int len = elm->vertex_idx - start_stroke;
 
                                shgrp = DRW_gpencil_shgroup_stroke_create(
-                                       e_data, vedata, psl->stroke_pass, e_data->gpencil_stroke_sh,
+                                       e_data, vedata, stroke_pass, e_data->gpencil_stroke_sh,
                                        ob, gpd, gps, gp_style, stl->storage->shgroup_id, elm->onion, scale);
 
                                DRW_shgroup_call_range_add(
@@ -1381,7 +1384,7 @@ static void DRW_gpencil_shgroups_create(
                                const int len = elm->vertex_idx - start_point;
 
                                shgrp = DRW_gpencil_shgroup_point_create(
-                                       e_data, vedata, psl->stroke_pass, e_data->gpencil_point_sh,
+                                       e_data, vedata, stroke_pass, e_data->gpencil_point_sh,
                                        ob, gpd, gp_style, stl->storage->shgroup_id, elm->onion, scale);
 
                                DRW_shgroup_call_range_add(
@@ -1398,7 +1401,7 @@ static void DRW_gpencil_shgroups_create(
                                const int len = elm->vertex_idx - start_fill;
 
                                shgrp = DRW_gpencil_shgroup_fill_create(
-                                       e_data, vedata, psl->stroke_pass, e_data->gpencil_fill_sh,
+                                       e_data, vedata, stroke_pass, e_data->gpencil_fill_sh,
                                        gpd, gpl, gp_style, stl->storage->shgroup_id);
 
                                DRW_shgroup_call_range_add(
index dda0f5c..69b495c 100644 (file)
@@ -332,11 +332,16 @@ void GPENCIL_cache_init(void *vedata)
        stl->g_data->gp_object_cache = NULL;
 
        {
-               /* Stroke pass */
-               psl->stroke_pass = DRW_pass_create(
+               /* Stroke pass 2D */
+               psl->stroke_pass_2d = DRW_pass_create(
                        "GPencil Stroke Pass",
                        DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS | DRW_STATE_BLEND);
                stl->storage->shgroup_id = 0;
+               /* Stroke pass 3D */
+               psl->stroke_pass_3d = DRW_pass_create(
+                       "GPencil Stroke Pass",
+                       DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
+               stl->storage->shgroup_id = 0;
 
                /* edit pass */
                psl->edit_pass = DRW_pass_create(
@@ -689,7 +694,7 @@ static void gpencil_free_obj_runtime(GPENCIL_StorageList *stl)
 static void gpencil_draw_pass_range(
        GPENCIL_FramebufferList *fbl, GPENCIL_StorageList *stl,
        GPENCIL_PassList *psl, GPENCIL_TextureList *txl,
-       GPUFrameBuffer *fb,
+       GPUFrameBuffer *fb, bGPdata *gpd,
        DRWShadingGroup *init_shgrp, DRWShadingGroup *end_shgrp, bool multi)
 {
        if (init_shgrp == NULL) {
@@ -702,7 +707,8 @@ static void gpencil_draw_pass_range(
        }
 
        DRW_draw_pass_subset(
-               psl->stroke_pass, init_shgrp, end_shgrp);
+               GPENCIL_3D_DRAWMODE(gpd) ? psl->stroke_pass_3d : psl->stroke_pass_2d,
+               init_shgrp, end_shgrp);
 
        if ((!stl->storage->is_mat_preview) && (multi)) {
                MULTISAMPLE_GP_SYNC_DISABLE(stl->storage->multisamples, fbl, fb, txl);
@@ -811,7 +817,7 @@ void GPENCIL_draw_scene(void *ved)
                                                        /* draw pending groups */
                                                        gpencil_draw_pass_range(
                                                                fbl, stl, psl, txl, fbl->temp_fb_a,
-                                                               init_shgrp, end_shgrp, is_last);
+                                                               gpd, init_shgrp, end_shgrp, is_last);
 
                                                        /* draw current group in separated texture */
                                                        init_shgrp = array_elm->init_shgrp;
@@ -821,7 +827,7 @@ void GPENCIL_draw_scene(void *ved)
                                                        GPU_framebuffer_clear_color_depth(fbl->temp_fb_fx, clearcol, 1.0f);
                                                        gpencil_draw_pass_range(
                                                                fbl, stl, psl, txl, fbl->temp_fb_fx,
-                                                               init_shgrp, end_shgrp,
+                                                               gpd, init_shgrp, end_shgrp,
                                                                is_last);
 
                                                        /* Blend A texture and FX texture */
@@ -850,7 +856,7 @@ void GPENCIL_draw_scene(void *ved)
                                        /* last group */
                                        gpencil_draw_pass_range(
                                                fbl, stl, psl, txl, fbl->temp_fb_a,
-                                               init_shgrp, end_shgrp,
+                                               gpd, init_shgrp, end_shgrp,
                                                true);
                                }
 
index 97e3b7c..e1caef6 100644 (file)
@@ -174,7 +174,8 @@ typedef struct GPENCIL_StorageList {
 } GPENCIL_StorageList;
 
 typedef struct GPENCIL_PassList {
-       struct DRWPass *stroke_pass;
+       struct DRWPass *stroke_pass_2d;
+       struct DRWPass *stroke_pass_3d;
        struct DRWPass *edit_pass;
        struct DRWPass *drawing_pass;
        struct DRWPass *mix_pass;
@@ -451,4 +452,7 @@ void GPENCIL_render_to_image(void *vedata, struct RenderEngine *engine, struct R
        } \
 }
 
+#define GPENCIL_3D_DRAWMODE(gpd) \
+       ((gpd) && (gpd->draw_mode == GP_DRAWMODE_3D) && (gpd->xray_mode == GP_XRAY_3DSPACE))
+
 #endif /* __GPENCIL_ENGINE_H__ */
index 1d57cdc..29803c7 100644 (file)
@@ -15,6 +15,7 @@ uniform int texture_mix;
 uniform int texture_flip;
 uniform float texture_opacity;
 uniform int xraymode;
+uniform int drawmode;
 uniform float layer_opacity;
 
 uniform sampler2D myTexture;
@@ -32,6 +33,9 @@ uniform int texture_clamp;
 #define GP_XRAY_3DSPACE 1
 #define GP_XRAY_BACK  2
 
+#define GP_DRAWMODE_2D 0
+#define GP_DRAWMODE_3D 1
+
 in vec4 finalColor;
 in vec2 texCoord_interp;
 out vec4 fragColor;
@@ -136,7 +140,13 @@ void main()
                gl_FragDepth = 0.000001;
        }
        else if (xraymode == GP_XRAY_3DSPACE) {
-               gl_FragDepth = gl_FragCoord.z;
+       /* if 3D mode, move slightly the fill to avoid z-fighting between stroke and fill on same stroke */
+               if (drawmode == GP_DRAWMODE_3D) {
+                       gl_FragDepth = gl_FragCoord.z * 1.0001;
+               }
+               else {
+                       gl_FragDepth = gl_FragCoord.z;
+               }
        }
        else if  (xraymode == GP_XRAY_BACK) {
                gl_FragDepth = 0.999999;
@@ -144,4 +154,5 @@ void main()
        else {
                gl_FragDepth = 0.000001;
        }
+       
 }
index 4f9de5e..3c5b90a 100644 (file)
@@ -510,7 +510,11 @@ typedef struct bGPdata {
        char _pad2[6];
        int   totstroke;
        int   totpoint;
-       char _pad3[4];
+
+       /** Draw mode for strokes (eGP_DrawMode). */
+       short draw_mode;
+       char _pad3[2];
+
        bGPgrid grid;
 
        bGPdata_Runtime runtime;
@@ -616,6 +620,12 @@ typedef enum eGP_DepthOrdering {
        GP_XRAY_BACK  = 2,
 } eGP_DepthOrdering;
 
+/* draw modes (Use 2D or 3D position) */
+typedef enum eGP_DrawMode {
+       GP_DRAWMODE_2D = 0,
+       GP_DRAWMODE_3D = 1
+} eGP_DrawMode;
+
 /* ***************************************** */
 /* Mode Checking Macros */
 
index 2409f12..b604092 100644 (file)
@@ -51,6 +51,12 @@ static const EnumPropertyItem parent_type_items[] = {
 };
 
 #ifndef RNA_RUNTIME
+static EnumPropertyItem rna_enum_gpencil_drawmodes_items[] = {
+       {GP_DRAWMODE_2D, "2D", 0, "2D Layers",  "Draw strokes using grease pencil layers to define order"},
+       {GP_DRAWMODE_3D, "3D", 0, "3D Location", "Draw strokes using real 3D position in 3D space"},
+       {0, NULL, 0, NULL, NULL},
+};
+
 static EnumPropertyItem rna_enum_gpencil_xraymodes_items[] = {
        {GP_XRAY_FRONT, "FRONT", 0, "Front", "Draw all strokes in front"},
        {GP_XRAY_3DSPACE, "3DSPACE", 0, "3D Space", "Draw strokes relative to other objects in 3D space"},
@@ -1490,6 +1496,14 @@ static void rna_def_gpencil_data(BlenderRNA *brna)
        RNA_def_property_srna(prop, "IDMaterials"); /* see rna_ID.c */
        RNA_def_property_collection_funcs(prop, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "rna_IDMaterials_assign_int");
 
+       /* draw modes */
+       prop = RNA_def_property(srna, "draw_mode", PROP_ENUM, PROP_NONE);
+       RNA_def_property_enum_sdna(prop, NULL, "draw_mode");
+       RNA_def_property_enum_items(prop, rna_enum_gpencil_drawmodes_items);
+       RNA_def_property_ui_text(prop, "Mode",
+               "Defines how the strokes are ordered in 3D space");
+       RNA_def_property_update(prop, NC_GPENCIL | ND_DATA, "rna_GPencil_update");
+
        /* xray modes */
        prop = RNA_def_property(srna, "xray_mode", PROP_ENUM, PROP_NONE);
        RNA_def_property_enum_sdna(prop, NULL, "xray_mode");