Skip to content

Commit

Permalink
Porting SkeletonEditorGizmo for 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TokageItLab committed Feb 6, 2021
1 parent b70836c commit c1db100
Show file tree
Hide file tree
Showing 26 changed files with 1,403 additions and 150 deletions.
25 changes: 25 additions & 0 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3501,6 +3501,31 @@ void AnimationTrackEditor::insert_transform_key(Node3D *p_node, const String &p_
_query_insert(id);
}

bool AnimationTrackEditor::has_transform_key(Node3D *p_node, const String &p_sub) {
if (!keying) {
return false;
}
if (!animation.is_valid()) {
return false;
}
if (!root) {
return false;
}

//let's build a node path
String path = root->get_path_to(p_node);
if (p_sub != "") {
path += ":" + p_sub;
}
int track_id = animation->find_track(path);
if (track_id >= 0) {
if (animation->track_get_type(track_id) == Animation::TYPE_TRANSFORM) {
return true;
}
}
return false;
}

void AnimationTrackEditor::_insert_animation_key(NodePath p_path, const Variant &p_value) {
String path = p_path;

Expand Down
1 change: 1 addition & 0 deletions editor/animation_track_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ class AnimationTrackEditor : public VBoxContainer {
void insert_node_value_key(Node *p_node, const String &p_property, const Variant &p_value, bool p_only_if_exists = false);
void insert_value_key(const String &p_property, const Variant &p_value, bool p_advance);
void insert_transform_key(Node3D *p_node, const String &p_sub, const Transform &p_xform);
bool has_transform_key(Node3D *p_node, const String &p_sub);

void show_select_node_warning(bool p_show);

Expand Down
4 changes: 2 additions & 2 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6898,15 +6898,15 @@ bool EditorPluginList::forward_gui_input(const Ref<InputEvent> &p_event) {
return discard;
}

bool EditorPluginList::forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event, bool serve_when_force_input_enabled) {
bool EditorPluginList::forward_spatial_gui_input(int p_index, Camera3D *p_camera, const Ref<InputEvent> &p_event, bool serve_when_force_input_enabled) {
bool discard = false;

for (int i = 0; i < plugins_list.size(); i++) {
if ((!serve_when_force_input_enabled) && plugins_list[i]->is_input_event_forwarding_always_enabled()) {
continue;
}

if (plugins_list[i]->forward_spatial_gui_input(p_camera, p_event)) {
if (plugins_list[i]->forward_spatial_gui_input(p_index, p_camera, p_event)) {
discard = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ class EditorPluginList : public Object {
bool forward_gui_input(const Ref<InputEvent> &p_event);
void forward_canvas_draw_over_viewport(Control *p_overlay);
void forward_canvas_force_draw_over_viewport(Control *p_overlay);
bool forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event, bool serve_when_force_input_enabled);
bool forward_spatial_gui_input(int p_index, Camera3D *p_camera, const Ref<InputEvent> &p_event, bool serve_when_force_input_enabled);
void forward_spatial_draw_over_viewport(Control *p_overlay);
void forward_spatial_force_draw_over_viewport(Control *p_overlay);
void add_plugin(EditorPlugin *p_plugin);
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ int EditorPlugin::update_overlays() const {
}
}

bool EditorPlugin::forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
bool EditorPlugin::forward_spatial_gui_input(int p_index, Camera3D *p_camera, const Ref<InputEvent> &p_event) {
if (get_script_instance() && get_script_instance()->has_method("forward_spatial_gui_input")) {
return get_script_instance()->call("forward_spatial_gui_input", p_camera, p_event);
}
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class EditorPlugin : public Node {
virtual void forward_canvas_draw_over_viewport(Control *p_overlay);
virtual void forward_canvas_force_draw_over_viewport(Control *p_overlay);

virtual bool forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event);
virtual bool forward_spatial_gui_input(int p_index, Camera3D *p_camera, const Ref<InputEvent> &p_event);
virtual void forward_spatial_draw_over_viewport(Control *p_overlay);
virtual void forward_spatial_force_draw_over_viewport(Control *p_overlay);

Expand Down
1 change: 1 addition & 0 deletions editor/editor_themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void editor_register_and_generate_icons(Ref<Theme> p_theme, bool p_dark_theme =
exceptions.insert("EditorPivot");
exceptions.insert("EditorHandle");
exceptions.insert("Editor3DHandle");
exceptions.insert("EditorBoneHandle");
exceptions.insert("Godot");
exceptions.insert("Sky");
exceptions.insert("EditorControlAnchor");
Expand Down
1 change: 1 addition & 0 deletions editor/icons/EditorBoneHandle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions editor/icons/ToolBoneMove.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions editor/icons/ToolBoneRest.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions editor/icons/ToolBoneRotate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions editor/icons/ToolBoneScale.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions editor/icons/ToolBoneSelect.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 73 additions & 21 deletions editor/node_3d_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "core/math/geometry_2d.h"
#include "core/math/geometry_3d.h"
#include "core/math/quick_hull.h"
#include "editor/plugins/skeleton_3d_editor_plugin.h"
#include "scene/3d/audio_stream_player_3d.h"
#include "scene/3d/baked_lightmap.h"
#include "scene/3d/collision_polygon_3d.h"
Expand Down Expand Up @@ -1546,8 +1547,34 @@ void Position3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
/////

Skeleton3DGizmoPlugin::Skeleton3DGizmoPlugin() {
Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/skeleton", Color(1, 0.8, 0.4));
create_material("skeleton_material", gizmo_color);
skeleton_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/skeleton", Color(1, 0.8, 0.4));
selected_bone_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/selected_bone", Color(1, 0, 0));
bone_axis_length = EDITOR_DEF("editors/3d_gizmos/gizmo_settings/bone_axis_length", (float)0.015);
create_material("skeleton_material", skeleton_color);
selected_mat = Ref<ShaderMaterial>(memnew(ShaderMaterial));
selected_sh = Ref<Shader>(memnew(Shader));
selected_sh->set_code(" \
shader_type spatial; \
render_mode unshaded; \
uniform vec4 albedo : hint_color = vec4(1,1,1,1); \
uniform sampler2D texture_albedo : hint_albedo; \
void vertex() { \
if (!OUTPUT_IS_SRGB) { \
COLOR.rgb = mix( pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb* (1.0 / 12.92), lessThan(COLOR.rgb,vec3(0.04045)) ); \
} \
VERTEX = VERTEX; \
POSITION=PROJECTION_MATRIX*INV_CAMERA_MATRIX*WORLD_MATRIX*vec4(VERTEX.xyz,1.0); \
POSITION.z = mix(POSITION.z, 0, 0.998); \
} \
void fragment() { \
vec2 base_uv = UV; \
vec4 albedo_tex = texture(texture_albedo,base_uv); \
albedo_tex *= COLOR; \
if (albedo.a * albedo_tex.a < 0.5) { discard; } \
ALBEDO = albedo.rgb * albedo_tex.rgb; \
} \
");
selected_mat->set_shader(selected_sh);
}

bool Skeleton3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
Expand All @@ -1567,7 +1594,12 @@ void Skeleton3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {

p_gizmo->clear();

Ref<Material> material = get_material("skeleton_material", p_gizmo);
Ref<Material> material;
if (p_gizmo->is_selected()) {
material = selected_mat;
} else {
material = get_material("skeleton_material", p_gizmo);
}

Ref<SurfaceTool> surface_tool(memnew(SurfaceTool));

Expand All @@ -1588,12 +1620,16 @@ void Skeleton3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {

weights.write[0] = 1;

Color bone_color;
AABB aabb;

Color bonecolor = Color(1.0, 0.4, 0.4, 0.3);
Color rootcolor = Color(0.4, 1.0, 0.4, 0.1);

for (int i_bone = 0; i_bone < skel->get_bone_count(); i_bone++) {
if (skel->get_bone_parent(i_bone) == skel->get_selected_bone()) {
bone_color = selected_bone_color;
} else {
bone_color = skeleton_color;
}

int i = skel->get_process_order(i_bone);

int parent = skel->get_bone_parent(i);
Expand Down Expand Up @@ -1621,16 +1657,32 @@ void Skeleton3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Vector3 first;
Vector3 points[4];
int pointidx = 0;
Color axis_color[3];
axis_color[0] = Color(1, 0, 0);
axis_color[1] = Color(0, 1, 0);
axis_color[2] = Color(0, 0, 1);
for (int j = 0; j < 3; j++) {
bones.write[0] = parent;
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(rootcolor);
surface_tool->add_vertex(v0 - grests[parent].basis[j].normalized() * dist * 0.05);
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(rootcolor);
surface_tool->add_vertex(v0 + grests[parent].basis[j].normalized() * dist * 0.05);
if (p_gizmo->is_selected()) {
bones.write[0] = i;
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(axis_color[j]);
surface_tool->add_vertex(v1);
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(axis_color[j]);
surface_tool->add_vertex(v1 + grests[i].basis[j].normalized() * bone_axis_length);
} else {
bones.write[0] = i;
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(axis_color[j]);
surface_tool->add_vertex(v1 - grests[i].basis[j].normalized() * dist * 0.05);
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(axis_color[j]);
surface_tool->add_vertex(v1 + grests[i].basis[j].normalized() * dist * 0.05);
}

if (j == closest) {
continue;
Expand All @@ -1654,22 +1706,22 @@ void Skeleton3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
bones.write[0] = parent;
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(bonecolor);
surface_tool->set_color(bone_color);
surface_tool->add_vertex(v0);
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(bonecolor);
surface_tool->set_color(bone_color);
surface_tool->add_vertex(point);

bones.write[0] = parent;
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(bonecolor);
surface_tool->set_color(bone_color);
surface_tool->add_vertex(point);
bones.write[0] = i;
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(bonecolor);
surface_tool->set_color(bone_color);
surface_tool->add_vertex(v1);
points[pointidx++] = point;
}
Expand All @@ -1680,11 +1732,11 @@ void Skeleton3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
bones.write[0] = parent;
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(bonecolor);
surface_tool->set_color(bone_color);
surface_tool->add_vertex(points[j]);
surface_tool->set_bones(bones);
surface_tool->set_weights(weights);
surface_tool->set_color(bonecolor);
surface_tool->set_color(bone_color);
surface_tool->add_vertex(points[(j + 1) % 4]);
}

Expand Down
5 changes: 5 additions & 0 deletions editor/node_3d_editor_gizmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ class Position3DGizmoPlugin : public EditorNode3DGizmoPlugin {

class Skeleton3DGizmoPlugin : public EditorNode3DGizmoPlugin {
GDCLASS(Skeleton3DGizmoPlugin, EditorNode3DGizmoPlugin);
Color skeleton_color = Color(1, 0.8, 0.4);
Color selected_bone_color = Color(1, 0, 0);
float bone_axis_length = 0.015;
Ref<ShaderMaterial> selected_mat;
Ref<Shader> selected_sh;

public:
bool has_gizmo(Node3D *p_spatial) override;
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/collision_polygon_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void CollisionPolygon3DEditor::_wip_close() {
undo_redo->commit_action();
}

bool CollisionPolygon3DEditor::forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
bool CollisionPolygon3DEditor::forward_spatial_gui_input(int p_index, Camera3D *p_camera, const Ref<InputEvent> &p_event) {
if (!node) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/collision_polygon_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CollisionPolygon3DEditor : public HBoxContainer {
static void _bind_methods();

public:
virtual bool forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event);
virtual bool forward_spatial_gui_input(int p_index, Camera3D *p_camera, const Ref<InputEvent> &p_event);
void edit(Node *p_collision_polygon);
CollisionPolygon3DEditor(EditorNode *p_editor);
~CollisionPolygon3DEditor();
Expand All @@ -100,7 +100,7 @@ class Polygon3DEditorPlugin : public EditorPlugin {
EditorNode *editor;

public:
virtual bool forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override { return collision_polygon_editor->forward_spatial_gui_input(p_camera, p_event); }
virtual bool forward_spatial_gui_input(int p_index, Camera3D *p_camera, const Ref<InputEvent> &p_event) override { return collision_polygon_editor->forward_spatial_gui_input(p_index, p_camera, p_event); }

virtual String get_name() const override { return "Polygon3DEditor"; }
bool has_main_screen() const override { return false; }
Expand Down
Loading

0 comments on commit c1db100

Please sign in to comment.