Work on the ability for overriding Worlds.

This commit is contained in:
Relintai 2022-08-29 20:56:17 +02:00
parent 00c25ed930
commit 49af677ca8
4 changed files with 170 additions and 29 deletions

View File

@ -301,32 +301,9 @@ void Viewport::_notification(int p_what) {
parent = nullptr; parent = nullptr;
} }
current_canvas = find_world_2d()->get_canvas(); _on_world_override_changed(this);
RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
RenderingServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
_update_listener();
_update_listener_2d();
find_world_2d()->_register_viewport(this, Rect2());
add_to_group("_viewports"); add_to_group("_viewports");
if (get_tree()->is_debugging_collisions_hint()) {
//2D
Physics2DServer::get_singleton()->space_set_debug_contacts(find_world_2d()->get_space(), get_tree()->get_collision_debug_contact_count());
contact_2d_debug = RID_PRIME(RenderingServer::get_singleton()->canvas_item_create());
RenderingServer::get_singleton()->canvas_item_set_parent(contact_2d_debug, find_world_2d()->get_canvas());
//3D
PhysicsServer::get_singleton()->space_set_debug_contacts(find_world_3d()->get_space(), get_tree()->get_collision_debug_contact_count());
contact_3d_debug_multimesh = RID_PRIME(RenderingServer::get_singleton()->multimesh_create());
RenderingServer::get_singleton()->multimesh_allocate(contact_3d_debug_multimesh, get_tree()->get_collision_debug_contact_count(), RS::MULTIMESH_TRANSFORM_3D, RS::MULTIMESH_COLOR_8BIT);
RenderingServer::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, 0);
RenderingServer::get_singleton()->multimesh_set_mesh(contact_3d_debug_multimesh, get_tree()->get_debug_contact_mesh()->get_rid());
contact_3d_debug_instance = RID_PRIME(RenderingServer::get_singleton()->instance_create());
RenderingServer::get_singleton()->instance_set_base(contact_3d_debug_instance, contact_3d_debug_multimesh);
RenderingServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance, find_world_3d()->get_scenario());
//RenderingServer::get_singleton()->instance_geometry_set_flag(contact_3d_debug_instance, RS::INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS, true);
}
RS::get_singleton()->viewport_set_active(viewport, true); RS::get_singleton()->viewport_set_active(viewport, true);
} break; } break;
@ -811,6 +788,44 @@ bool Viewport::is_audio_listener_2d() const {
Listener2D *Viewport::get_listener_2d() const { Listener2D *Viewport::get_listener_2d() const {
return listener_2d; return listener_2d;
} }
void Viewport::_on_world_override_changed(World *p_old_world) {
//ERR_FAIL_COND(!p_old_world);
//World *active_world = get_active_world();
//if (!active_world) {
// return;
//}
current_canvas = find_world_2d()->get_canvas();
RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
RenderingServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
_update_listener();
_update_listener_2d();
find_world_2d()->_register_viewport(this, Rect2());
if (get_tree()->is_debugging_collisions_hint()) {
//2D
Physics2DServer::get_singleton()->space_set_debug_contacts(find_world_2d()->get_space(), get_tree()->get_collision_debug_contact_count());
contact_2d_debug = RID_PRIME(RenderingServer::get_singleton()->canvas_item_create());
RenderingServer::get_singleton()->canvas_item_set_parent(contact_2d_debug, find_world_2d()->get_canvas());
//3D
PhysicsServer::get_singleton()->space_set_debug_contacts(find_world_3d()->get_space(), get_tree()->get_collision_debug_contact_count());
contact_3d_debug_multimesh = RID_PRIME(RenderingServer::get_singleton()->multimesh_create());
RenderingServer::get_singleton()->multimesh_allocate(contact_3d_debug_multimesh, get_tree()->get_collision_debug_contact_count(), RS::MULTIMESH_TRANSFORM_3D, RS::MULTIMESH_COLOR_8BIT);
RenderingServer::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, 0);
RenderingServer::get_singleton()->multimesh_set_mesh(contact_3d_debug_multimesh, get_tree()->get_debug_contact_mesh()->get_rid());
contact_3d_debug_instance = RID_PRIME(RenderingServer::get_singleton()->instance_create());
RenderingServer::get_singleton()->instance_set_base(contact_3d_debug_instance, contact_3d_debug_multimesh);
RenderingServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance, find_world_3d()->get_scenario());
//RenderingServer::get_singleton()->instance_geometry_set_flag(contact_3d_debug_instance, RS::INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS, true);
}
}
void Viewport::enable_canvas_transform_override(bool p_enable) { void Viewport::enable_canvas_transform_override(bool p_enable) {
if (override_canvas_transform == p_enable) { if (override_canvas_transform == p_enable) {

View File

@ -311,6 +311,7 @@ protected:
void _on_set_use_own_world_3d(bool p_use_own_world_3d); void _on_set_use_own_world_3d(bool p_use_own_world_3d);
void _on_set_world_3d(const Ref<World3D> &p_old_world); void _on_set_world_3d(const Ref<World3D> &p_old_world);
void _on_set_world_2d(const Ref<World2D> &p_old_world_2d); void _on_set_world_2d(const Ref<World2D> &p_old_world_2d);
void _on_world_override_changed(World *p_old_world);
private: private:
friend class ViewportTexture; friend class ViewportTexture;

View File

@ -5,6 +5,7 @@
#include "scene/3d/world_environment_3d.h" #include "scene/3d/world_environment_3d.h"
#include "scene/resources/world_2d.h" #include "scene/resources/world_2d.h"
#include "scene/resources/world_3d.h" #include "scene/resources/world_3d.h"
#include "viewport.h"
Ref<World2D> World::get_world_2d() const { Ref<World2D> World::get_world_2d() const {
return world_2d; return world_2d;
@ -107,7 +108,18 @@ void World::set_use_own_world_3d(bool p_use_own_world_3d) {
_on_set_use_own_world_3d(p_use_own_world_3d); _on_set_use_own_world_3d(p_use_own_world_3d);
} }
bool World::get_override_in_parent_viewport() {
return _override_in_parent_viewport;
}
void World::set_override_in_parent_viewport(const bool value) {
_override_in_parent_viewport = value;
}
Ref<World2D> World::find_world_2d() const { Ref<World2D> World::find_world_2d() const {
if (_override_world) {
return _override_world->find_world_2d();
}
if (world_2d.is_valid()) { if (world_2d.is_valid()) {
return world_2d; return world_2d;
} else if (_parent_world) { } else if (_parent_world) {
@ -118,6 +130,10 @@ Ref<World2D> World::find_world_2d() const {
} }
Ref<World3D> World::find_world_3d() const { Ref<World3D> World::find_world_3d() const {
if (_override_world) {
return _override_world->find_world_3d();
}
if (own_world_3d.is_valid()) { if (own_world_3d.is_valid()) {
return own_world_3d; return own_world_3d;
} else if (world_3d.is_valid()) { } else if (world_3d.is_valid()) {
@ -129,8 +145,47 @@ Ref<World3D> World::find_world_3d() const {
} }
} }
World *World::get_override_world() {
return _override_world;
}
World *World::get_override_world_or_this() {
if (!_override_world) {
return this;
}
return _override_world;
}
void World::set_override_world(World *p_world) {
if (p_world == _override_world) {
return;
}
World *old_world = _override_world;
if (old_world) {
old_world->_remove_overridden_world(this);
}
_override_world = p_world;
if (_override_world) {
_override_world->_add_overridden_world(this);
}
_on_world_override_changed(old_world);
}
void World::set_override_world_bind(Node *p_world) {
World *w = Object::cast_to<World>(p_world);
ERR_FAIL_COND(p_world && !w);
set_override_world(w);
}
World::World() { World::World() {
world_2d = Ref<World2D>(memnew(World2D)); world_2d = Ref<World2D>(memnew(World2D));
_override_world = NULL;
_override_in_parent_viewport = false;
} }
World::~World() { World::~World() {
} }
@ -181,6 +236,26 @@ void World::_propagate_exit_world(Node *p_node) {
} }
} }
void World::_add_overridden_world(World *p_world) {
ERR_FAIL_COND(!p_world);
_overriding_worlds.push_back(p_world);
}
void World::_remove_overridden_world(World *p_world) {
ERR_FAIL_COND(!p_world);
for (int i = 0; i < _overriding_worlds.size(); ++i) {
World *w = _overriding_worlds[i];
if (w == p_world) {
_overriding_worlds.remove(i);
return;
}
}
ERR_FAIL();
}
void World::_own_world_3d_changed() { void World::_own_world_3d_changed() {
ERR_FAIL_COND(world_3d.is_null()); ERR_FAIL_COND(world_3d.is_null());
ERR_FAIL_COND(own_world_3d.is_null()); ERR_FAIL_COND(own_world_3d.is_null());
@ -204,6 +279,9 @@ void World::_on_set_world_3d(const Ref<World3D> &p_old_world) {
void World::_on_set_world_2d(const Ref<World2D> &p_old_world_2d) { void World::_on_set_world_2d(const Ref<World2D> &p_old_world_2d) {
} }
void World::_on_world_override_changed(World *p_old_world) {
}
void World::_notification(int p_what) { void World::_notification(int p_what) {
switch (p_what) { switch (p_what) {
case NOTIFICATION_ENTER_TREE: { case NOTIFICATION_ENTER_TREE: {
@ -213,6 +291,27 @@ void World::_notification(int p_what) {
_parent_world = nullptr; _parent_world = nullptr;
} }
} break; } break;
case NOTIFICATION_READY: {
if (_override_in_parent_viewport) {
if (get_parent()) {
World *w = get_parent()->get_viewport();
if (w) {
w->set_override_world(this);
}
}
}
} break;
case NOTIFICATION_EXIT_TREE: {
for (int i = 0; i < _overriding_worlds.size(); ++i) {
World *w = _overriding_worlds[i];
w->set_override_world(NULL);
}
_override_world = NULL;
} break;
} }
} }
@ -225,12 +324,20 @@ void World::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_world_3d", "world"), &World::set_world_3d); ClassDB::bind_method(D_METHOD("set_world_3d", "world"), &World::set_world_3d);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world", PROPERTY_HINT_RESOURCE_TYPE, "World3D"), "set_world_3d", "get_world_3d"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world", PROPERTY_HINT_RESOURCE_TYPE, "World3D"), "set_world_3d", "get_world_3d");
ClassDB::bind_method(D_METHOD("set_use_own_world_3d", "enable"), &World::set_use_own_world_3d);
ClassDB::bind_method(D_METHOD("is_using_own_world_3d"), &World::is_using_own_world_3d); ClassDB::bind_method(D_METHOD("is_using_own_world_3d"), &World::is_using_own_world_3d);
ClassDB::bind_method(D_METHOD("set_use_own_world_3d", "enable"), &World::set_use_own_world_3d);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world_3d"), "set_use_own_world_3d", "is_using_own_world_3d"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world_3d"), "set_use_own_world_3d", "is_using_own_world_3d");
ClassDB::bind_method(D_METHOD("get_override_in_parent_viewport"), &World::get_override_in_parent_viewport);
ClassDB::bind_method(D_METHOD("set_override_in_parent_viewport", "enable"), &World::set_override_in_parent_viewport);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_in_parent_viewport"), "set_override_in_parent_viewport", "get_override_in_parent_viewport");
ClassDB::bind_method(D_METHOD("find_world_2d"), &World::find_world_2d); ClassDB::bind_method(D_METHOD("find_world_2d"), &World::find_world_2d);
ClassDB::bind_method(D_METHOD("find_world_3d"), &World::find_world_3d); ClassDB::bind_method(D_METHOD("find_world_3d"), &World::find_world_3d);
ClassDB::bind_method(D_METHOD("get_override_world"), &World::get_override_world);
ClassDB::bind_method(D_METHOD("get_override_world_or_this"), &World::get_override_world_or_this);
ClassDB::bind_method(D_METHOD("set_override_world", "world"), &World::set_override_world_bind);
ClassDB::bind_method(D_METHOD("_own_world_3d_changed"), &World::_own_world_3d_changed); ClassDB::bind_method(D_METHOD("_own_world_3d_changed"), &World::_own_world_3d_changed);
} }

View File

@ -1,6 +1,7 @@
#ifndef WORLD_H #ifndef WORLD_H
#define WORLD_H #define WORLD_H
#include "core/containers/vector.h"
#include "core/object/reference.h" #include "core/object/reference.h"
#include "node.h" #include "node.h"
@ -18,33 +19,50 @@ public:
Ref<World3D> get_world_3d() const; Ref<World3D> get_world_3d() const;
void set_world_3d(const Ref<World3D> &p_world); void set_world_3d(const Ref<World3D> &p_world);
void set_use_own_world_3d(bool p_use_own_world_3d);
bool is_using_own_world_3d() const; bool is_using_own_world_3d() const;
void set_use_own_world_3d(const bool p_use_own_world_3d);
bool get_override_in_parent_viewport();
void set_override_in_parent_viewport(const bool value);
Ref<World2D> find_world_2d() const; Ref<World2D> find_world_2d() const;
Ref<World3D> find_world_3d() const; Ref<World3D> find_world_3d() const;
World *get_override_world();
World *get_override_world_or_this();
void set_override_world(World *p_world);
void set_override_world_bind(Node *p_world);
World(); World();
~World(); ~World();
protected: protected:
void _notification(int p_what); void _add_overridden_world(World *p_world);
void _remove_overridden_world(World *p_world);
static void _bind_methods();
virtual void _own_world_3d_changed(); virtual void _own_world_3d_changed();
virtual void _on_set_use_own_world_3d(bool p_use_own_world_3d); virtual void _on_set_use_own_world_3d(bool p_use_own_world_3d);
virtual void _on_set_world_3d(const Ref<World3D> &p_old_world); virtual void _on_set_world_3d(const Ref<World3D> &p_old_world);
virtual void _on_set_world_2d(const Ref<World2D> &p_old_world_2d); virtual void _on_set_world_2d(const Ref<World2D> &p_old_world_2d);
virtual void _on_world_override_changed(World *p_old_world);
void _propagate_enter_world(Node *p_node); void _propagate_enter_world(Node *p_node);
void _propagate_exit_world(Node *p_node); void _propagate_exit_world(Node *p_node);
void _notification(int p_what);
static void _bind_methods();
Ref<World2D> world_2d; Ref<World2D> world_2d;
Ref<World3D> world_3d; Ref<World3D> world_3d;
Ref<World3D> own_world_3d; Ref<World3D> own_world_3d;
World *_parent_world; World *_parent_world;
bool _override_in_parent_viewport;
World *_override_world;
Vector<World *> _overriding_worlds;
}; };
#endif #endif