node_3d_editor_gizmos.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**************************************************************************/
  2. /* node_3d_editor_gizmos.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef NODE_3D_EDITOR_GIZMOS_H
  31. #define NODE_3D_EDITOR_GIZMOS_H
  32. #include "core/math/dynamic_bvh.h"
  33. #include "core/templates/hash_map.h"
  34. #include "core/templates/local_vector.h"
  35. #include "scene/3d/camera_3d.h"
  36. #include "scene/3d/node_3d.h"
  37. #include "scene/3d/skeleton_3d.h"
  38. class Timer;
  39. class EditorNode3DGizmoPlugin;
  40. class EditorNode3DGizmo : public Node3DGizmo {
  41. GDCLASS(EditorNode3DGizmo, Node3DGizmo);
  42. struct Instance {
  43. RID instance;
  44. Ref<Mesh> mesh;
  45. Ref<Material> material;
  46. Ref<SkinReference> skin_reference;
  47. bool extra_margin = false;
  48. Transform3D xform;
  49. void create_instance(Node3D *p_base, bool p_hidden = false);
  50. };
  51. bool selected;
  52. Vector<Vector3> collision_segments;
  53. Ref<TriangleMesh> collision_mesh;
  54. Vector<Vector3> handles;
  55. Vector<int> handle_ids;
  56. Vector<Vector3> secondary_handles;
  57. Vector<int> secondary_handle_ids;
  58. real_t selectable_icon_size;
  59. bool billboard_handle;
  60. bool valid;
  61. bool hidden;
  62. Vector<Instance> instances;
  63. Node3D *spatial_node = nullptr;
  64. DynamicBVH::ID bvh_node_id;
  65. void _set_node_3d(Node *p_node) { set_node_3d(Object::cast_to<Node3D>(p_node)); }
  66. void _update_bvh();
  67. protected:
  68. static void _bind_methods();
  69. EditorNode3DGizmoPlugin *gizmo_plugin = nullptr;
  70. GDVIRTUAL0(_redraw)
  71. GDVIRTUAL2RC(String, _get_handle_name, int, bool)
  72. GDVIRTUAL2RC(bool, _is_handle_highlighted, int, bool)
  73. GDVIRTUAL2RC(Variant, _get_handle_value, int, bool)
  74. GDVIRTUAL2(_begin_handle_action, int, bool)
  75. GDVIRTUAL4(_set_handle, int, bool, const Camera3D *, Vector2)
  76. GDVIRTUAL4(_commit_handle, int, bool, Variant, bool)
  77. GDVIRTUAL2RC(int, _subgizmos_intersect_ray, const Camera3D *, Vector2)
  78. GDVIRTUAL2RC(Vector<int>, _subgizmos_intersect_frustum, const Camera3D *, TypedArray<Plane>)
  79. GDVIRTUAL1RC(Transform3D, _get_subgizmo_transform, int)
  80. GDVIRTUAL2(_set_subgizmo_transform, int, Transform3D)
  81. GDVIRTUAL3(_commit_subgizmos, Vector<int>, TypedArray<Transform3D>, bool)
  82. public:
  83. void add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard = false, const Color &p_modulate = Color(1, 1, 1));
  84. void add_vertices(const Vector<Vector3> &p_vertices, const Ref<Material> &p_material, Mesh::PrimitiveType p_primitive_type, bool p_billboard = false, const Color &p_modulate = Color(1, 1, 1));
  85. void add_mesh(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material = Ref<Material>(), const Transform3D &p_xform = Transform3D(), const Ref<SkinReference> &p_skin_reference = Ref<SkinReference>());
  86. void add_collision_segments(const Vector<Vector3> &p_lines);
  87. void add_collision_triangles(const Ref<TriangleMesh> &p_tmesh);
  88. void add_unscaled_billboard(const Ref<Material> &p_material, real_t p_scale = 1, const Color &p_modulate = Color(1, 1, 1));
  89. void add_handles(const Vector<Vector3> &p_handles, const Ref<Material> &p_material, const Vector<int> &p_ids = Vector<int>(), bool p_billboard = false, bool p_secondary = false);
  90. void add_solid_box(const Ref<Material> &p_material, Vector3 p_size, Vector3 p_position = Vector3(), const Transform3D &p_xform = Transform3D());
  91. virtual bool is_handle_highlighted(int p_id, bool p_secondary) const;
  92. virtual String get_handle_name(int p_id, bool p_secondary) const;
  93. virtual Variant get_handle_value(int p_id, bool p_secondary) const;
  94. virtual void begin_handle_action(int p_id, bool p_secondary);
  95. virtual void set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point);
  96. virtual void commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false);
  97. virtual int subgizmos_intersect_ray(Camera3D *p_camera, const Vector2 &p_point) const;
  98. virtual Vector<int> subgizmos_intersect_frustum(const Camera3D *p_camera, const Vector<Plane> &p_frustum) const;
  99. virtual Transform3D get_subgizmo_transform(int p_id) const;
  100. virtual void set_subgizmo_transform(int p_id, Transform3D p_transform);
  101. virtual void commit_subgizmos(const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel = false);
  102. void set_selected(bool p_selected) { selected = p_selected; }
  103. bool is_selected() const { return selected; }
  104. void set_node_3d(Node3D *p_node);
  105. Node3D *get_node_3d() const { return spatial_node; }
  106. Ref<EditorNode3DGizmoPlugin> get_plugin() const { return gizmo_plugin; }
  107. bool intersect_frustum(const Camera3D *p_camera, const Vector<Plane> &p_frustum);
  108. void handles_intersect_ray(Camera3D *p_camera, const Vector2 &p_point, bool p_shift_pressed, int &r_id, bool &r_secondary);
  109. bool intersect_ray(Camera3D *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal);
  110. bool is_subgizmo_selected(int p_id) const;
  111. Vector<int> get_subgizmo_selection() const;
  112. virtual void clear() override;
  113. virtual void create() override;
  114. virtual void transform() override;
  115. virtual void redraw() override;
  116. virtual void free() override;
  117. virtual bool is_editable() const;
  118. void set_hidden(bool p_hidden);
  119. void set_plugin(EditorNode3DGizmoPlugin *p_plugin);
  120. EditorNode3DGizmo();
  121. ~EditorNode3DGizmo();
  122. };
  123. class EditorNode3DGizmoPlugin : public Resource {
  124. GDCLASS(EditorNode3DGizmoPlugin, Resource);
  125. public:
  126. static const int VISIBLE = 0;
  127. static const int HIDDEN = 1;
  128. static const int ON_TOP = 2;
  129. protected:
  130. int current_state;
  131. HashSet<EditorNode3DGizmo *> current_gizmos;
  132. HashMap<String, Vector<Ref<StandardMaterial3D>>> materials;
  133. static void _bind_methods();
  134. virtual bool has_gizmo(Node3D *p_spatial);
  135. virtual Ref<EditorNode3DGizmo> create_gizmo(Node3D *p_spatial);
  136. GDVIRTUAL1RC(bool, _has_gizmo, Node3D *)
  137. GDVIRTUAL1RC(Ref<EditorNode3DGizmo>, _create_gizmo, Node3D *)
  138. GDVIRTUAL0RC(String, _get_gizmo_name)
  139. GDVIRTUAL0RC(int, _get_priority)
  140. GDVIRTUAL0RC(bool, _can_be_hidden)
  141. GDVIRTUAL0RC(bool, _is_selectable_when_hidden)
  142. GDVIRTUAL1(_redraw, Ref<EditorNode3DGizmo>)
  143. GDVIRTUAL3RC(String, _get_handle_name, Ref<EditorNode3DGizmo>, int, bool)
  144. GDVIRTUAL3RC(bool, _is_handle_highlighted, Ref<EditorNode3DGizmo>, int, bool)
  145. GDVIRTUAL3RC(Variant, _get_handle_value, Ref<EditorNode3DGizmo>, int, bool)
  146. GDVIRTUAL3(_begin_handle_action, Ref<EditorNode3DGizmo>, int, bool)
  147. GDVIRTUAL5(_set_handle, Ref<EditorNode3DGizmo>, int, bool, const Camera3D *, Vector2)
  148. GDVIRTUAL5(_commit_handle, Ref<EditorNode3DGizmo>, int, bool, Variant, bool)
  149. GDVIRTUAL3RC(int, _subgizmos_intersect_ray, Ref<EditorNode3DGizmo>, const Camera3D *, Vector2)
  150. GDVIRTUAL3RC(Vector<int>, _subgizmos_intersect_frustum, Ref<EditorNode3DGizmo>, const Camera3D *, TypedArray<Plane>)
  151. GDVIRTUAL2RC(Transform3D, _get_subgizmo_transform, Ref<EditorNode3DGizmo>, int)
  152. GDVIRTUAL3(_set_subgizmo_transform, Ref<EditorNode3DGizmo>, int, Transform3D)
  153. GDVIRTUAL4(_commit_subgizmos, Ref<EditorNode3DGizmo>, Vector<int>, TypedArray<Transform3D>, bool)
  154. public:
  155. void create_material(const String &p_name, const Color &p_color, bool p_billboard = false, bool p_on_top = false, bool p_use_vertex_color = false);
  156. void create_icon_material(const String &p_name, const Ref<Texture2D> &p_texture, bool p_on_top = false, const Color &p_albedo = Color(1, 1, 1, 1));
  157. void create_handle_material(const String &p_name, bool p_billboard = false, const Ref<Texture2D> &p_texture = nullptr);
  158. void add_material(const String &p_name, Ref<StandardMaterial3D> p_material);
  159. Ref<StandardMaterial3D> get_material(const String &p_name, const Ref<EditorNode3DGizmo> &p_gizmo = Ref<EditorNode3DGizmo>());
  160. virtual String get_gizmo_name() const;
  161. virtual int get_priority() const;
  162. virtual bool can_be_hidden() const;
  163. virtual bool is_selectable_when_hidden() const;
  164. virtual void redraw(EditorNode3DGizmo *p_gizmo);
  165. virtual bool is_handle_highlighted(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const;
  166. virtual String get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const;
  167. virtual Variant get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const;
  168. virtual void begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary);
  169. virtual void set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point);
  170. virtual void commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel = false);
  171. virtual int subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const;
  172. virtual Vector<int> subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const;
  173. virtual Transform3D get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const;
  174. virtual void set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform);
  175. virtual void commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel = false);
  176. Ref<EditorNode3DGizmo> get_gizmo(Node3D *p_spatial);
  177. void set_state(int p_state);
  178. int get_state() const;
  179. void unregister_gizmo(EditorNode3DGizmo *p_gizmo);
  180. EditorNode3DGizmoPlugin();
  181. virtual ~EditorNode3DGizmoPlugin();
  182. };
  183. #endif // NODE_3D_EDITOR_GIZMOS_H