diff --git a/doc/classes/Camera2D.xml b/doc/classes/Camera2D.xml index 4620b3d93c1..e8f4cb49b6c 100644 --- a/doc/classes/Camera2D.xml +++ b/doc/classes/Camera2D.xml @@ -21,13 +21,6 @@ Aligns the camera to the tracked node. - - - - - Removes any [Camera2D] from the ancestor [Viewport]'s internal currently-assigned camera. - - @@ -67,13 +60,6 @@ Returns the camera limit for the specified [enum Side]. See also [member limit_bottom], [member limit_top], [member limit_left], and [member limit_right]. - - - - - Make this the current 2D camera for the scene (viewport and layer), in case there are many cameras in the scene. - - @@ -109,7 +95,7 @@ The Camera2D's anchor point. See [enum AnchorMode] constants. - + If [code]true[/code], the camera is the active camera for the current scene. Only one camera can be current, so setting a different camera [code]current[/code] will disable this one. diff --git a/doc/classes/Camera3D.xml b/doc/classes/Camera3D.xml index afba478a20d..0f0bfdc654e 100644 --- a/doc/classes/Camera3D.xml +++ b/doc/classes/Camera3D.xml @@ -176,8 +176,8 @@ [codeblock] # This code block is part of a script that inherits from Node3D. # `control` is a reference to a node inheriting from Control. - control.visible = not get_viewport().get_camera().is_position_behind(global_transform.origin) - control.rect_position = get_viewport().get_camera().unproject_position(global_transform.origin) + control.visible = not get_viewport().get_camera_3d().is_position_behind(global_transform.origin) + control.rect_position = get_viewport().get_camera_3d().unproject_position(global_transform.origin) [/codeblock] diff --git a/doc/classes/Viewport.xml b/doc/classes/Viewport.xml index 00827fe324b..bf58dc4d8c3 100644 --- a/doc/classes/Viewport.xml +++ b/doc/classes/Viewport.xml @@ -36,11 +36,18 @@ Returns the 3D world of the viewport, or if none the world of the parent viewport. - + + + + + Returns the currently active 2D camera. Returns null if there are no active cameras. + + + - Returns the active 3D camera. + Returns the currently active 3D camera. diff --git a/editor/debugger/script_editor_debugger.cpp b/editor/debugger/script_editor_debugger.cpp index e704609639f..06b02cceb48 100644 --- a/editor/debugger/script_editor_debugger.cpp +++ b/editor/debugger/script_editor_debugger.cpp @@ -793,7 +793,7 @@ void ScriptEditorDebugger::_notification(int p_what) { } else if (camera_override >= CameraOverride::OVERRIDE_3D_1) { int viewport_idx = camera_override - CameraOverride::OVERRIDE_3D_1; Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(viewport_idx); - Camera3D *const cam = viewport->get_camera(); + Camera3D *const cam = viewport->get_camera_3d(); Array msg; msg.push_back(cam->get_camera_transform()); diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index 0764a4c46c5..a0884a81cd0 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -2438,7 +2438,7 @@ void Node3DEditorViewport::_notification(int p_what) { Node *scene_root = editor->get_scene_tree_dock()->get_editor_data()->get_edited_scene_root(); if (previewing_cinema && scene_root != nullptr) { - Camera3D *cam = scene_root->get_viewport()->get_camera(); + Camera3D *cam = scene_root->get_viewport()->get_camera_3d(); if (cam != nullptr && cam != previewing) { //then switch the viewport's camera to the scene's viewport camera if (previewing != nullptr) { diff --git a/editor/plugins/node_3d_editor_plugin.h b/editor/plugins/node_3d_editor_plugin.h index fa0f4e950fb..cc65cb53ca2 100644 --- a/editor/plugins/node_3d_editor_plugin.h +++ b/editor/plugins/node_3d_editor_plugin.h @@ -494,7 +494,7 @@ public: AcceptDialog *p_accept); SubViewport *get_viewport_node() { return viewport; } - Camera3D *get_camera() { return camera; } // return the default camera object. + Camera3D *get_camera_3d() { return camera; } // return the default camera object. Node3DEditorViewport(Node3DEditor *p_spatial_editor, EditorNode *p_editor, int p_index); ~Node3DEditorViewport(); diff --git a/scene/2d/camera_2d.cpp b/scene/2d/camera_2d.cpp index f2930819877..0e559159575 100644 --- a/scene/2d/camera_2d.cpp +++ b/scene/2d/camera_2d.cpp @@ -241,6 +241,10 @@ void Camera2D::_notification(int p_what) { viewport = get_viewport(); } + if (is_current()) { + viewport->_camera_2d_set(this); + } + canvas = get_canvas(); RID vp = viewport->get_viewport_rid(); @@ -259,6 +263,7 @@ void Camera2D::_notification(int p_what) { if (is_current()) { if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) { viewport->set_canvas_transform(Transform2D()); + clear_current(); } } remove_from_group(group_name); @@ -392,18 +397,29 @@ Camera2D::Camera2DProcessCallback Camera2D::get_process_callback() const { void Camera2D::_make_current(Object *p_which) { if (p_which == this) { current = true; + if (is_inside_tree()) { + get_viewport()->_camera_2d_set(this); + update(); + } } else { current = false; + if (is_inside_tree()) { + if (get_viewport()->get_camera_2d() == this) { + get_viewport()->_camera_2d_set(nullptr); + } + update(); + } } } -void Camera2D::_set_current(bool p_current) { +void Camera2D::set_current(bool p_current) { if (p_current) { make_current(); + } else { + if (current) { + clear_current(); + } } - - current = p_current; - update(); } bool Camera2D::is_current() const { @@ -411,18 +427,19 @@ bool Camera2D::is_current() const { } void Camera2D::make_current() { - if (!is_inside_tree()) { - current = true; - } else { + if (is_inside_tree()) { get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_make_current", this); + } else { + current = true; } _update_scroll(); } void Camera2D::clear_current() { - current = false; if (is_inside_tree()) { get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_make_current", (Object *)nullptr); + } else { + current = false; } } @@ -659,17 +676,14 @@ void Camera2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_rotating", "rotating"), &Camera2D::set_rotating); ClassDB::bind_method(D_METHOD("is_rotating"), &Camera2D::is_rotating); - ClassDB::bind_method(D_METHOD("make_current"), &Camera2D::make_current); - ClassDB::bind_method(D_METHOD("clear_current"), &Camera2D::clear_current); - ClassDB::bind_method(D_METHOD("_make_current"), &Camera2D::_make_current); - ClassDB::bind_method(D_METHOD("_update_scroll"), &Camera2D::_update_scroll); ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &Camera2D::set_process_callback); ClassDB::bind_method(D_METHOD("get_process_callback"), &Camera2D::get_process_callback); - ClassDB::bind_method(D_METHOD("_set_current", "current"), &Camera2D::_set_current); + ClassDB::bind_method(D_METHOD("set_current", "current"), &Camera2D::set_current); ClassDB::bind_method(D_METHOD("is_current"), &Camera2D::is_current); + ClassDB::bind_method(D_METHOD("_make_current"), &Camera2D::_make_current); ClassDB::bind_method(D_METHOD("set_limit", "margin", "limit"), &Camera2D::set_limit); ClassDB::bind_method(D_METHOD("get_limit", "margin"), &Camera2D::get_limit); @@ -725,7 +739,7 @@ void Camera2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset"); ADD_PROPERTY(PropertyInfo(Variant::INT, "anchor_mode", PROPERTY_HINT_ENUM, "Fixed TopLeft,Drag Center"), "set_anchor_mode", "get_anchor_mode"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotating"), "set_rotating", "is_rotating"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "_set_current", "is_current"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "zoom"), "set_zoom", "get_zoom"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport", PROPERTY_USAGE_NONE), "set_custom_viewport", "get_custom_viewport"); ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_callback", "get_process_callback"); diff --git a/scene/2d/camera_2d.h b/scene/2d/camera_2d.h index 7494e526cc2..95b49cf0765 100644 --- a/scene/2d/camera_2d.h +++ b/scene/2d/camera_2d.h @@ -83,7 +83,7 @@ protected: void _update_scroll(); void _make_current(Object *p_which); - void _set_current(bool p_current); + void set_current(bool p_current); void _set_old_smoothing(real_t p_enable); diff --git a/scene/3d/camera_3d.cpp b/scene/3d/camera_3d.cpp index 1ed8c0b4eb9..0c38d85a6b8 100644 --- a/scene/3d/camera_3d.cpp +++ b/scene/3d/camera_3d.cpp @@ -85,14 +85,14 @@ void Camera3D::_update_camera() { // here goes listener stuff /* if (viewport_ptr && is_inside_scene() && is_current()) - get_viewport()->_camera_transform_changed_notify(); + get_viewport()->_camera_3d_transform_changed_notify(); */ if (get_tree()->is_node_being_edited(this) || !is_current()) { return; } - get_viewport()->_camera_transform_changed_notify(); + get_viewport()->_camera_3d_transform_changed_notify(); } void Camera3D::_notification(int p_what) { @@ -104,9 +104,9 @@ void Camera3D::_notification(int p_what) { viewport = get_viewport(); ERR_FAIL_COND(!viewport); - bool first_camera = viewport->_camera_add(this); + bool first_camera = viewport->_camera_3d_add(this); if (current || first_camera) { - viewport->_camera_set(this); + viewport->_camera_3d_set(this); } } break; @@ -128,7 +128,7 @@ void Camera3D::_notification(int p_what) { } if (viewport) { - viewport->_camera_remove(this); + viewport->_camera_3d_remove(this); viewport = nullptr; } @@ -220,7 +220,7 @@ void Camera3D::make_current() { return; } - get_viewport()->_camera_set(this); + get_viewport()->_camera_3d_set(this); //get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,camera_group,"_camera_make_current",this); } @@ -231,11 +231,11 @@ void Camera3D::clear_current(bool p_enable_next) { return; } - if (get_viewport()->get_camera() == this) { - get_viewport()->_camera_set(nullptr); + if (get_viewport()->get_camera_3d() == this) { + get_viewport()->_camera_3d_set(nullptr); if (p_enable_next) { - get_viewport()->_camera_make_next_current(this); + get_viewport()->_camera_3d_make_next_current(this); } } } @@ -250,7 +250,7 @@ void Camera3D::set_current(bool p_current) { bool Camera3D::is_current() const { if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) { - return get_viewport()->get_camera() == this; + return get_viewport()->get_camera_3d() == this; } else { return current; } diff --git a/scene/3d/cpu_particles_3d.cpp b/scene/3d/cpu_particles_3d.cpp index 178a269f177..9e0bc9f3a10 100644 --- a/scene/3d/cpu_particles_3d.cpp +++ b/scene/3d/cpu_particles_3d.cpp @@ -1003,7 +1003,7 @@ void CPUParticles3D::_update_particle_data_buffer() { sorter.sort(order, pc); } else if (draw_order == DRAW_ORDER_VIEW_DEPTH) { ERR_FAIL_NULL(get_viewport()); - Camera3D *c = get_viewport()->get_camera(); + Camera3D *c = get_viewport()->get_camera_3d(); if (c) { Vector3 dir = c->get_global_transform().basis.get_axis(2); //far away to close diff --git a/scene/3d/gpu_particles_collision_3d.cpp b/scene/3d/gpu_particles_collision_3d.cpp index c1e71b95651..5b88a6ec9f0 100644 --- a/scene/3d/gpu_particles_collision_3d.cpp +++ b/scene/3d/gpu_particles_collision_3d.cpp @@ -596,7 +596,7 @@ void GPUParticlesCollisionHeightField::_notification(int p_what) { } if (follow_camera_mode && get_viewport()) { - Camera3D *cam = get_viewport()->get_camera(); + Camera3D *cam = get_viewport()->get_camera_3d(); if (cam) { Transform3D xform = get_global_transform(); Vector3 x_axis = xform.basis.get_axis(Vector3::AXIS_X).normalized(); diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 51af70d2952..5a0a27520b8 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -37,6 +37,7 @@ #include "core/os/os.h" #include "core/string/translation.h" +#include "scene/2d/camera_2d.h" #include "scene/2d/collision_object_2d.h" #include "scene/3d/camera_3d.h" #include "scene/3d/collision_object_3d.h" @@ -195,7 +196,7 @@ void Viewport::_collision_object_input_event(CollisionObject3D *p_object, Camera return; //discarded } } - p_object->_input_event(camera, p_input_event, p_pos, p_normal, p_shape); + p_object->_input_event(camera_3d, p_input_event, p_pos, p_normal, p_shape); physics_last_object_transform = object_transform; physics_last_camera_transform = camera_transform; physics_last_id = id; @@ -463,7 +464,7 @@ void Viewport::_notification(int p_what) { } } - if (cameras.size() && !camera) { + if (cameras.size() && !camera_3d) { //there are cameras but no current camera, pick first in tree and make it current Camera3D *first = nullptr; for (Set::Element *E = cameras.front(); E; E = E->next()) { @@ -742,8 +743,8 @@ void Viewport::_process_picking() { if (physics_object_capture.is_valid()) { CollisionObject3D *co = Object::cast_to(ObjectDB::get_instance(physics_object_capture)); - if (co && camera) { - _collision_object_input_event(co, camera, ev, Vector3(), Vector3(), 0); + if (co && camera_3d) { + _collision_object_input_event(co, camera_3d, ev, Vector3(), Vector3(), 0); captured = true; if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) { physics_object_capture = ObjectID(); @@ -760,16 +761,16 @@ void Viewport::_process_picking() { if (last_id.is_valid()) { if (ObjectDB::get_instance(last_id) && last_object) { //good, exists - _collision_object_input_event(last_object, camera, ev, result.position, result.normal, result.shape); + _collision_object_input_event(last_object, camera_3d, ev, result.position, result.normal, result.shape); if (last_object->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) { physics_object_capture = last_id; } } } } else { - if (camera) { - Vector3 from = camera->project_ray_origin(pos); - Vector3 dir = camera->project_ray_normal(pos); + if (camera_3d) { + Vector3 from = camera_3d->project_ray_origin(pos); + Vector3 dir = camera_3d->project_ray_normal(pos); PhysicsDirectSpaceState3D *space = PhysicsServer3D::get_singleton()->space_get_direct_state(find_world_3d()->get_space()); if (space) { @@ -777,8 +778,8 @@ void Viewport::_process_picking() { ObjectID new_collider; if (col) { CollisionObject3D *co = Object::cast_to(result.collider); - if (co && co->can_process()) { - _collision_object_input_event(co, camera, ev, result.position, result.normal, result.shape); + if (co) { + _collision_object_input_event(co, camera_3d, ev, result.position, result.normal, result.shape); last_object = co; last_id = result.collider_id; new_collider = last_id; @@ -1032,64 +1033,68 @@ void Viewport::_listener_make_next_current(Listener3D *p_exclude) { } } else { // Attempt to reset listener to the camera position - if (camera != nullptr) { + if (camera_3d != nullptr) { _update_listener(); - _camera_transform_changed_notify(); + _camera_3d_transform_changed_notify(); } } } #endif -void Viewport::_camera_transform_changed_notify() { +void Viewport::_camera_3d_transform_changed_notify() { #ifndef _3D_DISABLED #endif } -void Viewport::_camera_set(Camera3D *p_camera) { +void Viewport::_camera_3d_set(Camera3D *p_camera) { #ifndef _3D_DISABLED - if (camera == p_camera) { + if (camera_3d == p_camera) { return; } - if (camera) { - camera->notification(Camera3D::NOTIFICATION_LOST_CURRENT); + if (camera_3d) { + camera_3d->notification(Camera3D::NOTIFICATION_LOST_CURRENT); } - camera = p_camera; + camera_3d = p_camera; if (!camera_override) { - if (camera) { - RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera()); + if (camera_3d) { + RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera_3d->get_camera()); } else { RenderingServer::get_singleton()->viewport_attach_camera(viewport, RID()); } } - if (camera) { - camera->notification(Camera3D::NOTIFICATION_BECAME_CURRENT); + if (camera_3d) { + camera_3d->notification(Camera3D::NOTIFICATION_BECAME_CURRENT); } _update_listener(); - _camera_transform_changed_notify(); + _camera_3d_transform_changed_notify(); #endif } -bool Viewport::_camera_add(Camera3D *p_camera) { +void Viewport::_camera_2d_set(Camera2D *p_camera_2d) { + camera_2d = p_camera_2d; +} + +bool Viewport::_camera_3d_add(Camera3D *p_camera) { cameras.insert(p_camera); return cameras.size() == 1; } -void Viewport::_camera_remove(Camera3D *p_camera) { +void Viewport::_camera_3d_remove(Camera3D *p_camera) { cameras.erase(p_camera); - if (camera == p_camera) { - camera->notification(Camera3D::NOTIFICATION_LOST_CURRENT); - camera = nullptr; + if (camera_3d == p_camera) { + camera_3d->notification(Camera3D::NOTIFICATION_LOST_CURRENT); + camera_3d = nullptr; } } #ifndef _3D_DISABLED -void Viewport::_camera_make_next_current(Camera3D *p_exclude) { +void Viewport::_camera_3d_make_next_current(Camera3D *p_exclude) { for (Set::Element *E = cameras.front(); E; E = E->next()) { if (p_exclude == E->get()) { continue; @@ -1097,7 +1102,7 @@ void Viewport::_camera_make_next_current(Camera3D *p_exclude) { if (!E->get()->is_inside_tree()) { continue; } - if (camera != nullptr) { + if (camera_3d != nullptr) { return; } @@ -1286,8 +1291,12 @@ Listener3D *Viewport::get_listener() const { return listener; } -Camera3D *Viewport::get_camera() const { - return camera; +Camera3D *Viewport::get_camera_3d() const { + return camera_3d; +} + +Camera2D *Viewport::get_camera_2d() const { + return camera_2d; } void Viewport::enable_camera_override(bool p_enable) { @@ -1305,8 +1314,8 @@ void Viewport::enable_camera_override(bool p_enable) { if (p_enable) { RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera_override.rid); - } else if (camera) { - RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera()); + } else if (camera_3d) { + RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera_3d->get_camera()); } else { RenderingServer::get_singleton()->viewport_attach_camera(viewport, RID()); } @@ -3548,7 +3557,8 @@ void Viewport::_bind_methods() { ClassDB::bind_method(D_METHOD("set_use_own_world_3d", "enable"), &Viewport::set_use_own_world_3d); ClassDB::bind_method(D_METHOD("is_using_own_world_3d"), &Viewport::is_using_own_world_3d); - ClassDB::bind_method(D_METHOD("get_camera"), &Viewport::get_camera); + ClassDB::bind_method(D_METHOD("get_camera_3d"), &Viewport::get_camera_3d); + ClassDB::bind_method(D_METHOD("get_camera_2d"), &Viewport::get_camera_2d); ClassDB::bind_method(D_METHOD("set_as_audio_listener", "enable"), &Viewport::set_as_audio_listener); ClassDB::bind_method(D_METHOD("is_audio_listener"), &Viewport::is_audio_listener); diff --git a/scene/main/viewport.h b/scene/main/viewport.h index 59715ebd0e0..7b7f1c38182 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -214,7 +214,8 @@ private: } } camera_override; - Camera3D *camera = nullptr; + Camera3D *camera_3d = nullptr; + Camera2D *camera_2d = nullptr; Set cameras; Set canvas_layers; @@ -450,11 +451,14 @@ private: void _listener_make_next_current(Listener3D *p_exclude); friend class Camera3D; - void _camera_transform_changed_notify(); - void _camera_set(Camera3D *p_camera); - bool _camera_add(Camera3D *p_camera); //true if first - void _camera_remove(Camera3D *p_camera); - void _camera_make_next_current(Camera3D *p_exclude); + void _camera_3d_transform_changed_notify(); + void _camera_3d_set(Camera3D *p_camera); + bool _camera_3d_add(Camera3D *p_camera); //true if first + void _camera_3d_remove(Camera3D *p_camera); + void _camera_3d_make_next_current(Camera3D *p_exclude); + + friend class Camera2D; + void _camera_2d_set(Camera2D *p_camera_2d); friend class CanvasLayer; void _canvas_layer_add(CanvasLayer *p_canvas_layer); @@ -497,7 +501,8 @@ public: uint64_t get_processed_events_count() const { return event_count; } Listener3D *get_listener() const; - Camera3D *get_camera() const; + Camera3D *get_camera_3d() const; + Camera2D *get_camera_2d() const; void enable_camera_override(bool p_enable); bool is_camera_override_enabled() const;