Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriySalnikov committed Mar 25, 2024
1 parent c626f27 commit 45de581
Show file tree
Hide file tree
Showing 11 changed files with 529 additions and 635 deletions.
15 changes: 13 additions & 2 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,25 @@ func _physics_process(delta: float) -> void:
if !physics_tick_processed:
physics_tick_processed = true
main_update(delta)

_update_timers(delta)


# Physics specific:
if not zylann_example:
DebugDraw3D.draw_line($"Lines/8".global_position, $Lines/Target.global_position, Color.YELLOW)

_draw_rays_casts()

## Additional drawing in the Viewport
if false:
var _w1 = DebugDraw3D.new_scoped_config().set_viewport(%OtherWorldBox.get_viewport()).set_thickness(0.01).set_center_brightness(1)
DebugDraw3D.draw_box_xf(Transform3D(Basis()
.scaled(Vector3.ONE*0.3)
.rotated(Vector3(0,0,1), PI/4)
.rotated(Vector3(0,1,0), wrapf(Time.get_ticks_msec() / -1500.0, 0, TAU) - PI/4), %OtherWorldBox.global_transform.origin),
Color.BROWN, true, 0.4)


func main_update(delta: float) -> void:
if Input.is_action_just_pressed("ui_end"):
Expand Down Expand Up @@ -328,8 +339,8 @@ func _text_tests():
DebugDraw2D.set_text("Instances", render_stats.instances + render_stats.instances_physics, 1)
DebugDraw2D.set_text("Lines", render_stats.lines + render_stats.lines_physics, 2)
DebugDraw2D.set_text("Total Visible", render_stats.total_visible, 3)
DebugDraw2D.set_text("Visible Instances", render_stats.visible_instances + render_stats.visible_instances_physics, 4)
DebugDraw2D.set_text("Visible Lines", render_stats.visible_lines + render_stats.visible_lines_physics, 5)
DebugDraw2D.set_text("Visible Instances", render_stats.visible_instances, 4)
DebugDraw2D.set_text("Visible Lines", render_stats.visible_lines, 5)

DebugDraw2D.set_text("---", null, 6)

Expand Down
4 changes: 2 additions & 2 deletions examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.531922, -1.34723, 1.44924)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.34837, -1.08298, 4.36414)

[node name="AABB_fixed" type="Node3D" parent="Boxes"]
transform = Transform3D(0.834492, 0, -0.551019, 0, 1, 0, 0.55102, 0, 0.834493, -3.12735, -0.835878, 0.470324)
transform = Transform3D(0.834492, 0, -0.551019, 0, 1, 0, 0.55102, 0, 0.834493, -3.71325, -1.03995, 0.470324)

