spatial_editor_gizmos.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*************************************************************************/
  2. /* spatial_editor_gizmos.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 SPATIAL_EDITOR_GIZMOS_H
  31. #define SPATIAL_EDITOR_GIZMOS_H
  32. #include "editor/plugins/spatial_editor_plugin.h"
  33. #include "scene/3d/body_shape.h"
  34. #include "scene/3d/camera.h"
  35. #include "scene/3d/light.h"
  36. #include "scene/3d/listener.h"
  37. #include "scene/3d/mesh_instance.h"
  38. #include "scene/3d/navigation_mesh.h"
  39. #include "scene/3d/portal.h"
  40. #include "scene/3d/position_3d.h"
  41. #include "scene/3d/ray_cast.h"
  42. #include "scene/3d/room_instance.h"
  43. #include "scene/3d/spatial_sample_player.h"
  44. #include "scene/3d/spatial_stream_player.h"
  45. #include "scene/3d/test_cube.h"
  46. #include "scene/3d/visibility_notifier.h"
  47. #include "scene/3d/collision_polygon.h"
  48. #include "scene/3d/physics_joint.h"
  49. #include "scene/3d/vehicle_body.h"
  50. class Camera;
  51. class EditorSpatialGizmo : public SpatialEditorGizmo {
  52. OBJ_TYPE(EditorSpatialGizmo, SpatialGizmo);
  53. struct Instance {
  54. RID instance;
  55. Ref<Mesh> mesh;
  56. RID skeleton;
  57. bool billboard;
  58. bool unscaled;
  59. bool can_intersect;
  60. bool extra_margin;
  61. Instance() {
  62. billboard = false;
  63. unscaled = false;
  64. can_intersect = false;
  65. extra_margin = false;
  66. }
  67. void create_instance(Spatial *p_base);
  68. };
  69. Vector<Vector3> collision_segments;
  70. Ref<TriangleMesh> collision_mesh;
  71. struct Handle {
  72. Vector3 pos;
  73. bool billboard;
  74. };
  75. Vector<Vector3> handles;
  76. Vector<Vector3> secondary_handles;
  77. bool billboard_handle;
  78. bool valid;
  79. Spatial *base;
  80. Vector<Instance> instances;
  81. Spatial *spatial_node;
  82. void _set_spatial_node(Node *p_node) { set_spatial_node(p_node->cast_to<Spatial>()); }
  83. protected:
  84. void add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard = false);
  85. void add_mesh(const Ref<Mesh> &p_mesh, bool p_billboard = false, const RID &p_skeleton = RID());
  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, float p_scale = 1);
  89. void add_handles(const Vector<Vector3> &p_handles, bool p_billboard = false, bool p_secondary = false);
  90. void set_spatial_node(Spatial *p_node);
  91. static void _bind_methods();
  92. public:
  93. virtual Vector3 get_handle_pos(int p_idx) const;
  94. virtual bool intersect_frustum(const Camera *p_camera, const Vector<Plane> &p_frustum);
  95. virtual bool intersect_ray(const Camera *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal, int *r_gizmo_handle = NULL, bool p_sec_first = false);
  96. void clear();
  97. void create();
  98. void transform();
  99. virtual void redraw();
  100. void free();
  101. EditorSpatialGizmo();
  102. ~EditorSpatialGizmo();
  103. };
  104. class LightSpatialGizmo : public EditorSpatialGizmo {
  105. OBJ_TYPE(LightSpatialGizmo, EditorSpatialGizmo);
  106. Light *light;
  107. public:
  108. virtual String get_handle_name(int p_idx) const;
  109. virtual Variant get_handle_value(int p_idx) const;
  110. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  111. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  112. void redraw();
  113. LightSpatialGizmo(Light *p_light = NULL);
  114. };
  115. class ListenerSpatialGizmo : public EditorSpatialGizmo {
  116. OBJ_TYPE(ListenerSpatialGizmo, EditorSpatialGizmo);
  117. Listener *listener;
  118. public:
  119. void redraw();
  120. ListenerSpatialGizmo(Listener *p_listener = NULL);
  121. };
  122. class CameraSpatialGizmo : public EditorSpatialGizmo {
  123. OBJ_TYPE(CameraSpatialGizmo, EditorSpatialGizmo);
  124. Camera *camera;
  125. public:
  126. virtual String get_handle_name(int p_idx) const;
  127. virtual Variant get_handle_value(int p_idx) const;
  128. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  129. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  130. void redraw();
  131. CameraSpatialGizmo(Camera *p_camera = NULL);
  132. };
  133. class MeshInstanceSpatialGizmo : public EditorSpatialGizmo {
  134. OBJ_TYPE(MeshInstanceSpatialGizmo, EditorSpatialGizmo);
  135. MeshInstance *mesh;
  136. public:
  137. void redraw();
  138. MeshInstanceSpatialGizmo(MeshInstance *p_mesh = NULL);
  139. };
  140. class Position3DSpatialGizmo : public EditorSpatialGizmo {
  141. OBJ_TYPE(Position3DSpatialGizmo, EditorSpatialGizmo);
  142. Position3D *p3d;
  143. public:
  144. void redraw();
  145. Position3DSpatialGizmo(Position3D *p_p3d = NULL);
  146. };
  147. class SkeletonSpatialGizmo : public EditorSpatialGizmo {
  148. OBJ_TYPE(SkeletonSpatialGizmo, EditorSpatialGizmo);
  149. Skeleton *skel;
  150. public:
  151. void redraw();
  152. SkeletonSpatialGizmo(Skeleton *p_skel = NULL);
  153. };
  154. class SpatialPlayerSpatialGizmo : public EditorSpatialGizmo {
  155. OBJ_TYPE(SpatialPlayerSpatialGizmo, EditorSpatialGizmo);
  156. SpatialPlayer *splayer;
  157. public:
  158. void redraw();
  159. SpatialPlayerSpatialGizmo(SpatialPlayer *p_splayer = NULL);
  160. };
  161. class TestCubeSpatialGizmo : public EditorSpatialGizmo {
  162. OBJ_TYPE(TestCubeSpatialGizmo, EditorSpatialGizmo);
  163. TestCube *tc;
  164. public:
  165. void redraw();
  166. TestCubeSpatialGizmo(TestCube *p_tc = NULL);
  167. };
  168. class RoomSpatialGizmo : public EditorSpatialGizmo {
  169. OBJ_TYPE(RoomSpatialGizmo, EditorSpatialGizmo);
  170. struct _EdgeKey {
  171. Vector3 from;
  172. Vector3 to;
  173. bool operator<(const _EdgeKey &p_with) const { return from == p_with.from ? to < p_with.to : from < p_with.from; }
  174. };
  175. Room *room;
  176. public:
  177. void redraw();
  178. RoomSpatialGizmo(Room *p_room = NULL);
  179. };
  180. class PortalSpatialGizmo : public EditorSpatialGizmo {
  181. OBJ_TYPE(PortalSpatialGizmo, EditorSpatialGizmo);
  182. Portal *portal;
  183. public:
  184. void redraw();
  185. PortalSpatialGizmo(Portal *p_portal = NULL);
  186. };
  187. class VisibilityNotifierGizmo : public EditorSpatialGizmo {
  188. OBJ_TYPE(VisibilityNotifierGizmo, EditorSpatialGizmo);
  189. VisibilityNotifier *notifier;
  190. public:
  191. virtual String get_handle_name(int p_idx) const;
  192. virtual Variant get_handle_value(int p_idx) const;
  193. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  194. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  195. void redraw();
  196. VisibilityNotifierGizmo(VisibilityNotifier *p_notifier = NULL);
  197. };
  198. class CollisionShapeSpatialGizmo : public EditorSpatialGizmo {
  199. OBJ_TYPE(CollisionShapeSpatialGizmo, EditorSpatialGizmo);
  200. CollisionShape *cs;
  201. public:
  202. virtual String get_handle_name(int p_idx) const;
  203. virtual Variant get_handle_value(int p_idx) const;
  204. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  205. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  206. void redraw();
  207. CollisionShapeSpatialGizmo(CollisionShape *p_cs = NULL);
  208. };
  209. class CollisionPolygonSpatialGizmo : public EditorSpatialGizmo {
  210. OBJ_TYPE(CollisionPolygonSpatialGizmo, EditorSpatialGizmo);
  211. CollisionPolygon *polygon;
  212. public:
  213. void redraw();
  214. CollisionPolygonSpatialGizmo(CollisionPolygon *p_polygon = NULL);
  215. };
  216. class RayCastSpatialGizmo : public EditorSpatialGizmo {
  217. OBJ_TYPE(RayCastSpatialGizmo, EditorSpatialGizmo);
  218. RayCast *raycast;
  219. public:
  220. void redraw();
  221. RayCastSpatialGizmo(RayCast *p_raycast = NULL);
  222. };
  223. class VehicleWheelSpatialGizmo : public EditorSpatialGizmo {
  224. OBJ_TYPE(VehicleWheelSpatialGizmo, EditorSpatialGizmo);
  225. VehicleWheel *car_wheel;
  226. public:
  227. void redraw();
  228. VehicleWheelSpatialGizmo(VehicleWheel *p_car_wheel = NULL);
  229. };
  230. class NavigationMeshSpatialGizmo : public EditorSpatialGizmo {
  231. OBJ_TYPE(NavigationMeshSpatialGizmo, EditorSpatialGizmo);
  232. struct _EdgeKey {
  233. Vector3 from;
  234. Vector3 to;
  235. bool operator<(const _EdgeKey &p_with) const { return from == p_with.from ? to < p_with.to : from < p_with.from; }
  236. };
  237. NavigationMeshInstance *navmesh;
  238. public:
  239. void redraw();
  240. NavigationMeshSpatialGizmo(NavigationMeshInstance *p_navmesh = NULL);
  241. };
  242. class PinJointSpatialGizmo : public EditorSpatialGizmo {
  243. OBJ_TYPE(PinJointSpatialGizmo, EditorSpatialGizmo);
  244. PinJoint *p3d;
  245. public:
  246. void redraw();
  247. PinJointSpatialGizmo(PinJoint *p_p3d = NULL);
  248. };
  249. class HingeJointSpatialGizmo : public EditorSpatialGizmo {
  250. OBJ_TYPE(HingeJointSpatialGizmo, EditorSpatialGizmo);
  251. HingeJoint *p3d;
  252. public:
  253. void redraw();
  254. HingeJointSpatialGizmo(HingeJoint *p_p3d = NULL);
  255. };
  256. class SliderJointSpatialGizmo : public EditorSpatialGizmo {
  257. OBJ_TYPE(SliderJointSpatialGizmo, EditorSpatialGizmo);
  258. SliderJoint *p3d;
  259. public:
  260. void redraw();
  261. SliderJointSpatialGizmo(SliderJoint *p_p3d = NULL);
  262. };
  263. class ConeTwistJointSpatialGizmo : public EditorSpatialGizmo {
  264. OBJ_TYPE(ConeTwistJointSpatialGizmo, EditorSpatialGizmo);
  265. ConeTwistJoint *p3d;
  266. public:
  267. void redraw();
  268. ConeTwistJointSpatialGizmo(ConeTwistJoint *p_p3d = NULL);
  269. };
  270. class Generic6DOFJointSpatialGizmo : public EditorSpatialGizmo {
  271. OBJ_TYPE(Generic6DOFJointSpatialGizmo, EditorSpatialGizmo);
  272. Generic6DOFJoint *p3d;
  273. public:
  274. void redraw();
  275. Generic6DOFJointSpatialGizmo(Generic6DOFJoint *p_p3d = NULL);
  276. };
  277. class SpatialEditorGizmos {
  278. public:
  279. Ref<FixedMaterial> create_line_material(const Color &p_base_color);
  280. Ref<FixedMaterial> create_solid_material(const Color &p_base_color);
  281. Ref<FixedMaterial> handle2_material;
  282. Ref<FixedMaterial> handle_material;
  283. Ref<FixedMaterial> light_material;
  284. Ref<FixedMaterial> light_material_omni_icon;
  285. Ref<FixedMaterial> light_material_directional_icon;
  286. Ref<FixedMaterial> camera_material;
  287. Ref<FixedMaterial> skeleton_material;
  288. Ref<FixedMaterial> room_material;
  289. Ref<FixedMaterial> portal_material;
  290. Ref<FixedMaterial> raycast_material;
  291. Ref<FixedMaterial> visibility_notifier_material;
  292. Ref<FixedMaterial> car_wheel_material;
  293. Ref<FixedMaterial> joint_material;
  294. Ref<FixedMaterial> navmesh_edge_material;
  295. Ref<FixedMaterial> navmesh_solid_material;
  296. Ref<FixedMaterial> navmesh_edge_material_disabled;
  297. Ref<FixedMaterial> navmesh_solid_material_disabled;
  298. Ref<FixedMaterial> listener_icon;
  299. Ref<FixedMaterial> sample_player_icon;
  300. Ref<FixedMaterial> stream_player_icon;
  301. Ref<FixedMaterial> visibility_notifier_icon;
  302. Ref<FixedMaterial> shape_material;
  303. Ref<Texture> handle_t;
  304. Ref<Mesh> pos3d_mesh;
  305. Ref<Mesh> listener_line_mesh;
  306. static SpatialEditorGizmos *singleton;
  307. Ref<TriangleMesh> test_cube_tm;
  308. Ref<SpatialEditorGizmo> get_gizmo(Spatial *p_spatial);
  309. SpatialEditorGizmos();
  310. };
  311. #endif // SPATIAL_EDITOR_GIZMOS_H