Skip to content

Commit

Permalink
fixed usage of force_use_camera_from_scene
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriySalnikov committed Mar 31, 2024
1 parent c6b8e7f commit f8737cc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
6 changes: 2 additions & 4 deletions src/3d/config_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ class DebugDraw3DConfig : public RefCounted {
bool is_use_frustum_culling() const;

/**
* Set the culling distance after which instances will stop being displayed to improve performance.
*
* If set to 0, this parameter will be ignored and instances will be drawn at any distance.
* Change the distance between the Far and Near Planes of the Viewport's Camera3D.
*/
void set_frustum_length_scale(const real_t &_distance);
real_t get_frustum_length_scale() const;

/**
* Set to force the use of the scene's default camera.
* Set the forced use of the scene camera instead of the editor camera.
*/
void set_force_use_camera_from_scene(const bool &_state);
bool is_force_use_camera_from_scene() const;
Expand Down
12 changes: 6 additions & 6 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ void DebugDraw3D::add_or_update_line_with_thickness(real_t p_exp_time, std::uniq
InstanceType::LINE_VOLUMETRIC,
p_exp_time,
GET_PROC_TYPE(),
Transform3D(Basis().looking_at(center, get_up_vector(center)).scaled(Vector3(len, len, len)), a), // slow
Transform3D(Basis().looking_at(center, get_up_vector(center)).scaled(VEC3_ONE(len)), a), // slow
p_col,
SphereBounds(a + center, len * .5f));
}
Expand Down Expand Up @@ -647,7 +647,7 @@ void DebugDraw3D::draw_sphere(const Vector3 &position, const real_t &radius, con
ZoneScoped;
CHECK_BEFORE_CALL();
real_t scale = radius * 2;
draw_sphere_base(Transform3D(Basis().scaled(Vector3(scale, scale, scale)), position), color, duration);
draw_sphere_base(Transform3D(Basis().scaled(VEC3_ONE(scale)), position), color, duration);
}

