Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/curve.h
9896 views
1
/**************************************************************************/
2
/* curve.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/io/resource.h"
34
35
// y(x) curve
36
class Curve : public Resource {
37
GDCLASS(Curve, Resource);
38
39
public:
40
static const char *SIGNAL_RANGE_CHANGED;
41
static const char *SIGNAL_DOMAIN_CHANGED;
42
43
enum TangentMode {
44
TANGENT_FREE = 0,
45
TANGENT_LINEAR,
46
TANGENT_MODE_COUNT
47
};
48
49
struct Point {
50
Vector2 position;
51
real_t left_tangent = 0.0;
52
real_t right_tangent = 0.0;
53
TangentMode left_mode = TANGENT_FREE;
54
TangentMode right_mode = TANGENT_FREE;
55
56
Point() {
57
}
58
59
Point(const Vector2 &p_position,
60
real_t p_left = 0.0,
61
real_t p_right = 0.0,
62
TangentMode p_left_mode = TANGENT_FREE,
63
TangentMode p_right_mode = TANGENT_FREE) {
64
position = p_position;
65
left_tangent = p_left;
66
right_tangent = p_right;
67
left_mode = p_left_mode;
68
right_mode = p_right_mode;
69
}
70
};
71
72
Curve();
73
74
int get_point_count() const { return _points.size(); }
75
76
void set_point_count(int p_count);
77
78
int add_point(Vector2 p_position,
79
real_t p_left_tangent = 0,
80
real_t p_right_tangent = 0,
81
TangentMode p_left_mode = TANGENT_FREE,
82
TangentMode p_right_mode = TANGENT_FREE);
83
int add_point_no_update(Vector2 p_position,
84
real_t p_left_tangent = 0,
85
real_t p_right_tangent = 0,
86
TangentMode p_left_mode = TANGENT_FREE,
87
TangentMode p_right_mode = TANGENT_FREE);
88
void remove_point(int p_index);
89
void clear_points();
90
91
int get_index(real_t p_offset) const;
92
93
void set_point_value(int p_index, real_t p_position);
94
int set_point_offset(int p_index, real_t p_offset);
95
Vector2 get_point_position(int p_index) const;
96
97
Point get_point(int p_index) const;
98
99
real_t get_min_value() const { return _min_value; }
100
void set_min_value(real_t p_min);
101
real_t get_max_value() const { return _max_value; }
102
void set_max_value(real_t p_max);
103
real_t get_value_range() const { return _max_value - _min_value; }
104
105
real_t get_min_domain() const { return _min_domain; }
106
void set_min_domain(real_t p_min);
107
real_t get_max_domain() const { return _max_domain; }
108
void set_max_domain(real_t p_max);
109
real_t get_domain_range() const { return _max_domain - _min_domain; }
110
111
Array get_limits() const;
112
void set_limits(const Array &p_input);
113
114
real_t sample(real_t p_offset) const;
115
real_t sample_local_nocheck(int p_index, real_t p_local_offset) const;
116
117
void clean_dupes();
118
119
void set_point_left_tangent(int p_index, real_t p_tangent);
120
void set_point_right_tangent(int p_index, real_t p_tangent);
121
void set_point_left_mode(int p_index, TangentMode p_mode);
122
void set_point_right_mode(int p_index, TangentMode p_mode);
123
124
real_t get_point_left_tangent(int p_index) const;
125
real_t get_point_right_tangent(int p_index) const;
126
TangentMode get_point_left_mode(int p_index) const;
127
TangentMode get_point_right_mode(int p_index) const;
128
129
void update_auto_tangents(int p_index);
130
131
Array get_data() const;
132
void set_data(Array p_input);
133
134
void bake();
135
void _bake() const;
136
int get_bake_resolution() const { return _bake_resolution; }
137
void set_bake_resolution(int p_resolution);
138
real_t sample_baked(real_t p_offset) const;
139
140
void ensure_default_setup(real_t p_min, real_t p_max);
141
142
bool _set(const StringName &p_name, const Variant &p_value);
143
bool _get(const StringName &p_name, Variant &r_ret) const;
144
void _get_property_list(List<PropertyInfo> *p_list) const;
145
146
protected:
147
static void _bind_methods();
148
149
private:
150
void mark_dirty();
151
int _add_point(Vector2 p_position,
152
real_t p_left_tangent = 0,
153
real_t p_right_tangent = 0,
154
TangentMode p_left_mode = TANGENT_FREE,
155
TangentMode p_right_mode = TANGENT_FREE,
156
bool p_mark_dirty = true);
157
void _remove_point(int p_index, bool p_mark_dirty = true);
158
159
LocalVector<Point> _points;
160
mutable bool _baked_cache_dirty = false;
161
mutable Vector<real_t> _baked_cache;
162
int _bake_resolution = 100;
163
real_t _min_value = 0.0;
164
real_t _max_value = 1.0;
165
real_t _min_domain = 0.0;
166
real_t _max_domain = 1.0;
167
};
168
169
VARIANT_ENUM_CAST(Curve::TangentMode)
170
171
class Curve2D : public Resource {
172
GDCLASS(Curve2D, Resource);
173
174
struct Point {
175
Vector2 in;
176
Vector2 out;
177
Vector2 position;
178
};
179
180
LocalVector<Point> points;
181
182
struct BakedPoint {
183
real_t ofs = 0.0;
184
Vector2 point;
185
};
186
187
mutable bool baked_cache_dirty = false;
188
mutable PackedVector2Array baked_point_cache;
189
mutable PackedVector2Array baked_forward_vector_cache;
190
mutable Vector<real_t> baked_dist_cache;
191
mutable real_t baked_max_ofs = 0.0;
192
193
void mark_dirty();
194
195
static Vector2 _calculate_tangent(const Vector2 &p_begin, const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t);
196
void _bake() const;
197
198
real_t bake_interval = 5.0;
199
200
struct Interval {
201
int idx;
202
real_t frac;
203
};
204
Interval _find_interval(real_t p_offset) const;
205
Vector2 _sample_baked(Interval p_interval, bool p_cubic) const;
206
Transform2D _sample_posture(Interval p_interval) const;
207
208
void _bake_segment2d(RBMap<real_t, Vector2> &r_bake, real_t p_begin, real_t p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_max_depth, real_t p_tol) const;
209
void _bake_segment2d_even_length(RBMap<real_t, Vector2> &r_bake, real_t p_begin, real_t p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_max_depth, real_t p_length) const;
210
Dictionary _get_data() const;
211
void _set_data(const Dictionary &p_data);
212
213
bool _set(const StringName &p_name, const Variant &p_value);
214
bool _get(const StringName &p_name, Variant &r_ret) const;
215
void _get_property_list(List<PropertyInfo> *p_list) const;
216
217
void _add_point(const Vector2 &p_position, const Vector2 &p_in = Vector2(), const Vector2 &p_out = Vector2(), int p_atpos = -1);
218
void _remove_point(int p_index);
219
220
Vector<RBMap<real_t, Vector2>> _tessellate_even_length(int p_max_stages = 5, real_t p_length = 0.2) const;
221
222
protected:
223
static void _bind_methods();
224
225
public:
226
int get_point_count() const;
227
void set_point_count(int p_count);
228
void add_point(const Vector2 &p_position, const Vector2 &p_in = Vector2(), const Vector2 &p_out = Vector2(), int p_atpos = -1);
229
void set_point_position(int p_index, const Vector2 &p_position);
230
Vector2 get_point_position(int p_index) const;
231
void set_point_in(int p_index, const Vector2 &p_in);
232
Vector2 get_point_in(int p_index) const;
233
void set_point_out(int p_index, const Vector2 &p_out);
234
Vector2 get_point_out(int p_index) const;
235
void remove_point(int p_index);
236
void clear_points();
237
238
Vector2 sample(int p_index, real_t p_offset) const;
239
Vector2 samplef(real_t p_findex) const;
240
241
void set_bake_interval(real_t p_tolerance);
242
real_t get_bake_interval() const;
243
244
real_t get_baked_length() const;
245
Vector2 sample_baked(real_t p_offset, bool p_cubic = false) const;
246
Transform2D sample_baked_with_rotation(real_t p_offset, bool p_cubic = false) const;
247
PackedVector2Array get_points() const;
248
PackedVector2Array get_baked_points() const; //useful for going through
249
Vector2 get_closest_point(const Vector2 &p_to_point) const;
250
real_t get_closest_offset(const Vector2 &p_to_point) const;
251
252
PackedVector2Array tessellate(int p_max_stages = 5, real_t p_tolerance = 4) const; //useful for display
253
PackedVector2Array tessellate_even_length(int p_max_stages = 5, real_t p_length = 20.0) const; // Useful for baking.
254
};
255
256
class Curve3D : public Resource {
257
GDCLASS(Curve3D, Resource);
258
259
struct Point {
260
Vector3 in;
261
Vector3 out;
262
Vector3 position;
263
real_t tilt = 0.0;
264
};
265
266
LocalVector<Point> points;
267
#ifdef TOOLS_ENABLED
268
// For Path3DGizmo.
269
mutable Vector<size_t> points_in_cache;
270
#endif
271
272
bool closed = false;
273
274
mutable bool baked_cache_dirty = false;
275
mutable PackedVector3Array baked_point_cache;
276
mutable Vector<real_t> baked_tilt_cache;
277
mutable PackedVector3Array baked_up_vector_cache;
278
mutable PackedVector3Array baked_forward_vector_cache;
279
mutable Vector<real_t> baked_dist_cache;
280
mutable real_t baked_max_ofs = 0.0;
281
282
void mark_dirty();
283
284
static Vector3 _calculate_tangent(const Vector3 &p_begin, const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t);
285
void _bake() const;
286
287
struct Interval {
288
int idx;
289
real_t frac;
290
};
291
Interval _find_interval(real_t p_offset) const;
292
Vector3 _sample_baked(Interval p_interval, bool p_cubic) const;
293
real_t _sample_baked_tilt(Interval p_interval) const;
294
Basis _sample_posture(Interval p_interval, bool p_apply_tilt = false) const;
295
Basis _compose_posture(int p_index) const;
296
297
real_t bake_interval = 0.2;
298
bool up_vector_enabled = true;
299
300
void _bake_segment3d(RBMap<real_t, Vector3> &r_bake, real_t p_begin, real_t p_end, const Vector3 &p_a, const Vector3 &p_out, const Vector3 &p_b, const Vector3 &p_in, int p_depth, int p_max_depth, real_t p_tol) const;
301
void _bake_segment3d_even_length(RBMap<real_t, Vector3> &r_bake, real_t p_begin, real_t p_end, const Vector3 &p_a, const Vector3 &p_out, const Vector3 &p_b, const Vector3 &p_in, int p_depth, int p_max_depth, real_t p_length) const;
302
Dictionary _get_data() const;
303
void _set_data(const Dictionary &p_data);
304
305
bool _set(const StringName &p_name, const Variant &p_value);
306
bool _get(const StringName &p_name, Variant &r_ret) const;
307
void _get_property_list(List<PropertyInfo> *p_list) const;
308
309
void _add_point(const Vector3 &p_position, const Vector3 &p_in = Vector3(), const Vector3 &p_out = Vector3(), int p_atpos = -1);
310
void _remove_point(int p_index);
311
312
Vector<RBMap<real_t, Vector3>> _tessellate_even_length(int p_max_stages = 5, real_t p_length = 0.2) const;
313
314
protected:
315
static void _bind_methods();
316
317
public:
318
#ifdef TOOLS_ENABLED
319
// For Path3DGizmo.
320
Basis get_point_baked_posture(int p_index, bool p_apply_tilt = false) const;
321
#endif
322
323
int get_point_count() const;
324
void set_point_count(int p_count);
325
void add_point(const Vector3 &p_position, const Vector3 &p_in = Vector3(), const Vector3 &p_out = Vector3(), int p_atpos = -1);
326
void set_point_position(int p_index, const Vector3 &p_position);
327
Vector3 get_point_position(int p_index) const;
328
void set_point_tilt(int p_index, real_t p_tilt);
329
real_t get_point_tilt(int p_index) const;
330
void set_point_in(int p_index, const Vector3 &p_in);
331
Vector3 get_point_in(int p_index) const;
332
void set_point_out(int p_index, const Vector3 &p_out);
333
Vector3 get_point_out(int p_index) const;
334
void remove_point(int p_index);
335
void clear_points();
336
337
Vector3 sample(int p_index, real_t p_offset) const;
338
Vector3 samplef(real_t p_findex) const;
339
340
void set_closed(bool p_closed);
341
bool is_closed() const;
342
void set_bake_interval(real_t p_tolerance);
343
real_t get_bake_interval() const;
344
void set_up_vector_enabled(bool p_enable);
345
bool is_up_vector_enabled() const;
346
347
real_t get_baked_length() const;
348
Vector3 sample_baked(real_t p_offset, bool p_cubic = false) const;
349
Transform3D sample_baked_with_rotation(real_t p_offset, bool p_cubic = false, bool p_apply_tilt = false) const;
350
real_t sample_baked_tilt(real_t p_offset) const;
351
Vector3 sample_baked_up_vector(real_t p_offset, bool p_apply_tilt = false) const;
352
PackedVector3Array get_baked_points() const; // Useful for going through.
353
Vector<real_t> get_baked_tilts() const; //useful for going through
354
PackedVector3Array get_baked_up_vectors() const;
355
Vector3 get_closest_point(const Vector3 &p_to_point) const;
356
real_t get_closest_offset(const Vector3 &p_to_point) const;
357
PackedVector3Array get_points() const;
358
359
PackedVector3Array tessellate(int p_max_stages = 5, real_t p_tolerance = 4) const; // Useful for display.
360
PackedVector3Array tessellate_even_length(int p_max_stages = 5, real_t p_length = 0.2) const; // Useful for baking.
361
};
362
363