Skip to content

Commit

Permalink
Change RayCast3D gizmo to arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
Mielek committed May 25, 2020
1 parent ab3d7f2 commit 7e46a3a
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions editor/node_3d_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1908,15 +1908,47 @@ void RayCast3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {

p_gizmo->clear();

const Ref<StandardMaterial3D> material =
get_material(raycast->is_enabled() ? "shape_material" : "shape_material_disabled", p_gizmo);

const Vector3 ray = raycast->get_cast_to();
const float ray_length = ray.length();

const Quat arrow_direction(Vector3(0, 0, 1), ray.normalized());
// Scale arrow for small ray length
const Vector3 arrow_scale = Vector3(1, 1, 1) * (ray_length < 1.0 ? ray_length : 1.0);
const float arrow_length = ray_length < 1 ? 1 : ray_length;
// Fix arrow head size
const float arrow_head = arrow_length - 0.2;

const int arrow_points = 9;
Vector3 arrow[arrow_points] = {
Vector3(0, 0, 0),
Vector3(0, 0, arrow_length),
Vector3(0, 0.1, arrow_head),
Vector3(0, 0.01, arrow_head),
Vector3(0, 0.01, 0),
Vector3(0, -0.01, 0),
Vector3(0, -0.01, arrow_head),
Vector3(0, -0.1, arrow_head),
Vector3(0, 0, arrow_length),
};

Vector<Vector3> lines;

lines.push_back(Vector3());
lines.push_back(raycast->get_cast_to());
int arrow_sides = 2;
for (int i = 0; i < arrow_sides; i++) {
Basis ma(arrow_direction * Quat(Vector3(0, 0, 1), Math_PI * i / arrow_sides), arrow_scale);

const Ref<StandardMaterial3D> material =
get_material(raycast->is_enabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
for (int j = 0; j < arrow_points - 1; j++) {
Vector3 v1 = ma.xform(arrow[j]);
Vector3 v2 = ma.xform(arrow[(j + 1) % arrow_points]);

p_gizmo->add_lines(lines, material);
lines.push_back(v1);
lines.push_back(v2);
}
}
p_gizmo->add_lines(lines, material, false);
p_gizmo->add_collision_segments(lines);
}

Expand Down

0 comments on commit 7e46a3a

Please sign in to comment.