interpolated_camera.cpp 5.4 KB

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