1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*************************************************************************/
/* interpolated_camera.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "interpolated_camera.h"
void InterpolatedCamera::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
if (get_tree()->is_editor_hint() && enabled)
set_fixed_process(false);
} break;
case NOTIFICATION_PROCESS: {
if (!enabled)
break;
if (has_node(target)) {
Spatial *node = get_node(target)->cast_to<Spatial>();
if (!node)
break;
float delta = speed * get_process_delta_time();
Transform target_xform = node->get_global_transform();
Transform local_transform = get_global_transform();
local_transform = local_transform.interpolate_with(target_xform, delta);
set_global_transform(local_transform);
if (node->cast_to<Camera>()) {
Camera *cam = node->cast_to<Camera>();
if (cam->get_projection() == get_projection()) {
float new_near = Math::lerp(get_znear(), cam->get_znear(), delta);
float new_far = Math::lerp(get_zfar(), cam->get_zfar(), delta);
if (cam->get_projection() == PROJECTION_ORTHOGONAL) {
float size = Math::lerp(get_size(), cam->get_size(), delta);
set_orthogonal(size, new_near, new_far);
} else {
float fov = Math::lerp(get_fov(), cam->get_fov(), delta);
set_perspective(fov, new_near, new_far);
}
}
}
}
} break;
}
}
void InterpolatedCamera::_set_target(const Object *p_target) {
ERR_FAIL_NULL(p_target);
set_target(p_target->cast_to<Spatial>());
}
void InterpolatedCamera::set_target(const Spatial *p_target) {
ERR_FAIL_NULL(p_target);
target = get_path_to(p_target);
}
void InterpolatedCamera::set_target_path(const NodePath &p_path) {
target = p_path;
}
NodePath InterpolatedCamera::get_target_path() const {
return target;
}
void InterpolatedCamera::set_interpolation_enabled(bool p_enable) {
if (enabled == p_enable)
return;
enabled = p_enable;
if (p_enable) {
if (is_inside_tree() && get_tree()->is_editor_hint())
return;
set_process(true);
} else
set_process(false);
}
bool InterpolatedCamera::is_interpolation_enabled() const {
return enabled;
}
void InterpolatedCamera::set_speed(real_t p_speed) {
speed = p_speed;
}
real_t InterpolatedCamera::get_speed() const {
return speed;
}
void InterpolatedCamera::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_target_path", "target_path"), &InterpolatedCamera::set_target_path);
ClassDB::bind_method(D_METHOD("get_target_path"), &InterpolatedCamera::get_target_path);
ClassDB::bind_method(D_METHOD("set_target", "target"), &InterpolatedCamera::_set_target);
ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InterpolatedCamera::set_speed);
ClassDB::bind_method(D_METHOD("get_speed"), &InterpolatedCamera::get_speed);
ClassDB::bind_method(D_METHOD("set_interpolation_enabled", "target_path"), &InterpolatedCamera::set_interpolation_enabled);
ClassDB::bind_method(D_METHOD("is_interpolation_enabled"), &InterpolatedCamera::is_interpolation_enabled);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target"), "set_target_path", "get_target_path");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed"), "set_speed", "get_speed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_interpolation_enabled", "is_interpolation_enabled");
}
InterpolatedCamera::InterpolatedCamera() {
enabled = false;
speed = 1;
}
|