[node name="AABB" type="Node3D" parent="Boxes"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.99963, -0.869998, 0.205034)
Expand Down Expand Up @@ -635,7 +635,7 @@ transform = Transform3D(-0.998873, -0.0207882, -0.0355641, 0.0855357, -0.5714, -
transform = Transform3D(-0.998873, -0.0207882, -0.0355641, 0.0855353, -0.5714, -2.68836, 0.0136016, -0.249864, 0.572533, -1.01372, -3.80486, 1.25019)

[node name="Target" type="Node3D" parent="Lines"]
transform = Transform3D(1, -2.7352e-06, 2.60722e-07, 3.7807e-06, 1, 0, -3.59182e-07, -1.49012e-08, 1, -0.69134, 0.176476, 1.30597)
transform = Transform3D(1, -2.7352e-06, 2.60722e-07, 4.10378e-06, 1, 0, -4.28605e-07, -1.49012e-08, 1, -0.69134, 0.176475, 1.30597)

[node name="LagTest" type="CSGBox3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 7, -2, 0)
Expand Down
7 changes: 3 additions & 4 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ void DebugDraw3D::add_or_update_line_with_thickness(real_t _exp_time, std::uniqu
_col);
} else {
for (int i = 0; i < _line_count; i += 2) {
ZoneScoped;
ZoneScopedN("Convert AB to xf");
Vector3 a = _lines.get()[i];
Vector3 diff = _lines.get()[i + 1] - a;
real_t len = diff.length();
Expand Down Expand Up @@ -773,9 +773,8 @@ void DebugDraw3D::draw_aabb(const AABB &aabb, const Color &color, const real_t &
ZoneScoped;
CHECK_BEFORE_CALL();

Vector3 bottom, top, diag;
MathUtils::get_diagonal_vectors(aabb.position, aabb.position + aabb.size, bottom, top, diag);
draw_box_xf(Transform3D(Basis().scaled(diag), bottom), color, false, duration);
AABB a(aabb.abs());
draw_box_xf(Transform3D(Basis().scaled(a.size), a.position), color, false, duration);
}

void DebugDraw3D::draw_aabb_ab(const Vector3 &a, const Vector3 &b, const Color &color, const real_t &duration) {
Expand Down
2 changes: 1 addition & 1 deletion src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DebugDraw3DStats;

#ifndef DISABLE_DEBUG_RENDERING
class DebugGeometryContainer;
class DelayedRendererLine;
struct DelayedRendererLine;

enum class InstanceType : char;
enum class ConvertableInstanceType : char;
Expand Down
175 changes: 84 additions & 91 deletions src/3d/debug_geometry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,20 @@ void DebugGeometryContainer::update_geometry(double delta) {
LOCK_GUARD(owner->datalock);

// cleanup and get available viewports
std::vector<Viewport *> available_viewports = geometry_pool.get_and_validate_viewports();
std::unordered_set<Viewport *> available_viewports = geometry_pool.get_and_validate_viewports();

// Do not update geometry if frozen
if (owner->get_config()->is_freeze_3d_render())
return;

if (immediate_mesh_storage.mesh->get_surface_count())
if (immediate_mesh_storage.mesh->get_surface_count()) {
ZoneScopedN("Clear lines");
immediate_mesh_storage.mesh->clear_surfaces();
}

// Return if nothing to do
if (!owner->is_debug_enabled()) {
ZoneScopedN("Reset instances");
for (auto &item : multi_mesh_storage) {
if (item.mesh->get_visible_instance_count())
item.mesh->set_visible_instance_count(0);
Expand All @@ -209,121 +212,111 @@ void DebugGeometryContainer::update_geometry(double delta) {
set_render_layer_mask(owner->get_config()->get_geometry_render_layers());
}

std::unordered_map<const Viewport *, std::shared_ptr<GeometryPoolCullingData> > culling_data;
for (const auto &vp_p : available_viewports) {
// Collect frustums and camera positions
std::shared_ptr<GeometryPoolCullingData> culling_data;
std::vector<Vector3> cameras_positions;
{
// TODO fix in editor!
ZoneScopedN("Get frustums");
std::vector<std::array<Plane, 6> > frustum_planes;
std::vector<Vector3> cameras_positions;
cameras_positions.reserve(1);

std::vector<Array> frustum_arrays;
frustum_arrays.reserve(1);

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

if ((owner->get_config()->is_force_use_camera_from_scene() || !custom_editor_viewports.size()) && vp_cam) {
frustum_arrays.push_back(vp_cam->get_frustum());
cameras_positions.push_back(vp_cam->get_position());
} 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());
cameras_positions.push_back(c->get_position());
for (const auto &vp_p : available_viewports) {
// Collect frustums and camera positions

std::vector<Array> frustum_arrays;
frustum_arrays.reserve(1);

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

if ((owner->get_config()->is_force_use_camera_from_scene() || !custom_editor_viewports.size()) && vp_cam) {
frustum_arrays.push_back(vp_cam->get_frustum());
cameras_positions.push_back(vp_cam->get_position());
} 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());
cameras_positions.push_back(c->get_position());
}
}
}
}

// Convert Array to vector
if (owner->get_config()->is_use_frustum_culling() && frustum_arrays.size()) {
frustum_planes.reserve(frustum_arrays.size());

for (auto &arr : frustum_arrays) {
if (arr.size() == 6) {
std::array<Plane, 6> a;
for (int i = 0; i < arr.size(); i++)
a[i] = (Plane)arr[i];
frustum_planes.push_back(a);
// Convert Array to vector
if (owner->get_config()->is_use_frustum_culling() && frustum_arrays.size()) {
for (auto &arr : frustum_arrays) {
if (arr.size() == 6) {
std::array<Plane, 6> a;
for (int i = 0; i < arr.size(); i++)
a[i] = (Plane)arr[i];
frustum_planes.push_back(a);
}
}
}
}

culling_data[vp_p] = std::make_shared<GeometryPoolCullingData>(frustum_planes, /*TODO replace by scoped somehow*/ owner->get_config()->get_culling_distance(), cameras_positions);
culling_data = std::make_shared<GeometryPoolCullingData>(frustum_planes, /*TODO replace by scoped somehow*/ owner->get_config()->get_culling_distance(), cameras_positions);
}

// Update visibility
geometry_pool.update_visibility(culling_data);

// Debug bounds of instances and lines
if (owner->get_config()->is_visible_instance_bounds()) {
ZoneScopedN("Debug bounds");
struct sphereDebug {
Vector3 pos;
real_t radius;
Viewport *vp;
};
std::vector<sphereDebug> new_instances;

auto draw_instance_spheres = [&new_instances](Viewport *vp, DelayedRendererInstance *o) {
if (!o->is_visible || o->is_expired())
return;
new_instances.push_back({ o->bounds.position, o->bounds.Radius, vp });
};

geometry_pool.for_each_instance(draw_instance_spheres);

auto cfg = std::make_shared<DebugDraw3DScopeConfig::Data>(owner->scoped_config()->data);

// Draw custom sphere for 1 frame
for (auto &i : new_instances) {
cfg->viewport = i.vp;
Viewport *vp;
if (available_viewports.size()) {
vp = *available_viewports.begin();

std::vector<sphereDebug> new_instances;
geometry_pool.for_each_instance([&new_instances](DelayedRendererInstance *o) {
if (!o->is_visible || o->is_expired())
return;
new_instances.push_back({ o->bounds.position, o->bounds.Radius });
});

auto cfg = std::make_shared<DebugDraw3DScopeConfig::Data>(owner->scoped_config()->data);

// Draw custom sphere for 1 frame
for (auto &i : new_instances) {
cfg->viewport = vp;

geometry_pool.add_or_update_instance(
cfg,
InstanceType::SPHERE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(Vector3_ONE * i.radius * 2), i.pos),
Colors::debug_bounds,
SphereBounds(i.pos, i.radius),
&Colors::empty_color);
}

geometry_pool.add_or_update_instance(
cfg,
InstanceType::SPHERE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(Vector3_ONE * i.radius * 2), i.pos),
Colors::debug_bounds,
SphereBounds(i.pos, i.radius),
&Colors::empty_color,
[](auto d) { d->is_used_one_time = true; });
geometry_pool.for_each_line([this, &cfg, &vp](DelayedRendererLine *o) {
if (!o->is_visible || o->is_expired())
return;

cfg->viewport = vp;
geometry_pool.add_or_update_instance(
cfg,
InstanceType::CUBE_CENTERED,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(o->bounds.extent * 2), o->bounds.center),
Colors::debug_bounds,
SphereBounds(o->bounds.center, abs(o->bounds.extent.length())),
&Colors::empty_color);
});
}

geometry_pool.for_each_line([this, &cfg](Viewport *vp, DelayedRendererLine *o) {
if (!o->is_visible || o->is_expired())
return;

Vector3 bottom, top, diag;
MathUtils::get_diagonal_vectors(o->bounds.position, o->bounds.position + o->bounds.size, bottom, top, diag);
cfg->viewport = vp;

geometry_pool.add_or_update_instance(
cfg,
InstanceType::CUBE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(diag), bottom),
Colors::debug_bounds,
SphereBounds(bottom + diag * 0.5f, abs(diag.length() * 0.5f)),
&Colors::empty_color,
[](auto d) { d->is_used_one_time = true; });
});
}

// Draw immediate lines
geometry_pool.fill_lines_data(immediate_mesh_storage.mesh, delta);

// Update MultiMeshInstances
static std::array<Ref<MultiMesh> *, (int)InstanceType::MAX> meshes;
std::vector<Ref<MultiMesh> *> meshes((int)InstanceType::MAX);
for (int i = 0; i < (int)InstanceType::MAX; i++) {
meshes[i] = &multi_mesh_storage[i].mesh;
}

geometry_pool.fill_instance_data(meshes, delta);
geometry_pool.fill_lines_data(immediate_mesh_storage.mesh, delta);
geometry_pool.fill_instance_data(meshes, culling_data, delta);

geometry_pool.scan_visible_instances();
geometry_pool.update_expiration(delta, ProcessType::PROCESS);
geometry_pool.reset_counter(delta, ProcessType::PROCESS);

Expand Down
Loading

0 comments on commit 45de581

Please sign in to comment.