Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/camera_3d.h
9896 views
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
#pragma once
32
33
#include "core/templates/interpolated_property.h"
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/compositor.h"
38
#include "scene/resources/environment.h"
39
40
class Camera3D : public Node3D {
41
GDCLASS(Camera3D, Node3D);
42
43
public:
44
enum ProjectionType {
45
PROJECTION_PERSPECTIVE,
46
PROJECTION_ORTHOGONAL,
47
PROJECTION_FRUSTUM
48
};
49
50
enum KeepAspect {
51
KEEP_WIDTH,
52
KEEP_HEIGHT
53
};
54
55
enum DopplerTracking {
56
DOPPLER_TRACKING_DISABLED,
57
DOPPLER_TRACKING_IDLE_STEP,
58
DOPPLER_TRACKING_PHYSICS_STEP
59
};
60
61
private:
62
bool force_change = false;
63
bool current = false;
64
Viewport *viewport = nullptr;
65
66
ProjectionType mode = PROJECTION_PERSPECTIVE;
67
68
InterpolatedProperty<real_t> fov = 75.0;
69
InterpolatedProperty<real_t> size = 1.0;
70
InterpolatedProperty<Vector2> frustum_offset;
71
// _ prefix to avoid conflict with Windows defines.
72
InterpolatedProperty<real_t> _near = 0.05;
73
InterpolatedProperty<real_t> _far = 4000.0;
74
real_t v_offset = 0.0;
75
real_t h_offset = 0.0;
76
KeepAspect keep_aspect = KEEP_HEIGHT;
77
78
RID camera;
79
RID scenario_id;
80
81
// String camera_group;
82
83
uint32_t layers = 0xfffff;
84
85
Ref<Environment> environment;
86
Ref<CameraAttributes> attributes;
87
Ref<Compositor> compositor;
88
void _attributes_changed();
89
90
// void _camera_make_current(Node *p_camera);
91
friend class Viewport;
92
void _update_audio_listener_state();
93
TypedArray<Plane> _get_frustum() const;
94
95
DopplerTracking doppler_tracking = DOPPLER_TRACKING_DISABLED;
96
Ref<VelocityTracker3D> velocity_tracker;
97
98
#ifndef PHYSICS_3D_DISABLED
99
RID pyramid_shape;
100
Vector<Vector3> pyramid_shape_points;
101
#endif // PHYSICS_3D_DISABLED
102
103
// These can be set by derived Cameras.
104
bool _desired_process_internal = false;
105
bool _desired_physics_process_internal = false;
106
107
void _update_process_mode();
108
109
protected:
110
// Use from derived classes to set process modes instead of setting directly.
111
// This is because physics interpolation may need to request process modes additionally.
112
void set_desired_process_modes(bool p_process_internal, bool p_physics_process_internal);
113
114
virtual void _physics_interpolated_changed() override;
115
virtual Transform3D _get_adjusted_camera_transform(const Transform3D &p_xform) const;
116
///////////////////////////////////////////////////////
117
118
void _update_camera();
119
virtual void _request_camera_update();
120
void _update_camera_mode();
121
122
virtual void fti_pump_property() override;
123
virtual void fti_update_servers_property() override;
124
virtual void fti_update_servers_xform() override;
125
126
void _notification(int p_what);
127
void _validate_property(PropertyInfo &p_property) const;
128
129
static void _bind_methods();
130
131
Projection _get_camera_projection(real_t p_near) const;
132
133
public:
134
enum {
135
NOTIFICATION_BECAME_CURRENT = 50,
136
NOTIFICATION_LOST_CURRENT = 51
137
};
138
139
void set_perspective(real_t p_fovy_degrees, real_t p_z_near, real_t p_z_far);
140
void set_orthogonal(real_t p_size, real_t p_z_near, real_t p_z_far);
141
void set_frustum(real_t p_size, Vector2 p_offset, real_t p_z_near, real_t p_z_far);
142
void set_projection(Camera3D::ProjectionType p_mode);
143
144
void make_current();
145
void clear_current(bool p_enable_next = true);
146
void set_current(bool p_enabled);
147
bool is_current() const;
148
149
RID get_camera() const;
150
151
real_t get_fov() const;
152
real_t get_size() const;
153
real_t get_far() const;
154
real_t get_near() const;
155
Vector2 get_frustum_offset() const;
156
157
ProjectionType get_projection() const;
158
159
void set_fov(real_t p_fov);
160
void set_size(real_t p_size);
161
void set_far(real_t p_far);
162
void set_near(real_t p_near);
163
void set_frustum_offset(Vector2 p_offset);
164
165
virtual Transform3D get_camera_transform() const;
166
virtual Projection get_camera_projection() const;
167
168
virtual Vector3 project_ray_normal(const Point2 &p_pos) const;
169
virtual Vector3 project_ray_origin(const Point2 &p_pos) const;
170
virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
171
virtual Point2 unproject_position(const Vector3 &p_pos) const;
172
bool is_position_behind(const Vector3 &p_pos) const;
173
virtual Vector3 project_position(const Point2 &p_point, real_t p_z_depth) const;
174
175
Vector<Vector3> get_near_plane_points() const;
176
177
void set_cull_mask(uint32_t p_layers);
178
uint32_t get_cull_mask() const;
179
180
void set_cull_mask_value(int p_layer_number, bool p_enable);
181
bool get_cull_mask_value(int p_layer_number) const;
182
183
virtual Vector<Plane> get_frustum() const;
184
bool is_position_in_frustum(const Vector3 &p_position) const;
185
186
void set_environment(const Ref<Environment> &p_environment);
187
Ref<Environment> get_environment() const;
188
189
void set_attributes(const Ref<CameraAttributes> &p_effects);
190
Ref<CameraAttributes> get_attributes() const;
191
192
void set_compositor(const Ref<Compositor> &p_compositor);
193
Ref<Compositor> get_compositor() const;
194
195
void set_keep_aspect_mode(KeepAspect p_aspect);
196
KeepAspect get_keep_aspect_mode() const;
197
198
void set_v_offset(real_t p_offset);
199
real_t get_v_offset() const;
200
201
void set_h_offset(real_t p_offset);
202
real_t get_h_offset() const;
203
204
void set_doppler_tracking(DopplerTracking p_tracking);
205
DopplerTracking get_doppler_tracking() const;
206
207
Vector3 get_doppler_tracked_velocity() const;
208
209
#ifndef PHYSICS_3D_DISABLED
210
RID get_pyramid_shape_rid();
211
#endif // PHYSICS_3D_DISABLED
212
213
Camera3D();
214
~Camera3D();
215
};
216
217
VARIANT_ENUM_CAST(Camera3D::ProjectionType);
218
VARIANT_ENUM_CAST(Camera3D::KeepAspect);
219
VARIANT_ENUM_CAST(Camera3D::DopplerTracking);
220
221