add viewport.get_camera_2d()

* there is now a more clear distinction between camera_2d and camera_3d functions in the engine code
* simplified camera2d's exported interface - now everything happens directly with the 'current' variable and make_current and clear_current are no longer exposed- there were some situations where calling one instead of set_current would result in incomplete results
* rebased to current godot master
This commit is contained in:
Josh Chandler 2020-04-28 11:04:07 -04:00
parent e849dc1791
commit 879f84d8f8
13 changed files with 113 additions and 91 deletions

View file

@ -21,13 +21,6 @@
Aligns the camera to the tracked node.
</description>
</method>
<method name="clear_current">
<return type="void">
</return>
<description>
Removes any [Camera2D] from the ancestor [Viewport]'s internal currently-assigned camera.
</description>
</method>
<method name="force_update_scroll">
<return type="void">
</return>
@ -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].
</description>
</method>
<method name="make_current">
<return type="void">
</return>
<description>
Make this the current 2D camera for the scene (viewport and layer), in case there are many cameras in the scene.
</description>
</method>
<method name="reset_smoothing">
<return type="void">
</return>
@ -109,7 +95,7 @@
<member name="anchor_mode" type="int" setter="set_anchor_mode" getter="get_anchor_mode" enum="Camera2D.AnchorMode" default="1">
The Camera2D's anchor point. See [enum AnchorMode] constants.
</member>
<member name="current" type="bool" setter="_set_current" getter="is_current" default="false">
<member name="current" type="bool" setter="set_current" getter="is_current" default="false">
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.
</member>
<member name="custom_viewport" type="Node" setter="set_custom_viewport" getter="get_custom_viewport">

View file

@ -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]
</description>
</method>

View file

@ -36,11 +36,18 @@
Returns the 3D world of the viewport, or if none the world of the parent viewport.
</description>
</method>
<method name="get_camera" qualifiers="const">
<method name="get_camera_2d" qualifiers="const">
<return type="Camera2D">
</return>
<description>
Returns the currently active 2D camera. Returns null if there are no active cameras.
</description>
</method>
<method name="get_camera_3d" qualifiers="const">
<return type="Camera3D">
</return>
<description>
Returns the active 3D camera.
Returns the currently active 3D camera.
</description>
</method>
<method name="get_final_transform" qualifiers="const">

View file

@ -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());

View file

@ -2418,7 +2418,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) {

View file

@ -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();

View file

@ -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");

View file

@ -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);

View file

@ -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;
}

View file

@ -1007,7 +1007,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

View file

@ -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();

View file

@ -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;
@ -462,7 +463,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<Camera3D *>::Element *E = cameras.front(); E; E = E->next()) {
@ -755,8 +756,8 @@ void Viewport::_process_picking() {
if (physics_object_capture.is_valid()) {
CollisionObject3D *co = Object::cast_to<CollisionObject3D>(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();
@ -773,16 +774,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) {
@ -790,8 +791,8 @@ void Viewport::_process_picking() {
ObjectID new_collider;
if (col) {
CollisionObject3D *co = Object::cast_to<CollisionObject3D>(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;
@ -1045,64 +1046,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<Camera3D *>::Element *E = cameras.front(); E; E = E->next()) {
if (p_exclude == E->get()) {
continue;
@ -1110,7 +1115,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;
}
@ -1299,8 +1304,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) {
@ -1318,8 +1327,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());
}
@ -3517,7 +3526,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);

View file

@ -213,7 +213,8 @@ private:
}
} camera_override;
Camera3D *camera = nullptr;
Camera3D *camera_3d = nullptr;
Camera2D *camera_2d = nullptr;
Set<Camera3D *> cameras;
Set<CanvasLayer *> canvas_layers;
@ -444,11 +445,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);
@ -491,7 +495,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;