spatial_editor_gizmos.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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/audio_stream_player_3d.h"
  34. #include "scene/3d/camera.h"
  35. #include "scene/3d/collision_polygon.h"
  36. #include "scene/3d/collision_shape.h"
  37. #include "scene/3d/gi_probe.h"
  38. #include "scene/3d/light.h"
  39. #include "scene/3d/listener.h"
  40. #include "scene/3d/mesh_instance.h"
  41. #include "scene/3d/navigation_mesh.h"
  42. #include "scene/3d/particles.h"
  43. #include "scene/3d/physics_joint.h"
  44. #include "scene/3d/portal.h"
  45. #include "scene/3d/position_3d.h"
  46. #include "scene/3d/ray_cast.h"
  47. #include "scene/3d/reflection_probe.h"
  48. #include "scene/3d/room_instance.h"
  49. #include "scene/3d/vehicle_body.h"
  50. #include "scene/3d/visibility_notifier.h"
  51. class Camera;
  52. class EditorSpatialGizmo : public SpatialEditorGizmo {
  53. GDCLASS(EditorSpatialGizmo, SpatialGizmo);
  54. struct Instance {
  55. RID instance;
  56. Ref<ArrayMesh> mesh;
  57. RID skeleton;
  58. bool billboard;
  59. bool unscaled;
  60. bool can_intersect;
  61. bool extra_margin;
  62. Instance() {
  63. billboard = false;
  64. unscaled = false;
  65. can_intersect = false;
  66. extra_margin = false;
  67. }
  68. void create_instance(Spatial *p_base);
  69. };
  70. Vector<Vector3> collision_segments;
  71. Ref<TriangleMesh> collision_mesh;
  72. Rect3 collision_mesh_bounds;
  73. struct Handle {
  74. Vector3 pos;
  75. bool billboard;
  76. };
  77. Vector<Vector3> handles;
  78. Vector<Vector3> secondary_handles;
  79. bool billboard_handle;
  80. bool valid;
  81. Spatial *base;
  82. Vector<Instance> instances;
  83. Spatial *spatial_node;
  84. void _set_spatial_node(Node *p_node) { set_spatial_node(Object::cast_to<Spatial>(p_node)); }
  85. protected:
  86. void add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard = false);
  87. void add_mesh(const Ref<ArrayMesh> &p_mesh, bool p_billboard = false, const RID &p_skeleton = RID());
  88. void add_collision_segments(const Vector<Vector3> &p_lines);
  89. void add_collision_triangles(const Ref<TriangleMesh> &p_tmesh, const Rect3 &p_bounds = Rect3());
  90. void add_unscaled_billboard(const Ref<Material> &p_material, float p_scale = 1);
  91. void add_handles(const Vector<Vector3> &p_handles, bool p_billboard = false, bool p_secondary = false);
  92. void add_solid_box(Ref<Material> &p_material, Vector3 size);
  93. void set_spatial_node(Spatial *p_node);
  94. static void _bind_methods();
  95. Ref<SpatialMaterial> 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);
  96. Ref<SpatialMaterial> create_icon_material(const String &p_name, const Ref<Texture> &p_texture, bool p_on_top = false, const Color &p_albedo = Color(1, 1, 1, 1));
  97. public:
  98. virtual Vector3 get_handle_pos(int p_idx) const;
  99. virtual bool intersect_frustum(const Camera *p_camera, const Vector<Plane> &p_frustum);
  100. 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);
  101. void clear();
  102. void create();
  103. void transform();
  104. virtual void redraw();
  105. void free();
  106. virtual bool is_editable() const;
  107. virtual bool can_draw() const;
  108. EditorSpatialGizmo();
  109. ~EditorSpatialGizmo();
  110. };
  111. class LightSpatialGizmo : public EditorSpatialGizmo {
  112. GDCLASS(LightSpatialGizmo, EditorSpatialGizmo);
  113. Light *light;
  114. public:
  115. virtual String get_handle_name(int p_idx) const;
  116. virtual Variant get_handle_value(int p_idx) const;
  117. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  118. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  119. void redraw();
  120. LightSpatialGizmo(Light *p_light = NULL);
  121. };
  122. class AudioStreamPlayer3DSpatialGizmo : public EditorSpatialGizmo {
  123. GDCLASS(AudioStreamPlayer3DSpatialGizmo, EditorSpatialGizmo);
  124. AudioStreamPlayer3D *player;
  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. AudioStreamPlayer3DSpatialGizmo(AudioStreamPlayer3D *p_player = NULL);
  132. };
  133. class CameraSpatialGizmo : public EditorSpatialGizmo {
  134. GDCLASS(CameraSpatialGizmo, EditorSpatialGizmo);
  135. Camera *camera;
  136. public:
  137. virtual String get_handle_name(int p_idx) const;
  138. virtual Variant get_handle_value(int p_idx) const;
  139. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  140. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  141. void redraw();
  142. CameraSpatialGizmo(Camera *p_camera = NULL);
  143. };
  144. class MeshInstanceSpatialGizmo : public EditorSpatialGizmo {
  145. GDCLASS(MeshInstanceSpatialGizmo, EditorSpatialGizmo);
  146. MeshInstance *mesh;
  147. public:
  148. virtual bool can_draw() const;
  149. void redraw();
  150. MeshInstanceSpatialGizmo(MeshInstance *p_mesh = NULL);
  151. };
  152. class Position3DSpatialGizmo : public EditorSpatialGizmo {
  153. GDCLASS(Position3DSpatialGizmo, EditorSpatialGizmo);
  154. Position3D *p3d;
  155. public:
  156. void redraw();
  157. Position3DSpatialGizmo(Position3D *p_p3d = NULL);
  158. };
  159. class SkeletonSpatialGizmo : public EditorSpatialGizmo {
  160. GDCLASS(SkeletonSpatialGizmo, EditorSpatialGizmo);
  161. Skeleton *skel;
  162. public:
  163. void redraw();
  164. SkeletonSpatialGizmo(Skeleton *p_skel = NULL);
  165. };
  166. #if 0
  167. class PortalSpatialGizmo : public EditorSpatialGizmo {
  168. GDCLASS(PortalSpatialGizmo, EditorSpatialGizmo);
  169. Portal *portal;
  170. public:
  171. void redraw();
  172. PortalSpatialGizmo(Portal *p_portal = NULL);
  173. };
  174. #endif
  175. class VisibilityNotifierGizmo : public EditorSpatialGizmo {
  176. GDCLASS(VisibilityNotifierGizmo, EditorSpatialGizmo);
  177. VisibilityNotifier *notifier;
  178. public:
  179. virtual String get_handle_name(int p_idx) const;
  180. virtual Variant get_handle_value(int p_idx) const;
  181. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  182. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  183. void redraw();
  184. VisibilityNotifierGizmo(VisibilityNotifier *p_notifier = NULL);
  185. };
  186. class ParticlesGizmo : public EditorSpatialGizmo {
  187. GDCLASS(ParticlesGizmo, EditorSpatialGizmo);
  188. Particles *particles;
  189. public:
  190. virtual String get_handle_name(int p_idx) const;
  191. virtual Variant get_handle_value(int p_idx) const;
  192. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  193. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  194. void redraw();
  195. ParticlesGizmo(Particles *p_particles = NULL);
  196. };
  197. class ReflectionProbeGizmo : public EditorSpatialGizmo {
  198. GDCLASS(ReflectionProbeGizmo, EditorSpatialGizmo);
  199. ReflectionProbe *probe;
  200. public:
  201. virtual String get_handle_name(int p_idx) const;
  202. virtual Variant get_handle_value(int p_idx) const;
  203. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  204. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  205. void redraw();
  206. ReflectionProbeGizmo(ReflectionProbe *p_probe = NULL);
  207. };
  208. class GIProbeGizmo : public EditorSpatialGizmo {
  209. GDCLASS(GIProbeGizmo, EditorSpatialGizmo);
  210. GIProbe *probe;
  211. public:
  212. virtual String get_handle_name(int p_idx) const;
  213. virtual Variant get_handle_value(int p_idx) const;
  214. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  215. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  216. void redraw();
  217. GIProbeGizmo(GIProbe *p_probe = NULL);
  218. };
  219. class CollisionShapeSpatialGizmo : public EditorSpatialGizmo {
  220. GDCLASS(CollisionShapeSpatialGizmo, EditorSpatialGizmo);
  221. CollisionShape *cs;
  222. public:
  223. virtual String get_handle_name(int p_idx) const;
  224. virtual Variant get_handle_value(int p_idx) const;
  225. virtual void set_handle(int p_idx, Camera *p_camera, const Point2 &p_point);
  226. virtual void commit_handle(int p_idx, const Variant &p_restore, bool p_cancel = false);
  227. void redraw();
  228. CollisionShapeSpatialGizmo(CollisionShape *p_cs = NULL);
  229. };
  230. class CollisionPolygonSpatialGizmo : public EditorSpatialGizmo {
  231. GDCLASS(CollisionPolygonSpatialGizmo, EditorSpatialGizmo);
  232. CollisionPolygon *polygon;
  233. public:
  234. void redraw();
  235. CollisionPolygonSpatialGizmo(CollisionPolygon *p_polygon = NULL);
  236. };
  237. class RayCastSpatialGizmo : public EditorSpatialGizmo {
  238. GDCLASS(RayCastSpatialGizmo, EditorSpatialGizmo);
  239. RayCast *raycast;
  240. public:
  241. void redraw();
  242. RayCastSpatialGizmo(RayCast *p_raycast = NULL);
  243. };
  244. class VehicleWheelSpatialGizmo : public EditorSpatialGizmo {
  245. GDCLASS(VehicleWheelSpatialGizmo, EditorSpatialGizmo);
  246. VehicleWheel *car_wheel;
  247. public:
  248. void redraw();
  249. VehicleWheelSpatialGizmo(VehicleWheel *p_car_wheel = NULL);
  250. };
  251. class NavigationMeshSpatialGizmo : public EditorSpatialGizmo {
  252. GDCLASS(NavigationMeshSpatialGizmo, EditorSpatialGizmo);
  253. struct _EdgeKey {
  254. Vector3 from;
  255. Vector3 to;
  256. bool operator<(const _EdgeKey &p_with) const { return from == p_with.from ? to < p_with.to : from < p_with.from; }
  257. };
  258. NavigationMeshInstance *navmesh;
  259. public:
  260. void redraw();
  261. NavigationMeshSpatialGizmo(NavigationMeshInstance *p_navmesh = NULL);
  262. };
  263. class PinJointSpatialGizmo : public EditorSpatialGizmo {
  264. GDCLASS(PinJointSpatialGizmo, EditorSpatialGizmo);
  265. PinJoint *p3d;
  266. public:
  267. void redraw();
  268. PinJointSpatialGizmo(PinJoint *p_p3d = NULL);
  269. };
  270. class HingeJointSpatialGizmo : public EditorSpatialGizmo {
  271. GDCLASS(HingeJointSpatialGizmo, EditorSpatialGizmo);
  272. HingeJoint *p3d;
  273. public:
  274. void redraw();
  275. HingeJointSpatialGizmo(HingeJoint *p_p3d = NULL);
  276. };
  277. class SliderJointSpatialGizmo : public EditorSpatialGizmo {
  278. GDCLASS(SliderJointSpatialGizmo, EditorSpatialGizmo);
  279. SliderJoint *p3d;
  280. public:
  281. void redraw();
  282. SliderJointSpatialGizmo(SliderJoint *p_p3d = NULL);
  283. };
  284. class ConeTwistJointSpatialGizmo : public EditorSpatialGizmo {
  285. GDCLASS(ConeTwistJointSpatialGizmo, EditorSpatialGizmo);
  286. ConeTwistJoint *p3d;
  287. public:
  288. void redraw();
  289. ConeTwistJointSpatialGizmo(ConeTwistJoint *p_p3d = NULL);
  290. };
  291. class Generic6DOFJointSpatialGizmo : public EditorSpatialGizmo {
  292. GDCLASS(Generic6DOFJointSpatialGizmo, EditorSpatialGizmo);
  293. Generic6DOFJoint *p3d;
  294. public:
  295. void redraw();
  296. Generic6DOFJointSpatialGizmo(Generic6DOFJoint *p_p3d = NULL);
  297. };
  298. class SpatialEditorGizmos {
  299. public:
  300. HashMap<String, Ref<SpatialMaterial> > material_cache;
  301. Ref<SpatialMaterial> handle2_material;
  302. Ref<SpatialMaterial> handle2_material_billboard;
  303. Ref<SpatialMaterial> handle_material;
  304. Ref<SpatialMaterial> handle_material_billboard;
  305. Ref<Texture> handle_t;
  306. Ref<ArrayMesh> pos3d_mesh;
  307. Ref<ArrayMesh> listener_line_mesh;
  308. static SpatialEditorGizmos *singleton;
  309. Ref<SpatialEditorGizmo> get_gizmo(Spatial *p_spatial);
  310. SpatialEditorGizmos();
  311. };
  312. #endif // SPATIAL_EDITOR_GIZMOS_H