camera_3d.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/environment.h"
  36. #ifdef MINGW_ENABLED
  37. #undef near
  38. #undef far
  39. #endif
  40. class Camera3D : public Node3D {
  41. GDCLASS(Camera3D, Node3D);
  42. public:
  43. enum ProjectionType {
  44. PROJECTION_PERSPECTIVE,
  45. PROJECTION_ORTHOGONAL,
  46. PROJECTION_FRUSTUM
  47. };
  48. enum KeepAspect {
  49. KEEP_WIDTH,
  50. KEEP_HEIGHT
  51. };
  52. enum DopplerTracking {
  53. DOPPLER_TRACKING_DISABLED,
  54. DOPPLER_TRACKING_IDLE_STEP,
  55. DOPPLER_TRACKING_PHYSICS_STEP
  56. };
  57. private:
  58. bool force_change = false;
  59. bool current = false;
  60. Viewport *viewport = nullptr;
  61. ProjectionType mode = PROJECTION_PERSPECTIVE;
  62. real_t fov = 75.0;
  63. real_t size = 1.0;
  64. Vector2 frustum_offset;
  65. real_t near = 0.05;
  66. real_t far = 4000.0;
  67. real_t v_offset = 0.0;
  68. real_t h_offset = 0.0;
  69. KeepAspect keep_aspect = KEEP_HEIGHT;
  70. RID camera;
  71. RID scenario_id;
  72. // String camera_group;
  73. uint32_t layers = 0xfffff;
  74. Ref<Environment> environment;
  75. Ref<CameraAttributes> attributes;
  76. void _attributes_changed();
  77. // void _camera_make_current(Node *p_camera);
  78. friend class Viewport;
  79. void _update_audio_listener_state();
  80. TypedArray<Plane> _get_frustum() const;
  81. DopplerTracking doppler_tracking = DOPPLER_TRACKING_DISABLED;
  82. Ref<VelocityTracker3D> velocity_tracker;
  83. RID pyramid_shape;
  84. Vector<Vector3> pyramid_shape_points;
  85. protected:
  86. void _update_camera();
  87. virtual void _request_camera_update();
  88. void _update_camera_mode();
  89. void _notification(int p_what);
  90. void _validate_property(PropertyInfo &p_property) const;
  91. static void _bind_methods();
  92. Projection _get_camera_projection(real_t p_near) const;
  93. public:
  94. enum {
  95. NOTIFICATION_BECAME_CURRENT = 50,
  96. NOTIFICATION_LOST_CURRENT = 51
  97. };
  98. void set_perspective(real_t p_fovy_degrees, real_t p_z_near, real_t p_z_far);
  99. void set_orthogonal(real_t p_size, real_t p_z_near, real_t p_z_far);
  100. void set_frustum(real_t p_size, Vector2 p_offset, real_t p_z_near, real_t p_z_far);
  101. void set_projection(Camera3D::ProjectionType p_mode);
  102. void make_current();
  103. void clear_current(bool p_enable_next = true);
  104. void set_current(bool p_enabled);
  105. bool is_current() const;
  106. RID get_camera() const;
  107. real_t get_fov() const;
  108. real_t get_size() const;
  109. real_t get_far() const;
  110. real_t get_near() const;
  111. Vector2 get_frustum_offset() const;
  112. ProjectionType get_projection() const;
  113. void set_fov(real_t p_fov);
  114. void set_size(real_t p_size);
  115. void set_far(real_t p_far);
  116. void set_near(real_t p_near);
  117. void set_frustum_offset(Vector2 p_offset);
  118. virtual Transform3D get_camera_transform() const;
  119. virtual Projection get_camera_projection() const;
  120. virtual Vector3 project_ray_normal(const Point2 &p_pos) const;
  121. virtual Vector3 project_ray_origin(const Point2 &p_pos) const;
  122. virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
  123. virtual Point2 unproject_position(const Vector3 &p_pos) const;
  124. bool is_position_behind(const Vector3 &p_pos) const;
  125. virtual Vector3 project_position(const Point2 &p_point, real_t p_z_depth) const;
  126. Vector<Vector3> get_near_plane_points() const;
  127. void set_cull_mask(uint32_t p_layers);
  128. uint32_t get_cull_mask() const;
  129. void set_cull_mask_value(int p_layer_number, bool p_enable);
  130. bool get_cull_mask_value(int p_layer_number) const;
  131. virtual Vector<Plane> get_frustum() const;
  132. bool is_position_in_frustum(const Vector3 &p_position) const;
  133. void set_environment(const Ref<Environment> &p_environment);
  134. Ref<Environment> get_environment() const;
  135. void set_attributes(const Ref<CameraAttributes> &p_effects);
  136. Ref<CameraAttributes> get_attributes() const;
  137. void set_keep_aspect_mode(KeepAspect p_aspect);
  138. KeepAspect get_keep_aspect_mode() const;
  139. void set_v_offset(real_t p_offset);
  140. real_t get_v_offset() const;
  141. void set_h_offset(real_t p_offset);
  142. real_t get_h_offset() const;
  143. void set_doppler_tracking(DopplerTracking p_tracking);
  144. DopplerTracking get_doppler_tracking() const;
  145. Vector3 get_doppler_tracked_velocity() const;
  146. RID get_pyramid_shape_rid();
  147. Camera3D();
  148. ~Camera3D();
  149. };
  150. VARIANT_ENUM_CAST(Camera3D::ProjectionType);
  151. VARIANT_ENUM_CAST(Camera3D::KeepAspect);
  152. VARIANT_ENUM_CAST(Camera3D::DopplerTracking);
  153. #endif // CAMERA_3D_H