Fix T59152: dynamic topology constant detail should be in world space.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Wed, 23 Jan 2019 15:48:02 +0000 (16:48 +0100)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Wed, 23 Jan 2019 15:48:02 +0000 (16:48 +0100)
It seems more predictable, and makes more sense for future multi-object modes.

source/blender/blenkernel/BKE_pbvh.h
source/blender/blenkernel/intern/pbvh_bmesh.c
source/blender/editors/sculpt_paint/sculpt.c

index 47fedb565fc52e224058a8f5552d6e737b482cf8..0067f63ded0d71bc1a6de4563df87a4e671b2d77 100644 (file)
@@ -108,7 +108,7 @@ bool BKE_pbvh_node_raycast(
 bool BKE_pbvh_bmesh_node_raycast_detail(
         PBVHNode *node,
         const float ray_start[3], const float ray_normal[3],
-        float *depth, float *r_detail);
+        float *depth, float *r_edge_length);
 
 /* for orthographic cameras, project the far away ray segment points to the root node so
  * we can have better precision. */
index 3369b05ea607409166a765e564503435b46993ef..53dafdc97879e4911d8efc4e89235367ee4dcce1 100644 (file)
@@ -1549,7 +1549,7 @@ bool pbvh_bmesh_node_raycast(
 bool BKE_pbvh_bmesh_node_raycast_detail(
         PBVHNode *node,
         const float ray_start[3], const float ray_normal[3],
-        float *depth, float *r_detail)
+        float *depth, float *r_edge_length)
 {
        if (node->flag & PBVH_FullyHidden)
                return 0;
@@ -1588,7 +1588,7 @@ bool BKE_pbvh_bmesh_node_raycast_detail(
                float len3 = len_squared_v3v3(v_tri[2]->co, v_tri[0]->co);
 
                /* detail returned will be set to the maximum allowed size, so take max here */
-               *r_detail = sqrtf(max_fff(len1, len2, len3));
+               *r_edge_length = sqrtf(max_fff(len1, len2, len3));
        }
 
        return hit;
index e72b5490f8a5bef455f1a6b5264c62e021c5f22b..b2df942b2fa12939ed4f04f3046cd5887f636f2d 100644 (file)
@@ -1644,7 +1644,7 @@ typedef struct {
        const float *ray_start, *ray_normal;
        bool hit;
        float depth;
-       float detail;
+       float edge_length;
 } SculptDetailRaycastData;
 
 typedef struct {
@@ -4684,7 +4684,7 @@ static void sculpt_raycast_detail_cb(PBVHNode *node, void *data_v, float *tmin)
        if (BKE_pbvh_node_get_tmin(node) < *tmin) {
                SculptDetailRaycastData *srd = data_v;
                if (BKE_pbvh_bmesh_node_raycast_detail(node, srd->ray_start, srd->ray_normal,
-                                                      &srd->depth, &srd->detail))
+                                                      &srd->depth, &srd->edge_length))
                {
                        srd->hit = 1;
                        *tmin = srd->depth;
@@ -4972,7 +4972,8 @@ static void sculpt_stroke_update_step(bContext *C, struct PaintStroke *UNUSED(st
        sculpt_restore_mesh(sd, ob);
 
        if (sd->flags & (SCULPT_DYNTOPO_DETAIL_CONSTANT | SCULPT_DYNTOPO_DETAIL_MANUAL)) {
-               BKE_pbvh_bmesh_detail_size_set(ss->pbvh, 1.0f / sd->constant_detail);
+               float object_space_constant_detail = sd->constant_detail * mat4_to_scale(ob->imat);
+               BKE_pbvh_bmesh_detail_size_set(ss->pbvh, 1.0f / object_space_constant_detail);
        }
        else if (sd->flags & SCULPT_DYNTOPO_DETAIL_BRUSH) {
                BKE_pbvh_bmesh_detail_size_set(ss->pbvh, ss->cache->radius * sd->detail_percent / 100.0f);
@@ -5915,7 +5916,8 @@ static int sculpt_detail_flood_fill_exec(bContext *C, wmOperator *UNUSED(op))
        size = max_fff(dim[0], dim[1], dim[2]);
 
        /* update topology size */
-       BKE_pbvh_bmesh_detail_size_set(ss->pbvh, 1.0f / sd->constant_detail);
+       float object_space_constant_detail = sd->constant_detail * mat4_to_scale(ob->imat);
+       BKE_pbvh_bmesh_detail_size_set(ss->pbvh, 1.0f / object_space_constant_detail);
 
        sculpt_undo_push_begin("Dynamic topology flood fill");
        sculpt_undo_push_node(ob, NULL, SCULPT_UNDO_COORDS);
@@ -5988,14 +5990,14 @@ static void sample_detail(bContext *C, int mx, int my)
        srd.ray_start = ray_start;
        srd.ray_normal = ray_normal;
        srd.depth = depth;
-       srd.detail = sd->constant_detail;
+       srd.edge_length = 0.0f;
 
        BKE_pbvh_raycast(ob->sculpt->pbvh, sculpt_raycast_detail_cb, &srd,
                         ray_start, ray_normal, false);
 
-       if (srd.hit) {
-               /* convert edge length to detail resolution */
-               sd->constant_detail = 1.0f / srd.detail;
+       if (srd.hit && srd.edge_length > 0.0f) {
+               /* Convert edge length to world space detail resolution. */
+               sd->constant_detail = mat4_to_scale(ob->obmat) / srd.edge_length;
        }
 
        /* Restore context. */