Blender V5.3
eevee_camera.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_camera.h"
10#include "BKE_scene.hh"
11#include "BKE_screen.hh"
12#include "BLI_enum_flags.hh"
13#include "BLI_math_base.hh"
14#include "BLI_math_matrix.hh"
15#include "BLI_rect.hh"
16
17#include "DRW_render.hh"
18
19#include "DNA_camera_types.h"
20#include "DNA_view3d_types.h"
21
22#include "RE_engine.h"
23#include "RE_pipeline.h"
24#include "render_types.h"
25
26#include "eevee_camera.hh"
27#include "eevee_instance.hh"
28
29namespace blender::eevee {
30
37 POS_X = 1u << 0u,
38 NEG_X = 1u << 1u,
39 POS_Y = 1u << 2u,
40 NEG_Y = 1u << 3u,
41 BACK = 1u << 4u, /* Pos Z. */
42 FRONT = 1u << 5u, /* Neg Z. */
43};
47 PanoramicViewBits::NEG_X |
48 PanoramicViewBits::POS_Y |
49 PanoramicViewBits::NEG_Y;
50
52{
53 constexpr float angle_epsilon = 1.0e-4f;
54 const float half_fov = cam.fisheye_fov * 0.5f;
56 if (half_fov >= float(M_PI_4) - angle_epsilon) {
58 }
59 /* The back face becomes visible once the FOV cone reaches its corners. */
60 if (half_fov >= float(M_PI) - atanf(float(M_SQRT2)) - angle_epsilon) {
62 }
63 return mask;
64}
65
66/* True if `tan(theta) >= 1` somewhere in `[theta_lo, theta_hi]`. */
67static bool tan_range_reaches_one(float theta_lo, float theta_hi)
68{
69 BLI_assert(theta_lo <= theta_hi);
70 constexpr float inv_period = 1.0f / (2.0f * float(M_PI_2));
71 float min_k = ceilf((theta_lo - float(M_PI_2)) * inv_period);
72 float max_k = floorf((theta_hi - float(M_PI_4)) * inv_period);
73 return min_k <= max_k;
74}
75
76/* Same as `tan_range_reaches_one` but for `tan(theta) <= -1`. */
77static bool tan_range_reaches_minus_one(float theta_lo, float theta_hi)
78{
79 return tan_range_reaches_one(-theta_hi, -theta_lo);
80}
81
82/* Shifting or zooming push the sampled UV out of the front face so we need to include the faces
83 * it spills into. Note that `tan` is cyclical so a wide shift wraps around and needs the
84 * opposite neighbor face */
86{
87 const float2 lo = math::min(cam.uv_bias, cam.uv_bias + cam.uv_scale);
88 const float2 hi = math::max(cam.uv_bias, cam.uv_bias + cam.uv_scale);
89
90 /* see `equiangular_cubemap_face_to_direction` in eevee_camera_lib.bsl.hh */
91 const float alpha_lo = (0.5f - hi.x) * float(M_PI_2);
92 const float alpha_hi = (0.5f - lo.x) * float(M_PI_2);
93 const float beta_lo = (lo.y - 0.5f) * float(M_PI_2);
94 const float beta_hi = (hi.y - 0.5f) * float(M_PI_2);
95
97 /* direction.x = -tan(alpha). */
98 if (tan_range_reaches_one(alpha_lo, alpha_hi)) {
100 }
101 if (tan_range_reaches_minus_one(alpha_lo, alpha_hi)) {
103 }
104 /* direction.y = tan(beta). */
105 if (tan_range_reaches_one(beta_lo, beta_hi)) {
107 }
108 if (tan_range_reaches_minus_one(beta_lo, beta_hi)) {
110 }
111 return mask;
112}
113
115{
116 switch (cam.type) {
126 /* TODO(L3GiaBao): Can exclude faces when longitude/latitude (or u/v for central cylindrical)
127 * is restricted. */
128 return PANORAMIC_VIEW_ALL;
129 default:
131 }
132}
133
134/* Panoramic camera space of Cycles (see `blender_camera_matrix`). */
136{
137 if (type == CAMERA_PANO_MIRROR) {
138 /* X right, Y backward, Z up. */
139 return float4x4(float4(1.0f, 0.0f, 0.0f, 0.0f),
140 float4(0.0f, 0.0f, -1.0f, 0.0f),
141 float4(0.0f, 1.0f, 0.0f, 0.0f),
142 float4(0.0f, 0.0f, 0.0f, 1.0f));
143 }
144 /* X forward, Y left, Z up. */
145 return float4x4(float4(0.0f, -1.0f, 0.0f, 0.0f),
146 float4(0.0f, 0.0f, -1.0f, 0.0f),
147 float4(-1.0f, 0.0f, 0.0f, 0.0f),
148 float4(0.0f, 0.0f, 0.0f, 1.0f));
149}
150
152{
153 switch (cam.type) {
157 /* TODO(L3GiaBao): Clip the side faces. This needs a film filter that handles a render pixel
158 * density higher than the film one. */
159 const float half_fov = cam.fisheye_fov * 0.5f;
160 return (half_fov < float(M_PI_4)) ? tanf(half_fov) : 1.0f;
161 }
162 default:
163 return 1.0f;
164 }
165}
166
167/* -------------------------------------------------------------------- */
170
172{
173 const Object *camera_eval = inst_.camera_eval_object;
174
175 CameraData &data = data_;
176
177 if (camera_eval && camera_eval->type == OB_CAMERA) {
178 const blender::Camera *cam = reinterpret_cast<const blender::Camera *>(camera_eval->data);
179 switch (cam->type) {
180 default:
181 case CAM_PERSP:
182 data.type = CAMERA_PERSP;
183 break;
184 case CAM_ORTHO:
185 data.type = CAMERA_ORTHO;
186 break;
187 case CAM_PANO: {
188 switch (cam->panorama_type) {
189 default:
192 break;
195 break;
198 break;
201 break;
204 break;
207 break;
210 break;
211 }
212 break;
213 }
214 }
215 }
216 else if (inst_.drw_view) {
217 data.type = inst_.drw_view->is_persp() ? CAMERA_PERSP : CAMERA_ORTHO;
218 }
219 else {
220 /* Light-probe baking. */
221 data.type = CAMERA_PERSP;
222 }
223
224 float overscan = 0.0f;
225 if ((inst_.scene->eevee.flag & SCE_EEVEE_OVERSCAN) && (inst_.drw_view || inst_.render)) {
226 overscan = inst_.scene->eevee.overscan / 100.0f;
227 }
228 overscan_changed_ = assign_if_different(overscan_, overscan);
229 camera_changed_ = assign_if_different(last_camera_object_, inst_.camera_orig_object);
230}
231
233{
234 const Object *camera_eval = inst_.camera_eval_object;
235
236 CameraData &data = data_;
237
238 int2 display_extent = inst_.film.display_extent_get();
239 int2 film_extent = inst_.film.film_extent_get();
240 int2 film_offset = inst_.film.film_offset_get();
241 /* Over-scan in film pixel. Not the same as `render_overscan_get`.
242 * Panoramic camera render through per-face subviews so this isn't applicable */
243 int film_overscan = this->is_panoramic() ? 0 : Film::overscan_pixels_get(overscan_, film_extent);
244
245 rcti film_rect;
246 BLI_rcti_init(&film_rect,
247 film_offset.x,
248 film_offset.x + film_extent.x,
249 film_offset.y,
250 film_offset.y + film_extent.y);
251
252 Bounds<float2> uv_region = {float2(0.0f), float2(display_extent)};
253 if (inst_.drw_view) {
254 float2 uv_scale = float4(inst_.rv3d->viewcamtexcofac).xy();
255 float2 uv_bias = float4(inst_.rv3d->viewcamtexcofac).zw();
256 /* UV region inside the display extent reference frame. */
257 uv_region.min = (-uv_bias * float2(display_extent)) / uv_scale;
258 uv_region.max = uv_region.min + (float2(display_extent) / uv_scale);
259 }
260
261 data.uv_scale = float2(film_extent + film_overscan * 2) / uv_region.size();
262 data.uv_bias = (float2(film_offset - film_overscan) - uv_region.min) / uv_region.size();
263
264 if (inst_.is_baking()) {
265 /* Any view so that shadows and light culling works during irradiance bake. */
266 draw::View &view = inst_.volume_probes.bake.view_z_;
267 data.viewmat = view.viewmat();
268 data.viewinv = view.viewinv();
269 data.winmat = view.winmat();
270 data.type = CAMERA_ORTHO;
271
272 /* \note Follow camera parameters where distances are positive in front of the camera. */
273 data.clip_near = -view.far_clip();
274 data.clip_far = -view.near_clip();
275 data.fisheye_fov = data.fisheye_lens = -1.0f;
276 data.fisheye_sensor = float2(1.0f);
277 data.fisheye_polynomial_bias = 0.0f;
278 data.fisheye_polynomial_coefficients = float4(0.0f);
279 data.central_cylindrical_range = float4(0.0f);
280 data.equirect_bias = float2(0.0f);
281 data.equirect_scale = float2(0.0f);
282 data.uv_scale = float2(1.0f);
283 data.uv_bias = float2(0.0f);
284 }
285 else if (inst_.drw_view) {
286 data.viewmat = inst_.drw_view->viewmat();
287 data.viewinv = inst_.drw_view->viewinv();
288 data.winmat = inst_.drw_view->winmat();
289
290 if (film_offset != int2(0) || film_extent != display_extent) {
291 data.winmat = projection_crop_matrix(film_offset, film_extent, display_extent) * data.winmat;
292 }
293
294 if (overscan_ != 0.0f) {
295 data.winmat = projection_overscan_matrix(film_extent, int2(film_overscan)) * data.winmat;
296 }
297 }
298 else if (inst_.render) {
299 const Render *re = inst_.render->re;
300
301 RE_GetCameraWindow(inst_.render->re, camera_eval, data.winmat.ptr());
302
303 RE_GetCameraModelMatrix(re, camera_eval, data.viewinv.ptr());
304 data.viewmat = math::invert(data.viewinv);
305
306 rctf viewplane = re->viewplane;
307 BKE_camera_params_crop_viewplane(&viewplane, UNPACK2(display_extent), &film_rect);
308
310 re->clip_start,
311 re->clip_end,
312 viewplane,
313 overscan_,
314 data.winmat.ptr());
315 }
316 else {
317 data.viewmat = float4x4::identity();
318 data.viewinv = float4x4::identity();
319 data.winmat = math::projection::perspective(-0.1f, 0.1f, -0.1f, 0.1f, 0.1f, 1.0f);
320 }
321
322 data.wininv = math::invert(data.winmat);
323 data.persmat = data.winmat * data.viewmat;
324 data.persinv = math::invert(data.persmat);
325
326 const float4x4 camera_from_view = this->is_panoramic() ? panoramic_camera_from_view(data.type) :
328 data.shading_viewmat = camera_from_view * data.viewmat;
329 data.shading_viewinv = data.viewinv * math::transpose(camera_from_view);
330
331 is_camera_object_ = false;
332 if (camera_eval && camera_eval->type == OB_CAMERA) {
333 const blender::Camera *cam = reinterpret_cast<const blender::Camera *>(camera_eval->data);
334 data.clip_near = cam->clip_start;
335 data.clip_far = cam->clip_end;
336 if (this->is_panoramic()) {
337 /* Panoramic projection doesn't use winmat so offset the shared uv_bias instead. */
338 data.uv_bias += float2(cam->shiftx, cam->shifty);
339 }
340 if (data.type == CAMERA_PANO_EQUIRECT) {
341 data.equirect_bias.x = -cam->longitude_min + M_PI_2;
342 data.equirect_bias.y = -cam->latitude_min + M_PI_2;
343 data.equirect_scale.x = cam->longitude_min - cam->longitude_max;
344 data.equirect_scale.y = cam->latitude_min - cam->latitude_max;
345 /* Combine with uv_scale/bias to avoid doing extra computation. */
346 data.equirect_bias += data.uv_bias * data.equirect_scale;
347 data.equirect_scale *= data.uv_scale;
348 data.equirect_scale_inv = 1.0f / data.equirect_scale;
349 }
350 else {
351 data.equirect_bias = float2(0.0f);
352 data.equirect_scale = float2(0.0f);
353 data.equirect_scale_inv = float2(0.0f);
354 }
355 data.fisheye_fov = cam->fisheye_fov;
356 data.fisheye_lens = cam->fisheye_lens;
357 data.fisheye_polynomial_bias = cam->fisheye_polynomial_k0;
358 data.fisheye_polynomial_coefficients = float4(cam->fisheye_polynomial_k1,
359 cam->fisheye_polynomial_k2,
360 cam->fisheye_polynomial_k3,
361 cam->fisheye_polynomial_k4);
362 data.central_cylindrical_range = float4(
363 -cam->central_cylindrical_range_u_min,
364 -cam->central_cylindrical_range_u_max,
365 cam->central_cylindrical_range_v_min / cam->central_cylindrical_radius,
366 cam->central_cylindrical_range_v_max / cam->central_cylindrical_radius);
367 int render_width, render_height;
368 BKE_render_resolution(&inst_.scene->r, false, &render_width, &render_height);
369 const float fit_xratio = float(render_width) * inst_.scene->r.xasp;
370 const float fit_yratio = float(render_height) * inst_.scene->r.yasp;
371 const int sensor_fit = BKE_camera_sensor_fit(cam->sensor_fit, fit_xratio, fit_yratio);
372 const float sensor_size = BKE_camera_sensor_size(
373 cam->sensor_fit, cam->sensor_x, cam->sensor_y);
374 if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
375 data.fisheye_sensor.x = sensor_size;
376 data.fisheye_sensor.y = sensor_size * fit_yratio / fit_xratio;
377 }
378 else {
379 data.fisheye_sensor.x = sensor_size * fit_xratio / fit_yratio;
380 data.fisheye_sensor.y = sensor_size;
381 }
382 is_camera_object_ = true;
383 }
384 else if (inst_.drw_view) {
385 /* \note Follow camera parameters where distances are positive in front of the camera. */
386 data.clip_near = -inst_.drw_view->near_clip();
387 data.clip_far = -inst_.drw_view->far_clip();
388 data.fisheye_fov = data.fisheye_lens = -1.0f;
389 data.fisheye_sensor = float2(1.0f);
390 data.fisheye_polynomial_bias = 0.0f;
391 data.fisheye_polynomial_coefficients = float4(0.0f);
392 data.central_cylindrical_range = float4(0.0f);
393 data.equirect_bias = float2(0.0f);
394 data.equirect_scale = float2(0.0f);
395 data.equirect_scale_inv = float2(0.0f);
396 }
397
398 /* 5% over-scan for DoF (see eevee_view.cc) plus user config. */
399 data.panoramic_view_overscan = this->is_panoramic() ? 1.05f + overscan_ : 1.0f;
400 data.panoramic_view_mask = uint32_t(this->is_panoramic() ? panoramic_view_mask_get(data) :
402 data.panoramic_front_face_extent = panoramic_front_face_extent_get(data);
403 data_.initialized = true;
404
405 update_bounds();
406}
407
408void Camera::update_bounds()
409{
410 if (this->is_panoramic()) {
411 bound_sphere.center = data_.viewinv.location();
412 bound_sphere.radius = data_.clip_far;
413 /* Each cubemap face covers a fixed 90 degree FOV. At unit distance from the camera, a face's
414 * corners are at (+-1, +-1), so the diagonal is the diagonal of that 2x2 square. */
415 data_.screen_diagonal_length = 2.0f * float(M_SQRT2);
416 return;
417 }
418
419 float left, right, bottom, top, near, far;
420 projmat_dimensions(data_.winmat.ptr(), &left, &right, &bottom, &top, &near, &far);
421
422 BoundBox bbox;
423 bbox.vec[0][2] = bbox.vec[3][2] = bbox.vec[7][2] = bbox.vec[4][2] = -near;
424 bbox.vec[0][0] = bbox.vec[3][0] = left;
425 bbox.vec[4][0] = bbox.vec[7][0] = right;
426 bbox.vec[0][1] = bbox.vec[4][1] = bottom;
427 bbox.vec[7][1] = bbox.vec[3][1] = top;
428
429 /* Get the coordinates of the far plane. */
430 if (!this->is_orthographic()) {
431 float sca_far = far / near;
432 left *= sca_far;
433 right *= sca_far;
434 bottom *= sca_far;
435 top *= sca_far;
436 }
437
438 bbox.vec[1][2] = bbox.vec[2][2] = bbox.vec[6][2] = bbox.vec[5][2] = -far;
439 bbox.vec[1][0] = bbox.vec[2][0] = left;
440 bbox.vec[6][0] = bbox.vec[5][0] = right;
441 bbox.vec[1][1] = bbox.vec[5][1] = bottom;
442 bbox.vec[2][1] = bbox.vec[6][1] = top;
443
444 bound_sphere.center = {0.0f, 0.0f, 0.0f};
445 bound_sphere.radius = 0.0f;
446
447 for (auto i : IndexRange(8)) {
448 bound_sphere.center += float3(bbox.vec[i]);
449 }
450 bound_sphere.center /= 8.0f;
451 for (auto i : IndexRange(8)) {
452 float dist_sqr = math::distance_squared(bound_sphere.center, float3(bbox.vec[i]));
453 bound_sphere.radius = max_ff(bound_sphere.radius, dist_sqr);
454 }
455 bound_sphere.radius = sqrtf(bound_sphere.radius);
456
457 /* Transform into world space. */
458 bound_sphere.center = math::transform_point(data_.viewinv, bound_sphere.center);
459
460 /* Compute diagonal length. */
461 float2 p0 = float2(bbox.vec[0]) / (this->is_perspective() ? bbox.vec[0][2] : 1.0f);
462 float2 p1 = float2(bbox.vec[7]) / (this->is_perspective() ? bbox.vec[7][2] : 1.0f);
463 data_.screen_diagonal_length = math::distance(p0, p1);
464}
465
466float4x4 Camera::projection_crop_matrix(int2 film_offset, int2 film_extent, int2 display_extent)
467{
468 float2 uv_min = float2(film_offset) / float2(display_extent);
469 float2 uv_max = float2(film_offset + film_extent) / float2(display_extent);
470
471 float2 ndc_min = uv_min * 2.0f - 1.0f;
472 float2 ndc_max = uv_max * 2.0f - 1.0f;
473
474 float2 ndc_size = ndc_max - ndc_min;
475 float2 ndc_center = (ndc_min + ndc_max) * 0.5f;
476
477 float2 scale = 2.0f / ndc_size;
478 float2 offset = -ndc_center * scale;
479
480 float4x4 crop_matrix = float4x4::identity();
481 crop_matrix[0][0] = scale.x;
482 crop_matrix[1][1] = scale.y;
483 crop_matrix[3][0] = offset.x;
484 crop_matrix[3][1] = offset.y;
485
486 return crop_matrix;
487}
488
489float4x4 Camera::projection_overscan_matrix(int2 film_extent, int2 film_overscan)
490{
491 float2 overscan_scale = float2(film_extent) / float2(film_extent + film_overscan * 2);
492
493 float4x4 overscan_matrix = float4x4::identity();
494 overscan_matrix[0][0] = overscan_scale.x;
495 overscan_matrix[1][1] = overscan_scale.y;
496
497 return overscan_matrix;
498}
499
501
502} // namespace blender::eevee
Camera data-block and utility functions.
#define M_SQRT2
#define M_PI_2
#define M_PI
#define M_PI_4
#define UNPACK2(a)
constexpr IndexRange()=default
const float4x4 & winmat(int view_id=0) const
Definition draw_view.hh:148
const float4x4 & viewmat(int view_id=0) const
Definition draw_view.hh:136
float near_clip(int view_id=0) const
Definition draw_view.hh:115
const float4x4 & viewinv(int view_id=0) const
Definition draw_view.hh:142
float far_clip(int view_id=0) const
Definition draw_view.hh:106
bool is_orthographic() const
bool is_perspective() const
static int overscan_pixels_get(float overscan, int2 extent)
float3x3 scale(float3x3 mat, float2 scale)
ENUM_OPERATORS(Layer::Flags)
static PanoramicViewBits panoramic_fisheye_view_mask_get(const CameraData &cam)
static float4x4 panoramic_camera_from_view(const eCameraType type)
static PanoramicViewBits panoramic_view_mask_get(const CameraData &cam)
static PanoramicViewBits panoramic_equiangular_cubemap_face_view_mask_get(const CameraData &cam)
static bool tan_range_reaches_one(float theta_lo, float theta_hi)
constexpr PanoramicViewBits PANORAMIC_VIEW_SIDES
static bool tan_range_reaches_minus_one(float theta_lo, float theta_hi)
constexpr PanoramicViewBits PANORAMIC_VIEW_ALL
static float panoramic_front_face_extent_get(const CameraData &cam)
MatBase< T, 4, 4 > perspective(T left, T right, T bottom, T top, T near_clip, T far_clip)
Create a perspective projection matrix using OpenGL coordinate convention: Maps each axis range to [-...
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
T distance(const T &a, const T &b)
T min(const T &a, const T &b)
CartesianBasis invert(const CartesianBasis &basis)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T max(const T &a, const T &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool assign_if_different(T &old_value, T &&new_value)
void projmat_dimensions(const float winmat[4][4], float *r_left, float *r_right, float *r_bottom, float *r_top, float *r_near, float *r_far)
void RE_GetCameraWindow(Render *re, const Object *camera, float r_winmat[4][4])
Definition initrender.cc:62
@ CAMERA_SENSOR_FIT_HOR
MatBase< float, 4, 4 > float4x4
void BKE_render_resolution(const RenderData *r, const bool use_crop, int *r_width, int *r_height)
Definition scene.cc:3168
VecBase< float, 4 > float4
@ CAM_PANORAMA_CENTRAL_CYLINDRICAL
@ CAM_PANORAMA_EQUIRECTANGULAR
@ CAM_PANORAMA_MIRRORBALL
@ CAM_PANORAMA_FISHEYE_EQUISOLID
@ CAM_PANORAMA_EQUIANGULAR_CUBEMAP_FACE
@ CAM_PANORAMA_FISHEYE_LENS_POLYNOMIAL
@ CAM_PANORAMA_FISHEYE_EQUIDISTANT
unsigned int uint32_t
Definition BKE_text.h:81
void RE_GetWindowMatrixWithOverscan(bool is_ortho, float clip_start, float clip_end, rctf viewplane, float overscan, float r_winmat[4][4])
Definition initrender.cc:79
void RE_GetCameraModelMatrix(const Render *re, const Object *camera, float r_modelmat[4][4])
Definition initrender.cc:74
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
BMesh const char void * data
int BKE_camera_sensor_fit(int sensor_fit, float sizex, float sizey)
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
float BKE_camera_sensor_size(int sensor_fit, float sensor_x, float sensor_y)
MINLINE float max_ff(float a, float b)
VecBase< float, 3 > float3
void BKE_camera_params_crop_viewplane(rctf *viewplane, int winx, int winy, const rcti *region)
#define floorf
#define sqrtf
#define atanf
#define tanf
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const c_style_mat & ptr() const
float x
float y