diff --git a/doc/classes/InterpolatedCamera.xml b/doc/classes/InterpolatedCamera.xml index 065ae0f94f0..e2738091a14 100644 --- a/doc/classes/InterpolatedCamera.xml +++ b/doc/classes/InterpolatedCamera.xml @@ -22,6 +22,9 @@ If [code]true[/code], and a target is set, the camera will move automatically. + + The camera's process callback. See [enum InterpolatedCameraProcessMode]. + How quickly the camera moves toward its target. Higher values will result in tighter camera motion. @@ -30,5 +33,11 @@ + + The camera updates with the [code]_physics_process[/code] callback. + + + The camera updates with the [code]_process[/code] callback. + diff --git a/scene/3d/interpolated_camera.cpp b/scene/3d/interpolated_camera.cpp index a42f50d6a0b..1f92bd8f8ea 100644 --- a/scene/3d/interpolated_camera.cpp +++ b/scene/3d/interpolated_camera.cpp @@ -35,12 +35,10 @@ void InterpolatedCamera::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { - if (Engine::get_singleton()->is_editor_hint() && enabled) { - set_process_internal(false); - } - + _update_process_mode(); } break; - case NOTIFICATION_INTERNAL_PROCESS: { + case NOTIFICATION_INTERNAL_PROCESS: + case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { if (!enabled) { break; } @@ -50,7 +48,7 @@ void InterpolatedCamera::_notification(int p_what) { break; } - float delta = speed * get_process_delta_time(); + float delta = speed * (process_mode == INTERPOLATED_CAMERA_PROCESS_PHYSICS ? get_physics_process_delta_time() : 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); @@ -76,6 +74,18 @@ void InterpolatedCamera::_notification(int p_what) { } } +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; +} + void InterpolatedCamera::_set_target(const Object *p_target) { ERR_FAIL_NULL(p_target); set_target(Object::cast_to(p_target)); @@ -99,13 +109,19 @@ void InterpolatedCamera::set_interpolation_enabled(bool p_enable) { return; } enabled = p_enable; - if (p_enable) { - if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) { - return; - } + _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) { set_process_internal(true); + set_physics_process_internal(false); } else { set_process_internal(false); + set_physics_process_internal(true); } } @@ -132,12 +148,20 @@ void InterpolatedCamera::_bind_methods() { 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); + 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); + 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"); + 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); } InterpolatedCamera::InterpolatedCamera() { enabled = false; speed = 1; + process_mode = INTERPOLATED_CAMERA_PROCESS_IDLE; } diff --git a/scene/3d/interpolated_camera.h b/scene/3d/interpolated_camera.h index 58f9827e5bf..2bc4f08c5be 100644 --- a/scene/3d/interpolated_camera.h +++ b/scene/3d/interpolated_camera.h @@ -36,9 +36,19 @@ class InterpolatedCamera : public Camera { GDCLASS(InterpolatedCamera, Camera); +public: + enum InterpolatedCameraProcessMode { + INTERPOLATED_CAMERA_PROCESS_PHYSICS, + INTERPOLATED_CAMERA_PROCESS_IDLE + }; + +private: bool enabled; real_t speed; NodePath target; + InterpolatedCameraProcessMode process_mode; + + void _update_process_mode(); protected: void _notification(int p_what); @@ -56,7 +66,12 @@ public: void set_interpolation_enabled(bool p_enable); bool is_interpolation_enabled() const; + void set_process_mode(InterpolatedCameraProcessMode p_mode); + InterpolatedCameraProcessMode get_process_mode() const; + InterpolatedCamera(); }; +VARIANT_ENUM_CAST(InterpolatedCamera::InterpolatedCameraProcessMode); + #endif // INTERPOLATED_CAMERA_H