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