camera.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. protected:
  76. void _update_camera();
  77. virtual void _request_camera_update();
  78. void _update_camera_mode();
  79. virtual void _physics_interpolated_changed();
  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_frustum(float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
  91. void set_projection(Camera::Projection p_mode);
  92. void make_current();
  93. void clear_current(bool p_enable_next = true);
  94. void set_current(bool p_current);
  95. bool is_current() const;
  96. RID get_camera() const;
  97. float get_fov() const;
  98. float get_size() const;
  99. float get_zfar() const;
  100. float get_znear() const;
  101. Vector2 get_frustum_offset() const;
  102. Projection get_projection() const;
  103. void set_fov(float p_fov);
  104. void set_size(float p_size);
  105. void set_zfar(float p_zfar);
  106. void set_znear(float p_znear);
  107. void set_frustum_offset(Vector2 p_offset);
  108. virtual Transform get_camera_transform() const;
  109. virtual Vector3 project_ray_normal(const Point2 &p_pos) const;
  110. virtual Vector3 project_ray_origin(const Point2 &p_pos) const;
  111. virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
  112. virtual Point2 unproject_position(const Vector3 &p_pos) const;
  113. bool is_position_behind(const Vector3 &p_pos) const;
  114. virtual Vector3 project_position(const Point2 &p_point, float p_z_depth) const;
  115. Vector<Vector3> get_near_plane_points() const;
  116. void set_cull_mask(uint32_t p_layers);
  117. uint32_t get_cull_mask() const;
  118. void set_cull_mask_bit(int p_layer, bool p_enable);
  119. bool get_cull_mask_bit(int p_layer) const;
  120. virtual Vector<Plane> get_frustum() const;
  121. void set_environment(const Ref<Environment> &p_environment);
  122. Ref<Environment> get_environment() const;
  123. void set_keep_aspect_mode(KeepAspect p_aspect);
  124. KeepAspect get_keep_aspect_mode() const;
  125. void set_v_offset(float p_offset);
  126. float get_v_offset() const;
  127. void set_h_offset(float p_offset);
  128. float get_h_offset() const;
  129. void set_doppler_tracking(DopplerTracking p_tracking);
  130. DopplerTracking get_doppler_tracking() const;
  131. Vector3 get_doppler_tracked_velocity() const;
  132. Camera();
  133. ~Camera();
  134. };
  135. VARIANT_ENUM_CAST(Camera::Projection);
  136. VARIANT_ENUM_CAST(Camera::KeepAspect);
  137. VARIANT_ENUM_CAST(Camera::DopplerTracking);
  138. class ClippedCamera : public Camera {
  139. GDCLASS(ClippedCamera, Camera);
  140. public:
  141. enum ProcessMode {
  142. CLIP_PROCESS_PHYSICS,
  143. CLIP_PROCESS_IDLE,
  144. };
  145. private:
  146. ProcessMode process_mode;
  147. RID pyramid_shape;
  148. float margin;
  149. float clip_offset;
  150. uint32_t collision_mask;
  151. bool clip_to_areas;
  152. bool clip_to_bodies;
  153. Set<RID> exclude;
  154. Vector<Vector3> points;
  155. protected:
  156. void _notification(int p_what);
  157. static void _bind_methods();
  158. virtual Transform get_camera_transform() const;
  159. public:
  160. void set_clip_to_areas(bool p_clip);
  161. bool is_clip_to_areas_enabled() const;
  162. void set_clip_to_bodies(bool p_clip);
  163. bool is_clip_to_bodies_enabled() const;
  164. void set_margin(float p_margin);
  165. float get_margin() const;
  166. void set_process_mode(ProcessMode p_mode);
  167. ProcessMode get_process_mode() const;
  168. void set_collision_mask(uint32_t p_mask);
  169. uint32_t get_collision_mask() const;
  170. void set_collision_mask_bit(int p_bit, bool p_value);
  171. bool get_collision_mask_bit(int p_bit) const;
  172. void add_exception_rid(const RID &p_rid);
  173. void add_exception(const Object *p_object);
  174. void remove_exception_rid(const RID &p_rid);
  175. void remove_exception(const Object *p_object);
  176. void clear_exceptions();
  177. float get_clip_offset() const;
  178. ClippedCamera();
  179. ~ClippedCamera();
  180. };
  181. VARIANT_ENUM_CAST(ClippedCamera::ProcessMode);
  182. #endif // CAMERA_H