Blender V5.3
eevee_camera_lib.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
17
18namespace eevee::camera {
19
20/* -------------------------------------------------------------------- */
25
27{
28 /* U wraps around behind the camera at the edges, like Cycles. */
29 float phi = atan(-dir.x, -dir.z) + M_PI_2;
30 float theta = acos(dir.y / length(dir));
31 return (float2(phi, theta) - cam.equirect_bias) * cam.equirect_scale_inv;
32}
33
35{
36 uv = uv * cam.equirect_scale + cam.equirect_bias;
37 float phi = uv.x;
38 float theta = uv.y;
39 float sin_theta = sin(theta);
40 return float3(sin_theta * cos(phi), cos(theta), -sin_theta * sin(phi));
41}
42
44{
45 float2 uv;
46 uv.x = 0.5f - atan(-dir.x, -dir.z) * 2.0f / M_PI;
47 uv.y = atan(dir.y, -dir.z) * 2.0f / M_PI + 0.5f;
48 return (uv - cam.uv_bias) / cam.uv_scale;
49}
50
52{
53 uv = uv * cam.uv_scale + cam.uv_bias;
54 float u = tan((0.5f - uv.x) * M_PI_2);
55 float v = tan((uv.y - 0.5f) * M_PI_2);
56 return normalize(float3(-u, v, -1.0f));
57}
58
60{
61 const float cylinder_height = dir.y / length(dir.xz);
62 const float theta = atan(-dir.x, -dir.z);
63 const float2 uv = (float2(theta, cylinder_height) - cam.central_cylindrical_range.xz) /
64 (cam.central_cylindrical_range.yw - cam.central_cylindrical_range.xz);
65 return (uv - cam.uv_bias) / cam.uv_scale;
66}
67
69{
70 uv = uv * cam.uv_scale + cam.uv_bias;
71 const float theta = mix(cam.central_cylindrical_range.x, cam.central_cylindrical_range.y, uv.x);
72 const float cylinder_height = mix(
73 cam.central_cylindrical_range.z, cam.central_cylindrical_range.w, uv.y);
74 return normalize(float3(-sin(theta), cylinder_height, -cos(theta)));
75}
76
78{
79 float r = atan(length(dir.xy), -dir.z) / cam.fisheye_fov;
80 float phi = atan(dir.y, dir.x);
81 float2 uv = r * float2(cos(phi), sin(phi)) + 0.5f;
82 return (uv - cam.uv_bias) / cam.uv_scale;
83}
84
86{
87 uv = uv * cam.uv_scale + cam.uv_bias;
88 uv = (uv - 0.5f) * 2.0f;
89 float r = length(uv);
90 if (r > 1.0f) {
91 return float3(0.0f);
92 }
93 float phi = safe_acos(uv.x * safe_rcp(r));
94 float theta = r * cam.fisheye_fov * 0.5f;
95 if (uv.y < 0.0f) {
96 phi = -phi;
97 }
98 return float3(cos(phi) * sin(theta), sin(phi) * sin(theta), -cos(theta));
99}
100
102{
103 float theta = safe_acos(-dir.z);
104 float r = 2.0f * cam.fisheye_lens * sin(theta * 0.5f);
105 float2 dir_xy = dir.xy;
106 float2 uv = r * safe_normalize(dir_xy);
107 uv = uv / cam.fisheye_sensor + 0.5f;
108 return (uv - cam.uv_bias) / cam.uv_scale;
109}
110
112{
113 uv = uv * cam.uv_scale + cam.uv_bias;
114 uv = (uv - 0.5f) * cam.fisheye_sensor;
115
116 float r = length(uv);
117 float rmax = 2.0f * cam.fisheye_lens * sin(cam.fisheye_fov * 0.25f);
118 if (r > rmax) {
119 return float3(0.0f);
120 }
121
122 float theta = 2.0f * asin(r / (2.0f * cam.fisheye_lens));
123 float phi = safe_acos(uv.x * safe_rcp(r));
124 if (uv.y < 0.0f) {
125 phi = -phi;
126 }
127 return float3(cos(phi) * sin(theta), sin(phi) * sin(theta), -cos(theta));
128}
129
131{
132 const float theta = -atan(length(dir.xy), -dir.z);
133
134 float r = (theta - cam.fisheye_polynomial_bias) / cam.fisheye_polynomial_coefficients.x;
135 const float4 diff_coefficients = float4(1.0f, 2.0f, 3.0f, 4.0f) *
136 cam.fisheye_polynomial_coefficients;
137 /* Clamp the search to the monotonic region of the polynomial to avoid diverging. */
138 const float side = (r < 0.0f) ? -1.0f : 1.0f;
139 float r_min = 0.0f;
140 float r_max = FLT_MAX;
141
142 for (int i = 0; i < 20; i++) {
143 const float old_r = r;
144 const float r2 = r * r;
145 const float4 rr = float4(r, r2, r2 * r, r2 * r2);
146 const float F_r = theta -
147 (cam.fisheye_polynomial_bias + dot(cam.fisheye_polynomial_coefficients, rr));
148 const float dF_r = dot(diff_coefficients, float4(1.0f, r, r2, r2 * r));
149 const bool is_folded = dF_r * cam.fisheye_polynomial_coefficients.x <= 0.0f;
150 const float step = F_r / dF_r;
151 if (is_folded || step * side < 0.0f) {
152 r_max = min(r_max, r * side);
153 }
154 else {
155 r_min = max(r_min, r * side);
156 }
157 r += step;
158 if (is_folded || r * side < r_min || r * side > r_max) {
159 r = side * 0.5f * (r_min + r_max);
160 }
161
162 if (abs(r - old_r) < 1.0e-6f) {
163 break;
164 }
165 }
166
167 float2 dir_xy = dir.xy;
168 float2 uv = r * safe_normalize(dir_xy);
169 uv = uv / cam.fisheye_sensor + 0.5f;
170 return (uv - cam.uv_bias) / cam.uv_scale;
171}
172
174{
175 uv = uv * cam.uv_scale + cam.uv_bias;
176 uv = (uv - 0.5f) * cam.fisheye_sensor;
177
178 const float r = length(uv);
179 const float r2 = r * r;
180 const float4 rr = float4(r, r2, r2 * r, r2 * r2);
181 const float theta = -(cam.fisheye_polynomial_bias +
182 dot(cam.fisheye_polynomial_coefficients, rr));
183
184 if (abs(theta) > 0.5f * cam.fisheye_fov) {
185 return float3(0.0f);
186 }
187
188 float phi = safe_acos(uv.x * safe_rcp(r));
189 if (uv.y < 0.0f) {
190 phi = -phi;
191 }
192 return float3(cos(phi) * sin(theta), sin(phi) * sin(theta), -cos(theta));
193}
194
196{
197 dir = normalize(dir);
198 dir.z -= 1.0f;
199 dir *= safe_rcp(2.0f * safe_sqrt(-0.5f * dir.z));
200 float2 uv = 0.5f * dir.xy + 0.5f;
201 return (uv - cam.uv_bias) / cam.uv_scale;
202}
203
205{
206 uv = uv * cam.uv_scale + cam.uv_bias;
207 float3 dir;
208 dir.xy = uv * 2.0f - 1.0f;
209 if (length_squared(dir.xy) > 1.0f) {
210 return float3(0.0f);
211 }
212 dir.z = -safe_sqrt(1.0f - square(dir.x) - square(dir.y));
213 constexpr float3 I = float3(0.0f, 0.0f, 1.0f);
214 return reflect(I, dir);
215}
216
218
219/* -------------------------------------------------------------------- */
222
224{
225 return project_point(projmat, float3(uv * 2.0f - 1.0f, 0.0f));
226}
227
228float2 uv_from_view(float4x4 projmat, bool is_persp, float3 vV)
229{
230 float4 tmp = projmat * float4(vV, 1.0f);
231 if (is_persp && tmp.w <= 0.0f) {
232 /* Return invalid coordinates for points behind the camera.
233 * This can happen with panoramic projections. */
234 return float2(-1.0f);
235 }
236 return (tmp.xy / tmp.w) * 0.5f + 0.5f;
237}
238
240
241/* -------------------------------------------------------------------- */
244
245float3 view_from_uv(CameraData cam, float2 uv)
246{
247 if (is_panoramic(cam.type) && cam.type != CAMERA_PANO_EQUIRECT &&
248 cam.type != CAMERA_PANO_CENTRAL_CYLINDRICAL)
249 {
250 /* uv can land past the camera's frame in viewport so we need to bound it. Equirectangular
251 * and central cylindrical are periodic/meant to wrap instead of clip. Add a margin to avoid
252 * clipping edge AA taps. */
253 const float margin = 0.1f;
254 float2 frame_uv = uv * cam.uv_scale + cam.uv_bias;
255 if (any(lessThan(frame_uv, float2(-margin))) ||
256 any(greaterThan(frame_uv, float2(1.0f + margin))))
257 {
258 return float3(0.0f);
259 }
260 }
261 float3 vV;
262 switch (cam.type) {
263 default:
264 case CAMERA_ORTHO:
265 case CAMERA_PERSP:
266 return view_from_uv(cam.wininv, uv);
267 case CAMERA_PANO_EQUIRECT:
268 vV = equirectangular_to_direction(cam, uv);
269 break;
270 case CAMERA_PANO_EQUIANGULAR_CUBEMAP_FACE:
272 break;
273 case CAMERA_PANO_EQUIDISTANT:
274 vV = fisheye_to_direction(cam, uv);
275 break;
276 case CAMERA_PANO_EQUISOLID:
277 vV = fisheye_equisolid_to_direction(cam, uv);
278 break;
279 case CAMERA_PANO_FISHEYE_LENS_POLYNOMIAL:
281 break;
282 case CAMERA_PANO_CENTRAL_CYLINDRICAL:
284 break;
285 case CAMERA_PANO_MIRROR:
286 vV = mirror_ball_to_direction(cam, uv);
287 break;
288 }
289 return vV;
290}
291
292float2 uv_from_view(CameraData cam, float3 vV)
293{
294 switch (cam.type) {
295 default:
296 case CAMERA_ORTHO:
297 return uv_from_view(cam.winmat, false, vV);
298 case CAMERA_PERSP:
299 return uv_from_view(cam.winmat, true, vV);
300 case CAMERA_PANO_EQUIRECT:
301 return equirectangular_from_direction(cam, vV);
302 case CAMERA_PANO_EQUIANGULAR_CUBEMAP_FACE:
304 case CAMERA_PANO_EQUISOLID:
305 return fisheye_equisolid_from_direction(cam, vV);
306 case CAMERA_PANO_FISHEYE_LENS_POLYNOMIAL:
308 case CAMERA_PANO_CENTRAL_CYLINDRICAL:
310 case CAMERA_PANO_EQUIDISTANT:
311 return fisheye_from_direction(cam, vV);
312 case CAMERA_PANO_MIRROR:
313 return mirror_ball_from_direction(cam, vV);
314 }
315}
316
317float2 uv_from_world(CameraData cam, float3 P)
318{
319 float3 vV = transform_direction(cam.viewmat, normalize(P));
320 return uv_from_view(cam, vV);
321}
322
324
325} // namespace eevee::camera
#define M_PI_2
#define M_PI
ccl_device_inline float sin_theta(const float3 w)
btScalar max
#define FLT_MAX
#define atan
#define tan
#define sin
bool any(VecOp< bool, D >) RET
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
#define cos
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define asin
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
float length(VecOp< float, D >) RET
#define acos
float square(float v)
float3 transform_direction(float3x3 mat, float3 direction)
float2 project_point(float3x3 mat, float2 point)
float safe_rcp(float a)
float safe_sqrt(float a)
float safe_acos(float a)
float length_squared(VecT a)
VecT safe_normalize(VecT vector)
MatBase< float, 4, 4 > float4x4
float2 equirectangular_from_direction(CameraData cam, float3 dir)
float2 fisheye_equisolid_from_direction(CameraData cam, float3 dir)
float2 uv_from_world(CameraData cam, float3 P)
float3 equiangular_cubemap_face_to_direction(CameraData cam, float2 uv)
float3 fisheye_lens_polynomial_to_direction(CameraData cam, float2 uv)
float3 equirectangular_to_direction(CameraData cam, float2 uv)
float3 central_cylindrical_to_direction(CameraData cam, float2 uv)
float3 view_from_uv(float4x4 projmat, float2 uv)
float3 fisheye_to_direction(CameraData cam, float2 uv)
float2 uv_from_view(float4x4 projmat, bool is_persp, float3 vV)
float2 fisheye_from_direction(CameraData cam, float3 dir)
float3 mirror_ball_to_direction(CameraData cam, float2 uv)
float2 equiangular_cubemap_face_from_direction(CameraData cam, float3 dir)
float3 fisheye_equisolid_to_direction(CameraData cam, float2 uv)
float2 central_cylindrical_from_direction(CameraData cam, float3 dir)
float2 fisheye_lens_polynomial_from_direction(CameraData cam, float3 dir)
float2 mirror_ball_from_direction(CameraData cam, float3 dir)
#define I
#define mix
#define min(a, b)
Definition sort.cc:38
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float w
Definition sky_math.h:225