interpolated_camera.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*************************************************************************/
  2. /* interpolated_camera.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "interpolated_camera.h"
  31. #include "core/engine.h"
  32. void InterpolatedCamera::_notification(int p_what) {
  33. switch (p_what) {
  34. case NOTIFICATION_ENTER_TREE: {
  35. if (Engine::get_singleton()->is_editor_hint() && enabled)
  36. set_process_internal(false);
  37. } break;
  38. case NOTIFICATION_INTERNAL_PROCESS: {
  39. if (!enabled)
  40. break;
  41. if (has_node(target)) {
  42. Spatial *node = Object::cast_to<Spatial>(get_node(target));
  43. if (!node)
  44. break;
  45. float delta = speed * get_process_delta_time();
  46. Transform target_xform = node->get_global_transform();
  47. Transform local_transform = get_global_transform();
  48. local_transform = local_transform.interpolate_with(target_xform, delta);
  49. set_global_transform(local_transform);
  50. Camera *cam = Object::cast_to<Camera>(node);
  51. if (cam) {
  52. if (cam->get_projection() == get_projection()) {
  53. float new_near = Math::lerp(get_znear(), cam->get_znear(), delta);
  54. float new_far = Math::lerp(get_zfar(), cam->get_zfar(), delta);
  55. if (cam->get_projection() == PROJECTION_ORTHOGONAL) {
  56. float size = Math::lerp(get_size(), cam->get_size(), delta);
  57. set_orthogonal(size, new_near, new_far);
  58. } else {
  59. float fov = Math::lerp(get_fov(), cam->get_fov(), delta);
  60. set_perspective(fov, new_near, new_far);
  61. }
  62. }
  63. }
  64. }
  65. } break;
  66. }
  67. }
  68. void InterpolatedCamera::_set_target(const Object *p_target) {
  69. ERR_FAIL_NULL(p_target);
  70. set_target(Object::cast_to<Spatial>(p_target));
  71. }
  72. void InterpolatedCamera::set_target(const Spatial *p_target) {
  73. ERR_FAIL_NULL(p_target);
  74. target = get_path_to(p_target);
  75. }
  76. void InterpolatedCamera::set_target_path(const NodePath &p_path) {
  77. target = p_path;
  78. }
  79. NodePath InterpolatedCamera::get_target_path() const {
  80. return target;
  81. }
  82. void InterpolatedCamera::set_interpolation_enabled(bool p_enable) {
  83. if (enabled == p_enable)
  84. return;
  85. enabled = p_enable;
  86. if (p_enable) {
  87. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
  88. return;
  89. set_process_internal(true);
  90. } else
  91. set_process_internal(false);
  92. }
  93. bool InterpolatedCamera::is_interpolation_enabled() const {
  94. return enabled;
  95. }
  96. void InterpolatedCamera::set_speed(real_t p_speed) {
  97. speed = p_speed;
  98. }
  99. real_t InterpolatedCamera::get_speed() const {
  100. return speed;
  101. }
  102. void InterpolatedCamera::_bind_methods() {
  103. ClassDB::bind_method(D_METHOD("set_target_path", "target_path"), &InterpolatedCamera::set_target_path);
  104. ClassDB::bind_method(D_METHOD("get_target_path"), &InterpolatedCamera::get_target_path);
  105. ClassDB::bind_method(D_METHOD("set_target", "target"), &InterpolatedCamera::_set_target);
  106. ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InterpolatedCamera::set_speed);
  107. ClassDB::bind_method(D_METHOD("get_speed"), &InterpolatedCamera::get_speed);
  108. ClassDB::bind_method(D_METHOD("set_interpolation_enabled", "target_path"), &InterpolatedCamera::set_interpolation_enabled);
  109. ClassDB::bind_method(D_METHOD("is_interpolation_enabled"), &InterpolatedCamera::is_interpolation_enabled);
  110. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target"), "set_target_path", "get_target_path");
  111. ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed"), "set_speed", "get_speed");
  112. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_interpolation_enabled", "is_interpolation_enabled");
  113. }
  114. InterpolatedCamera::InterpolatedCamera() {
  115. enabled = false;
  116. speed = 1;
  117. }