Skip to content

Commit

Permalink
fixed path to lines conversion (2 extra points were created).
Browse files Browse the repository at this point in the history
frustum culling is now enabled by default.
culling now takes thickness into account.
  • Loading branch information
DmitriySalnikov committed Mar 28, 2024
1 parent 03690de commit e268ce4
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 44 deletions.
42 changes: 22 additions & 20 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,27 @@ func _process(delta) -> void:
## Since physics frames may not be called every frame or may be called multiple times in one frame,
## there is an additional check to ensure that a new frame has been drawn before updating the data.
func _physics_process(delta: float) -> void:
if update_in_physics:
if !physics_tick_processed:
physics_tick_processed = true
if !physics_tick_processed:
physics_tick_processed = true
if update_in_physics:
main_update(delta)

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

if more_test_cases:
_draw_rays_casts()

## Additional drawing in the Viewport
if true:
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)
_update_timers(delta)

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

if more_test_cases:
_draw_rays_casts()

## Additional drawing in the Viewport
if true:
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:
Expand Down Expand Up @@ -226,6 +225,7 @@ func main_update(delta: float) -> void:
points_below2.append(c.global_position + Vector3.DOWN * 2)
points_below3.append(c.global_position + Vector3.DOWN * 3)
points_below4.append(c.global_position + Vector3.DOWN * 4)

for x in points.size()-1:
lines_above.append(points[x] + Vector3.UP)
lines_above.append(points[x+1] + Vector3.UP)
Expand Down Expand Up @@ -431,6 +431,7 @@ func _draw_array_of_boxes():
var z_size := 3
var mul := 1
var cubes_max_time := 1.25
var cfg = DebugDraw3D.new_scoped_config()

if draw_1m_boxes:
x_size = 100
Expand All @@ -444,6 +445,7 @@ func _draw_array_of_boxes():
for y in y_size:
for z in z_size:
var size = Vector3.ONE
cfg.set_thickness(randf_range(0, 0.1))
#var size = Vector3(randf_range(0.1, 100),randf_range(0.1, 100),randf_range(0.1, 100))
DebugDraw3D.draw_box(Vector3(x * mul, (-4-z) * mul, y * mul), Quaternion.IDENTITY, size, DebugDraw3D.empty_color, false, cubes_max_time)
timer_cubes = cubes_max_time
Expand Down
2 changes: 1 addition & 1 deletion src/3d/config_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool DebugDraw3DConfig::is_use_frustum_culling() const {
}

void DebugDraw3DConfig::set_culling_distance(const real_t &_distance) {
cull_by_distance = Math::clamp(_distance, -1.0f, (real_t)INT_MAX);
cull_by_distance = Math::clamp(_distance, 0.0f, 1.0f);
}