void DebugDraw3D::draw_sphere_xf(const Transform3D &transform, const Color &color, const real_t &duration) {
Expand Down Expand Up @@ -810,7 +810,7 @@ void DebugDraw3D::draw_line_hit(const Vector3 &start, const Vector3 &end, const
InstanceType::BILLBOARD_SQUARE,
duration,
GET_PROC_TYPE(),
Transform3D(Basis().scaled(Vector3_ONE * hit_size), hit),
Transform3D(Basis().scaled(VEC3_ONE(hit_size)), hit),
IS_DEFAULT_COLOR(hit_color) ? config->get_line_hit_color() : hit_color,
SphereBounds(hit, MathUtils::CubeRadiusForSphere * hit_size),
&Colors::empty_color);
Expand Down Expand Up @@ -907,7 +907,7 @@ void DebugDraw3D::create_arrow(const Vector3 &p_a, const Vector3 &p_b, const Col
real_t size = (p_is_absolute_size ? p_arrow_size : dir.length() * p_arrow_size) * 2;

Vector3 up = get_up_vector(dir);
Transform3D t = Transform3D(Basis().looking_at(dir, up).scaled(Vector3(size, size, size)), p_b);
Transform3D t = Transform3D(Basis().looking_at(dir, up).scaled(VEC3_ONE(size)), p_b);

GET_SCOPED_CFG_AND_DGC();

Expand Down Expand Up @@ -991,7 +991,7 @@ void DebugDraw3D::draw_square(const Vector3 &position, const real_t &size, const
ZoneScoped;
CHECK_BEFORE_CALL();

Transform3D t(Basis().scaled(Vector3_ONE * size), position);
Transform3D t(Basis().scaled(VEC3_ONE(size)), position);

LOCK_GUARD(datalock);
GET_SCOPED_CFG_AND_DGC();
Expand Down Expand Up @@ -1021,7 +1021,7 @@ void DebugDraw3D::draw_plane(const Plane &plane, const Color &color, const Vecto
Vector3 center_pos = plane.project(anchor_point == Vector3_INF ? (cam ? cam->get_global_position() : Vector3()) : anchor_point);
real_t plane_size = scfg->plane_size != INFINITY ? scfg->plane_size : (cam ? (real_t)cam->get_far() : 1000);
Transform3D t(Basis(), center_pos);
t = t.looking_at(center_pos + plane.normal, get_up_vector(plane.normal)).scaled_local(Vector3_ONE * plane_size);
t = t.looking_at(center_pos + plane.normal, get_up_vector(plane.normal)).scaled_local(VEC3_ONE(plane_size));
Color custom_col = Color::from_hsv(front_color.get_h(), Math::clamp(front_color.get_s() - 0.25f, 0.f, 1.f), Math::clamp(front_color.get_v() - 0.25f, 0.f, 1.f), front_color.a);

dgc->geometry_pool.add_or_update_instance(
Expand Down
47 changes: 33 additions & 14 deletions src/3d/debug_geometry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,41 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
std::vector<std::pair<Array, Camera3D *> > frustum_arrays;
frustum_arrays.reserve(1);

#ifdef DEBUG_ENABLED
auto custom_editor_viewports = owner->get_custom_editor_viewports();
Camera3D *vp_cam = vp_p->get_camera_3d();

const auto editor_vp = std::find(custom_editor_viewports.cbegin(), custom_editor_viewports.cend(), vp_p);

// TODO vp_cam is NULL. is_force_use_camera_from_scene is not working
if ((owner->get_config()->is_force_use_camera_from_scene() || editor_vp == custom_editor_viewports.cend()) && vp_cam) {
frustum_arrays.push_back({ vp_cam->get_frustum(), vp_cam });
} else if (custom_editor_viewports.size() > 0) {
for (auto vp : custom_editor_viewports) {
if (vp->get_update_mode() == SubViewport::UpdateMode::UPDATE_ALWAYS) {
auto c = vp->get_camera_3d();
frustum_arrays.push_back({ c->get_frustum(), c });
bool is_editor_vp = std::find_if(
custom_editor_viewports.cbegin(),
custom_editor_viewports.cend(),
[&vp_p](const auto &it) { return it == vp_p; }) != custom_editor_viewports.cend();

if (Engine::get_singleton()->is_editor_hint() && is_editor_vp) {
Camera3D *cam = nullptr;
Node *root = SCENE_TREE()->get_edited_scene_root();
if (root) {
cam = root->get_viewport()->get_camera_3d();
}

if (owner->config->is_force_use_camera_from_scene() && cam) {
frustum_arrays.push_back({ cam->get_frustum(), cam });
} else if (custom_editor_viewports.size() > 0) {
for (const auto &evp : custom_editor_viewports) {
if (evp->get_update_mode() == SubViewport::UpdateMode::UPDATE_ALWAYS) {
Camera3D *cam = evp->get_camera_3d();
if (cam) {
frustum_arrays.push_back({ cam->get_frustum(), cam });
}
}
}
}
} else {
#endif
Camera3D *vp_cam = vp_p->get_camera_3d();
if (vp_cam) {
frustum_arrays.push_back({ vp_cam->get_frustum(), vp_cam });
}
#ifdef DEBUG_ENABLED
}
#endif

// Convert Array to vector
if (frustum_arrays.size()) {
Expand Down Expand Up @@ -310,7 +329,7 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
InstanceType::SPHERE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(Vector3(radius, radius, radius) * 2), center),
Transform3D(Basis().scaled(VEC3_ONE(radius) * 2), center),
Colors::debug_sphere_bounds,
SphereBounds(center, radius));

Expand Down Expand Up @@ -349,7 +368,7 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
InstanceType::SPHERE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(Vector3(radius, radius, radius) * 2), center),
Transform3D(Basis().scaled(VEC3_ONE(radius) * 2), center),
Colors::debug_sphere_bounds,
SphereBounds(center, radius));
});
Expand Down
4 changes: 2 additions & 2 deletions src/3d/geometry_generators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ Ref<ArrayMesh> GeometryGenerator::ConvertWireframeToVolumetric(Ref<ArrayMesh> me
void GeometryGenerator::GenerateVolumetricSegment(const Vector3 &a, const Vector3 &b, const Vector3 &normal, PackedVector3Array &vertexes, PackedVector3Array &custom0, PackedInt32Array &indexes, PackedVector2Array &uv, const bool &add_caps) {
ZoneScoped;
bool debug_size = false;
Vector3 debug_mult = debug_size ? Vector3(1, 1, 1) * 0.5 : Vector3();
Vector3 debug_mult = debug_size ? Vector3_ONE * 0.5 : Vector3();
Vector3 dir = (b - a).normalized();
int64_t base_idx = vertexes.size();

Expand Down Expand Up @@ -385,7 +385,7 @@ void GeometryGenerator::GenerateVolumetricSegment(const Vector3 &a, const Vector
void GeometryGenerator::GenerateVolumetricSegmentBevel(const Vector3 &a, const Vector3 &b, const Vector3 &normal, PackedVector3Array &vertexes, PackedVector3Array &custom0, PackedInt32Array &indexes, PackedVector2Array &uv, const bool &add_caps) {
ZoneScoped;
bool debug_size = false;
Vector3 debug_mult = debug_size ? Vector3(1, 1, 1) * 0.5f : Vector3();
Vector3 debug_mult = debug_size ? Vector3_ONE * 0.5f : Vector3();
real_t half_len = .5f;
// real_t half_len = Math::clamp(len * .5f, .0f, 0.5f);
Vector3 dir = (b - a).normalized();
Expand Down
7 changes: 6 additions & 1 deletion src/debug_draw_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ void DebugDrawManager::_integrate_into_engine() {
}
debug_draw_3d_singleton->set_custom_editor_viewport(editor_viewports);

if (editor_viewports.size()) {
debug_draw_3d_singleton->scoped_config()->set_viewport(editor_viewports[0]);
}

/*
// Used to explore the editor tree.
auto f = FileAccess::open("user://tree.txt", FileAccess::WRITE);
Expand All @@ -333,10 +337,11 @@ void DebugDrawManager::_integrate_into_engine() {

// will be auto deleted as child
add_child(default_canvas);

debug_draw_3d_singleton->scoped_config()->set_viewport(SCENE_ROOT());
}

debug_draw_2d_singleton->set_custom_canvas(debug_draw_2d_singleton->custom_canvas);
debug_draw_3d_singleton->scoped_config()->set_viewport(SCENE_ROOT());
_connect_scene_changed();
#endif

Expand Down
1 change: 1 addition & 0 deletions src/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ constexpr size_t INSTANCE_DATA_FLOAT_COUNT = ((sizeof(godot::Transform3D) + size

// TODO: temp constants.

#define VEC3_ONE(comp) Vector3(comp, comp, comp)
const extern godot::Vector3 Vector3_ZERO;
const extern godot::Vector3 Vector3_INF;
const extern godot::Vector3 Vector3_ONE;
Expand Down

0 comments on commit f8737cc

Please sign in to comment.