From 480473f1f1008168e7784221b852a6d370a0f4f5 Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Sat, 15 Apr 2017 00:44:05 -0300 Subject: Object.raycast: Test the hit on the BoundBox first This avoids the unnecessary creation of bvhtree, which can be highly inefficient in some cases (for example: in the `operator_modal_view3d_raycast.py` template) --- source/blender/makesrna/intern/rna_object_api.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/blender/makesrna/intern/rna_object_api.c b/source/blender/makesrna/intern/rna_object_api.c index c680abe71a4..3b2e57a06e5 100644 --- a/source/blender/makesrna/intern/rna_object_api.c +++ b/source/blender/makesrna/intern/rna_object_api.c @@ -321,13 +321,19 @@ static void rna_Object_ray_cast( float origin[3], float direction[3], float distance, int *r_success, float r_location[3], float r_normal[3], int *r_index) { - BVHTreeFromMesh treeData = {NULL}; - if (ob->derivedFinal == NULL) { BKE_reportf(reports, RPT_ERROR, "Object '%s' has no mesh data to be used for ray casting", ob->id.name + 2); return; } + /* Test BoundBox */ + BoundBox *bb = BKE_object_boundbox_get(ob); + if (bb && !isect_ray_aabb_v3_simple(origin, direction, bb->vec[0], bb->vec[6], NULL, NULL)) { + goto finally; + } + + BVHTreeFromMesh treeData = {NULL}; + /* no need to managing allocation or freeing of the BVH data. this is generated and freed as needed */ bvhtree_from_mesh_looptri(&treeData, ob->derivedFinal, 0.0f, 4, 6); -- cgit v1.2.3