From 579e8ebe79a1fd5ebd2fb4562c4d4b3f8c22f47d Mon Sep 17 00:00:00 2001 From: YimingWu Date: Sat, 22 Jan 2022 16:41:08 +0800 Subject: LineArt: Back face culling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Option to discard back faced triangles, this speeds up calculation especially for when you only want to show visible feature lines. Reviewed By: Antonio Vazquez (antoniov), Aleš Jelovčan (frogstomp) Differential Revision: https://developer.blender.org/D13848 --- .../gpencil_modifiers/intern/MOD_gpencillineart.c | 1 + .../gpencil_modifiers/intern/lineart/MOD_lineart.h | 1 + .../gpencil_modifiers/intern/lineart/lineart_cpu.c | 43 ++++++++++++++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) (limited to 'source/blender/gpencil_modifiers') diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c b/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c index 1dc6816980c..ca48dc5b025 100644 --- a/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c +++ b/source/blender/gpencil_modifiers/intern/MOD_gpencillineart.c @@ -409,6 +409,7 @@ static void options_panel_draw(const bContext *UNUSED(C), Panel *panel) uiItemR(col, ptr, "use_clip_plane_boundaries", 0, NULL, ICON_NONE); uiItemR(col, ptr, "use_crease_on_smooth", 0, IFACE_("Crease On Smooth"), ICON_NONE); uiItemR(col, ptr, "use_crease_on_sharp", 0, IFACE_("Crease On Sharp"), ICON_NONE); + uiItemR(col, ptr, "use_back_face_culling", 0, NULL, ICON_NONE); } static void style_panel_draw(const bContext *UNUSED(C), Panel *panel) diff --git a/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h b/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h index 2691dcc8408..c3a93867079 100644 --- a/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h +++ b/source/blender/gpencil_modifiers/intern/lineart/MOD_lineart.h @@ -300,6 +300,7 @@ typedef struct LineartRenderBuffer { bool use_loose_edge_chain; bool use_geometry_space_chain; bool use_image_boundary_trimming; + bool use_back_face_culling; bool filter_face_mark; bool filter_face_mark_invert; diff --git a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c index 923646a133a..2e0030b2112 100644 --- a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c +++ b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c @@ -1346,6 +1346,10 @@ static void lineart_main_cull_triangles(LineartRenderBuffer *rb, bool clip_far) /* Select the triangle in the array. */ tri = (void *)(((uchar *)eln->pointer) + rb->triangle_size * i); + if (tri->flags & LRT_CULL_DISCARD) { + continue; + } + LRT_CULL_DECIDE_INSIDE LRT_CULL_ENSURE_MEMORY lineart_triangle_cull_single(rb, @@ -1536,8 +1540,31 @@ static uint16_t lineart_identify_feature_line(LineartRenderBuffer *rb, double dot_1 = 0, dot_2 = 0; double result; - if (rb->cam_is_persp) { - sub_v3_v3v3_db(view_vector, l->gloc, rb->camera_pos); + if (rb->use_contour || rb->use_back_face_culling) { + + if (rb->cam_is_persp) { + sub_v3_v3v3_db(view_vector, rb->camera_pos, l->gloc); + } + else { + view_vector = rb->view_vector; + } + + dot_1 = dot_v3v3_db(view_vector, tri1->gn); + dot_2 = dot_v3v3_db(view_vector, tri2->gn); + + if (rb->use_contour && (result = dot_1 * dot_2) <= 0 && (dot_1 + dot_2)) { + edge_flag_result |= LRT_EDGE_FLAG_CONTOUR; + } + + /* Because the ray points towards the camera, so backface is when dot value being negative.*/ + if (rb->use_back_face_culling) { + if (dot_1 < 0) { + tri1->flags |= LRT_CULL_DISCARD; + } + if (dot_2 < 0) { + tri2->flags |= LRT_CULL_DISCARD; + } + } } else { view_vector = rb->view_vector; @@ -3160,6 +3187,12 @@ static LineartRenderBuffer *lineart_create_render_buffer(Scene *scene, rb->chain_preserve_details = (lmd->calculation_flags & LRT_CHAIN_PRESERVE_DETAILS) != 0; + /* This is used to limit calculation to a certain level to save time, lines who have higher + * occlusion levels will get ignored. */ + rb->max_occlusion_level = lmd->level_end_override; + + rb->use_back_face_culling = (lmd->calculation_flags & LRT_USE_BACK_FACE_CULLING) != 0; + int16_t edge_types = lmd->edge_types_override; rb->use_contour = (edge_types & LRT_EDGE_FLAG_CONTOUR) != 0; @@ -4178,12 +4211,6 @@ bool MOD_lineart_compute_feature_lines(Depsgraph *depsgraph, * See definition of LineartTriangleThread for details. */ rb->triangle_size = lineart_triangle_size_get(scene, rb); - /* This is used to limit calculation to a certain level to save time, lines who have higher - * occlusion levels will get ignored. */ - rb->max_occlusion_level = (lmd->flags & LRT_GPENCIL_USE_CACHE) ? - lmd->level_end_override : - (lmd->use_multiple_levels ? lmd->level_end : lmd->level_start); - /* FIXME(Yiming): See definition of int #LineartRenderBuffer::_source_type for detailed. */ rb->_source_type = lmd->source_type; rb->_source_collection = lmd->source_collection; -- cgit v1.2.3