camera.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**************************************************************************/
  2. /* camera.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 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. class Camera : public Spatial {
  37. GDCLASS(Camera, Spatial);
  38. public:
  39. enum Projection {
  40. PROJECTION_PERSPECTIVE,
  41. PROJECTION_ORTHOGONAL,
  42. PROJECTION_FRUSTUM
  43. };
  44. enum KeepAspect {
  45. KEEP_WIDTH,
  46. KEEP_HEIGHT
  47. };
  48. enum DopplerTracking {
  49. DOPPLER_TRACKING_DISABLED,
  50. DOPPLER_TRACKING_IDLE_STEP,
  51. DOPPLER_TRACKING_PHYSICS_STEP
  52. };
  53. private:
  54. bool force_change;
  55. bool current;
  56. Viewport *viewport;
  57. Projection mode;
  58. float fov;
  59. float size;
  60. Vector2 frustum_offset;
  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. //void _camera_make_current(Node *p_camera);
  71. friend class Viewport;
  72. void _update_audio_listener_state();
  73. DopplerTracking doppler_tracking;
  74. Ref<SpatialVelocityTracker> velocity_tracker;
  75. bool affect_lod = true;
  76. ///////////////////////////////////////////////////////
  77. // INTERPOLATION FUNCTIONS
  78. void _physics_interpolation_ensure_transform_calculated(bool p_force = false) const;
  79. void _physics_interpolation_ensure_data_flipped();
  80. // These can be set by derived Cameras,
  81. // if they wish to do processing (while still
  82. // allowing physics interpolation to function).
  83. bool _desired_process_internal = false;
  84. bool _desired_physics_process_internal = false;
  85. mutable struct InterpolationData {
  86. Transform xform_curr;
  87. Transform xform_prev;
  88. Transform xform_interpolated;
  89. Transform camera_xform_interpolated; // After modification according to camera type.
  90. uint32_t last_update_physics_tick = 0;
  91. uint32_t last_update_frame = UINT32_MAX;
  92. } _interpolation_data;
  93. void _update_process_mode();
  94. protected:
  95. // Use from derived classes to set process modes instead of setting directly.
  96. // This is because physics interpolation may need to request process modes additionally.
  97. void set_desired_process_modes(bool p_process_internal, bool p_physics_process_internal);
  98. // Opportunity for derived classes to interpolate extra attributes.
  99. virtual void physics_interpolation_flip_data() {}
  100. virtual void _physics_interpolated_changed();
  101. virtual Transform _get_adjusted_camera_transform(const Transform &p_xform) const;
  102. ///////////////////////////////////////////////////////
  103. void _update_camera();
  104. virtual void _request_camera_update();
  105. void _update_camera_mode();
  106. void _notification(int p_what);
  107. virtual void _validate_property(PropertyInfo &p_property) const;
  108. static void _bind_methods();
  109. public:
  110. enum {
  111. NOTIFICATION_BECAME_CURRENT = 50,
  112. NOTIFICATION_LOST_CURRENT = 51
  113. };
  114. void set_perspective(float p_fovy_degrees, float p_z_near, float p_z_far);
  115. void set_orthogonal(float p_size, float p_z_near, float p_z_far);
  116. void set_frustum(float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
  117. void set_projection(Camera::Projection p_mode);
  118. void make_current();
  119. void clear_current(bool p_enable_next = true);
  120. void set_current(bool p_current);
  121. bool is_current() const;
  122. RID get_camera() const;
  123. float get_fov() const;
  124. float get_size() const;
  125. float get_zfar() const;
  126. float get_znear() const;
  127. Vector2 get_frustum_offset() const;
  128. Projection get_projection() const;
  129. void set_fov(float p_fov);
  130. void set_size(float p_size);
  131. void set_zfar(float p_zfar);
  132. void set_znear(float p_znear);
  133. void set_frustum_offset(Vector2 p_offset);
  134. Transform get_camera_transform() const;
  135. virtual Vector3 project_ray_normal(const Point2 &p_pos) const;
  136. virtual Vector3 project_ray_origin(const Point2 &p_pos) const;
  137. virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
  138. virtual Point2 unproject_position(const Vector3 &p_pos) const;
  139. bool safe_unproject_position(const Vector3 &p_pos, Point2 &r_result) const;
  140. bool is_position_behind(const Vector3 &p_pos) const;
  141. virtual Vector3 project_position(const Point2 &p_point, float p_z_depth) const;
  142. Vector<Vector3> get_near_plane_points() const;
  143. void set_cull_mask(uint32_t p_layers);
  144. uint32_t get_cull_mask() const;
  145. void set_cull_mask_bit(int p_layer, bool p_enable);
  146. bool get_cull_mask_bit(int p_layer) const;
  147. virtual Vector<Plane> get_frustum() const;
  148. void set_environment(const Ref<Environment> &p_environment);
  149. Ref<Environment> get_environment() const;
  150. void set_keep_aspect_mode(KeepAspect p_aspect);
  151. KeepAspect get_keep_aspect_mode() const;
  152. void set_v_offset(float p_offset);
  153. float get_v_offset() const;
  154. void set_h_offset(float p_offset);
  155. float get_h_offset() const;
  156. void set_doppler_tracking(DopplerTracking p_tracking);
  157. DopplerTracking get_doppler_tracking() const;
  158. Vector3 get_doppler_tracked_velocity() const;
  159. void set_affect_lod(bool p_enable) { affect_lod = p_enable; }
  160. bool get_affect_lod() const { return affect_lod; }
  161. Camera();
  162. ~Camera();
  163. };
  164. VARIANT_ENUM_CAST(Camera::Projection);
  165. VARIANT_ENUM_CAST(Camera::KeepAspect);
  166. VARIANT_ENUM_CAST(Camera::DopplerTracking);
  167. class ClippedCamera : public Camera {
  168. GDCLASS(ClippedCamera, Camera);
  169. public:
  170. enum ProcessMode {
  171. CLIP_PROCESS_PHYSICS,
  172. CLIP_PROCESS_IDLE,
  173. };
  174. private:
  175. ProcessMode process_mode;
  176. RID pyramid_shape;
  177. float margin;
  178. float clip_offset = 0;
  179. uint32_t collision_mask;
  180. bool clip_to_areas;
  181. bool clip_to_bodies;
  182. Set<RID> exclude;
  183. Vector<Vector3> points;
  184. ///////////////////////////////////////////////////////
  185. // INTERPOLATION FUNCTIONS
  186. struct InterpolatedData {
  187. float clip_offset_curr = 0;
  188. float clip_offset_prev = 0;
  189. } _interpolation_data;
  190. protected:
  191. virtual Transform _get_adjusted_camera_transform(const Transform &p_xform) const;
  192. virtual void physics_interpolation_flip_data();
  193. virtual void _physics_interpolated_changed();
  194. ///////////////////////////////////////////////////////
  195. void _notification(int p_what);
  196. static void _bind_methods();
  197. public:
  198. void set_clip_to_areas(bool p_clip);
  199. bool is_clip_to_areas_enabled() const;
  200. void set_clip_to_bodies(bool p_clip);
  201. bool is_clip_to_bodies_enabled() const;
  202. void set_margin(float p_margin);
  203. float get_margin() const;
  204. void set_process_mode(ProcessMode p_mode);
  205. ProcessMode get_process_mode() const;
  206. void set_collision_mask(uint32_t p_mask);
  207. uint32_t get_collision_mask() const;
  208. void set_collision_mask_bit(int p_bit, bool p_value);
  209. bool get_collision_mask_bit(int p_bit) const;
  210. void add_exception_rid(const RID &p_rid);
  211. void add_exception(const Object *p_object);
  212. void remove_exception_rid(const RID &p_rid);
  213. void remove_exception(const Object *p_object);
  214. void clear_exceptions();
  215. float get_clip_offset() const;
  216. ClippedCamera();
  217. ~ClippedCamera();
  218. };
  219. VARIANT_ENUM_CAST(ClippedCamera::ProcessMode);
  220. #endif // CAMERA_H