real_t DebugDraw3DConfig::get_culling_distance() const {
Expand Down
6 changes: 2 additions & 4 deletions src/3d/config_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class DebugDraw3DConfig : public RefCounted {
int32_t geometry_render_layers = 1;
bool freeze_3d_render = false;
bool visible_instance_bounds = false;
bool use_frustum_culling = false;
real_t cull_by_distance = -1;
bool use_frustum_culling = true;
real_t cull_by_distance = 0;
bool force_use_camera_from_scene = false;
Color line_hit_color = Colors::red;
Color line_after_hit_color = Colors::green;
Expand All @@ -48,8 +48,6 @@ class DebugDraw3DConfig : public RefCounted {

/**
* Set whether frustum culling is used.
* @warning
* At this point, activating this parameter is likely to decrease performance.
*/
void set_use_frustum_culling(const bool &_state);
bool is_use_frustum_culling() const;
Expand Down
11 changes: 5 additions & 6 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,15 +634,14 @@ void DebugDraw3D::draw_sphere_base(const Transform3D &transform, const Color &co
GET_PROC_TYPE(),
transform,
IS_DEFAULT_COLOR(color) ? Colors::chartreuse : color,

// TODO add line thickness to the radius
SphereBounds(transform.origin, MathUtils::get_max_basis_length(transform.basis) * 0.51f));
SphereBounds(transform.origin, MathUtils::get_max_basis_length(transform.basis) * 0.5f));
}

void DebugDraw3D::draw_sphere(const Vector3 &position, const real_t &radius, const Color &color, const real_t &duration) {
ZoneScoped;
CHECK_BEFORE_CALL();
draw_sphere_base(Transform3D(Basis().scaled(Vector3_ONE * (radius * 2)), position), color, duration);
real_t scale = radius * 2;
draw_sphere_base(Transform3D(Basis().scaled(Vector3(scale, scale, scale)), position), color, duration);
}

void DebugDraw3D::draw_sphere_xf(const Transform3D &transform, const Color &color, const real_t &duration) {
Expand Down Expand Up @@ -884,7 +883,7 @@ void DebugDraw3D::draw_line_path(const PackedVector3Array &path, const Color &co
return;
}

size_t s = path.size() * 2;
size_t s = (path.size() - 1) * 2;
std::unique_ptr<Vector3[]> l(new Vector3[s]);
GeometryGenerator::CreateLinesFromPathWireframe(path, l.get());

Expand Down Expand Up @@ -953,7 +952,7 @@ void DebugDraw3D::draw_arrow_path(const PackedVector3Array &path, const Color &c
ZoneScoped;
CHECK_BEFORE_CALL();

size_t s = path.size() * 2;
size_t s = (path.size() - 1) * 2;
std::unique_ptr<Vector3[]> l(new Vector3[s]);
GeometryGenerator::CreateLinesFromPathWireframe(path, l.get());

Expand Down
40 changes: 32 additions & 8 deletions src/3d/debug_geometry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ Ref<World3D> DebugGeometryContainer::get_world() {
return base_world_viewport;
}

// TODO add mark_dirty for 3d to reduce editor updates if only delayed shapes are displayed.
void DebugGeometryContainer::update_geometry(double p_delta) {
ZoneScoped;
LOCK_GUARD(owner->datalock);
Expand Down Expand Up @@ -298,32 +297,57 @@ void DebugGeometryContainer::update_geometry(double p_delta) {
// Draw custom sphere for 1 frame
for (auto &i : new_instances) {
cfg->viewport = vp;
Vector3 diag = i.max - i.min;
Vector3 center = i.center;
real_t radius = i.radius;

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

geometry_pool.add_or_update_instance(
cfg,
InstanceType::CUBE_CENTERED,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(diag), center),
Colors::debug_rough_box_bounds,
SphereBounds(center, radius));
}

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

Vector3 diag = o->bounds.max - o->bounds.min;
Vector3 center = o->bounds.center;
real_t radius = o->bounds.radius;

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

// TODO test bounds
geometry_pool.add_or_update_instance(
cfg,
InstanceType::SPHERE,
0,
ProcessType::PROCESS,
Transform3D(Basis().scaled(Vector3(radius, radius, radius) * 2), center),
Colors::debug_sphere_bounds,
SphereBounds(center, radius));
});
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/3d/render_instances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,11 @@ void GeometryPool::add_or_update_instance(const std::shared_ptr<DebugDraw3DScope
auto &proc = pools[(int)p_proc];
DelayedRendererInstance *inst = proc.instances[(int)p_type].get(p_exp_time > 0);

SphereBounds thick_sphere = p_bounds;
thick_sphere.radius += p_cfg->thickness * 0.5f;

inst->data = GeometryPoolData3DInstance(p_transform, p_col, p_custom_col ? *p_custom_col : _scoped_config_to_custom(p_cfg));
inst->bounds = p_bounds;
inst->bounds = thick_sphere;
inst->expiration_time = p_exp_time;
inst->is_used_one_time = false;
inst->is_visible = true;
Expand Down
4 changes: 3 additions & 1 deletion src/common/colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const Color Colors::chartreuse = Color(0.5f, 1, 0, 1);
const Color Colors::crimson = Color(0.86f, 0.08f, 0.24f, 1);
const Color Colors::dark_orange = Color(1, 0.55f, 0, 1);
const Color Colors::dark_salmon = Color(0.91f, 0.59f, 0.48f, 1);
const Color Colors::debug_bounds = Color(1, 0.55f, 0, 1);
const Color Colors::debug_sphere_bounds = Color(1, 0.55f, 0, 1);
const Color Colors::debug_box_bounds = Color(0, 0.71f, 1, 1);
const Color Colors::debug_rough_box_bounds = Color(0, 1, 0.55f, 1);
const Color Colors::dodgerblue = Color(0.12f, 0.56f, 1, 1);
const Color Colors::forest_green = Color(0.13f, 0.55f, 0.13f, 1);
const Color Colors::gray_bg = Color(0.3f, 0.3f, 0.3f, 0.8f);
Expand Down
4 changes: 3 additions & 1 deletion src/common/colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class Colors {
const static godot::Color crimson;
const static godot::Color dark_orange;
const static godot::Color dark_salmon;
const static godot::Color debug_bounds;
const static godot::Color debug_sphere_bounds;
const static godot::Color debug_box_bounds;
const static godot::Color debug_rough_box_bounds;
const static godot::Color dodgerblue;
const static godot::Color forest_green;
const static godot::Color gray_bg;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/math_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ AABBMinMax::AABBMinMax() :
max() {}

AABBMinMax::AABBMinMax(const AABB &p_from) :
radius(p_from.size.length() * 0.5f),
radius(p_from.size.length() * .5f),
min(p_from.position),
max(p_from.position + p_from.size) {
center = p_from.position + p_from.size * 0.5f;
Expand Down
26 changes: 25 additions & 1 deletion src/utils/math_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ struct AABBMinMax {
AABBMinMax(const SphereBounds &p_from);

_FORCE_INLINE_ bool intersects(const AABBMinMax &p_aabb) const;
_FORCE_INLINE_ void merge_with(const AABBMinMax &p_aabb);
_FORCE_INLINE_ void reset();

_FORCE_INLINE_ operator AABB() {
return AABB(min, max - min);
}
};

void MathUtils::get_diagonal_vectors(const Vector3 &p_a, const Vector3 &p_b, Vector3 &r_bottom, Vector3 &r_top, Vector3 &r_diag) {
Expand Down Expand Up @@ -177,4 +183,22 @@ bool AABBMinMax::intersects(const AABBMinMax &p_aabb) const {
max.y > p_aabb.min.y &&
min.z < p_aabb.max.z &&
max.z > p_aabb.min.z;
}
}

_FORCE_INLINE_ void AABBMinMax::merge_with(const AABBMinMax &p_aabb) {
if (radius) {
max = max.max(p_aabb.max);
min = min.min(p_aabb.min);
} else {
radius = 1;
max = p_aabb.max;
min = p_aabb.min;
}
}

_FORCE_INLINE_ void AABBMinMax::reset() {
center = Vector3();
radius = 0;
max = Vector3();
min = Vector3();
}

0 comments on commit e268ce4

Please sign in to comment.