Blender V5.3
object_cull.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <cstdlib>
6
7#include "scene/camera.h"
8
9#include "BLI_bounds.hh"
10
11#include "blender/object_cull.h"
12#include "blender/util.h"
13
15
17 : use_scene_camera_cull_(false),
18 use_camera_cull_(false),
19 camera_cull_margin_(0.0f),
20 use_scene_distance_cull_(false),
21 use_distance_cull_(false),
22 distance_cull_margin_(0.0f)
23{
24 if ((b_scene.r.mode & blender::R_SIMPLIFY) != 0) {
25 blender::PointerRNA scene_rna_ptr = RNA_id_pointer_create(&b_scene.id);
26 blender::PointerRNA cscene = RNA_pointer_get(&scene_rna_ptr, "cycles");
27
28 const bool cam_supported = (scene->camera->get_camera_type() == CAMERA_PERSPECTIVE) ||
29 (scene->camera->get_camera_type() == CAMERA_ORTHOGRAPHIC);
30
31 use_scene_camera_cull_ = cam_supported && ((b_scene.r.scemode & blender::R_MULTIVIEW) == 0) &&
32 get_boolean(cscene, "use_camera_cull");
33 use_scene_distance_cull_ = cam_supported &&
34 ((b_scene.r.scemode & blender::R_MULTIVIEW) == 0) &&
35 get_boolean(cscene, "use_distance_cull");
36
37 camera_cull_margin_ = get_float(cscene, "camera_cull_margin");
38 distance_cull_margin_ = get_float(cscene, "distance_cull_margin");
39
40 if (distance_cull_margin_ == 0.0f) {
41 use_scene_distance_cull_ = false;
42 }
43 }
44}
45
47{
48 if (!use_scene_camera_cull_ && !use_scene_distance_cull_) {
49 return;
50 }
51
52 blender::PointerRNA b_ob_rna_ptr = RNA_id_pointer_create(&b_ob.id);
53 blender::PointerRNA cobject = RNA_pointer_get(&b_ob_rna_ptr, "cycles");
54
55 use_camera_cull_ = use_scene_camera_cull_ && get_boolean(cobject, "use_camera_cull");
56 use_distance_cull_ = use_scene_distance_cull_ && get_boolean(cobject, "use_distance_cull");
57
58 if (use_camera_cull_ || use_distance_cull_) {
59 /* Need to have proper projection matrix. */
60 scene->camera->update(scene);
61 }
62}
63
65{
66 if (!use_camera_cull_ && !use_distance_cull_) {
67 return false;
68 }
69
70 /* Compute world space bounding box corners. */
71 float3 bb[8];
72 std::array<blender::float3, 8> boundbox;
73 if (const std::optional<blender::Bounds<blender::float3>> bounds =
74 BKE_object_boundbox_eval_cached_get(&b_ob))
75 {
76 boundbox = blender::bounds::corners(*bounds);
77 }
78 else {
79 boundbox.fill(blender::float3(0));
80 }
81 for (int i = 0; i < 8; ++i) {
82 const float3 p = make_float3(boundbox[i].x, boundbox[i].y, boundbox[i].z);
83 bb[i] = transform_point(&tfm, p);
84 }
85
86 const bool camera_culled = use_camera_cull_ && test_camera(scene, bb);
87 const bool distance_culled = use_distance_cull_ && test_distance(scene, bb);
88
89 return ((camera_culled && distance_culled) || (camera_culled && !use_distance_cull_) ||
90 (distance_culled && !use_camera_cull_));
91}
92
93/* TODO(sergey): Not really optimal, consider approaches based on k-DOP in order
94 * to reduce number of objects which are wrongly considered visible.
95 */
96bool BlenderObjectCulling::test_camera(Scene *scene, const float3 bb[8])
97{
98 Camera *cam = scene->camera;
99 const ProjectionTransform &worldtondc = cam->worldtondc;
102 bool all_behind = true;
103 for (int i = 0; i < 8; ++i) {
104 float3 p = bb[i];
105 const float4 b = make_float4(p, 1.0f);
106 const float4 c = make_float4(
107 dot(worldtondc.x, b), dot(worldtondc.y, b), dot(worldtondc.z, b), dot(worldtondc.w, b));
108 p = make_float3(c / c.w);
109 if (c.z < 0.0f) {
110 p.x = 1.0f - p.x;
111 p.y = 1.0f - p.y;
112 }
113 if (c.z >= -camera_cull_margin_) {
114 all_behind = false;
115 }
116 bb_min = min(bb_min, p);
117 bb_max = max(bb_max, p);
118 }
119 if (all_behind) {
120 return true;
121 }
122 return (bb_min.x >= 1.0f + camera_cull_margin_ || bb_min.y >= 1.0f + camera_cull_margin_ ||
123 bb_max.x <= -camera_cull_margin_ || bb_max.y <= -camera_cull_margin_);
124}
125
126bool BlenderObjectCulling::test_distance(Scene *scene, const float3 bb[8])
127{
128 const float3 camera_position = transform_get_column(&scene->camera->get_matrix(), 3);
131
132 /* Find min & max points for x & y & z on bounding box */
133 for (int i = 0; i < 8; ++i) {
134 const float3 p = bb[i];
135 bb_min = min(bb_min, p);
136 bb_max = max(bb_max, p);
137 }
138
139 const float3 closest_point = max(min(bb_max, camera_position), bb_min);
140 return (len_squared(camera_position - closest_point) >
141 distance_cull_margin_ * distance_cull_margin_);
142}
143
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
bool test(Scene *scene, blender::Object &b_ob, const Transform &tfm)
void init_object(Scene *scene, blender::Object &b_ob)
BlenderObjectCulling(Scene *scene, blender::Scene &b_scene)
ProjectionTransform worldtondc
void update(Scene *scene)
Definition scene.h:133
Camera * camera
Definition scene.h:145
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define FLT_MAX
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > float3
float3 transform_point(float3x3 mat, float3 point)
ccl_device_inline float3 camera_position(KernelGlobals kg)
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
std::array< VecBase< T, 3 >, 8 > corners(const Bounds< VecBase< T, 3 > > &bounds)
VecBase< float, 3 > float3
#define make_float4
#define min(a, b)
Definition sort.cc:38
struct RenderData r
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float z
Definition sky_math.h:225
float w
Definition sky_math.h:225
ccl_device_inline float3 transform_get_column(const Transform *t, const int column)
Definition transform.h:387