camera.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************/
  2. /* camera.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 CAMERA_H
  31. #define CAMERA_H
  32. #include "scene/3d/spatial.h"
  33. #include "scene/3d/spatial_velocity_tracker.h"
  34. #include "scene/main/viewport.h"
  35. #include "scene/resources/environment.h"
  36. /**
  37. @author Juan Linietsky <reduzio@gmail.com>
  38. */
  39. class Camera : public Spatial {
  40. GDCLASS(Camera, Spatial);
  41. public:
  42. enum Projection {
  43. PROJECTION_PERSPECTIVE,
  44. PROJECTION_ORTHOGONAL
  45. };
  46. enum KeepAspect {
  47. KEEP_WIDTH,
  48. KEEP_HEIGHT
  49. };
  50. enum DopplerTracking {
  51. DOPPLER_TRACKING_DISABLED,
  52. DOPPLER_TRACKING_IDLE_STEP,
  53. DOPPLER_TRACKING_PHYSICS_STEP
  54. };
  55. private:
  56. bool force_change;
  57. bool current;
  58. Projection mode;
  59. float fov;
  60. float size;
  61. float near, far;
  62. float v_offset;
  63. float h_offset;
  64. KeepAspect keep_aspect;
  65. RID camera;
  66. RID scenario_id;
  67. //String camera_group;
  68. uint32_t layers;
  69. Ref<Environment> environment;
  70. virtual bool _can_gizmo_scale() const;
  71. //void _camera_make_current(Node *p_camera);
  72. friend class Viewport;
  73. void _update_audio_listener_state();
  74. DopplerTracking doppler_tracking;
  75. Ref<SpatialVelocityTracker> velocity_tracker;
  76. protected:
  77. void _update_camera();
  78. virtual void _request_camera_update();
  79. void _update_camera_mode();
  80. void _notification(int p_what);
  81. virtual void _validate_property(PropertyInfo &p_property) const;
  82. static void _bind_methods();
  83. public:
  84. enum {
  85. NOTIFICATION_BECAME_CURRENT = 50,
  86. NOTIFICATION_LOST_CURRENT = 51
  87. };
  88. void set_perspective(float p_fovy_degrees, float p_z_near, float p_z_far);
  89. void set_orthogonal(float p_size, float p_z_near, float p_z_far);
  90. void set_projection(Camera::Projection p_mode);
  91. void make_current();
  92. void clear_current(bool p_enable_next = true);
  93. void set_current(bool p_current);
  94. bool is_current() const;
  95. RID get_camera() const;
  96. float get_fov() const;
  97. float get_size() const;
  98. float get_zfar() const;
  99. float get_znear() const;
  100. Projection get_projection() const;
  101. void set_fov(float p_fov);
  102. void set_size(float p_size);
  103. void set_zfar(float p_zfar);
  104. void set_znear(float p_znear);
  105. virtual Transform get_camera_transform() const;
  106. virtual Vector3 project_ray_normal(const Point2 &p_pos) const;
  107. virtual Vector3 project_ray_origin(const Point2 &p_pos) const;
  108. virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
  109. virtual Point2 unproject_position(const Vector3 &p_pos) const;
  110. bool is_position_behind(const Vector3 &p_pos) const;
  111. virtual Vector3 project_position(const Point2 &p_point) const;
  112. Vector<Vector3> get_near_plane_points() const;
  113. void set_cull_mask(uint32_t p_layers);
  114. uint32_t get_cull_mask() const;
  115. void set_cull_mask_bit(int p_layer, bool p_enable);
  116. bool get_cull_mask_bit(int p_layer) const;
  117. virtual Vector<Plane> get_frustum() const;
  118. void set_environment(const Ref<Environment> &p_environment);
  119. Ref<Environment> get_environment() const;
  120. void set_keep_aspect_mode(KeepAspect p_aspect);
  121. KeepAspect get_keep_aspect_mode() const;
  122. void set_v_offset(float p_offset);
  123. float get_v_offset() const;
  124. void set_h_offset(float p_offset);
  125. float get_h_offset() const;
  126. void set_doppler_tracking(DopplerTracking p_tracking);
  127. DopplerTracking get_doppler_tracking() const;
  128. Vector3 get_doppler_tracked_velocity() const;
  129. Camera();
  130. ~Camera();
  131. };
  132. VARIANT_ENUM_CAST(Camera::Projection);
  133. VARIANT_ENUM_CAST(Camera::KeepAspect);
  134. VARIANT_ENUM_CAST(Camera::DopplerTracking);
  135. class ClippedCamera : public Camera {
  136. GDCLASS(ClippedCamera, Camera);
  137. public:
  138. enum ProcessMode {
  139. CLIP_PROCESS_PHYSICS,
  140. CLIP_PROCESS_IDLE,
  141. };
  142. private:
  143. ProcessMode process_mode;
  144. RID pyramid_shape;
  145. float margin;
  146. float clip_offset;
  147. uint32_t collision_mask;
  148. bool clip_to_areas;
  149. bool clip_to_bodies;
  150. Set<RID> exclude;
  151. Vector<Vector3> points;
  152. protected:
  153. void _notification(int p_what);
  154. static void _bind_methods();
  155. virtual Transform get_camera_transform() const;
  156. public:
  157. void set_clip_to_areas(bool p_clip);
  158. bool is_clip_to_areas_enabled() const;
  159. void set_clip_to_bodies(bool p_clip);
  160. bool is_clip_to_bodies_enabled() const;
  161. void set_margin(float p_margin);
  162. float get_margin() const;
  163. void set_process_mode(ProcessMode p_mode);
  164. ProcessMode get_process_mode() const;
  165. void set_collision_mask(uint32_t p_mask);
  166. uint32_t get_collision_mask() const;
  167. void set_collision_mask_bit(int p_bit, bool p_value);
  168. bool get_collision_mask_bit(int p_bit) const;
  169. void add_exception_rid(const RID &p_rid);
  170. void add_exception(const Object *p_object);
  171. void remove_exception_rid(const RID &p_rid);
  172. void remove_exception(const Object *p_object);
  173. void clear_exceptions();
  174. ClippedCamera();
  175. ~ClippedCamera();
  176. };
  177. VARIANT_ENUM_CAST(ClippedCamera::ProcessMode);
  178. #endif