[Bf-blender-cvs] [9ad8f9e3bac] greasepencil-refactor: GPencil: Refactor: Add Dots & Square drawing type

Clément Foucault noreply at git.blender.org
Thu Dec 12 02:24:31 CET 2019


Commit: 9ad8f9e3bacbaa264fedaf4653af92b03703879e
Author: Clément Foucault
Date:   Thu Dec 12 02:05:25 2019 +0100
Branches: greasepencil-refactor
https://developer.blender.org/rB9ad8f9e3bacbaa264fedaf4653af92b03703879e

GPencil: Refactor: Add Dots & Square drawing type

Also add vertex color opacity

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

M	source/blender/draw/engines/gpencil/gpencil_draw_data.c
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
M	source/blender/draw/engines/gpencil/shaders/gpencil_common_lib.glsl
M	source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
M	source/blender/draw/engines/gpencil/shaders/gpencil_vert.glsl

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

diff --git a/source/blender/draw/engines/gpencil/gpencil_draw_data.c b/source/blender/draw/engines/gpencil/gpencil_draw_data.c
index a57593ecb8b..d2842d35155 100644
--- a/source/blender/draw/engines/gpencil/gpencil_draw_data.c
+++ b/source/blender/draw/engines/gpencil/gpencil_draw_data.c
@@ -108,14 +108,34 @@ GPENCIL_MaterialPool *gpencil_material_pool_create(GPENCIL_PrivateData *pd, Obje
     gpMaterial *mat_data = &pool->mat_data[mat_id];
     MaterialGPencilStyle *gp_style = BKE_material_gpencil_settings_get(ob, i + 1);
 
-    mat_data->flag = 0;
+    if (gp_style->mode == GP_STYLE_MODE_LINE) {
+      mat_data->flag = 0;
+    }
+    else {
+      switch (gp_style->alignment_mode) {
+        case GP_STYLE_FOLLOW_PATH:
+          mat_data->flag = GP_STROKE_ALIGNMENT_STROKE;
+          break;
+        case GP_STYLE_FOLLOW_OBJ:
+          mat_data->flag = GP_STROKE_ALIGNMENT_OBJECT;
+          break;
+        case GP_STYLE_FOLLOW_FIXED:
+        default:
+          mat_data->flag = GP_STROKE_ALIGNMENT_FIXED;
+          break;
+      }
+
+      if (gp_style->mode == GP_STYLE_MODE_DOTS) {
+        mat_data->flag |= GP_STROKE_DOTS;
+      }
+    }
 
     /* Stroke Style */
     if ((gp_style->stroke_style == GP_STYLE_STROKE_STYLE_TEXTURE) && (gp_style->sima)) {
       /* TODO finish. */
       bool premul;
       pool->tex_stroke[mat_id] = gpencil_image_texture_get(gp_style->sima, &premul);
-      mat_data->flag |= GP_STROKE_TEXTURE_USE;
+      mat_data->flag |= pool->tex_stroke[mat_id] ? GP_STROKE_TEXTURE_USE : 0;
       mat_data->flag |= premul ? GP_STROKE_TEXTURE_PREMUL : 0;
       copy_v4_v4(mat_data->stroke_color, gp_style->stroke_rgba);
       mat_data->stroke_texture_mix = 1.0f - gp_style->mix_stroke_factor;
@@ -132,7 +152,7 @@ GPENCIL_MaterialPool *gpencil_material_pool_create(GPENCIL_PrivateData *pd, Obje
       bool use_clip = (gp_style->flag & GP_STYLE_COLOR_TEX_CLAMP) != 0;
       bool premul;
       pool->tex_fill[mat_id] = gpencil_image_texture_get(gp_style->ima, &premul);
-      mat_data->flag |= GP_FILL_TEXTURE_USE;
+      mat_data->flag |= pool->tex_fill[mat_id] ? GP_FILL_TEXTURE_USE : 0;
       mat_data->flag |= premul ? GP_FILL_TEXTURE_PREMUL : 0;
       mat_data->flag |= use_clip ? GP_FILL_TEXTURE_CLIP : 0;
       gpencil_uv_transform_get(gp_style->texture_offset,
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index 96b2bbe02f5..995ea811491 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -288,12 +288,9 @@ static void GPENCIL_engine_init_new(void *ved)
 
   DRW_texture_ensure_2d(&txl->dummy_texture, 1, 1, GPU_R8, 0);
 
-  if (txl->checker_texture == NULL) {
-    float pixels[4][4] = {{1.0f, 1.0f, 1.0f, 1.0f},
-                          {0.0f, 0.0f, 0.0f, 0.0f},
-                          {0.0f, 0.0f, 0.0f, 0.0f},
-                          {1.0f, 1.0f, 1.0f, 1.0f}};
-    txl->checker_texture = DRW_texture_create_2d(2, 2, GPU_RGBA8, DRW_TEX_WRAP, (float *)pixels);
+  if (txl->dummy_texture == NULL) {
+    float pixels[1][4] = {{1.0f, 0.0f, 1.0f, 1.0f}};
+    txl->dummy_texture = DRW_texture_create_2d(1, 1, GPU_RGBA8, DRW_TEX_WRAP, (float *)pixels);
   }
 
   GPENCIL_ViewLayerData *vldata = GPENCIL_view_layer_data_ensure();
@@ -309,7 +306,6 @@ static void GPENCIL_engine_init_new(void *ved)
   stl->pd->gp_layer_pool = vldata->gp_layer_pool;
   stl->pd->gp_vfx_pool = vldata->gp_vfx_pool;
   stl->pd->last_material_pool = NULL;
-  stl->pd->checker_tex = txl->checker_texture;
 }
 
 void GPENCIL_engine_init(void *vedata)
@@ -793,6 +789,7 @@ static void gp_layer_cache_populate(bGPDlayer *gpl,
   DRW_shgroup_uniform_float_copy(iter->grp, "thicknessScale", object_scale);
   DRW_shgroup_uniform_float_copy(iter->grp, "thicknessOffset", (float)gpl->line_change);
   DRW_shgroup_uniform_float_copy(iter->grp, "thicknessWorldScale", thickness_scale);
+  DRW_shgroup_uniform_float_copy(iter->grp, "vertexColorOpacity", gpl->vertex_paint_opacity);
 }
 
 static void gp_stroke_cache_populate(bGPDlayer *UNUSED(gpl),
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index e7cd360f48e..1fc3139a306 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -79,6 +79,7 @@ typedef struct gpMaterial {
 #define GP_STROKE_TEXTURE_USE (1 << 3)
 #define GP_STROKE_TEXTURE_STENCIL (1 << 4)
 #define GP_STROKE_TEXTURE_PREMUL (1 << 5)
+#define GP_STROKE_DOTS (1 << 6)
 #define GP_FILL_TEXTURE_USE (1 << 10)
 #define GP_FILL_TEXTURE_PREMUL (1 << 11)
 #define GP_FILL_TEXTURE_CLIP (1 << 12)
@@ -315,8 +316,6 @@ typedef struct GPENCIL_FramebufferList {
 typedef struct GPENCIL_TextureList {
   /* Dummy texture to avoid errors cause by empty sampler. */
   struct GPUTexture *dummy_texture;
-  /* Checker texture used in checkerboard fill type. */
-  struct GPUTexture *checker_texture;
 
   /* multisample textures */
   struct GPUTexture *multisample_color;
@@ -389,8 +388,6 @@ typedef struct GPENCIL_PrivateData {
   struct BLI_memblock *gp_material_pool;
   /* Last used material pool. */
   GPENCIL_MaterialPool *last_material_pool;
-  /* Copy of txl pointer. */
-  struct GPUTexture *checker_tex;
   /* Current frame */
   int cfra;
 } GPENCIL_PrivateData;
diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_common_lib.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_common_lib.glsl
index fc83885bb26..319bf7cbb07 100644
--- a/source/blender/draw/engines/gpencil/shaders/gpencil_common_lib.glsl
+++ b/source/blender/draw/engines/gpencil/shaders/gpencil_common_lib.glsl
@@ -23,6 +23,7 @@ struct gpMaterial {
 #define GP_STROKE_TEXTURE_USE (1 << 3)
 #define GP_STROKE_TEXTURE_STENCIL (1 << 4)
 #define GP_STROKE_TEXTURE_PREMUL (1 << 5)
+#define GP_STROKE_DOTS (1 << 6)
 #define GP_FILL_TEXTURE_USE (1 << 10)
 #define GP_FILL_TEXTURE_PREMUL (1 << 11)
 #define GP_FILL_TEXTURE_CLIP (1 << 12)
diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
index 03cda90a2e4..554d9389a9f 100644
--- a/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
+++ b/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
@@ -9,28 +9,38 @@ flat in int matFlag;
 
 out vec4 fragColor;
 
+float length_squared(vec2 v)
+{
+  return dot(v, v);
+}
+
 void main()
 {
   vec4 col;
   if (GP_FLAG_TEST(matFlag, GP_STROKE_TEXTURE_USE)) {
     bool premul = GP_FLAG_TEST(matFlag, GP_STROKE_TEXTURE_PREMUL);
     col = texture_read_as_srgb(gpStrokeTexture, premul, finalUvs);
-    col.rgb *= col.a;
   }
   else if (GP_FLAG_TEST(matFlag, GP_FILL_TEXTURE_USE)) {
     bool use_clip = GP_FLAG_TEST(matFlag, GP_FILL_TEXTURE_CLIP);
     vec2 uvs = (use_clip) ? clamp(finalUvs, 0.0, 1.0) : finalUvs;
     bool premul = GP_FLAG_TEST(matFlag, GP_FILL_TEXTURE_PREMUL);
     col = texture_read_as_srgb(gpFillTexture, premul, uvs);
-    col.rgb *= col.a;
   }
   else /* SOLID */ {
     col = vec4(1.0);
   }
+  col.rgb *= col.a;
   /* Composite all other colors on top of texture color.
    * Everything is premult by col.a to have the stencil effect. */
   fragColor = col * finalColorMul + col.a * finalColorAdd;
 
+  if (GP_FLAG_TEST(matFlag, GP_STROKE_DOTS)) {
+    const float rad_sqr_inv = 1.0 / 0.25;
+    float dist = 1.0 - rad_sqr_inv * length_squared(finalUvs - 0.5);
+    fragColor *= clamp(dist, 0.0, 1.0);
+  }
+
   if (fragColor.a < 0.001) {
     discard;
   }
diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_vert.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_vert.glsl
index e082b87ad62..e9317f8bef7 100644
--- a/source/blender/draw/engines/gpencil/shaders/gpencil_vert.glsl
+++ b/source/blender/draw/engines/gpencil/shaders/gpencil_vert.glsl
@@ -11,6 +11,7 @@ uniform float thicknessWorldScale;
 
 /* Per Layer */
 uniform float thicknessOffset;
+uniform float vertexColorOpacity;
 
 in vec2 ma1;
 in vec2 ma2;
@@ -21,6 +22,8 @@ in vec4 pos;  /* Prev adj vert */
 in vec4 pos1; /* Current edge */
 in vec4 pos2; /* Current edge */
 in vec4 pos3; /* Next adj vert */
+#define thickness1 pos1.w
+#define thickness2 pos2.w
 /* xy is UV for fills, z is U of stroke, w is cosine of UV angle with sign of sine.  */
 in vec4 uv1;
 in vec4 uv2;
@@ -71,11 +74,30 @@ vec2 safe_normalize(vec2 v)
   }
 }
 
+float stroke_thickness_modulate(float thickness)
+{
+  /* Modify stroke thickness by object and layer factors.-*/
+  thickness *= thicknessScale;
+  thickness += thicknessOffset;
+  thickness = max(1.0, thickness);
+
+  if (thicknessIsScreenSpace) {
+    /* Multiply offset by view Z so that offset is constant in screenspace.
+     * (e.i: does not change with the distance to camera) */
+    thickness *= gl_Position.w;
+  }
+  else {
+    /* World space point size. */
+    thickness *= thicknessWorldScale * ProjectionMatrix[1][1] * sizeViewport.y;
+  }
+  return thickness;
+}
+
 void stroke_color_output(vec4 stroke_col, vec4 vert_col, float vert_strength, float mix_tex)
 {
   /* Mix stroke with vertex color. */
   vec4 mixed_col;
-  mixed_col.rgb = mix(stroke_col.rgb, vert_col.rgb, vert_col.a);
+  mixed_col.rgb = mix(stroke_col.rgb, vert_col.rgb, vert_col.a * vertexColorOpacity);
   mixed_col.a = clamp(stroke_col.a * vert_strength, 0.0, 1.0);
   /**
    * This is what the fragment shader looks like.
@@ -96,8 +118,15 @@ void stroke_color_output(vec4 stroke_col, vec4 vert_col, float vert_strength, fl
 
 void stroke_vertex()
 {
+  int m = int(ma1.x);
+  bool is_dot = false;
+
+  if (m != -1.0) {
+    is_dot = GP_FLAG_TEST(materials[m].flag, GP_STROKE_ALIGNMENT);
+  }
+
   /* Enpoints, we discard the vertices. */
-  if (ma1.x == -1.0 || ma2.x == -1.0) {
+  if (ma1.x == -1.0 || (!is_dot && ma2.x == -1.0)) {
     discard_vert();
     return;
   }
@@ -105,10 +134,12 @@ void stroke_vertex()
   mat4 model_mat = model_matrix_get();
 
   /* Avoid using a vertex attrib for quad positioning. */
-  float x = float(gl_VertexID & 1);       /* [0..1] */
-  float y = float(gl_VertexID & 2) - 1.0; /* [-1..1] */
+  float x = float(gl_VertexID & 1) * 2.0

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list