diff options
author | Alexander Gavrilov <angavrilov@gmail.com> | 2016-10-07 13:27:11 +0300 |
---|---|---|
committer | Alexander Gavrilov <angavrilov@gmail.com> | 2016-10-07 21:47:00 +0300 |
commit | 0a2a006775543292786e8642b20a594771fdc81c (patch) | |
tree | a4691a5ef1775477db13dae13ee9d9dd80efa2bc /source/blender | |
parent | 2dccf5a6e881973858de2c230cb610a2ea3a03e1 (diff) |
Collision: skip expensive BVH update if the collider doesn't move.
Since the collision modifier cannot be disabled, it causes a constant
hit on the viewport animation playback FPS. Most of this overhead can
be automatically removed in the case when the collider is static.
The updates are only skipped when the collider was stationary during
the preceding update as well, so the state is stored in a field.
Knowing that the collider is static can also be used to disable similar
BVH updates for substeps in the actual cloth simulation code.
Differential Revision: https://developer.blender.org/D2277
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenkernel/intern/collision.c | 9 | ||||
-rw-r--r-- | source/blender/blenloader/intern/readfile.c | 1 | ||||
-rw-r--r-- | source/blender/makesdna/DNA_modifier_types.h | 3 | ||||
-rw-r--r-- | source/blender/modifiers/intern/MOD_collision.c | 17 |
4 files changed, 26 insertions, 4 deletions
diff --git a/source/blender/blenkernel/intern/collision.c b/source/blender/blenkernel/intern/collision.c index 35a7aafdbde..18ca1407ba0 100644 --- a/source/blender/blenkernel/intern/collision.c +++ b/source/blender/blenkernel/intern/collision.c @@ -71,6 +71,15 @@ void collision_move_object(CollisionModifierData *collmd, float step, float prev float tv[3] = {0, 0, 0}; unsigned int i = 0; + /* the collider doesn't move this frame */ + if (collmd->is_static) { + for (i = 0; i < collmd->mvert_num; i++) { + zero_v3(collmd->current_v[i].co); + } + + return; + } + for (i = 0; i < collmd->mvert_num; i++) { sub_v3_v3v3(tv, collmd->xnew[i].co, collmd->x[i].co); VECADDS(collmd->current_x[i].co, collmd->x[i].co, tv, prevstep); diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index d86a89a0e48..139cb84fea1 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -5192,6 +5192,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb) collmd->time_x = collmd->time_xnew = -1000; collmd->mvert_num = 0; collmd->tri_num = 0; + collmd->is_static = false; collmd->bvhtree = NULL; collmd->tri = NULL; diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h index 0424dc98a25..9187b76f012 100644 --- a/source/blender/makesdna/DNA_modifier_types.h +++ b/source/blender/makesdna/DNA_modifier_types.h @@ -621,6 +621,9 @@ typedef struct CollisionModifierData { unsigned int mvert_num; unsigned int tri_num; float time_x, time_xnew; /* cfra time of modifier */ + char is_static; /* collider doesn't move this frame, i.e. x[].co==xnew[].co */ + char pad[7]; + struct BVHTree *bvhtree; /* bounding volume hierarchy for this cloth object */ } CollisionModifierData; diff --git a/source/blender/modifiers/intern/MOD_collision.c b/source/blender/modifiers/intern/MOD_collision.c index ff0c6500479..e7ff0a90fbc 100644 --- a/source/blender/modifiers/intern/MOD_collision.c +++ b/source/blender/modifiers/intern/MOD_collision.c @@ -60,6 +60,7 @@ static void initData(ModifierData *md) collmd->time_x = collmd->time_xnew = -1000; collmd->mvert_num = 0; collmd->tri_num = 0; + collmd->is_static = false; collmd->bvhtree = NULL; } @@ -87,6 +88,7 @@ static void freeData(ModifierData *md) collmd->time_x = collmd->time_xnew = -1000; collmd->mvert_num = 0; collmd->tri_num = 0; + collmd->is_static = false; } } @@ -169,6 +171,7 @@ static void deformVerts(ModifierData *md, Object *ob, ob->pd->pdef_sboft); collmd->time_x = collmd->time_xnew = current_time; + collmd->is_static = true; } else if (mvert_num == collmd->mvert_num) { /* put positions to old positions */ @@ -179,14 +182,19 @@ static void deformVerts(ModifierData *md, Object *ob, memcpy(collmd->xnew, dm->getVertArray(dm), mvert_num * sizeof(MVert)); + bool is_static = true; + for (i = 0; i < mvert_num; i++) { /* we save global positions */ mul_m4_v3(ob->obmat, collmd->xnew[i].co); + + /* detect motion */ + is_static = is_static && equals_v3v3(collmd->x[i].co, collmd->xnew[i].co); } - + memcpy(collmd->current_xnew, collmd->x, mvert_num * sizeof(MVert)); memcpy(collmd->current_x, collmd->x, mvert_num * sizeof(MVert)); - + /* check if GUI setting has changed for bvh */ if (collmd->bvhtree) { if (ob->pd->pdef_sboft != BLI_bvhtree_get_epsilon(collmd->bvhtree)) { @@ -206,7 +214,7 @@ static void deformVerts(ModifierData *md, Object *ob, collmd->tri, collmd->tri_num, ob->pd->pdef_sboft); } - else { + else if (!collmd->is_static || !is_static) { /* recalc static bounding boxes */ bvhtree_update_from_mvert( collmd->bvhtree, @@ -214,7 +222,8 @@ static void deformVerts(ModifierData *md, Object *ob, collmd->tri, collmd->tri_num, true); } - + + collmd->is_static = is_static; collmd->time_xnew = current_time; } else if (mvert_num != collmd->mvert_num) { |