From eedd7b27f9fb45e67cb10a98880cb8a9db4cac48 Mon Sep 17 00:00:00 2001 From: Piotr Ostrowski Date: Mon, 5 Oct 2020 16:20:06 +0200 Subject: Fix T81392: subdivision surface poor shading without limit surface When the limit surface is disabled OpenSubdiv generates a set of linear patches which are only C0 continuous, not C1. This makes it impossible to evaluate derivatives at vertices which, in this mode, are by definition put at boundaries of patches. Normals are calculated from those derivatives. Solution is to disable normal calculation and let it be done downstream, as for other modifiers. This limitation is also the reason that non feature adaptive subdivision is badly suited for GPU evaluation. Differential Revision: https://developer.blender.org/D9103 --- source/blender/blenkernel/intern/subdiv_mesh.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenkernel/intern/subdiv_mesh.c b/source/blender/blenkernel/intern/subdiv_mesh.c index 8f97fb82db7..b0adec4e194 100644 --- a/source/blender/blenkernel/intern/subdiv_mesh.c +++ b/source/blender/blenkernel/intern/subdiv_mesh.c @@ -499,7 +499,9 @@ static void subdiv_accumulate_vertex_normal_and_displacement(SubdivMeshContext * BKE_subdiv_eval_displacement(subdiv, ptex_face_index, u, v, dPdu, dPdv, D); add_v3_v3(subdiv_vert->co, D); } - ++ctx->accumulated_counters[subdiv_vertex_index]; + if (ctx->accumulated_counters) { + ++ctx->accumulated_counters[subdiv_vertex_index]; + } } /** \} */ @@ -573,11 +575,11 @@ static void evaluate_vertex_and_apply_displacement_copy(const SubdivMeshContext MVert *subdiv_vert) { const int subdiv_vertex_index = subdiv_vert - ctx->subdiv_mesh->mvert; - const float inv_num_accumulated = 1.0f / ctx->accumulated_counters[subdiv_vertex_index]; /* Displacement is accumulated in subdiv vertex position. * Needs to be backed up before copying data from original vertex. */ float D[3] = {0.0f, 0.0f, 0.0f}; if (ctx->have_displacement) { + const float inv_num_accumulated = 1.0f / ctx->accumulated_counters[subdiv_vertex_index]; copy_v3_v3(D, subdiv_vert->co); mul_v3_fl(D, inv_num_accumulated); } @@ -606,11 +608,11 @@ static void evaluate_vertex_and_apply_displacement_interpolate( MVert *subdiv_vert) { const int subdiv_vertex_index = subdiv_vert - ctx->subdiv_mesh->mvert; - const float inv_num_accumulated = 1.0f / ctx->accumulated_counters[subdiv_vertex_index]; /* Displacement is accumulated in subdiv vertex position. * Needs to be backed up before copying data from original vertex. */ float D[3] = {0.0f, 0.0f, 0.0f}; if (ctx->have_displacement) { + const float inv_num_accumulated = 1.0f / ctx->accumulated_counters[subdiv_vertex_index]; copy_v3_v3(D, subdiv_vert->co); mul_v3_fl(D, inv_num_accumulated); } @@ -621,6 +623,7 @@ static void evaluate_vertex_and_apply_displacement_interpolate( add_v3_v3(subdiv_vert->co, D); /* Copy normal from accumulated storage. */ if (ctx->can_evaluate_normals) { + const float inv_num_accumulated = 1.0f / ctx->accumulated_counters[subdiv_vertex_index]; float N[3]; copy_v3_v3(N, ctx->accumulated_normals[subdiv_vertex_index]); mul_v3_fl(N, inv_num_accumulated); @@ -1213,7 +1216,7 @@ Mesh *BKE_subdiv_to_mesh(Subdiv *subdiv, subdiv_context.coarse_mesh = coarse_mesh; subdiv_context.subdiv = subdiv; subdiv_context.have_displacement = (subdiv->displacement_evaluator != NULL); - subdiv_context.can_evaluate_normals = !subdiv_context.have_displacement; + subdiv_context.can_evaluate_normals = !subdiv_context.have_displacement && subdiv_context.subdiv->settings.is_adaptive; /* Multi-threaded traversal/evaluation. */ BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH_GEOMETRY); SubdivForeachContext foreach_context; -- cgit v1.2.3