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