Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing debug color of Path3D. #82321

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/classes/Path3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
<member name="curve" type="Curve3D" setter="set_curve" getter="get_curve">
A [Curve3D] describing the path.
</member>
<member name="debug_custom_color" type="Color" setter="set_debug_custom_color" getter="get_debug_custom_color" default="Color(0, 0, 0, 1)">
The custom color to use to draw the shape in the editor.
If set to [code]Color(0.0, 0.0, 0.0)[/code] (by default), the color set in EditorSettings is used.
</member>
<member name="debug_show" type="bool" setter="set_debug_show" getter="get_debug_show" default="true">
If [code]true[/code], debug shape will be visible.
</member>
</members>
<signals>
<signal name="curve_changed">
Expand Down
15 changes: 11 additions & 4 deletions editor/plugins/path_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ void Path3DGizmo::commit_handle(int p_id, bool p_secondary, const Variant &p_res
void Path3DGizmo::redraw() {
clear();

Ref<StandardMaterial3D> path_material = gizmo_plugin->get_material("path_material", this);
Ref<StandardMaterial3D> path_thin_material = gizmo_plugin->get_material("path_thin_material", this);
Ref<StandardMaterial3D> path_tilt_material = gizmo_plugin->get_material("path_tilt_material", this);
Ref<StandardMaterial3D> path_tilt_muted_material = gizmo_plugin->get_material("path_tilt_muted_material", this);
Expand All @@ -284,6 +283,14 @@ void Path3DGizmo::redraw() {
return;
}

debug_material = gizmo_plugin->get_material("path_material", this);

Color path_color = path->get_debug_custom_color();
if (path_color != Color(0.0, 0.0, 0.0)) {
debug_material.instantiate();
debug_material->set_albedo(path_color);
}

real_t interval = 0.1;
const real_t length = c->get_baked_length();

Expand Down Expand Up @@ -346,8 +353,8 @@ void Path3DGizmo::redraw() {
}

add_collision_segments(_collision_segments);
add_lines(bones, path_material);
add_vertices(ribbon, path_material, Mesh::PRIMITIVE_LINE_STRIP);
add_lines(bones, debug_material);
add_vertices(ribbon, debug_material, Mesh::PRIMITIVE_LINE_STRIP);
}

// 2. Draw handles when selected.
Expand Down Expand Up @@ -427,7 +434,7 @@ void Path3DGizmo::redraw() {
const Vector3 edge = sin(a) * side + cos(a) * up;
disk.append(pos + edge * disk_size);
}
add_vertices(disk, path_tilt_material, Mesh::PRIMITIVE_LINE_STRIP);
add_vertices(disk, debug_material, Mesh::PRIMITIVE_LINE_STRIP);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/path_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Path3DGizmo : public EditorNode3DGizmo {
};

Path3D *path = nullptr;
Ref<StandardMaterial3D> debug_material;
mutable Vector3 original;
mutable float orig_in_length;
mutable float orig_out_length;
Expand Down
60 changes: 59 additions & 1 deletion scene/3d/path_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ void Path3D::_update_debug_mesh() {
RS::get_singleton()->instance_set_visible(debug_instance, false);
return;
}
if (!debug_show) {
RS::get_singleton()->instance_set_visible(debug_instance, false);
return;
}

real_t interval = 0.1;
const real_t length = curve->get_baked_length();
Expand Down Expand Up @@ -151,20 +155,64 @@ void Path3D::_update_debug_mesh() {
bone_array.resize(Mesh::ARRAY_MAX);
bone_array[Mesh::ARRAY_VERTEX] = bones;

_update_debug_path_material();

debug_mesh->clear_surfaces();
debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINE_STRIP, ribbon_array);
debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, bone_array);
debug_mesh->surface_set_material(1, debug_material);

RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
RS::get_singleton()->mesh_surface_set_material(debug_mesh->get_rid(), 0, st->get_debug_paths_material()->get_rid());
RS::get_singleton()->mesh_surface_set_material(debug_mesh->get_rid(), 1, st->get_debug_paths_material()->get_rid());
if (is_inside_tree()) {
RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
}
}

void Path3D::set_debug_custom_color(const Color &p_color) {
debug_custom_color = p_color;
_update_debug_mesh();
}

bool Path3D::get_debug_show() const {
return debug_show;
}

void Path3D::set_debug_show(bool p_show) {
debug_show = p_show;
_update_debug_mesh();
}

Ref<StandardMaterial3D> Path3D::get_debug_material() {
// _update_debug_path_material();
return debug_material;
}

const Color &Path3D::get_debug_custom_color() const {
return debug_custom_color;
}

void Path3D::_update_debug_path_material() {
SceneTree *st = SceneTree::get_singleton();
if (!debug_material.is_valid()) {
Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
debug_material = material;

material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
}

Color color = debug_custom_color;
if (color == Color(0.0, 0.0, 0.0)) {
// Use the default debug path color defined in the Project Settings.
color = st->get_debug_paths_color();
}

get_debug_material()->set_albedo(color);
}

void Path3D::_curve_changed() {
if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
update_gizmos();
Expand Down Expand Up @@ -211,8 +259,18 @@ void Path3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path3D::set_curve);
ClassDB::bind_method(D_METHOD("get_curve"), &Path3D::get_curve);

ClassDB::bind_method(D_METHOD("set_debug_show", "debug_show"), &Path3D::set_debug_show);
ClassDB::bind_method(D_METHOD("get_debug_show"), &Path3D::get_debug_show);

ClassDB::bind_method(D_METHOD("set_debug_custom_color", "debug_custom_color"), &Path3D::set_debug_custom_color);
ClassDB::bind_method(D_METHOD("get_debug_custom_color"), &Path3D::get_debug_custom_color);

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");

ADD_GROUP("Debug Shape", "debug_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_show"), "set_debug_show", "get_debug_show");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_custom_color"), "set_debug_custom_color", "get_debug_custom_color");

ADD_SIGNAL(MethodInfo("curve_changed"));
}

Expand Down
12 changes: 12 additions & 0 deletions scene/3d/path_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ class Path3D : public Node3D {

private:
Ref<Curve3D> curve;
bool debug_show = true;
RID debug_instance;
Color debug_custom_color;
Ref<ArrayMesh> debug_mesh;
Ref<Material> debug_material;

Callable update_callback; // Used only by CSG currently.

void _update_debug_mesh();
void _update_debug_path_material();
void _curve_changed();

protected:
Expand All @@ -58,6 +62,14 @@ class Path3D : public Node3D {
void set_curve(const Ref<Curve3D> &p_curve);
Ref<Curve3D> get_curve() const;

const Color &get_debug_custom_color() const;
void set_debug_custom_color(const Color &p_color);

bool get_debug_show() const;
void set_debug_show(bool p_show);

Ref<StandardMaterial3D> get_debug_material();

Path3D();
~Path3D();
};
Expand Down
Loading