2023-01-10 15:26:54 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* interpolated_camera.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "interpolated_camera.h"
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/engine.h"
|
2017-08-19 01:02:56 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void InterpolatedCamera::_notification(int p_what) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2020-08-18 18:29:32 +02:00
|
|
|
_update_process_mode();
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
2020-08-18 18:29:32 +02:00
|
|
|
case NOTIFICATION_INTERNAL_PROCESS:
|
|
|
|
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!enabled) {
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
if (has_node(target)) {
|
2017-08-24 22:58:51 +02:00
|
|
|
Spatial *node = Object::cast_to<Spatial>(get_node(target));
|
2021-05-05 12:44:11 +02:00
|
|
|
if (!node) {
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-08-18 18:29:32 +02:00
|
|
|
float delta = speed * (process_mode == INTERPOLATED_CAMERA_PROCESS_PHYSICS ? get_physics_process_delta_time() : get_process_delta_time());
|
2014-02-10 02:10:30 +01:00
|
|
|
Transform target_xform = node->get_global_transform();
|
2014-11-02 15:31:01 +01:00
|
|
|
Transform local_transform = get_global_transform();
|
2017-03-05 16:44:50 +01:00
|
|
|
local_transform = local_transform.interpolate_with(target_xform, delta);
|
2014-02-10 02:10:30 +01:00
|
|
|
set_global_transform(local_transform);
|
2017-09-06 23:50:18 +02:00
|
|
|
Camera *cam = Object::cast_to<Camera>(node);
|
|
|
|
if (cam) {
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (cam->get_projection() == PROJECTION_ORTHOGONAL) {
|
|
|
|
float size = Math::lerp(get_size(), cam->get_size(), delta);
|
|
|
|
set_orthogonal(size, new_near, new_far);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
float fov = Math::lerp(get_fov(), cam->get_fov(), delta);
|
|
|
|
set_perspective(fov, new_near, new_far);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 18:29:32 +02:00
|
|
|
void InterpolatedCamera::set_process_mode(InterpolatedCameraProcessMode p_mode) {
|
|
|
|
if (process_mode == p_mode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
process_mode = p_mode;
|
|
|
|
_update_process_mode();
|
|
|
|
}
|
|
|
|
|
|
|
|
InterpolatedCamera::InterpolatedCameraProcessMode InterpolatedCamera::get_process_mode() const {
|
|
|
|
return process_mode;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void InterpolatedCamera::_set_target(const Object *p_target) {
|
|
|
|
ERR_FAIL_NULL(p_target);
|
2017-08-24 22:58:51 +02:00
|
|
|
set_target(Object::cast_to<Spatial>(p_target));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterpolatedCamera::set_target(const Spatial *p_target) {
|
|
|
|
ERR_FAIL_NULL(p_target);
|
2017-03-05 16:44:50 +01:00
|
|
|
target = get_path_to(p_target);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void InterpolatedCamera::set_target_path(const NodePath &p_path) {
|
|
|
|
target = p_path;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
NodePath InterpolatedCamera::get_target_path() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpolatedCamera::set_interpolation_enabled(bool p_enable) {
|
2021-05-05 12:44:11 +02:00
|
|
|
if (enabled == p_enable) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
enabled = p_enable;
|
2020-08-18 18:29:32 +02:00
|
|
|
_update_process_mode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpolatedCamera::_update_process_mode() {
|
|
|
|
if (Engine::get_singleton()->is_editor_hint() || !enabled) {
|
|
|
|
set_process_internal(false);
|
|
|
|
set_physics_process_internal(false);
|
|
|
|
} else if (process_mode == INTERPOLATED_CAMERA_PROCESS_IDLE) {
|
2018-03-15 16:55:11 +01:00
|
|
|
set_process_internal(true);
|
2020-08-18 18:29:32 +02:00
|
|
|
set_physics_process_internal(false);
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2018-03-15 16:55:11 +01:00
|
|
|
set_process_internal(false);
|
2020-08-18 18:29:32 +02:00
|
|
|
set_physics_process_internal(true);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InterpolatedCamera::is_interpolation_enabled() const {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpolatedCamera::set_speed(real_t p_speed) {
|
2017-03-05 16:44:50 +01:00
|
|
|
speed = p_speed;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t InterpolatedCamera::get_speed() const {
|
|
|
|
return speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpolatedCamera::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_target", "target"), &InterpolatedCamera::_set_target);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_speed", "speed"), &InterpolatedCamera::set_speed);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_speed"), &InterpolatedCamera::get_speed);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-08-18 18:29:32 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &InterpolatedCamera::set_process_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_process_mode"), &InterpolatedCamera::get_process_mode);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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");
|
2020-08-18 18:29:32 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_mode", "get_process_mode");
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(INTERPOLATED_CAMERA_PROCESS_PHYSICS);
|
|
|
|
BIND_ENUM_CONSTANT(INTERPOLATED_CAMERA_PROCESS_IDLE);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
InterpolatedCamera::InterpolatedCamera() {
|
2017-03-05 16:44:50 +01:00
|
|
|
enabled = false;
|
|
|
|
speed = 1;
|
2020-08-18 18:29:32 +02:00
|
|
|
process_mode = INTERPOLATED_CAMERA_PROCESS_IDLE;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|