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
_notification(int p_what)32 void InterpolatedCamera::_notification(int p_what) {
33
34 switch (p_what) {
35 case NOTIFICATION_ENTER_TREE: {
36
37 if (get_tree()->is_editor_hint() && enabled)
38 set_fixed_process(false);
39
40 } break;
41 case NOTIFICATION_PROCESS: {
42
43 if (!enabled)
44 break;
45 if (has_node(target)) {
46
47 Spatial *node = get_node(target)->cast_to<Spatial>();
48 if (!node)
49 break;
50
51 float delta = speed * get_process_delta_time();
52 Transform target_xform = node->get_global_transform();
53 Transform local_transform = get_global_transform();
54 local_transform = local_transform.interpolate_with(target_xform, delta);
55 set_global_transform(local_transform);
56
57 if (node->cast_to<Camera>()) {
58 Camera *cam = node->cast_to<Camera>();
59 if (cam->get_projection() == get_projection()) {
60
61 float new_near = Math::lerp(get_znear(), cam->get_znear(), delta);
62 float new_far = Math::lerp(get_zfar(), cam->get_zfar(), delta);
63
64 if (cam->get_projection() == PROJECTION_ORTHOGONAL) {
65
66 float size = Math::lerp(get_size(), cam->get_size(), delta);
67 set_orthogonal(size, new_near, new_far);
68 } else {
69
70 float fov = Math::lerp(get_fov(), cam->get_fov(), delta);
71 set_perspective(fov, new_near, new_far);
72 }
73 }
74 }
75 }
76
77 } break;
78 }
79 }
80
_set_target(const Object * p_target)81 void InterpolatedCamera::_set_target(const Object *p_target) {
82
83 ERR_FAIL_NULL(p_target);
84 set_target(p_target->cast_to<Spatial>());
85 }
86
set_target(const Spatial * p_target)87 void InterpolatedCamera::set_target(const Spatial *p_target) {
88
89 ERR_FAIL_NULL(p_target);
90 target = get_path_to(p_target);
91 }
92
set_target_path(const NodePath & p_path)93 void InterpolatedCamera::set_target_path(const NodePath &p_path) {
94
95 target = p_path;
96 }
97
get_target_path() const98 NodePath InterpolatedCamera::get_target_path() const {
99
100 return target;
101 }
102
set_interpolation_enabled(bool p_enable)103 void InterpolatedCamera::set_interpolation_enabled(bool p_enable) {
104
105 if (enabled == p_enable)
106 return;
107 enabled = p_enable;
108 if (p_enable) {
109 if (is_inside_tree() && get_tree()->is_editor_hint())
110 return;
111 set_process(true);
112 } else
113 set_process(false);
114 }
115
is_interpolation_enabled() const116 bool InterpolatedCamera::is_interpolation_enabled() const {
117
118 return enabled;
119 }
120
set_speed(real_t p_speed)121 void InterpolatedCamera::set_speed(real_t p_speed) {
122
123 speed = p_speed;
124 }
125
get_speed() const126 real_t InterpolatedCamera::get_speed() const {
127
128 return speed;
129 }
130
_bind_methods()131 void InterpolatedCamera::_bind_methods() {
132
133 ObjectTypeDB::bind_method(_MD("set_target_path", "target_path"), &InterpolatedCamera::set_target_path);
134 ObjectTypeDB::bind_method(_MD("get_target_path"), &InterpolatedCamera::get_target_path);
135 ObjectTypeDB::bind_method(_MD("set_target", "target:Camera"), &InterpolatedCamera::_set_target);
136
137 ObjectTypeDB::bind_method(_MD("set_speed", "speed"), &InterpolatedCamera::set_speed);
138 ObjectTypeDB::bind_method(_MD("get_speed"), &InterpolatedCamera::get_speed);
139
140 ObjectTypeDB::bind_method(_MD("set_interpolation_enabled", "target_path"), &InterpolatedCamera::set_interpolation_enabled);
141 ObjectTypeDB::bind_method(_MD("is_interpolation_enabled"), &InterpolatedCamera::is_interpolation_enabled);
142
143 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target"), _SCS("set_target_path"), _SCS("get_target_path"));
144 ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed"), _SCS("set_speed"), _SCS("get_speed"));
145 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), _SCS("set_interpolation_enabled"), _SCS("is_interpolation_enabled"));
146 }
147
InterpolatedCamera()148 InterpolatedCamera::InterpolatedCamera() {
149
150 enabled = false;
151 speed = 1;
152 }
153