camera_3d.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**************************************************************************/
  2. /* camera_3d.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_3D_H
  31. #define CAMERA_3D_H
  32. #include "scene/3d/node_3d.h"
  33. #include "scene/3d/velocity_tracker_3d.h"
  34. #include "scene/resources/camera_attributes.h"
  35. #include "scene/resources/compositor.h"
  36. #include "scene/resources/environment.h"
  37. class Camera3D : public Node3D {
  38. GDCLASS(Camera3D, Node3D);
  39. public:
  40. enum ProjectionType {
  41. PROJECTION_PERSPECTIVE,
  42. PROJECTION_ORTHOGONAL,
  43. PROJECTION_FRUSTUM
  44. };
  45. enum KeepAspect {
  46. KEEP_WIDTH,
  47. KEEP_HEIGHT
  48. };
  49. enum DopplerTracking {
  50. DOPPLER_TRACKING_DISABLED,
  51. DOPPLER_TRACKING_IDLE_STEP,
  52. DOPPLER_TRACKING_PHYSICS_STEP
  53. };
  54. private:
  55. bool force_change = false;
  56. bool current = false;
  57. Viewport *viewport = nullptr;
  58. ProjectionType mode = PROJECTION_PERSPECTIVE;
  59. real_t fov = 75.0;
  60. real_t size = 1.0;
  61. Vector2 frustum_offset;
  62. // _ prefix to avoid conflict with Windows defines.
  63. real_t _near = 0.05;
  64. real_t _far = 4000.0;
  65. real_t v_offset = 0.0;
  66. real_t h_offset = 0.0;
  67. KeepAspect keep_aspect = KEEP_HEIGHT;
  68. RID camera;
  69. RID scenario_id;
  70. // String camera_group;
  71. uint32_t layers = 0xfffff;
  72. Ref<Environment> environment;
  73. Ref<CameraAttributes> attributes;
  74. Ref<Compositor> compositor;
  75. void _attributes_changed();
  76. // void _camera_make_current(Node *p_camera);
  77. friend class Viewport;
  78. void _update_audio_listener_state();
  79. TypedArray<Plane> _get_frustum() const;
  80. DopplerTracking doppler_tracking = DOPPLER_TRACKING_DISABLED;
  81. Ref<VelocityTracker3D> velocity_tracker;
  82. RID pyramid_shape;
  83. Vector<Vector3> pyramid_shape_points;
  84. ///////////////////////////////////////////////////////
  85. // INTERPOLATION FUNCTIONS
  86. void _physics_interpolation_ensure_transform_calculated(bool p_force = false) const;
  87. void _physics_interpolation_ensure_data_flipped();
  88. // These can be set by derived Camera3Ds, if they wish to do processing
  89. // (while still allowing physics interpolation to function).
  90. bool _desired_process_internal = false;
  91. bool _desired_physics_process_internal = false;
  92. mutable struct InterpolationData {
  93. Transform3D xform_curr;
  94. Transform3D xform_prev;
  95. Transform3D xform_interpolated;
  96. Transform3D camera_xform_interpolated; // After modification according to camera type.
  97. uint32_t last_update_physics_tick = 0;
  98. uint32_t last_update_frame = UINT32_MAX;
  99. } _interpolation_data;
  100. void _update_process_mode();
  101. protected:
  102. // Use from derived classes to set process modes instead of setting directly.
  103. // This is because physics interpolation may need to request process modes additionally.
  104. void set_desired_process_modes(bool p_process_internal, bool p_physics_process_internal);
  105. // Opportunity for derived classes to interpolate extra attributes.
  106. virtual void physics_interpolation_flip_data() {}
  107. virtual void _physics_interpolated_changed() override;
  108. virtual Transform3D _get_adjusted_camera_transform(const Transform3D &p_xform) const;
  109. ///////////////////////////////////////////////////////
  110. void _update_camera();
  111. virtual void _request_camera_update();
  112. void _update_camera_mode();
  113. void _notification(int p_what);
  114. void _validate_property(PropertyInfo &p_property) const;
  115. static void _bind_methods();
  116. Projection _get_camera_projection(real_t p_near) const;
  117. public:
  118. enum {
  119. NOTIFICATION_BECAME_CURRENT = 50,
  120. NOTIFICATION_LOST_CURRENT = 51
  121. };
  122. void set_perspective(real_t p_fovy_degrees, real_t p_z_near, real_t p_z_far);
  123. void set_orthogonal(real_t p_size, real_t p_z_near, real_t p_z_far);
  124. void set_frustum(real_t p_size, Vector2 p_offset, real_t p_z_near, real_t p_z_far);
  125. void set_projection(Camera3D::ProjectionType p_mode);
  126. void make_current();
  127. void clear_current(bool p_enable_next = true);
  128. void set_current(bool p_enabled);
  129. bool is_current() const;
  130. RID get_camera() const;
  131. real_t get_fov() const;
  132. real_t get_size() const;
  133. real_t get_far() const;
  134. real_t get_near() const;
  135. Vector2 get_frustum_offset() const;
  136. ProjectionType get_projection() const;
  137. void set_fov(real_t p_fov);
  138. void set_size(real_t p_size);
  139. void set_far(real_t p_far);
  140. void set_near(real_t p_near);
  141. void set_frustum_offset(Vector2 p_offset);
  142. virtual Transform3D get_camera_transform() const;
  143. virtual Projection get_camera_projection() const;
  144. virtual Vector3 project_ray_normal(const Point2 &p_pos) const;
  145. virtual Vector3 project_ray_origin(const Point2 &p_pos) const;
  146. virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
  147. virtual Point2 unproject_position(const Vector3 &p_pos) const;
  148. bool is_position_behind(const Vector3 &p_pos) const;
  149. virtual Vector3 project_position(const Point2 &p_point, real_t p_z_depth) const;
  150. Vector<Vector3> get_near_plane_points() const;
  151. void set_cull_mask(uint32_t p_layers);
  152. uint32_t get_cull_mask() const;
  153. void set_cull_mask_value(int p_layer_number, bool p_enable);
  154. bool get_cull_mask_value(int p_layer_number) const;
  155. virtual Vector<Plane> get_frustum() const;
  156. bool is_position_in_frustum(const Vector3 &p_position) const;
  157. void set_environment(const Ref<Environment> &p_environment);
  158. Ref<Environment> get_environment() const;
  159. void set_attributes(const Ref<CameraAttributes> &p_effects);
  160. Ref<CameraAttributes> get_attributes() const;
  161. void set_compositor(const Ref<Compositor> &p_compositor);
  162. Ref<Compositor> get_compositor() const;
  163. void set_keep_aspect_mode(KeepAspect p_aspect);
  164. KeepAspect get_keep_aspect_mode() const;
  165. void set_v_offset(real_t p_offset);
  166. real_t get_v_offset() const;
  167. void set_h_offset(real_t p_offset);
  168. real_t get_h_offset() const;
  169. void set_doppler_tracking(DopplerTracking p_tracking);
  170. DopplerTracking get_doppler_tracking() const;
  171. Vector3 get_doppler_tracked_velocity() const;
  172. RID get_pyramid_shape_rid();
  173. Camera3D();
  174. ~Camera3D();
  175. };
  176. VARIANT_ENUM_CAST(Camera3D::ProjectionType);
  177. VARIANT_ENUM_CAST(Camera3D::KeepAspect);
  178. VARIANT_ENUM_CAST(Camera3D::DopplerTracking);
  179. #endif // CAMERA_3D_H