| 1 | /**************************************************************************/ |
| 2 | /* camera_3d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "camera_3d.h" |
| 32 | |
| 33 | #include "core/math/projection.h" |
| 34 | #include "core/math/transform_interpolator.h" |
| 35 | #include "scene/main/viewport.h" |
| 36 | |
| 37 | void Camera3D::_update_audio_listener_state() { |
| 38 | } |
| 39 | |
| 40 | void Camera3D::_request_camera_update() { |
| 41 | _update_camera(); |
| 42 | } |
| 43 | |
| 44 | void Camera3D::fti_pump_property() { |
| 45 | switch (mode) { |
| 46 | default: |
| 47 | break; |
| 48 | case PROJECTION_PERSPECTIVE: { |
| 49 | fov.pump(); |
| 50 | } break; |
| 51 | case PROJECTION_ORTHOGONAL: { |
| 52 | size.pump(); |
| 53 | } break; |
| 54 | case PROJECTION_FRUSTUM: { |
| 55 | size.pump(); |
| 56 | frustum_offset.pump(); |
| 57 | } break; |
| 58 | } |
| 59 | _near.pump(); |
| 60 | _far.pump(); |
| 61 | |
| 62 | Node3D::fti_pump_property(); |
| 63 | } |
| 64 | |
| 65 | void Camera3D::fti_update_servers_property() { |
| 66 | if (camera.is_valid()) { |
| 67 | float f = Engine::get_singleton()->get_physics_interpolation_fraction(); |
| 68 | |
| 69 | switch (mode) { |
| 70 | default: |
| 71 | break; |
| 72 | case PROJECTION_PERSPECTIVE: { |
| 73 | // If there have been changes due to interpolation, update the servers. |
| 74 | if (fov.interpolate(f) || _near.interpolate(f) || _far.interpolate(f)) { |
| 75 | RS::get_singleton()->camera_set_perspective(camera, fov.interpolated(), _near.interpolated(), _far.interpolated()); |
| 76 | } |
| 77 | } break; |
| 78 | case PROJECTION_ORTHOGONAL: { |
| 79 | if (size.interpolate(f) || _near.interpolate(f) || _far.interpolate(f)) { |
| 80 | RS::get_singleton()->camera_set_orthogonal(camera, size.interpolated(), _near.interpolated(), _far.interpolated()); |
| 81 | } |
| 82 | } break; |
| 83 | case PROJECTION_FRUSTUM: { |
| 84 | if (size.interpolate(f) || frustum_offset.interpolate(f) || _near.interpolate(f) || _far.interpolate(f)) { |
| 85 | RS::get_singleton()->camera_set_frustum(camera, size.interpolated(), frustum_offset.interpolated(), _near.interpolated(), _far.interpolated()); |
| 86 | } |
| 87 | } break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | Node3D::fti_update_servers_property(); |
| 92 | } |
| 93 | |
| 94 | void Camera3D::fti_update_servers_xform() { |
| 95 | if (camera.is_valid()) { |
| 96 | Transform3D tr = _get_adjusted_camera_transform(_get_cached_global_transform_interpolated()); |
| 97 | RS::get_singleton()->camera_set_transform(camera, tr); |
| 98 | } |
| 99 | Node3D::fti_update_servers_xform(); |
| 100 | } |
| 101 | |
| 102 | void Camera3D::_update_camera_mode() { |
| 103 | force_change = true; |
| 104 | switch (mode) { |
| 105 | case PROJECTION_PERSPECTIVE: { |
| 106 | set_perspective(fov, _near, _far); |
| 107 | |
| 108 | } break; |
| 109 | case PROJECTION_ORTHOGONAL: { |
| 110 | set_orthogonal(size, _near, _far); |
| 111 | } break; |
| 112 | case PROJECTION_FRUSTUM: { |
| 113 | set_frustum(size, frustum_offset, _near, _far); |
| 114 | } break; |
| 115 | } |
| 116 | fti_notify_node_changed(false); |
| 117 | } |
| 118 | |
| 119 | void Camera3D::_validate_property(PropertyInfo &p_property) const { |
| 120 | if (Engine::get_singleton()->is_editor_hint()) { |
| 121 | if (p_property.name == "fov") { |
| 122 | if (mode != PROJECTION_PERSPECTIVE) { |
| 123 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 124 | } |
| 125 | } else if (p_property.name == "size") { |
| 126 | if (mode != PROJECTION_ORTHOGONAL && mode != PROJECTION_FRUSTUM) { |
| 127 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 128 | } |
| 129 | } else if (p_property.name == "frustum_offset") { |
| 130 | if (mode != PROJECTION_FRUSTUM) { |
| 131 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (attributes.is_valid()) { |
| 137 | const CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr()); |
| 138 | if (physical_attributes) { |
| 139 | if (p_property.name == "near" || p_property.name == "far" || p_property.name == "fov" || p_property.name == "keep_aspect") { |
| 140 | p_property.usage = PROPERTY_USAGE_READ_ONLY | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void Camera3D::_update_camera() { |
| 147 | if (!is_inside_tree()) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if (!is_physics_interpolated_and_enabled()) { |
| 152 | RenderingServer::get_singleton()->camera_set_transform(camera, get_camera_transform()); |
| 153 | } else { |
| 154 | // Force a refresh next frame. |
| 155 | fti_notify_node_changed(); |
| 156 | } |
| 157 | |
| 158 | if (is_part_of_edited_scene() || !is_current()) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | get_viewport()->_camera_3d_transform_changed_notify(); |
| 163 | } |
| 164 | |
| 165 | void Camera3D::_physics_interpolated_changed() { |
| 166 | _update_process_mode(); |
| 167 | } |
| 168 | |
| 169 | void Camera3D::set_desired_process_modes(bool p_process_internal, bool p_physics_process_internal) { |
| 170 | _desired_process_internal = p_process_internal; |
| 171 | _desired_physics_process_internal = p_physics_process_internal; |
| 172 | _update_process_mode(); |
| 173 | } |
| 174 | |
| 175 | void Camera3D::_update_process_mode() { |
| 176 | set_process_internal(_desired_process_internal); |
| 177 | set_physics_process_internal(_desired_physics_process_internal); |
| 178 | } |
| 179 | |
| 180 | void Camera3D::_notification(int p_what) { |
| 181 | switch (p_what) { |
| 182 | case NOTIFICATION_ENTER_WORLD: { |
| 183 | // Needs to track the Viewport because it's needed on NOTIFICATION_EXIT_WORLD |
| 184 | // and Spatial will handle it first, including clearing its reference to the Viewport, |
| 185 | // therefore making it impossible to subclasses to access it |
| 186 | viewport = get_viewport(); |
| 187 | ERR_FAIL_NULL(viewport); |
| 188 | |
| 189 | bool first_camera = viewport->_camera_3d_add(this); |
| 190 | if (current || first_camera) { |
| 191 | viewport->_camera_3d_set(this); |
| 192 | } |
| 193 | |
| 194 | #ifdef TOOLS_ENABLED |
| 195 | if (Engine::get_singleton()->is_editor_hint()) { |
| 196 | viewport->connect(SNAME("size_changed"), callable_mp((Node3D *)this, &Camera3D::update_gizmos)); |
| 197 | } |
| 198 | #endif |
| 199 | } break; |
| 200 | |
| 201 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 202 | #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED) |
| 203 | if (is_physics_interpolated_and_enabled()) { |
| 204 | if (!Engine::get_singleton()->is_in_physics_frame()) { |
| 205 | PHYSICS_INTERPOLATION_NODE_WARNING(get_instance_id(), "Interpolated Camera3D triggered from outside physics process"); |
| 206 | } |
| 207 | } |
| 208 | #endif |
| 209 | _request_camera_update(); |
| 210 | if (doppler_tracking != DOPPLER_TRACKING_DISABLED) { |
| 211 | velocity_tracker->update_position(get_global_transform().origin); |
| 212 | } |
| 213 | } break; |
| 214 | |
| 215 | case NOTIFICATION_EXIT_WORLD: { |
| 216 | if (!is_part_of_edited_scene()) { |
| 217 | if (is_current()) { |
| 218 | clear_current(); |
| 219 | current = true; //keep it true |
| 220 | |
| 221 | } else { |
| 222 | current = false; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (viewport) { |
| 227 | #ifdef TOOLS_ENABLED |
| 228 | if (Engine::get_singleton()->is_editor_hint()) { |
| 229 | viewport->disconnect(SNAME("size_changed"), callable_mp((Node3D *)this, &Camera3D::update_gizmos)); |
| 230 | } |
| 231 | #endif |
| 232 | viewport->_camera_3d_remove(this); |
| 233 | viewport = nullptr; |
| 234 | } |
| 235 | } break; |
| 236 | |
| 237 | case NOTIFICATION_BECAME_CURRENT: { |
| 238 | if (viewport) { |
| 239 | viewport->find_world_3d()->_register_camera(this); |
| 240 | } |
| 241 | _update_process_mode(); |
| 242 | } break; |
| 243 | |
| 244 | case NOTIFICATION_LOST_CURRENT: { |
| 245 | if (viewport) { |
| 246 | viewport->find_world_3d()->_remove_camera(this); |
| 247 | } |
| 248 | _update_process_mode(); |
| 249 | } break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | Transform3D Camera3D::_get_adjusted_camera_transform(const Transform3D &p_xform) const { |
| 254 | Transform3D tr = p_xform.orthonormalized(); |
| 255 | tr.origin += tr.basis.get_column(1) * v_offset; |
| 256 | tr.origin += tr.basis.get_column(0) * h_offset; |
| 257 | return tr; |
| 258 | } |
| 259 | |
| 260 | Transform3D Camera3D::get_camera_transform() const { |
| 261 | if (is_physics_interpolated_and_enabled() && !Engine::get_singleton()->is_in_physics_frame()) { |
| 262 | return _get_adjusted_camera_transform(_get_cached_global_transform_interpolated()); |
| 263 | } |
| 264 | |
| 265 | return _get_adjusted_camera_transform(get_global_transform()); |
| 266 | } |
| 267 | |
| 268 | Projection Camera3D::_get_camera_projection(real_t p_near) const { |
| 269 | Size2 viewport_size = get_viewport()->get_visible_rect().size; |
| 270 | Projection cm; |
| 271 | |
| 272 | switch (mode) { |
| 273 | case PROJECTION_PERSPECTIVE: { |
| 274 | cm.set_perspective(fov, viewport_size.aspect(), p_near, _far, keep_aspect == KEEP_WIDTH); |
| 275 | } break; |
| 276 | case PROJECTION_ORTHOGONAL: { |
| 277 | cm.set_orthogonal(size, viewport_size.aspect(), p_near, _far, keep_aspect == KEEP_WIDTH); |
| 278 | } break; |
| 279 | case PROJECTION_FRUSTUM: { |
| 280 | cm.set_frustum(size, viewport_size.aspect(), frustum_offset, p_near, _far); |
| 281 | } break; |
| 282 | } |
| 283 | |
| 284 | return cm; |
| 285 | } |
| 286 | |
| 287 | Projection Camera3D::get_camera_projection() const { |
| 288 | ERR_FAIL_COND_V_MSG(!is_inside_tree(), Projection(), "Camera is not inside the scene tree."); |
| 289 | return _get_camera_projection(_near); |
| 290 | } |
| 291 | |
| 292 | void Camera3D::set_perspective(real_t p_fovy_degrees, real_t p_z_near, real_t p_z_far) { |
| 293 | if (!force_change && fov == p_fovy_degrees && p_z_near == _near && p_z_far == _far && mode == PROJECTION_PERSPECTIVE) { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | fov = p_fovy_degrees; |
| 298 | _near = p_z_near; |
| 299 | _far = p_z_far; |
| 300 | mode = PROJECTION_PERSPECTIVE; |
| 301 | |
| 302 | RenderingServer::get_singleton()->camera_set_perspective(camera, fov, _near, _far); |
| 303 | update_gizmos(); |
| 304 | force_change = false; |
| 305 | } |
| 306 | |
| 307 | void Camera3D::set_orthogonal(real_t p_size, real_t p_z_near, real_t p_z_far) { |
| 308 | if (!force_change && size == p_size && p_z_near == _near && p_z_far == _far && mode == PROJECTION_ORTHOGONAL) { |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | size = p_size; |
| 313 | |
| 314 | _near = p_z_near; |
| 315 | _far = p_z_far; |
| 316 | mode = PROJECTION_ORTHOGONAL; |
| 317 | force_change = false; |
| 318 | |
| 319 | RenderingServer::get_singleton()->camera_set_orthogonal(camera, size, _near, _far); |
| 320 | update_gizmos(); |
| 321 | } |
| 322 | |
| 323 | void Camera3D::set_frustum(real_t p_size, Vector2 p_offset, real_t p_z_near, real_t p_z_far) { |
| 324 | if (!force_change && size == p_size && frustum_offset == p_offset && p_z_near == _near && p_z_far == _far && mode == PROJECTION_FRUSTUM) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | size = p_size; |
| 329 | frustum_offset = p_offset; |
| 330 | |
| 331 | _near = p_z_near; |
| 332 | _far = p_z_far; |
| 333 | mode = PROJECTION_FRUSTUM; |
| 334 | force_change = false; |
| 335 | |
| 336 | RenderingServer::get_singleton()->camera_set_frustum(camera, size, frustum_offset, _near, _far); |
| 337 | update_gizmos(); |
| 338 | } |
| 339 | |
| 340 | void Camera3D::set_projection(ProjectionType p_mode) { |
| 341 | if (p_mode == PROJECTION_PERSPECTIVE || p_mode == PROJECTION_ORTHOGONAL || p_mode == PROJECTION_FRUSTUM) { |
| 342 | mode = p_mode; |
| 343 | _update_camera_mode(); |
| 344 | notify_property_list_changed(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | RID Camera3D::get_camera() const { |
| 349 | return camera; |
| 350 | } |
| 351 | |
| 352 | void Camera3D::make_current() { |
| 353 | current = true; |
| 354 | |
| 355 | if (!is_inside_tree()) { |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | get_viewport()->_camera_3d_set(this); |
| 360 | } |
| 361 | |
| 362 | void Camera3D::clear_current(bool p_enable_next) { |
| 363 | current = false; |
| 364 | if (!is_inside_tree()) { |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | if (get_viewport()->get_camera_3d() == this) { |
| 369 | get_viewport()->_camera_3d_set(nullptr); |
| 370 | |
| 371 | if (p_enable_next && !Engine::get_singleton()->is_editor_hint()) { |
| 372 | get_viewport()->_camera_3d_make_next_current(this); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | void Camera3D::set_current(bool p_enabled) { |
| 378 | if (p_enabled) { |
| 379 | make_current(); |
| 380 | } else { |
| 381 | clear_current(); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | bool Camera3D::is_current() const { |
| 386 | if (is_inside_tree() && !is_part_of_edited_scene()) { |
| 387 | return get_viewport()->get_camera_3d() == this; |
| 388 | } else { |
| 389 | return current; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | Vector3 Camera3D::project_ray_normal(const Point2 &p_pos) const { |
| 394 | Vector3 ray = project_local_ray_normal(p_pos); |
| 395 | return get_camera_transform().basis.xform(ray).normalized(); |
| 396 | } |
| 397 | |
| 398 | Vector3 Camera3D::project_local_ray_normal(const Point2 &p_pos) const { |
| 399 | ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene."); |
| 400 | |
| 401 | Size2 viewport_size = get_viewport()->get_camera_rect_size(); |
| 402 | Vector2 cpos = get_viewport()->get_camera_coords(p_pos); |
| 403 | Vector3 ray; |
| 404 | |
| 405 | if (mode == PROJECTION_ORTHOGONAL) { |
| 406 | ray = Vector3(0, 0, -1); |
| 407 | } else { |
| 408 | Projection cm = _get_camera_projection(_near); |
| 409 | Vector2 screen_he = cm.get_viewport_half_extents(); |
| 410 | ray = Vector3(((cpos.x / viewport_size.width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (cpos.y / viewport_size.height)) * 2.0 - 1.0) * screen_he.y, -_near).normalized(); |
| 411 | } |
| 412 | |
| 413 | return ray; |
| 414 | } |
| 415 | |
| 416 | Vector3 Camera3D::project_ray_origin(const Point2 &p_pos) const { |
| 417 | ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene."); |
| 418 | |
| 419 | Size2 viewport_size = get_viewport()->get_camera_rect_size(); |
| 420 | Vector2 cpos = get_viewport()->get_camera_coords(p_pos); |
| 421 | ERR_FAIL_COND_V(viewport_size.y == 0, Vector3()); |
| 422 | |
| 423 | if (mode == PROJECTION_ORTHOGONAL) { |
| 424 | Vector2 pos = cpos / viewport_size; |
| 425 | real_t vsize, hsize; |
| 426 | if (keep_aspect == KEEP_WIDTH) { |
| 427 | vsize = size / viewport_size.aspect(); |
| 428 | hsize = size; |
| 429 | } else { |
| 430 | hsize = size * viewport_size.aspect(); |
| 431 | vsize = size; |
| 432 | } |
| 433 | |
| 434 | Vector3 ray; |
| 435 | ray.x = pos.x * (hsize)-hsize / 2; |
| 436 | ray.y = (1.0 - pos.y) * (vsize)-vsize / 2; |
| 437 | ray.z = -_near; |
| 438 | ray = get_camera_transform().xform(ray); |
| 439 | return ray; |
| 440 | } else { |
| 441 | return get_camera_transform().origin; |
| 442 | }; |
| 443 | } |
| 444 | |
| 445 | bool Camera3D::is_position_behind(const Vector3 &p_pos) const { |
| 446 | Transform3D t = get_global_transform(); |
| 447 | Vector3 eyedir = -t.basis.get_column(2).normalized(); |
| 448 | return eyedir.dot(p_pos - t.origin) < _near; |
| 449 | } |
| 450 | |
| 451 | Vector<Vector3> Camera3D::get_near_plane_points() const { |
| 452 | ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector<Vector3>(), "Camera is not inside scene."); |
| 453 | |
| 454 | Projection cm = _get_camera_projection(_near); |
| 455 | |
| 456 | Vector3 endpoints[8]; |
| 457 | cm.get_endpoints(Transform3D(), endpoints); |
| 458 | |
| 459 | Vector<Vector3> points = { |
| 460 | Vector3(), |
| 461 | endpoints[4], |
| 462 | endpoints[5], |
| 463 | endpoints[6], |
| 464 | endpoints[7] |
| 465 | }; |
| 466 | return points; |
| 467 | } |
| 468 | |
| 469 | Point2 Camera3D::unproject_position(const Vector3 &p_pos) const { |
| 470 | ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector2(), "Camera is not inside scene."); |
| 471 | |
| 472 | Size2 viewport_size = get_viewport()->get_visible_rect().size; |
| 473 | |
| 474 | Projection cm = _get_camera_projection(_near); |
| 475 | |
| 476 | Plane p(get_camera_transform().xform_inv(p_pos), 1.0); |
| 477 | |
| 478 | p = cm.xform4(p); |
| 479 | |
| 480 | // Prevent divide by zero. |
| 481 | // TODO: Investigate, this was causing NaNs. |
| 482 | ERR_FAIL_COND_V(p.d == 0, Point2()); |
| 483 | |
| 484 | p.normal /= p.d; |
| 485 | |
| 486 | Point2 res; |
| 487 | res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x; |
| 488 | res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y; |
| 489 | |
| 490 | return res; |
| 491 | } |
| 492 | |
| 493 | Vector3 Camera3D::project_position(const Point2 &p_point, real_t p_z_depth) const { |
| 494 | ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene."); |
| 495 | |
| 496 | if (p_z_depth == 0 && mode != PROJECTION_ORTHOGONAL) { |
| 497 | return get_global_transform().origin; |
| 498 | } |
| 499 | Size2 viewport_size = get_viewport()->get_visible_rect().size; |
| 500 | |
| 501 | Projection cm = _get_camera_projection(_near); |
| 502 | |
| 503 | Plane z_slice(Vector3(0, 0, 1), -p_z_depth); |
| 504 | Vector3 res; |
| 505 | z_slice.intersect_3(cm.get_projection_plane(Projection::Planes::PLANE_RIGHT), cm.get_projection_plane(Projection::Planes::PLANE_TOP), &res); |
| 506 | Vector2 vp_he(res.x, res.y); |
| 507 | |
| 508 | Vector2 point; |
| 509 | point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0; |
| 510 | point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0; |
| 511 | point *= vp_he; |
| 512 | |
| 513 | Vector3 p(point.x, point.y, -p_z_depth); |
| 514 | |
| 515 | return get_camera_transform().xform(p); |
| 516 | } |
| 517 | |
| 518 | void Camera3D::set_environment(const Ref<Environment> &p_environment) { |
| 519 | environment = p_environment; |
| 520 | if (environment.is_valid()) { |
| 521 | RS::get_singleton()->camera_set_environment(camera, environment->get_rid()); |
| 522 | } else { |
| 523 | RS::get_singleton()->camera_set_environment(camera, RID()); |
| 524 | } |
| 525 | _update_camera_mode(); |
| 526 | } |
| 527 | |
| 528 | Ref<Environment> Camera3D::get_environment() const { |
| 529 | return environment; |
| 530 | } |
| 531 | |
| 532 | void Camera3D::set_attributes(const Ref<CameraAttributes> &p_attributes) { |
| 533 | if (attributes.is_valid()) { |
| 534 | CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr()); |
| 535 | if (physical_attributes) { |
| 536 | attributes->disconnect_changed(callable_mp(this, &Camera3D::_attributes_changed)); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | attributes = p_attributes; |
| 541 | |
| 542 | if (attributes.is_valid()) { |
| 543 | CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr()); |
| 544 | if (physical_attributes) { |
| 545 | attributes->connect_changed(callable_mp(this, &Camera3D::_attributes_changed)); |
| 546 | _attributes_changed(); |
| 547 | } |
| 548 | |
| 549 | RS::get_singleton()->camera_set_camera_attributes(camera, attributes->get_rid()); |
| 550 | } else { |
| 551 | RS::get_singleton()->camera_set_camera_attributes(camera, RID()); |
| 552 | } |
| 553 | |
| 554 | notify_property_list_changed(); |
| 555 | } |
| 556 | |
| 557 | Ref<CameraAttributes> Camera3D::get_attributes() const { |
| 558 | return attributes; |
| 559 | } |
| 560 | |
| 561 | void Camera3D::_attributes_changed() { |
| 562 | CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr()); |
| 563 | ERR_FAIL_NULL(physical_attributes); |
| 564 | |
| 565 | fov = physical_attributes->get_fov(); |
| 566 | _near = physical_attributes->get_near(); |
| 567 | _far = physical_attributes->get_far(); |
| 568 | keep_aspect = KEEP_HEIGHT; |
| 569 | _update_camera_mode(); |
| 570 | } |
| 571 | |
| 572 | void Camera3D::set_compositor(const Ref<Compositor> &p_compositor) { |
| 573 | compositor = p_compositor; |
| 574 | if (compositor.is_valid()) { |
| 575 | RS::get_singleton()->camera_set_compositor(camera, compositor->get_rid()); |
| 576 | } else { |
| 577 | RS::get_singleton()->camera_set_compositor(camera, RID()); |
| 578 | } |
| 579 | _update_camera_mode(); |
| 580 | } |
| 581 | |
| 582 | Ref<Compositor> Camera3D::get_compositor() const { |
| 583 | return compositor; |
| 584 | } |
| 585 | |
| 586 | void Camera3D::set_keep_aspect_mode(KeepAspect p_aspect) { |
| 587 | keep_aspect = p_aspect; |
| 588 | RenderingServer::get_singleton()->camera_set_use_vertical_aspect(camera, p_aspect == KEEP_WIDTH); |
| 589 | _update_camera_mode(); |
| 590 | notify_property_list_changed(); |
| 591 | } |
| 592 | |
| 593 | Camera3D::KeepAspect Camera3D::get_keep_aspect_mode() const { |
| 594 | return keep_aspect; |
| 595 | } |
| 596 | |
| 597 | void Camera3D::set_doppler_tracking(DopplerTracking p_tracking) { |
| 598 | if (doppler_tracking == p_tracking) { |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | doppler_tracking = p_tracking; |
| 603 | if (p_tracking != DOPPLER_TRACKING_DISABLED) { |
| 604 | velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP); |
| 605 | if (is_inside_tree()) { |
| 606 | velocity_tracker->reset(get_global_transform().origin); |
| 607 | } |
| 608 | } |
| 609 | _update_camera_mode(); |
| 610 | } |
| 611 | |
| 612 | Camera3D::DopplerTracking Camera3D::get_doppler_tracking() const { |
| 613 | return doppler_tracking; |
| 614 | } |
| 615 | |
| 616 | void Camera3D::_bind_methods() { |
| 617 | ClassDB::bind_method(D_METHOD("project_ray_normal", "screen_point"), &Camera3D::project_ray_normal); |
| 618 | ClassDB::bind_method(D_METHOD("project_local_ray_normal", "screen_point"), &Camera3D::project_local_ray_normal); |
| 619 | ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera3D::project_ray_origin); |
| 620 | ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera3D::unproject_position); |
| 621 | ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera3D::is_position_behind); |
| 622 | ClassDB::bind_method(D_METHOD("project_position", "screen_point", "z_depth"), &Camera3D::project_position); |
| 623 | ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera3D::set_perspective); |
| 624 | ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera3D::set_orthogonal); |
| 625 | ClassDB::bind_method(D_METHOD("set_frustum", "size", "offset", "z_near", "z_far"), &Camera3D::set_frustum); |
| 626 | ClassDB::bind_method(D_METHOD("make_current"), &Camera3D::make_current); |
| 627 | ClassDB::bind_method(D_METHOD("clear_current", "enable_next"), &Camera3D::clear_current, DEFVAL(true)); |
| 628 | ClassDB::bind_method(D_METHOD("set_current", "enabled"), &Camera3D::set_current); |
| 629 | ClassDB::bind_method(D_METHOD("is_current"), &Camera3D::is_current); |
| 630 | ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera3D::get_camera_transform); |
| 631 | ClassDB::bind_method(D_METHOD("get_camera_projection"), &Camera3D::get_camera_projection); |
| 632 | ClassDB::bind_method(D_METHOD("get_fov"), &Camera3D::get_fov); |
| 633 | ClassDB::bind_method(D_METHOD("get_frustum_offset"), &Camera3D::get_frustum_offset); |
| 634 | ClassDB::bind_method(D_METHOD("get_size"), &Camera3D::get_size); |
| 635 | ClassDB::bind_method(D_METHOD("get_far"), &Camera3D::get_far); |
| 636 | ClassDB::bind_method(D_METHOD("get_near"), &Camera3D::get_near); |
| 637 | ClassDB::bind_method(D_METHOD("set_fov", "fov"), &Camera3D::set_fov); |
| 638 | ClassDB::bind_method(D_METHOD("set_frustum_offset", "offset"), &Camera3D::set_frustum_offset); |
| 639 | ClassDB::bind_method(D_METHOD("set_size", "size"), &Camera3D::set_size); |
| 640 | ClassDB::bind_method(D_METHOD("set_far", "far"), &Camera3D::set_far); |
| 641 | ClassDB::bind_method(D_METHOD("set_near", "near"), &Camera3D::set_near); |
| 642 | ClassDB::bind_method(D_METHOD("get_projection"), &Camera3D::get_projection); |
| 643 | ClassDB::bind_method(D_METHOD("set_projection", "mode"), &Camera3D::set_projection); |
| 644 | ClassDB::bind_method(D_METHOD("set_h_offset", "offset"), &Camera3D::set_h_offset); |
| 645 | ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera3D::get_h_offset); |
| 646 | ClassDB::bind_method(D_METHOD("set_v_offset", "offset"), &Camera3D::set_v_offset); |
| 647 | ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera3D::get_v_offset); |
| 648 | ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &Camera3D::set_cull_mask); |
| 649 | ClassDB::bind_method(D_METHOD("get_cull_mask"), &Camera3D::get_cull_mask); |
| 650 | ClassDB::bind_method(D_METHOD("set_environment", "env"), &Camera3D::set_environment); |
| 651 | ClassDB::bind_method(D_METHOD("get_environment"), &Camera3D::get_environment); |
| 652 | ClassDB::bind_method(D_METHOD("set_attributes", "env"), &Camera3D::set_attributes); |
| 653 | ClassDB::bind_method(D_METHOD("get_attributes"), &Camera3D::get_attributes); |
| 654 | ClassDB::bind_method(D_METHOD("set_compositor", "compositor"), &Camera3D::set_compositor); |
| 655 | ClassDB::bind_method(D_METHOD("get_compositor"), &Camera3D::get_compositor); |
| 656 | ClassDB::bind_method(D_METHOD("set_keep_aspect_mode", "mode"), &Camera3D::set_keep_aspect_mode); |
| 657 | ClassDB::bind_method(D_METHOD("get_keep_aspect_mode"), &Camera3D::get_keep_aspect_mode); |
| 658 | ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &Camera3D::set_doppler_tracking); |
| 659 | ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &Camera3D::get_doppler_tracking); |
| 660 | ClassDB::bind_method(D_METHOD("get_frustum"), &Camera3D::_get_frustum); |
| 661 | ClassDB::bind_method(D_METHOD("is_position_in_frustum", "world_point"), &Camera3D::is_position_in_frustum); |
| 662 | ClassDB::bind_method(D_METHOD("get_camera_rid"), &Camera3D::get_camera); |
| 663 | #ifndef PHYSICS_3D_DISABLED |
| 664 | ClassDB::bind_method(D_METHOD("get_pyramid_shape_rid"), &Camera3D::get_pyramid_shape_rid); |
| 665 | #endif // PHYSICS_3D_DISABLED |
| 666 | |
| 667 | ClassDB::bind_method(D_METHOD("set_cull_mask_value", "layer_number", "value"), &Camera3D::set_cull_mask_value); |
| 668 | ClassDB::bind_method(D_METHOD("get_cull_mask_value", "layer_number"), &Camera3D::get_cull_mask_value); |
| 669 | |
| 670 | //ClassDB::bind_method(D_METHOD("_camera_make_current"),&Camera::_camera_make_current ); |
| 671 | |
| 672 | ADD_PROPERTY(PropertyInfo(Variant::INT, "keep_aspect", PROPERTY_HINT_ENUM, "Keep Width,Keep Height"), "set_keep_aspect_mode", "get_keep_aspect_mode"); |
| 673 | ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask"); |
| 674 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment"); |
| 675 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_attributes", "get_attributes"); |
| 676 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "compositor", PROPERTY_HINT_RESOURCE_TYPE, "Compositor"), "set_compositor", "get_compositor"); |
| 677 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_h_offset", "get_h_offset"); |
| 678 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_v_offset", "get_v_offset"); |
| 679 | ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking"); |
| 680 | ADD_PROPERTY(PropertyInfo(Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal,Frustum"), "set_projection", "get_projection"); |
| 681 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current"); |
| 682 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov", PROPERTY_HINT_RANGE, "1,179,0.1,degrees"), "set_fov", "get_fov"); |
| 683 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_size", "get_size"); |
| 684 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frustum_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_frustum_offset", "get_frustum_offset"); |
| 685 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "near", PROPERTY_HINT_RANGE, "0.001,10,0.001,or_greater,exp,suffix:m"), "set_near", "get_near"); |
| 686 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "far", PROPERTY_HINT_RANGE, "0.01,4000,0.01,or_greater,exp,suffix:m"), "set_far", "get_far"); |
| 687 | |
| 688 | BIND_ENUM_CONSTANT(PROJECTION_PERSPECTIVE); |
| 689 | BIND_ENUM_CONSTANT(PROJECTION_ORTHOGONAL); |
| 690 | BIND_ENUM_CONSTANT(PROJECTION_FRUSTUM); |
| 691 | |
| 692 | BIND_ENUM_CONSTANT(KEEP_WIDTH); |
| 693 | BIND_ENUM_CONSTANT(KEEP_HEIGHT); |
| 694 | |
| 695 | BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED); |
| 696 | BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP); |
| 697 | BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP); |
| 698 | } |
| 699 | |
| 700 | real_t Camera3D::get_fov() const { |
| 701 | return fov; |
| 702 | } |
| 703 | |
| 704 | real_t Camera3D::get_size() const { |
| 705 | return size; |
| 706 | } |
| 707 | |
| 708 | real_t Camera3D::get_near() const { |
| 709 | return _near; |
| 710 | } |
| 711 | |
| 712 | Vector2 Camera3D::get_frustum_offset() const { |
| 713 | return frustum_offset; |
| 714 | } |
| 715 | |
| 716 | real_t Camera3D::get_far() const { |
| 717 | return _far; |
| 718 | } |
| 719 | |
| 720 | Camera3D::ProjectionType Camera3D::get_projection() const { |
| 721 | return mode; |
| 722 | } |
| 723 | |
| 724 | void Camera3D::set_fov(real_t p_fov) { |
| 725 | ERR_FAIL_COND(p_fov < 1 || p_fov > 179); |
| 726 | fov = p_fov; |
| 727 | _update_camera_mode(); |
| 728 | } |
| 729 | |
| 730 | void Camera3D::set_size(real_t p_size) { |
| 731 | ERR_FAIL_COND(p_size <= CMP_EPSILON); |
| 732 | size = p_size; |
| 733 | _update_camera_mode(); |
| 734 | } |
| 735 | |
| 736 | void Camera3D::set_near(real_t p_near) { |
| 737 | _near = p_near; |
| 738 | _update_camera_mode(); |
| 739 | } |
| 740 | |
| 741 | void Camera3D::set_frustum_offset(Vector2 p_offset) { |
| 742 | frustum_offset = p_offset; |
| 743 | _update_camera_mode(); |
| 744 | } |
| 745 | |
| 746 | void Camera3D::set_far(real_t p_far) { |
| 747 | _far = p_far; |
| 748 | _update_camera_mode(); |
| 749 | } |
| 750 | |
| 751 | void Camera3D::set_cull_mask(uint32_t p_layers) { |
| 752 | layers = p_layers; |
| 753 | RenderingServer::get_singleton()->camera_set_cull_mask(camera, layers); |
| 754 | _update_camera_mode(); |
| 755 | } |
| 756 | |
| 757 | uint32_t Camera3D::get_cull_mask() const { |
| 758 | return layers; |
| 759 | } |
| 760 | |
| 761 | void Camera3D::set_cull_mask_value(int p_layer_number, bool p_value) { |
| 762 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Render layer number must be between 1 and 20 inclusive."); |
| 763 | ERR_FAIL_COND_MSG(p_layer_number > 20, "Render layer number must be between 1 and 20 inclusive."); |
| 764 | uint32_t mask = get_cull_mask(); |
| 765 | if (p_value) { |
| 766 | mask |= 1 << (p_layer_number - 1); |
| 767 | } else { |
| 768 | mask &= ~(1 << (p_layer_number - 1)); |
| 769 | } |
| 770 | set_cull_mask(mask); |
| 771 | } |
| 772 | |
| 773 | bool Camera3D::get_cull_mask_value(int p_layer_number) const { |
| 774 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Render layer number must be between 1 and 20 inclusive."); |
| 775 | ERR_FAIL_COND_V_MSG(p_layer_number > 20, false, "Render layer number must be between 1 and 20 inclusive."); |
| 776 | return layers & (1 << (p_layer_number - 1)); |
| 777 | } |
| 778 | |
| 779 | Vector<Plane> Camera3D::get_frustum() const { |
| 780 | ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>()); |
| 781 | |
| 782 | Projection cm = _get_camera_projection(_near); |
| 783 | |
| 784 | return cm.get_projection_planes(get_camera_transform()); |
| 785 | } |
| 786 | |
| 787 | TypedArray<Plane> Camera3D::_get_frustum() const { |
| 788 | Variant ret = get_frustum(); |
| 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | bool Camera3D::is_position_in_frustum(const Vector3 &p_position) const { |
| 793 | Vector<Plane> frustum = get_frustum(); |
| 794 | for (int i = 0; i < frustum.size(); i++) { |
| 795 | if (frustum[i].is_point_over(p_position)) { |
| 796 | return false; |
| 797 | } |
| 798 | } |
| 799 | return true; |
| 800 | } |
| 801 | |
| 802 | void Camera3D::set_v_offset(real_t p_offset) { |
| 803 | v_offset = p_offset; |
| 804 | _update_camera(); |
| 805 | } |
| 806 | |
| 807 | real_t Camera3D::get_v_offset() const { |
| 808 | return v_offset; |
| 809 | } |
| 810 | |
| 811 | void Camera3D::set_h_offset(real_t p_offset) { |
| 812 | h_offset = p_offset; |
| 813 | _update_camera(); |
| 814 | } |
| 815 | |
| 816 | real_t Camera3D::get_h_offset() const { |
| 817 | return h_offset; |
| 818 | } |
| 819 | |
| 820 | Vector3 Camera3D::get_doppler_tracked_velocity() const { |
| 821 | if (doppler_tracking != DOPPLER_TRACKING_DISABLED) { |
| 822 | return velocity_tracker->get_tracked_linear_velocity(); |
| 823 | } else { |
| 824 | return Vector3(); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | #ifndef PHYSICS_3D_DISABLED |
| 829 | RID Camera3D::get_pyramid_shape_rid() { |
| 830 | ERR_FAIL_COND_V_MSG(!is_inside_tree(), RID(), "Camera is not inside scene."); |
| 831 | if (pyramid_shape == RID()) { |
| 832 | pyramid_shape_points = get_near_plane_points(); |
| 833 | pyramid_shape = PhysicsServer3D::get_singleton()->convex_polygon_shape_create(); |
| 834 | PhysicsServer3D::get_singleton()->shape_set_data(pyramid_shape, pyramid_shape_points); |
| 835 | |
| 836 | } else { //check if points changed |
| 837 | Vector<Vector3> local_points = get_near_plane_points(); |
| 838 | |
| 839 | bool all_equal = true; |
| 840 | |
| 841 | for (int i = 0; i < 5; i++) { |
| 842 | if (local_points[i] != pyramid_shape_points[i]) { |
| 843 | all_equal = false; |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | if (!all_equal) { |
| 849 | PhysicsServer3D::get_singleton()->shape_set_data(pyramid_shape, local_points); |
| 850 | pyramid_shape_points = local_points; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | return pyramid_shape; |
| 855 | } |
| 856 | #endif // PHYSICS_3D_DISABLED |
| 857 | |
| 858 | Camera3D::Camera3D() { |
| 859 | camera = RenderingServer::get_singleton()->camera_create(); |
| 860 | set_perspective(75.0, 0.05, 4000.0); |
| 861 | RenderingServer::get_singleton()->camera_set_cull_mask(camera, layers); |
| 862 | //active=false; |
| 863 | velocity_tracker.instantiate(); |
| 864 | set_notify_transform(true); |
| 865 | set_disable_scale(true); |
| 866 | } |
| 867 | |
| 868 | Camera3D::~Camera3D() { |
| 869 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 870 | RenderingServer::get_singleton()->free_rid(camera); |
| 871 | #ifndef PHYSICS_3D_DISABLED |
| 872 | if (pyramid_shape.is_valid()) { |
| 873 | ERR_FAIL_NULL(PhysicsServer3D::get_singleton()); |
| 874 | PhysicsServer3D::get_singleton()->free_rid(pyramid_shape); |
| 875 | } |
| 876 | #endif // PHYSICS_3D_DISABLED |
| 877 | } |