2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* curve.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "curve.h"
|
2017-08-27 21:07:15 +02:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/core_string_names.h"
|
2022-08-07 12:29:12 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-06-26 23:39:35 +02:00
|
|
|
const char *Curve::SIGNAL_RANGE_CHANGED = "range_changed";
|
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
Curve::Curve() {
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:12:22 +01:00
|
|
|
void Curve::set_point_count(int p_count) {
|
|
|
|
ERR_FAIL_COND(p_count < 0);
|
|
|
|
if (_points.size() >= p_count) {
|
|
|
|
_points.resize(p_count);
|
|
|
|
mark_dirty();
|
|
|
|
} else {
|
|
|
|
for (int i = p_count - _points.size(); i > 0; i--) {
|
2022-06-23 20:00:05 +02:00
|
|
|
_add_point(Vector2());
|
2022-02-12 19:12:22 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-23 20:00:05 +02:00
|
|
|
notify_property_list_changed();
|
2022-02-12 19:12:22 +01:00
|
|
|
}
|
|
|
|
|
2022-06-22 14:17:28 +02:00
|
|
|
int Curve::_add_point(Vector2 p_position, real_t p_left_tangent, real_t p_right_tangent, TangentMode p_left_mode, TangentMode p_right_mode) {
|
2017-04-30 16:27:10 +02:00
|
|
|
// Add a point and preserve order
|
|
|
|
|
|
|
|
// Curve bounds is in 0..1
|
2021-10-15 22:32:26 +02:00
|
|
|
if (p_position.x > MAX_X) {
|
|
|
|
p_position.x = MAX_X;
|
|
|
|
} else if (p_position.x < MIN_X) {
|
|
|
|
p_position.x = MIN_X;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (_points.size() == 0) {
|
2021-10-15 22:32:26 +02:00
|
|
|
_points.push_back(Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
|
2017-04-30 16:27:10 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
} else if (_points.size() == 1) {
|
|
|
|
// TODO Is the `else` able to handle this block already?
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t diff = p_position.x - _points[0].position.x;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
if (diff > 0) {
|
2021-10-15 22:32:26 +02:00
|
|
|
_points.push_back(Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
|
2017-04-30 16:27:10 +02:00
|
|
|
ret = 1;
|
|
|
|
} else {
|
2021-10-15 22:32:26 +02:00
|
|
|
_points.insert(0, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
|
2017-04-30 16:27:10 +02:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2021-10-15 22:32:26 +02:00
|
|
|
int i = get_index(p_position.x);
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
if (i == 0 && p_position.x < _points[0].position.x) {
|
2017-04-30 16:27:10 +02:00
|
|
|
// Insert before anything else
|
2021-10-15 22:32:26 +02:00
|
|
|
_points.insert(0, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
|
2017-04-30 16:27:10 +02:00
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
// Insert between i and i+1
|
|
|
|
++i;
|
2021-10-15 22:32:26 +02:00
|
|
|
_points.insert(i, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
|
2017-04-30 16:27:10 +02:00
|
|
|
ret = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 05:41:43 +02:00
|
|
|
update_auto_tangents(ret);
|
2017-06-28 02:42:38 +02:00
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
mark_dirty();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-22 14:17:28 +02:00
|
|
|
int Curve::add_point(Vector2 p_position, real_t p_left_tangent, real_t p_right_tangent, TangentMode p_left_mode, TangentMode p_right_mode) {
|
|
|
|
int ret = _add_point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode);
|
|
|
|
notify_property_list_changed();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
int Curve::get_index(real_t p_offset) const {
|
2017-04-30 16:27:10 +02:00
|
|
|
// Lower-bound float binary search
|
|
|
|
|
|
|
|
int imin = 0;
|
|
|
|
int imax = _points.size() - 1;
|
|
|
|
|
|
|
|
while (imax - imin > 1) {
|
|
|
|
int m = (imin + imax) / 2;
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t a = _points[m].position.x;
|
|
|
|
real_t b = _points[m + 1].position.x;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
if (a < p_offset && b < p_offset) {
|
2017-04-30 16:27:10 +02:00
|
|
|
imin = m;
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
} else if (a > p_offset) {
|
2017-04-30 16:27:10 +02:00
|
|
|
imax = m;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Will happen if the offset is out of bounds
|
2021-10-15 22:32:26 +02:00
|
|
|
if (p_offset > _points[imax].position.x) {
|
2017-04-30 16:27:10 +02:00
|
|
|
return imax;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-04-30 16:27:10 +02:00
|
|
|
return imin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curve::clean_dupes() {
|
|
|
|
bool dirty = false;
|
|
|
|
|
|
|
|
for (int i = 1; i < _points.size(); ++i) {
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t diff = _points[i - 1].position.x - _points[i].position.x;
|
2017-04-30 16:27:10 +02:00
|
|
|
if (diff <= CMP_EPSILON) {
|
2021-07-04 00:17:03 +02:00
|
|
|
_points.remove_at(i);
|
2017-04-30 16:27:10 +02:00
|
|
|
--i;
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (dirty) {
|
2017-04-30 16:27:10 +02:00
|
|
|
mark_dirty();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::set_point_left_tangent(int p_index, real_t p_tangent) {
|
|
|
|
ERR_FAIL_INDEX(p_index, _points.size());
|
|
|
|
_points.write[p_index].left_tangent = p_tangent;
|
|
|
|
_points.write[p_index].left_mode = TANGENT_FREE;
|
2017-04-30 16:27:10 +02:00
|
|
|
mark_dirty();
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::set_point_right_tangent(int p_index, real_t p_tangent) {
|
|
|
|
ERR_FAIL_INDEX(p_index, _points.size());
|
|
|
|
_points.write[p_index].right_tangent = p_tangent;
|
|
|
|
_points.write[p_index].right_mode = TANGENT_FREE;
|
2017-06-26 23:39:35 +02:00
|
|
|
mark_dirty();
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::set_point_left_mode(int p_index, TangentMode p_mode) {
|
|
|
|
ERR_FAIL_INDEX(p_index, _points.size());
|
|
|
|
_points.write[p_index].left_mode = p_mode;
|
|
|
|
if (p_index > 0) {
|
2017-06-26 23:39:35 +02:00
|
|
|
if (p_mode == TANGENT_LINEAR) {
|
2021-10-15 22:32:26 +02:00
|
|
|
Vector2 v = (_points[p_index - 1].position - _points[p_index].position).normalized();
|
|
|
|
_points.write[p_index].left_tangent = v.y / v.x;
|
2017-06-26 23:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mark_dirty();
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::set_point_right_mode(int p_index, TangentMode p_mode) {
|
|
|
|
ERR_FAIL_INDEX(p_index, _points.size());
|
|
|
|
_points.write[p_index].right_mode = p_mode;
|
|
|
|
if (p_index + 1 < _points.size()) {
|
2017-06-26 23:39:35 +02:00
|
|
|
if (p_mode == TANGENT_LINEAR) {
|
2021-10-15 22:32:26 +02:00
|
|
|
Vector2 v = (_points[p_index + 1].position - _points[p_index].position).normalized();
|
|
|
|
_points.write[p_index].right_tangent = v.y / v.x;
|
2017-06-26 23:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 16:27:10 +02:00
|
|
|
mark_dirty();
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve::get_point_left_tangent(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, _points.size(), 0);
|
|
|
|
return _points[p_index].left_tangent;
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve::get_point_right_tangent(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, _points.size(), 0);
|
|
|
|
return _points[p_index].right_tangent;
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
Curve::TangentMode Curve::get_point_left_mode(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, _points.size(), TANGENT_FREE);
|
|
|
|
return _points[p_index].left_mode;
|
2017-06-26 23:39:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
Curve::TangentMode Curve::get_point_right_mode(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, _points.size(), TANGENT_FREE);
|
|
|
|
return _points[p_index].right_mode;
|
2017-06-26 23:39:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 14:17:28 +02:00
|
|
|
void Curve::_remove_point(int p_index) {
|
2017-04-30 16:27:10 +02:00
|
|
|
ERR_FAIL_INDEX(p_index, _points.size());
|
2021-07-04 00:17:03 +02:00
|
|
|
_points.remove_at(p_index);
|
2017-04-30 16:27:10 +02:00
|
|
|
mark_dirty();
|
|
|
|
}
|
|
|
|
|
2022-06-22 14:17:28 +02:00
|
|
|
void Curve::remove_point(int p_index) {
|
|
|
|
_remove_point(p_index);
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
void Curve::clear_points() {
|
|
|
|
_points.clear();
|
|
|
|
mark_dirty();
|
2022-06-22 14:17:28 +02:00
|
|
|
notify_property_list_changed();
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::set_point_value(int p_index, real_t p_position) {
|
2017-04-30 16:27:10 +02:00
|
|
|
ERR_FAIL_INDEX(p_index, _points.size());
|
2021-10-15 22:32:26 +02:00
|
|
|
_points.write[p_index].position.y = p_position;
|
2017-06-26 23:39:35 +02:00
|
|
|
update_auto_tangents(p_index);
|
2017-04-30 16:27:10 +02:00
|
|
|
mark_dirty();
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
int Curve::set_point_offset(int p_index, real_t p_offset) {
|
2017-04-30 16:27:10 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_index, _points.size(), -1);
|
|
|
|
Point p = _points[p_index];
|
2022-06-22 14:17:28 +02:00
|
|
|
_remove_point(p_index);
|
|
|
|
int i = _add_point(Vector2(p_offset, p.position.y));
|
2018-07-25 03:11:03 +02:00
|
|
|
_points.write[i].left_tangent = p.left_tangent;
|
|
|
|
_points.write[i].right_tangent = p.right_tangent;
|
|
|
|
_points.write[i].left_mode = p.left_mode;
|
|
|
|
_points.write[i].right_mode = p.right_mode;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_index != i) {
|
2017-06-26 23:39:35 +02:00
|
|
|
update_auto_tangents(p_index);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-06-26 23:39:35 +02:00
|
|
|
update_auto_tangents(i);
|
2017-04-30 16:27:10 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
Vector2 Curve::get_point_position(int p_index) const {
|
2017-04-30 16:27:10 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_index, _points.size(), Vector2(0, 0));
|
2021-10-15 22:32:26 +02:00
|
|
|
return _points[p_index].position;
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 02:42:38 +02:00
|
|
|
Curve::Point Curve::get_point(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, _points.size(), Point());
|
|
|
|
return _points[p_index];
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::update_auto_tangents(int p_index) {
|
|
|
|
Point &p = _points.write[p_index];
|
2017-06-26 23:39:35 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
if (p_index > 0) {
|
2017-06-26 23:39:35 +02:00
|
|
|
if (p.left_mode == TANGENT_LINEAR) {
|
2021-10-15 22:32:26 +02:00
|
|
|
Vector2 v = (_points[p_index - 1].position - p.position).normalized();
|
2017-06-26 23:39:35 +02:00
|
|
|
p.left_tangent = v.y / v.x;
|
|
|
|
}
|
2021-10-15 22:32:26 +02:00
|
|
|
if (_points[p_index - 1].right_mode == TANGENT_LINEAR) {
|
|
|
|
Vector2 v = (_points[p_index - 1].position - p.position).normalized();
|
|
|
|
_points.write[p_index - 1].right_tangent = v.y / v.x;
|
2017-06-26 23:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
if (p_index + 1 < _points.size()) {
|
2019-04-08 11:03:37 +02:00
|
|
|
if (p.right_mode == TANGENT_LINEAR) {
|
2021-10-15 22:32:26 +02:00
|
|
|
Vector2 v = (_points[p_index + 1].position - p.position).normalized();
|
2017-06-26 23:39:35 +02:00
|
|
|
p.right_tangent = v.y / v.x;
|
|
|
|
}
|
2021-10-15 22:32:26 +02:00
|
|
|
if (_points[p_index + 1].left_mode == TANGENT_LINEAR) {
|
|
|
|
Vector2 v = (_points[p_index + 1].position - p.position).normalized();
|
|
|
|
_points.write[p_index + 1].left_tangent = v.y / v.x;
|
2017-06-26 23:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MIN_Y_RANGE 0.01
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::set_min_value(real_t p_min) {
|
2019-06-21 16:06:59 +02:00
|
|
|
if (_minmax_set_once & 0b11 && p_min > _max_value - MIN_Y_RANGE) {
|
2017-06-26 23:39:35 +02:00
|
|
|
_min_value = _max_value - MIN_Y_RANGE;
|
2019-06-21 16:06:59 +02:00
|
|
|
} else {
|
|
|
|
_minmax_set_once |= 0b10; // first bit is "min set"
|
2017-06-26 23:39:35 +02:00
|
|
|
_min_value = p_min;
|
2019-06-21 16:06:59 +02:00
|
|
|
}
|
2017-06-26 23:39:35 +02:00
|
|
|
// Note: min and max are indicative values,
|
|
|
|
// it's still possible that existing points are out of range at this point.
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME(SIGNAL_RANGE_CHANGED));
|
2017-06-26 23:39:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::set_max_value(real_t p_max) {
|
2019-06-21 16:06:59 +02:00
|
|
|
if (_minmax_set_once & 0b11 && p_max < _min_value + MIN_Y_RANGE) {
|
2017-06-26 23:39:35 +02:00
|
|
|
_max_value = _min_value + MIN_Y_RANGE;
|
2019-06-21 16:06:59 +02:00
|
|
|
} else {
|
|
|
|
_minmax_set_once |= 0b01; // second bit is "max set"
|
2017-06-26 23:39:35 +02:00
|
|
|
_max_value = p_max;
|
2019-06-21 16:06:59 +02:00
|
|
|
}
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME(SIGNAL_RANGE_CHANGED));
|
2017-06-26 23:39:35 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
real_t Curve::sample(real_t p_offset) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_points.size() == 0) {
|
2017-04-30 16:27:10 +02:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (_points.size() == 1) {
|
2021-10-15 22:32:26 +02:00
|
|
|
return _points[0].position.y;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
int i = get_index(p_offset);
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i == _points.size() - 1) {
|
2021-10-15 22:32:26 +02:00
|
|
|
return _points[i].position.y;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t local = p_offset - _points[i].position.x;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i == 0 && local <= 0) {
|
2021-10-15 22:32:26 +02:00
|
|
|
return _points[0].position.y;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
return sample_local_nocheck(i, local);
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
real_t Curve::sample_local_nocheck(int p_index, real_t p_local_offset) const {
|
2021-10-15 22:32:26 +02:00
|
|
|
const Point a = _points[p_index];
|
|
|
|
const Point b = _points[p_index + 1];
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
/* Cubic bézier
|
2017-09-07 21:48:50 +02:00
|
|
|
*
|
|
|
|
* ac-----bc
|
|
|
|
* / \
|
|
|
|
* / \ Here with a.right_tangent > 0
|
|
|
|
* / \ and b.left_tangent < 0
|
|
|
|
* / \
|
|
|
|
* a b
|
|
|
|
*
|
|
|
|
* |-d1--|-d2--|-d3--|
|
|
|
|
*
|
|
|
|
* d1 == d2 == d3 == d / 3
|
|
|
|
*/
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
// Control points are chosen at equal distances
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t d = b.position.x - a.position.x;
|
|
|
|
if (Math::is_zero_approx(d)) {
|
|
|
|
return b.position.y;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-10-15 22:32:26 +02:00
|
|
|
p_local_offset /= d;
|
2017-04-30 16:27:10 +02:00
|
|
|
d /= 3.0;
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t yac = a.position.y + d * a.right_tangent;
|
|
|
|
real_t ybc = b.position.y - d * b.left_tangent;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2022-06-27 19:41:32 +02:00
|
|
|
real_t y = Math::bezier_interpolate(a.position.y, yac, ybc, b.position.y, p_local_offset);
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curve::mark_dirty() {
|
|
|
|
_baked_cache_dirty = true;
|
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
|
|
|
|
Array Curve::get_data() const {
|
|
|
|
Array output;
|
2017-06-28 02:42:38 +02:00
|
|
|
const unsigned int ELEMS = 5;
|
|
|
|
output.resize(_points.size() * ELEMS);
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < _points.size(); ++j) {
|
|
|
|
const Point p = _points[j];
|
2017-06-28 02:42:38 +02:00
|
|
|
int i = j * ELEMS;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
output[i] = p.position;
|
2017-04-30 16:27:10 +02:00
|
|
|
output[i + 1] = p.left_tangent;
|
|
|
|
output[i + 2] = p.right_tangent;
|
2017-06-28 02:42:38 +02:00
|
|
|
output[i + 3] = p.left_mode;
|
|
|
|
output[i + 4] = p.right_mode;
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::set_data(const Array p_input) {
|
2017-06-28 02:42:38 +02:00
|
|
|
const unsigned int ELEMS = 5;
|
2021-10-15 22:32:26 +02:00
|
|
|
ERR_FAIL_COND(p_input.size() % ELEMS != 0);
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
_points.clear();
|
|
|
|
|
|
|
|
// Validate input
|
2021-10-15 22:32:26 +02:00
|
|
|
for (int i = 0; i < p_input.size(); i += ELEMS) {
|
|
|
|
ERR_FAIL_COND(p_input[i].get_type() != Variant::VECTOR2);
|
|
|
|
ERR_FAIL_COND(!p_input[i + 1].is_num());
|
|
|
|
ERR_FAIL_COND(p_input[i + 2].get_type() != Variant::FLOAT);
|
2017-06-26 23:39:35 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
ERR_FAIL_COND(p_input[i + 3].get_type() != Variant::INT);
|
|
|
|
int left_mode = p_input[i + 3];
|
2017-06-26 23:39:35 +02:00
|
|
|
ERR_FAIL_COND(left_mode < 0 || left_mode >= TANGENT_MODE_COUNT);
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
ERR_FAIL_COND(p_input[i + 4].get_type() != Variant::INT);
|
|
|
|
int right_mode = p_input[i + 4];
|
2017-06-26 23:39:35 +02:00
|
|
|
ERR_FAIL_COND(right_mode < 0 || right_mode >= TANGENT_MODE_COUNT);
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
_points.resize(p_input.size() / ELEMS);
|
2017-04-30 16:27:10 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < _points.size(); ++j) {
|
2018-07-25 03:11:03 +02:00
|
|
|
Point &p = _points.write[j];
|
2017-06-28 02:42:38 +02:00
|
|
|
int i = j * ELEMS;
|
2017-04-30 16:27:10 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
p.position = p_input[i];
|
|
|
|
p.left_tangent = p_input[i + 1];
|
|
|
|
p.right_tangent = p_input[i + 2];
|
|
|
|
int left_mode = p_input[i + 3];
|
|
|
|
int right_mode = p_input[i + 4];
|
2017-06-26 23:39:35 +02:00
|
|
|
p.left_mode = (TangentMode)left_mode;
|
|
|
|
p.right_mode = (TangentMode)right_mode;
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mark_dirty();
|
2022-06-22 14:17:28 +02:00
|
|
|
notify_property_list_changed();
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Curve::bake() {
|
|
|
|
_baked_cache.clear();
|
|
|
|
|
|
|
|
_baked_cache.resize(_bake_resolution);
|
|
|
|
|
|
|
|
for (int i = 1; i < _bake_resolution - 1; ++i) {
|
|
|
|
real_t x = i / static_cast<real_t>(_bake_resolution);
|
2022-07-24 18:47:57 +02:00
|
|
|
real_t y = sample(x);
|
2018-07-25 03:11:03 +02:00
|
|
|
_baked_cache.write[i] = y;
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_points.size() != 0) {
|
2021-10-15 22:32:26 +02:00
|
|
|
_baked_cache.write[0] = _points[0].position.y;
|
|
|
|
_baked_cache.write[_baked_cache.size() - 1] = _points[_points.size() - 1].position.y;
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_baked_cache_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curve::set_bake_resolution(int p_resolution) {
|
|
|
|
ERR_FAIL_COND(p_resolution < 1);
|
|
|
|
ERR_FAIL_COND(p_resolution > 1000);
|
|
|
|
_bake_resolution = p_resolution;
|
|
|
|
_baked_cache_dirty = true;
|
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
real_t Curve::sample_baked(real_t p_offset) const {
|
2017-04-30 16:27:10 +02:00
|
|
|
if (_baked_cache_dirty) {
|
|
|
|
// Last-second bake if not done already
|
2021-04-27 17:43:49 +02:00
|
|
|
const_cast<Curve *>(this)->bake();
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special cases if the cache is too small
|
|
|
|
if (_baked_cache.size() == 0) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_points.size() == 0) {
|
2017-04-30 16:27:10 +02:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-10-15 22:32:26 +02:00
|
|
|
return _points[0].position.y;
|
2017-04-30 16:27:10 +02:00
|
|
|
} else if (_baked_cache.size() == 1) {
|
|
|
|
return _baked_cache[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get interpolation index
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t fi = p_offset * _baked_cache.size();
|
2017-04-30 16:27:10 +02:00
|
|
|
int i = Math::floor(fi);
|
|
|
|
if (i < 0) {
|
|
|
|
i = 0;
|
|
|
|
fi = 0;
|
|
|
|
} else if (i >= _baked_cache.size()) {
|
|
|
|
i = _baked_cache.size() - 1;
|
|
|
|
fi = 0;
|
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
// Sample
|
2017-04-30 16:27:10 +02:00
|
|
|
if (i + 1 < _baked_cache.size()) {
|
|
|
|
real_t t = fi - i;
|
|
|
|
return Math::lerp(_baked_cache[i], _baked_cache[i + 1], t);
|
|
|
|
} else {
|
|
|
|
return _baked_cache[_baked_cache.size() - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve::ensure_default_setup(real_t p_min, real_t p_max) {
|
2018-07-07 01:21:13 +02:00
|
|
|
if (_points.size() == 0 && _min_value == 0 && _max_value == 1) {
|
|
|
|
add_point(Vector2(0, 1));
|
|
|
|
add_point(Vector2(1, 1));
|
|
|
|
set_min_value(p_min);
|
|
|
|
set_max_value(p_max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:12:22 +01:00
|
|
|
bool Curve::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
Vector<String> components = String(p_name).split("/", true, 2);
|
|
|
|
if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
|
|
|
|
int point_index = components[0].trim_prefix("point_").to_int();
|
|
|
|
String property = components[1];
|
|
|
|
if (property == "position") {
|
|
|
|
Vector2 position = p_value.operator Vector2();
|
|
|
|
set_point_offset(point_index, position.x);
|
|
|
|
set_point_value(point_index, position.y);
|
|
|
|
return true;
|
|
|
|
} else if (property == "left_tangent") {
|
|
|
|
set_point_left_tangent(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
} else if (property == "left_mode") {
|
|
|
|
int mode = p_value;
|
|
|
|
set_point_left_mode(point_index, (TangentMode)mode);
|
|
|
|
return true;
|
|
|
|
} else if (property == "right_tangent") {
|
|
|
|
set_point_right_tangent(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
} else if (property == "right_mode") {
|
|
|
|
int mode = p_value;
|
|
|
|
set_point_right_mode(point_index, (TangentMode)mode);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Curve::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
Vector<String> components = String(p_name).split("/", true, 2);
|
|
|
|
if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
|
|
|
|
int point_index = components[0].trim_prefix("point_").to_int();
|
|
|
|
String property = components[1];
|
|
|
|
if (property == "position") {
|
|
|
|
r_ret = get_point_position(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "left_tangent") {
|
|
|
|
r_ret = get_point_left_tangent(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "left_mode") {
|
|
|
|
r_ret = get_point_left_mode(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "right_tangent") {
|
|
|
|
r_ret = get_point_right_tangent(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "right_mode") {
|
|
|
|
r_ret = get_point_right_mode(point_index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curve::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
for (int i = 0; i < _points.size(); i++) {
|
|
|
|
PropertyInfo pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/position", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
|
|
|
|
if (i != 0) {
|
|
|
|
pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/left_tangent", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
|
|
|
|
pi = PropertyInfo(Variant::INT, vformat("point_%d/left_mode", i), PROPERTY_HINT_ENUM, "Free,Linear");
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != _points.size() - 1) {
|
|
|
|
pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/right_tangent", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
|
|
|
|
pi = PropertyInfo(Variant::INT, vformat("point_%d/right_mode", i), PROPERTY_HINT_ENUM, "Free,Linear");
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:27:10 +02:00
|
|
|
void Curve::_bind_methods() {
|
2018-09-07 20:07:44 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_count"), &Curve::get_point_count);
|
2022-02-12 19:12:22 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve::set_point_count);
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_point", "position", "left_tangent", "right_tangent", "left_mode", "right_mode"), &Curve::add_point, DEFVAL(0), DEFVAL(0), DEFVAL(TANGENT_FREE), DEFVAL(TANGENT_FREE));
|
2017-04-30 16:27:10 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("remove_point", "index"), &Curve::remove_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_points"), &Curve::clear_points);
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_position", "index"), &Curve::get_point_position);
|
2017-06-25 13:05:39 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_value", "index", "y"), &Curve::set_point_value);
|
2017-10-02 09:39:12 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_offset", "index", "offset"), &Curve::set_point_offset);
|
2022-07-24 18:47:57 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("sample", "offset"), &Curve::sample);
|
|
|
|
ClassDB::bind_method(D_METHOD("sample_baked", "offset"), &Curve::sample_baked);
|
2017-04-30 16:27:10 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_left_tangent", "index"), &Curve::get_point_left_tangent);
|
2017-10-02 09:39:12 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_right_tangent", "index"), &Curve::get_point_right_tangent);
|
2017-06-26 23:39:35 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_left_mode", "index"), &Curve::get_point_left_mode);
|
2017-10-02 09:39:12 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_right_mode", "index"), &Curve::get_point_right_mode);
|
2017-04-30 16:27:10 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_left_tangent", "index", "tangent"), &Curve::set_point_left_tangent);
|
2017-10-02 09:39:12 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_right_tangent", "index", "tangent"), &Curve::set_point_right_tangent);
|
2017-06-26 23:39:35 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_left_mode", "index", "mode"), &Curve::set_point_left_mode);
|
2017-10-02 09:39:12 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_right_mode", "index", "mode"), &Curve::set_point_right_mode);
|
2017-06-26 23:39:35 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_min_value"), &Curve::get_min_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_min_value", "min"), &Curve::set_min_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_value"), &Curve::get_max_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_max_value", "max"), &Curve::set_max_value);
|
2017-04-30 16:27:10 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("clean_dupes"), &Curve::clean_dupes);
|
|
|
|
ClassDB::bind_method(D_METHOD("bake"), &Curve::bake);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bake_resolution"), &Curve::get_bake_resolution);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bake_resolution", "resolution"), &Curve::set_bake_resolution);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_data"), &Curve::get_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve::set_data);
|
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_min_value", "get_min_value");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_max_value", "get_max_value");
|
2017-04-30 16:27:10 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_resolution", PROPERTY_HINT_RANGE, "1,1000,1"), "set_bake_resolution", "get_bake_resolution");
|
2021-11-03 23:06:17 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
|
2022-02-12 19:12:22 +01:00
|
|
|
ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
|
2017-06-26 23:39:35 +02:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo(SIGNAL_RANGE_CHANGED));
|
2017-09-12 21:09:06 +02:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(TANGENT_FREE);
|
|
|
|
BIND_ENUM_CONSTANT(TANGENT_LINEAR);
|
|
|
|
BIND_ENUM_CONSTANT(TANGENT_MODE_COUNT);
|
2017-04-30 16:27:10 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
int Curve2D::get_point_count() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return points.size();
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2022-02-12 19:12:22 +01:00
|
|
|
void Curve2D::set_point_count(int p_count) {
|
|
|
|
ERR_FAIL_COND(p_count < 0);
|
|
|
|
if (points.size() >= p_count) {
|
|
|
|
points.resize(p_count);
|
|
|
|
mark_dirty();
|
|
|
|
} else {
|
|
|
|
for (int i = p_count - points.size(); i > 0; i--) {
|
2022-06-23 20:00:05 +02:00
|
|
|
_add_point(Vector2());
|
2022-02-12 19:12:22 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-23 20:00:05 +02:00
|
|
|
notify_property_list_changed();
|
2022-02-12 19:12:22 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
void Curve2D::_add_point(const Vector2 &p_position, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Point n;
|
2021-10-15 22:32:26 +02:00
|
|
|
n.position = p_position;
|
2014-02-10 02:10:30 +01:00
|
|
|
n.in = p_in;
|
|
|
|
n.out = p_out;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_atpos >= 0 && p_atpos < points.size()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
points.insert(p_atpos, n);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
points.push_back(n);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2014-07-06 16:49:27 +02:00
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
void Curve2D::add_point(const Vector2 &p_position, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
|
|
|
|
_add_point(p_position, p_in, p_out, p_atpos);
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve2D::set_point_position(int p_index, const Vector2 &p_position) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
points.write[p_index].position = p_position;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
Vector2 Curve2D::get_point_position(int p_index) const {
|
2014-07-06 16:49:27 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
|
2021-10-15 22:32:26 +02:00
|
|
|
return points[p_index].position;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
void Curve2D::set_point_in(int p_index, const Vector2 &p_in) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
points.write[p_index].in = p_in;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
Vector2 Curve2D::get_point_in(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
|
2014-02-10 02:10:30 +01:00
|
|
|
return points[p_index].in;
|
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
void Curve2D::set_point_out(int p_index, const Vector2 &p_out) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
points.write[p_index].out = p_out;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
Vector2 Curve2D::get_point_out(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
|
2014-02-10 02:10:30 +01:00
|
|
|
return points[p_index].out;
|
|
|
|
}
|
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
void Curve2D::_remove_point(int p_index) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
2021-07-04 00:17:03 +02:00
|
|
|
points.remove_at(p_index);
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
void Curve2D::remove_point(int p_index) {
|
|
|
|
_remove_point(p_index);
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
2016-12-03 21:35:59 +01:00
|
|
|
void Curve2D::clear_points() {
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!points.is_empty()) {
|
2016-12-03 21:35:59 +01:00
|
|
|
points.clear();
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2022-06-23 20:00:05 +02:00
|
|
|
notify_property_list_changed();
|
2016-12-03 21:35:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
Vector2 Curve2D::sample(int p_index, const real_t p_offset) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
int pc = points.size();
|
2014-07-06 16:49:27 +02:00
|
|
|
ERR_FAIL_COND_V(pc == 0, Vector2());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_index >= pc - 1) {
|
2021-10-15 22:32:26 +02:00
|
|
|
return points[pc - 1].position;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (p_index < 0) {
|
2021-10-15 22:32:26 +02:00
|
|
|
return points[0].position;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
Vector2 p0 = points[p_index].position;
|
2014-07-06 16:49:27 +02:00
|
|
|
Vector2 p1 = p0 + points[p_index].out;
|
2021-10-15 22:32:26 +02:00
|
|
|
Vector2 p3 = points[p_index + 1].position;
|
2014-07-06 16:49:27 +02:00
|
|
|
Vector2 p2 = p3 + points[p_index + 1].in;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-06-27 19:41:32 +02:00
|
|
|
return p0.bezier_interpolate(p1, p2, p3, p_offset);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
Vector2 Curve2D::samplef(real_t p_findex) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_findex < 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_findex = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (p_findex >= points.size()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_findex = points.size();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
return sample((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-12 19:11:08 +01:00
|
|
|
void Curve2D::mark_dirty() {
|
|
|
|
baked_cache_dirty = true;
|
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:17:02 +02:00
|
|
|
void Curve2D::_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 {
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t mp = p_begin + (p_end - p_begin) * 0.5;
|
2022-06-27 19:41:32 +02:00
|
|
|
Vector2 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
|
|
|
|
Vector2 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
|
|
|
|
Vector2 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
Vector2 na = (mid - beg).normalized();
|
|
|
|
Vector2 nb = (end - mid).normalized();
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t dp = na.dot(nb);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-08-13 17:45:42 +02:00
|
|
|
if (dp < Math::cos(Math::deg_to_rad(p_tol))) {
|
2014-07-06 16:49:27 +02:00
|
|
|
r_bake[mp] = mid;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
if (p_depth < p_max_depth) {
|
|
|
|
_bake_segment2d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
|
|
|
|
_bake_segment2d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
void Curve2D::_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 {
|
|
|
|
Vector2 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
|
|
|
|
Vector2 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
|
|
|
|
|
|
|
|
real_t length = beg.distance_to(end);
|
|
|
|
|
|
|
|
if (length > p_length && p_depth < p_max_depth) {
|
|
|
|
real_t mp = (p_begin + p_end) * 0.5;
|
|
|
|
Vector2 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
|
|
|
|
r_bake[mp] = mid;
|
|
|
|
|
|
|
|
_bake_segment2d_even_length(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
|
|
|
|
_bake_segment2d_even_length(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
void Curve2D::_bake() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!baked_cache_dirty) {
|
2014-07-06 16:49:27 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
baked_max_ofs = 0;
|
|
|
|
baked_cache_dirty = false;
|
|
|
|
|
|
|
|
if (points.size() == 0) {
|
2022-02-01 19:19:13 +01:00
|
|
|
baked_point_cache.clear();
|
|
|
|
baked_dist_cache.clear();
|
2022-11-24 13:43:34 +01:00
|
|
|
baked_forward_vector_cache.clear();
|
2014-07-06 16:49:27 +02:00
|
|
|
return;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
if (points.size() == 1) {
|
|
|
|
baked_point_cache.resize(1);
|
2021-10-15 22:32:26 +02:00
|
|
|
baked_point_cache.set(0, points[0].position);
|
2021-08-21 09:57:59 +02:00
|
|
|
baked_dist_cache.resize(1);
|
|
|
|
baked_dist_cache.set(0, 0.0);
|
2022-11-24 13:43:34 +01:00
|
|
|
baked_forward_vector_cache.resize(1);
|
|
|
|
baked_forward_vector_cache.set(0, Vector2(0.0, 0.1));
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
// Tesselate curve to (almost) even length segments
|
|
|
|
{
|
|
|
|
Vector<RBMap<real_t, Vector2>> midpoints = _tessellate_even_length(10, bake_interval);
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
int pc = 1;
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
pc++;
|
|
|
|
pc += midpoints[i].size();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
baked_point_cache.resize(pc);
|
|
|
|
baked_dist_cache.resize(pc);
|
|
|
|
baked_forward_vector_cache.resize(pc);
|
2021-10-07 21:36:31 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
Vector2 *bpw = baked_point_cache.ptrw();
|
|
|
|
Vector2 *bfw = baked_forward_vector_cache.ptrw();
|
2021-10-07 21:36:31 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
// Collect positions and sample tilts and tangents for each baked points.
|
|
|
|
bpw[0] = points[0].position;
|
|
|
|
bfw[0] = points[0].position.bezier_derivative(points[0].position + points[0].out, points[1].position + points[1].in, points[1].position, 0.0).normalized();
|
|
|
|
int pidx = 0;
|
2014-07-06 16:49:27 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
|
|
|
|
pidx++;
|
|
|
|
bpw[pidx] = E.value;
|
|
|
|
bfw[pidx] = points[i].position.bezier_derivative(points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, E.key).normalized();
|
|
|
|
}
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
pidx++;
|
|
|
|
bpw[pidx] = points[i + 1].position;
|
|
|
|
bfw[pidx] = points[i].position.bezier_derivative(points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, 1.0).normalized();
|
|
|
|
}
|
2014-07-06 16:49:27 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
// Recalculate the baked distances.
|
|
|
|
real_t *bdw = baked_dist_cache.ptrw();
|
|
|
|
bdw[0] = 0.0;
|
|
|
|
for (int i = 0; i < pc - 1; i++) {
|
|
|
|
bdw[i + 1] = bdw[i] + bpw[i].distance_to(bpw[i + 1]);
|
|
|
|
}
|
|
|
|
baked_max_ofs = bdw[pc - 1];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve2D::get_baked_length() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2014-07-06 16:49:27 +02:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
return baked_max_ofs;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
Curve2D::Interval Curve2D::_find_interval(real_t p_offset) const {
|
|
|
|
Interval interval = {
|
|
|
|
-1,
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
ERR_FAIL_COND_V_MSG(baked_cache_dirty, interval, "Backed cache is dirty");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
int pc = baked_point_cache.size();
|
2022-11-24 13:43:34 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(pc < 2, interval, "Less than two points in cache");
|
2014-07-06 16:49:27 +02:00
|
|
|
|
2021-10-07 21:36:31 +02:00
|
|
|
int start = 0;
|
|
|
|
int end = pc;
|
|
|
|
int idx = (end + start) / 2;
|
|
|
|
// Binary search to find baked points.
|
2021-08-21 09:57:59 +02:00
|
|
|
while (start < idx) {
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t offset = baked_dist_cache[idx];
|
2021-08-21 09:57:59 +02:00
|
|
|
if (p_offset <= offset) {
|
|
|
|
end = idx;
|
|
|
|
} else {
|
|
|
|
start = idx;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-08-21 09:57:59 +02:00
|
|
|
idx = (end + start) / 2;
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t offset_begin = baked_dist_cache[idx];
|
|
|
|
real_t offset_end = baked_dist_cache[idx + 1];
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t idx_interval = offset_end - offset_begin;
|
2022-11-24 13:43:34 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, interval, "Offset out of range.");
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
interval.idx = idx;
|
|
|
|
if (idx_interval < FLT_EPSILON) {
|
|
|
|
interval.frac = 0.5; // For a very short interval, 0.5 is a reasonable choice.
|
|
|
|
ERR_FAIL_V_MSG(interval, "Zero length interval.");
|
|
|
|
}
|
|
|
|
|
|
|
|
interval.frac = (p_offset - offset_begin) / idx_interval;
|
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 Curve2D::_sample_baked(Interval p_interval, bool p_cubic) const {
|
|
|
|
// Assuming p_interval is valid.
|
|
|
|
ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Vector2(), "Invalid interval");
|
|
|
|
|
|
|
|
int idx = p_interval.idx;
|
|
|
|
real_t frac = p_interval.frac;
|
|
|
|
|
|
|
|
const Vector2 *r = baked_point_cache.ptr();
|
|
|
|
int pc = baked_point_cache.size();
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
if (p_cubic) {
|
|
|
|
Vector2 pre = idx > 0 ? r[idx - 1] : r[idx];
|
2021-09-15 15:40:33 +02:00
|
|
|
Vector2 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
|
2014-07-06 16:49:27 +02:00
|
|
|
return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
|
|
|
|
} else {
|
2020-03-16 10:07:33 +01:00
|
|
|
return r[idx].lerp(r[idx + 1], frac);
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
Transform2D Curve2D::_sample_posture(Interval p_interval) const {
|
|
|
|
// Assuming that p_interval is valid.
|
|
|
|
ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Transform2D(), "Invalid interval");
|
2022-08-05 11:03:00 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
int idx = p_interval.idx;
|
|
|
|
real_t frac = p_interval.frac;
|
2022-08-05 11:03:00 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
Vector2 forward_begin = baked_forward_vector_cache[idx];
|
|
|
|
Vector2 forward_end = baked_forward_vector_cache[idx + 1];
|
2022-08-05 11:03:00 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
// Build frames at both ends of the interval, then interpolate.
|
|
|
|
const Vector2 forward = forward_begin.slerp(forward_end, frac).normalized();
|
|
|
|
const Vector2 side = Vector2(-forward.y, forward.x);
|
|
|
|
|
2022-12-06 13:45:17 +01:00
|
|
|
return Transform2D(side, forward, Vector2(0.0, 0.0));
|
2022-11-24 13:43:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 Curve2D::sample_baked(real_t p_offset, bool p_cubic) const {
|
|
|
|
if (baked_cache_dirty) {
|
|
|
|
_bake();
|
2022-08-05 11:03:00 +02:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
// Validate: Curve may not have baked points.
|
|
|
|
int pc = baked_point_cache.size();
|
|
|
|
ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
|
2022-08-05 11:03:00 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
if (pc == 1) {
|
|
|
|
return baked_point_cache[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
|
|
|
|
|
|
|
|
Curve2D::Interval interval = _find_interval(p_offset);
|
|
|
|
return _sample_baked(interval, p_cubic);
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform2D Curve2D::sample_baked_with_rotation(real_t p_offset, bool p_cubic) const {
|
|
|
|
if (baked_cache_dirty) {
|
|
|
|
_bake();
|
2022-08-05 11:03:00 +02:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
// Validate: Curve may not have baked points.
|
|
|
|
const int point_count = baked_point_cache.size();
|
|
|
|
ERR_FAIL_COND_V_MSG(point_count == 0, Transform2D(), "No points in Curve3D.");
|
|
|
|
|
|
|
|
if (point_count == 1) {
|
|
|
|
Transform2D t;
|
|
|
|
t.set_origin(baked_point_cache.get(0));
|
|
|
|
ERR_FAIL_V_MSG(t, "Only 1 point in Curve2D.");
|
|
|
|
}
|
|
|
|
|
|
|
|
p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
|
|
|
|
|
|
|
|
// 0. Find interval for all sampling steps.
|
|
|
|
Curve2D::Interval interval = _find_interval(p_offset);
|
|
|
|
|
|
|
|
// 1. Sample position.
|
|
|
|
Vector2 pos = _sample_baked(interval, p_cubic);
|
2022-08-05 11:03:00 +02:00
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
// 2. Sample rotation frame.
|
|
|
|
Transform2D frame = _sample_posture(interval);
|
|
|
|
frame.set_origin(pos);
|
|
|
|
|
|
|
|
return frame;
|
2022-08-05 11:03:00 +02:00
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector2Array Curve2D::get_baked_points() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2014-07-06 16:49:27 +02:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
return baked_point_cache;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve2D::set_bake_interval(real_t p_tolerance) {
|
2014-07-06 16:49:27 +02:00
|
|
|
bake_interval = p_tolerance;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve2D::get_bake_interval() const {
|
2014-07-06 16:49:27 +02:00
|
|
|
return bake_interval;
|
|
|
|
}
|
|
|
|
|
2018-04-25 01:28:17 +02:00
|
|
|
Vector2 Curve2D::get_closest_point(const Vector2 &p_to_point) const {
|
2021-10-07 21:36:31 +02:00
|
|
|
// Brute force method.
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2018-04-25 01:28:17 +02:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2021-10-07 21:36:31 +02:00
|
|
|
// Validate: Curve may not have baked points.
|
2018-04-25 01:28:17 +02:00
|
|
|
int pc = baked_point_cache.size();
|
2019-08-08 22:11:48 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pc == 1) {
|
2018-04-25 01:28:17 +02:00
|
|
|
return baked_point_cache.get(0);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
const Vector2 *r = baked_point_cache.ptr();
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
Vector2 nearest;
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t nearest_dist = -1.0f;
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < pc - 1; i++) {
|
|
|
|
Vector2 origin = r[i];
|
|
|
|
Vector2 direction = (r[i + 1] - origin) / bake_interval;
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
|
2018-04-25 01:28:17 +02:00
|
|
|
Vector2 proj = origin + direction * d;
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t dist = proj.distance_squared_to(p_to_point);
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
if (nearest_dist < 0.0f || dist < nearest_dist) {
|
|
|
|
nearest = proj;
|
|
|
|
nearest_dist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nearest;
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve2D::get_closest_offset(const Vector2 &p_to_point) const {
|
2021-10-07 21:36:31 +02:00
|
|
|
// Brute force method.
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2018-04-25 01:28:17 +02:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2021-10-07 21:36:31 +02:00
|
|
|
// Validate: Curve may not have baked points.
|
2018-04-25 01:28:17 +02:00
|
|
|
int pc = baked_point_cache.size();
|
2019-08-08 22:11:48 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve2D.");
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pc == 1) {
|
2018-04-25 01:28:17 +02:00
|
|
|
return 0.0f;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
const Vector2 *r = baked_point_cache.ptr();
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t nearest = 0.0f;
|
|
|
|
real_t nearest_dist = -1.0f;
|
|
|
|
real_t offset = 0.0f;
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < pc - 1; i++) {
|
|
|
|
Vector2 origin = r[i];
|
|
|
|
Vector2 direction = (r[i + 1] - origin) / bake_interval;
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
|
2018-04-25 01:28:17 +02:00
|
|
|
Vector2 proj = origin + direction * d;
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t dist = proj.distance_squared_to(p_to_point);
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
if (nearest_dist < 0.0f || dist < nearest_dist) {
|
|
|
|
nearest = offset + d;
|
|
|
|
nearest_dist = dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += bake_interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nearest;
|
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
Dictionary Curve2D::_get_data() const {
|
|
|
|
Dictionary dc;
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector2Array d;
|
2014-07-06 16:49:27 +02:00
|
|
|
d.resize(points.size() * 3);
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector2 *w = d.ptrw();
|
2014-07-06 16:49:27 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2014-07-06 16:49:27 +02:00
|
|
|
w[i * 3 + 0] = points[i].in;
|
|
|
|
w[i * 3 + 1] = points[i].out;
|
2021-10-15 22:32:26 +02:00
|
|
|
w[i * 3 + 2] = points[i].position;
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
dc["points"] = d;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
return dc;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
void Curve2D::_set_data(const Dictionary &p_data) {
|
|
|
|
ERR_FAIL_COND(!p_data.has("points"));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector2Array rp = p_data["points"];
|
2014-07-06 16:49:27 +02:00
|
|
|
int pc = rp.size();
|
|
|
|
ERR_FAIL_COND(pc % 3 != 0);
|
|
|
|
points.resize(pc / 3);
|
2020-02-17 22:06:54 +01:00
|
|
|
const Vector2 *r = rp.ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
points.write[i].in = r[i * 3 + 0];
|
|
|
|
points.write[i].out = r[i * 3 + 1];
|
2021-10-15 22:32:26 +02:00
|
|
|
points.write[i].position = r[i * 3 + 2];
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
mark_dirty();
|
|
|
|
notify_property_list_changed();
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
PackedVector2Array Curve2D::tessellate(int p_max_stages, real_t p_tolerance) const {
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector2Array tess;
|
2014-07-06 16:49:27 +02:00
|
|
|
|
|
|
|
if (points.size() == 0) {
|
|
|
|
return tess;
|
|
|
|
}
|
2022-05-16 18:17:02 +02:00
|
|
|
|
|
|
|
// The current implementation requires a sorted map.
|
|
|
|
Vector<RBMap<real_t, Vector2>> midpoints;
|
2014-07-06 16:49:27 +02:00
|
|
|
|
|
|
|
midpoints.resize(points.size() - 1);
|
|
|
|
|
|
|
|
int pc = 1;
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
2021-10-15 22:32:26 +02:00
|
|
|
_bake_segment2d(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_tolerance);
|
2014-07-06 16:49:27 +02:00
|
|
|
pc++;
|
|
|
|
pc += midpoints[i].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
tess.resize(pc);
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector2 *bpw = tess.ptrw();
|
2021-10-15 22:32:26 +02:00
|
|
|
bpw[0] = points[0].position;
|
2014-07-06 16:49:27 +02:00
|
|
|
int pidx = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
2021-10-15 22:32:26 +02:00
|
|
|
for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
|
2014-07-06 16:49:27 +02:00
|
|
|
pidx++;
|
2021-08-09 22:13:42 +02:00
|
|
|
bpw[pidx] = E.value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2014-07-06 16:49:27 +02:00
|
|
|
|
|
|
|
pidx++;
|
2021-10-15 22:32:26 +02:00
|
|
|
bpw[pidx] = points[i + 1].position;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
return tess;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:43:34 +01:00
|
|
|
Vector<RBMap<real_t, Vector2>> Curve2D::_tessellate_even_length(int p_max_stages, real_t p_length) const {
|
|
|
|
Vector<RBMap<real_t, Vector2>> midpoints;
|
|
|
|
ERR_FAIL_COND_V_MSG(points.size() < 2, midpoints, "Curve must have at least 2 control point");
|
|
|
|
|
|
|
|
midpoints.resize(points.size() - 1);
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
_bake_segment2d_even_length(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_length);
|
|
|
|
}
|
|
|
|
return midpoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
PackedVector2Array Curve2D::tessellate_even_length(int p_max_stages, real_t p_length) const {
|
|
|
|
PackedVector2Array tess;
|
|
|
|
|
|
|
|
Vector<RBMap<real_t, Vector2>> midpoints = _tessellate_even_length(p_max_stages, p_length);
|
|
|
|
if (midpoints.size() == 0) {
|
|
|
|
return tess;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pc = 1;
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
pc++;
|
|
|
|
pc += midpoints[i].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
tess.resize(pc);
|
|
|
|
Vector2 *bpw = tess.ptrw();
|
|
|
|
bpw[0] = points[0].position;
|
|
|
|
int pidx = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
|
|
|
|
pidx++;
|
|
|
|
bpw[pidx] = E.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
pidx++;
|
|
|
|
bpw[pidx] = points[i + 1].position;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tess;
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:12:22 +01:00
|
|
|
bool Curve2D::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
Vector<String> components = String(p_name).split("/", true, 2);
|
|
|
|
if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
|
|
|
|
int point_index = components[0].trim_prefix("point_").to_int();
|
|
|
|
String property = components[1];
|
|
|
|
if (property == "position") {
|
|
|
|
set_point_position(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
} else if (property == "in") {
|
|
|
|
set_point_in(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
} else if (property == "out") {
|
|
|
|
set_point_out(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Curve2D::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
Vector<String> components = String(p_name).split("/", true, 2);
|
|
|
|
if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
|
|
|
|
int point_index = components[0].trim_prefix("point_").to_int();
|
|
|
|
String property = components[1];
|
|
|
|
if (property == "position") {
|
|
|
|
r_ret = get_point_position(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "in") {
|
|
|
|
r_ret = get_point_in(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "out") {
|
|
|
|
r_ret = get_point_out(point_index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curve2D::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
PropertyInfo pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/position", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
|
|
|
|
if (i != 0) {
|
|
|
|
pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/in", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != points.size() - 1) {
|
|
|
|
pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/out", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
void Curve2D::_bind_methods() {
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_count"), &Curve2D::get_point_count);
|
2022-02-12 19:12:22 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve2D::set_point_count);
|
2022-08-15 16:31:23 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve2D::add_point, DEFVAL(Vector2()), DEFVAL(Vector2()), DEFVAL(-1));
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve2D::set_point_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve2D::get_point_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve2D::set_point_in);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve2D::get_point_in);
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve2D::set_point_out);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve2D::get_point_out);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve2D::remove_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_points"), &Curve2D::clear_points);
|
2022-07-24 18:47:57 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("sample", "idx", "t"), &Curve2D::sample);
|
|
|
|
ClassDB::bind_method(D_METHOD("samplef", "fofs"), &Curve2D::samplef);
|
2017-02-13 12:47:24 +01:00
|
|
|
//ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve2D::bake,DEFVAL(10));
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve2D::set_bake_interval);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve2D::get_bake_interval);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve2D::get_baked_length);
|
2022-11-24 13:43:34 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("sample_baked", "offset", "cubic"), &Curve2D::sample_baked, DEFVAL(0.0), DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("sample_baked_with_rotation", "offset", "cubic"), &Curve2D::sample_baked_with_rotation, DEFVAL(0.0), DEFVAL(false));
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve2D::get_baked_points);
|
2018-04-25 01:28:17 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve2D::get_closest_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve2D::get_closest_offset);
|
2017-07-18 21:03:34 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve2D::tessellate, DEFVAL(5), DEFVAL(4));
|
2022-11-24 13:43:34 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("tessellate_even_length", "max_stages", "tolerance_length"), &Curve2D::tessellate_even_length, DEFVAL(5), DEFVAL(20.0));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_get_data"), &Curve2D::_get_data);
|
2022-08-08 14:18:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve2D::_set_data);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
|
2021-11-03 23:06:17 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
|
2022-02-12 19:12:22 +01:00
|
|
|
ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-07 21:36:31 +02:00
|
|
|
Curve2D::Curve2D() {}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
/***********************************************************************************/
|
|
|
|
/***********************************************************************************/
|
|
|
|
/***********************************************************************************/
|
|
|
|
/***********************************************************************************/
|
|
|
|
/***********************************************************************************/
|
|
|
|
/***********************************************************************************/
|
|
|
|
|
|
|
|
int Curve3D::get_point_count() const {
|
|
|
|
return points.size();
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2022-02-12 19:12:22 +01:00
|
|
|
void Curve3D::set_point_count(int p_count) {
|
|
|
|
ERR_FAIL_COND(p_count < 0);
|
|
|
|
if (points.size() >= p_count) {
|
|
|
|
points.resize(p_count);
|
|
|
|
mark_dirty();
|
|
|
|
} else {
|
|
|
|
for (int i = p_count - points.size(); i > 0; i--) {
|
2022-06-23 20:00:05 +02:00
|
|
|
_add_point(Vector3());
|
2022-02-12 19:12:22 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-23 20:00:05 +02:00
|
|
|
notify_property_list_changed();
|
2022-02-12 19:12:22 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
void Curve3D::_add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
|
2014-07-06 16:49:27 +02:00
|
|
|
Point n;
|
2021-10-15 22:32:26 +02:00
|
|
|
n.position = p_position;
|
2014-07-06 16:49:27 +02:00
|
|
|
n.in = p_in;
|
|
|
|
n.out = p_out;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_atpos >= 0 && p_atpos < points.size()) {
|
2014-07-06 16:49:27 +02:00
|
|
|
points.insert(p_atpos, n);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-07-06 16:49:27 +02:00
|
|
|
points.push_back(n);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-07-06 16:49:27 +02:00
|
|
|
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
void Curve3D::add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
|
|
|
|
_add_point(p_position, p_in, p_out, p_atpos);
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve3D::set_point_position(int p_index, const Vector3 &p_position) {
|
2014-07-06 16:49:27 +02:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
points.write[p_index].position = p_position;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-09-10 15:37:49 +02:00
|
|
|
Vector3 Curve3D::get_point_position(int p_index) const {
|
2014-07-06 16:49:27 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
|
2021-10-15 22:32:26 +02:00
|
|
|
return points[p_index].position;
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve3D::set_point_tilt(int p_index, real_t p_tilt) {
|
2014-07-06 16:49:27 +02:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
points.write[p_index].tilt = p_tilt;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve3D::get_point_tilt(int p_index) const {
|
2014-07-06 16:49:27 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_index, points.size(), 0);
|
|
|
|
return points[p_index].tilt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curve3D::set_point_in(int p_index, const Vector3 &p_in) {
|
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
points.write[p_index].in = p_in;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-07-06 16:49:27 +02:00
|
|
|
Vector3 Curve3D::get_point_in(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
|
|
|
|
return points[p_index].in;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curve3D::set_point_out(int p_index, const Vector3 &p_out) {
|
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
points.write[p_index].out = p_out;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Curve3D::get_point_out(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
|
|
|
|
return points[p_index].out;
|
|
|
|
}
|
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
void Curve3D::_remove_point(int p_index) {
|
2014-07-06 16:49:27 +02:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
2021-07-04 00:17:03 +02:00
|
|
|
points.remove_at(p_index);
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
void Curve3D::remove_point(int p_index) {
|
|
|
|
_remove_point(p_index);
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
2016-12-03 21:35:59 +01:00
|
|
|
void Curve3D::clear_points() {
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!points.is_empty()) {
|
2016-12-03 21:35:59 +01:00
|
|
|
points.clear();
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2022-06-23 20:00:05 +02:00
|
|
|
notify_property_list_changed();
|
2016-12-03 21:35:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
Vector3 Curve3D::sample(int p_index, real_t p_offset) const {
|
2014-07-06 16:49:27 +02:00
|
|
|
int pc = points.size();
|
|
|
|
ERR_FAIL_COND_V(pc == 0, Vector3());
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_index >= pc - 1) {
|
2021-10-15 22:32:26 +02:00
|
|
|
return points[pc - 1].position;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (p_index < 0) {
|
2021-10-15 22:32:26 +02:00
|
|
|
return points[0].position;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-07-06 16:49:27 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
Vector3 p0 = points[p_index].position;
|
2014-07-06 16:49:27 +02:00
|
|
|
Vector3 p1 = p0 + points[p_index].out;
|
2021-10-15 22:32:26 +02:00
|
|
|
Vector3 p3 = points[p_index + 1].position;
|
2014-07-06 16:49:27 +02:00
|
|
|
Vector3 p2 = p3 + points[p_index + 1].in;
|
|
|
|
|
2022-06-27 19:41:32 +02:00
|
|
|
return p0.bezier_interpolate(p1, p2, p3, p_offset);
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
Vector3 Curve3D::samplef(real_t p_findex) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_findex < 0) {
|
2014-07-06 16:49:27 +02:00
|
|
|
p_findex = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (p_findex >= points.size()) {
|
2014-07-06 16:49:27 +02:00
|
|
|
p_findex = points.size();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-07-06 16:49:27 +02:00
|
|
|
|
2022-07-24 18:47:57 +02:00
|
|
|
return sample((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
|
2014-07-06 16:49:27 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-02-12 19:11:08 +01:00
|
|
|
void Curve3D::mark_dirty() {
|
|
|
|
baked_cache_dirty = true;
|
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:17:02 +02:00
|
|
|
void Curve3D::_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 {
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t mp = p_begin + (p_end - p_begin) * 0.5;
|
2022-06-27 19:41:32 +02:00
|
|
|
Vector3 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
|
|
|
|
Vector3 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
|
|
|
|
Vector3 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector3 na = (mid - beg).normalized();
|
|
|
|
Vector3 nb = (end - mid).normalized();
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t dp = na.dot(nb);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-08-13 17:45:42 +02:00
|
|
|
if (dp < Math::cos(Math::deg_to_rad(p_tol))) {
|
2014-02-10 02:10:30 +01:00
|
|
|
r_bake[mp] = mid;
|
|
|
|
}
|
|
|
|
if (p_depth < p_max_depth) {
|
|
|
|
_bake_segment3d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
|
|
|
|
_bake_segment3d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
void Curve3D::_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 {
|
|
|
|
Vector3 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
|
|
|
|
Vector3 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
|
|
|
|
|
2022-11-27 03:23:27 +01:00
|
|
|
real_t length = beg.distance_to(end);
|
2022-11-23 04:11:58 +01:00
|
|
|
|
|
|
|
if (length > p_length && p_depth < p_max_depth) {
|
|
|
|
real_t mp = (p_begin + p_end) * 0.5;
|
|
|
|
Vector3 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
|
|
|
|
r_bake[mp] = mid;
|
|
|
|
|
2022-11-27 03:23:27 +01:00
|
|
|
_bake_segment3d_even_length(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
|
|
|
|
_bake_segment3d_even_length(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
|
2022-11-23 04:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Curve3D::_bake() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!baked_cache_dirty) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
baked_max_ofs = 0;
|
|
|
|
baked_cache_dirty = false;
|
|
|
|
|
|
|
|
if (points.size() == 0) {
|
2022-02-01 19:19:13 +01:00
|
|
|
baked_point_cache.clear();
|
|
|
|
baked_tilt_cache.clear();
|
|
|
|
baked_dist_cache.clear();
|
2022-08-07 12:29:12 +02:00
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
baked_forward_vector_cache.clear();
|
2022-08-07 12:29:12 +02:00
|
|
|
baked_up_vector_cache.clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (points.size() == 1) {
|
|
|
|
baked_point_cache.resize(1);
|
2021-10-15 22:32:26 +02:00
|
|
|
baked_point_cache.set(0, points[0].position);
|
2014-02-10 02:10:30 +01:00
|
|
|
baked_tilt_cache.resize(1);
|
|
|
|
baked_tilt_cache.set(0, points[0].tilt);
|
2021-08-21 09:57:59 +02:00
|
|
|
baked_dist_cache.resize(1);
|
|
|
|
baked_dist_cache.set(0, 0.0);
|
2022-11-23 04:11:58 +01:00
|
|
|
baked_forward_vector_cache.resize(1);
|
|
|
|
baked_forward_vector_cache.set(0, Vector3(0.0, 0.0, 1.0));
|
2018-05-19 00:14:25 +02:00
|
|
|
|
|
|
|
if (up_vector_enabled) {
|
|
|
|
baked_up_vector_cache.resize(1);
|
2022-11-23 04:11:58 +01:00
|
|
|
baked_up_vector_cache.set(0, Vector3(0.0, 1.0, 0.0));
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2022-02-01 19:19:13 +01:00
|
|
|
baked_up_vector_cache.clear();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
// Step 1: Tesselate curve to (almost) even length segments
|
|
|
|
{
|
|
|
|
Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(10, bake_interval);
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
int pc = 1;
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
pc++;
|
|
|
|
pc += midpoints[i].size();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
baked_point_cache.resize(pc);
|
|
|
|
baked_tilt_cache.resize(pc);
|
|
|
|
baked_dist_cache.resize(pc);
|
|
|
|
baked_forward_vector_cache.resize(pc);
|
|
|
|
|
|
|
|
Vector3 *bpw = baked_point_cache.ptrw();
|
|
|
|
real_t *btw = baked_tilt_cache.ptrw();
|
|
|
|
Vector3 *bfw = baked_forward_vector_cache.ptrw();
|
|
|
|
|
|
|
|
// Collect positions and sample tilts and tangents for each baked points.
|
|
|
|
bpw[0] = points[0].position;
|
|
|
|
bfw[0] = points[0].position.bezier_derivative(points[0].position + points[0].out, points[1].position + points[1].in, points[1].position, 0.0).normalized();
|
|
|
|
btw[0] = points[0].tilt;
|
|
|
|
int pidx = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
|
|
|
|
pidx++;
|
|
|
|
bpw[pidx] = E.value;
|
|
|
|
bfw[pidx] = points[i].position.bezier_derivative(points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, E.key).normalized();
|
|
|
|
btw[pidx] = Math::lerp(points[i].tilt, points[i + 1].tilt, E.key);
|
|
|
|
}
|
2021-10-07 21:36:31 +02:00
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
pidx++;
|
|
|
|
bpw[pidx] = points[i + 1].position;
|
|
|
|
bfw[pidx] = points[i].position.bezier_derivative(points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, 1.0).normalized();
|
|
|
|
btw[pidx] = points[i + 1].tilt;
|
2022-08-07 12:29:12 +02:00
|
|
|
}
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
// Recalculate the baked distances.
|
|
|
|
real_t *bdw = baked_dist_cache.ptrw();
|
|
|
|
bdw[0] = 0.0;
|
|
|
|
for (int i = 0; i < pc - 1; i++) {
|
|
|
|
bdw[i + 1] = bdw[i] + bpw[i].distance_to(bpw[i + 1]);
|
2022-08-07 12:29:12 +02:00
|
|
|
}
|
2022-11-23 04:11:58 +01:00
|
|
|
baked_max_ofs = bdw[pc - 1];
|
2022-08-07 12:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!up_vector_enabled) {
|
|
|
|
baked_up_vector_cache.resize(0);
|
|
|
|
return;
|
|
|
|
}
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// Step 2: Calculate the up vectors and the whole local reference frame
|
|
|
|
//
|
|
|
|
// See Dougan, Carl. "The parallel transport frame." Game Programming Gems 2 (2001): 215-219.
|
|
|
|
// for an example discussing about why not the Frenet frame.
|
|
|
|
{
|
2022-11-23 04:11:58 +01:00
|
|
|
int point_count = baked_point_cache.size();
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
baked_up_vector_cache.resize(point_count);
|
|
|
|
Vector3 *up_write = baked_up_vector_cache.ptrw();
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
const Vector3 *forward_ptr = baked_forward_vector_cache.ptr();
|
2022-08-07 12:29:12 +02:00
|
|
|
const Vector3 *points_ptr = baked_point_cache.ptr();
|
|
|
|
|
|
|
|
Basis frame; // X-right, Y-up, Z-forward.
|
|
|
|
Basis frame_prev;
|
|
|
|
|
|
|
|
// Set the initial frame based on Y-up rule.
|
|
|
|
{
|
2022-11-23 04:11:58 +01:00
|
|
|
Vector3 forward = forward_ptr[0];
|
2022-08-07 12:29:12 +02:00
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
if (abs(forward.dot(Vector3(0, 1, 0))) > 1.0 - UNIT_EPSILON) {
|
2022-08-07 12:29:12 +02:00
|
|
|
frame_prev = Basis::looking_at(-forward, Vector3(1, 0, 0));
|
2022-11-23 04:11:58 +01:00
|
|
|
} else {
|
|
|
|
frame_prev = Basis::looking_at(-forward, Vector3(0, 1, 0));
|
2022-08-07 12:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
up_write[0] = frame_prev.get_column(1);
|
2018-05-19 00:14:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// Calculate the Parallel Transport Frame.
|
|
|
|
for (int idx = 1; idx < point_count; idx++) {
|
2022-11-23 04:11:58 +01:00
|
|
|
Vector3 forward = forward_ptr[idx];
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
Basis rotate;
|
|
|
|
rotate.rotate_to_align(frame_prev.get_column(2), forward);
|
|
|
|
frame = rotate * frame_prev;
|
|
|
|
frame.orthonormalize(); // guard against float error accumulation
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
up_write[idx] = frame.get_column(1);
|
|
|
|
frame_prev = frame;
|
2018-05-19 00:14:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
bool is_loop = true;
|
|
|
|
// Loop smoothing only applies when the curve is a loop, which means two ends meet, and share forward directions.
|
|
|
|
{
|
|
|
|
if (!points_ptr[0].is_equal_approx(points_ptr[point_count - 1])) {
|
|
|
|
is_loop = false;
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
real_t dot = forward_ptr[0].dot(forward_ptr[point_count - 1]);
|
|
|
|
if (dot < 1.0 - UNIT_EPSILON) { // Alignment should not be too tight, or it dosen't work for coarse bake interval.
|
2022-08-07 12:29:12 +02:00
|
|
|
is_loop = false;
|
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// Twist up vectors, so that they align at two ends of the curve.
|
|
|
|
if (is_loop) {
|
|
|
|
const Vector3 up_start = up_write[0];
|
|
|
|
const Vector3 up_end = up_write[point_count - 1];
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
real_t sign = SIGN(up_end.cross(up_start).dot(forward_ptr[0]));
|
2022-08-07 12:29:12 +02:00
|
|
|
real_t full_angle = Quaternion(up_end, up_start).get_angle();
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
if (abs(full_angle) < CMP_EPSILON) {
|
2022-08-07 12:29:12 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
const real_t *dists = baked_dist_cache.ptr();
|
|
|
|
for (int idx = 1; idx < point_count; idx++) {
|
|
|
|
const real_t frac = dists[idx] / baked_max_ofs;
|
|
|
|
const real_t angle = Math::lerp((real_t)0.0, full_angle, frac);
|
2022-11-23 04:11:58 +01:00
|
|
|
Basis twist(forward_ptr[idx] * sign, angle);
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
up_write[idx] = twist.xform(up_write[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve3D::get_baked_length() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return baked_max_ofs;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
Curve3D::Interval Curve3D::_find_interval(real_t p_offset) const {
|
|
|
|
Interval interval = {
|
|
|
|
-1,
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
ERR_FAIL_COND_V_MSG(baked_cache_dirty, interval, "Backed cache is dirty");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int pc = baked_point_cache.size();
|
2022-08-07 12:29:12 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(pc < 2, interval, "Less than two points in cache");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-07 21:36:31 +02:00
|
|
|
int start = 0;
|
|
|
|
int end = pc;
|
|
|
|
int idx = (end + start) / 2;
|
|
|
|
// Binary search to find baked points.
|
2021-08-21 09:57:59 +02:00
|
|
|
while (start < idx) {
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t offset = baked_dist_cache[idx];
|
2021-08-21 09:57:59 +02:00
|
|
|
if (p_offset <= offset) {
|
|
|
|
end = idx;
|
|
|
|
} else {
|
|
|
|
start = idx;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-08-21 09:57:59 +02:00
|
|
|
idx = (end + start) / 2;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t offset_begin = baked_dist_cache[idx];
|
|
|
|
real_t offset_end = baked_dist_cache[idx + 1];
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t idx_interval = offset_end - offset_begin;
|
2022-08-07 12:29:12 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, interval, "Offset out of range.");
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
interval.idx = idx;
|
|
|
|
if (idx_interval < FLT_EPSILON) {
|
|
|
|
interval.frac = 0.5; // For a very short interval, 0.5 is a reasonable choice.
|
|
|
|
ERR_FAIL_V_MSG(interval, "Zero length interval.");
|
|
|
|
}
|
|
|
|
|
|
|
|
interval.frac = (p_offset - offset_begin) / idx_interval;
|
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Curve3D::_sample_baked(Interval p_interval, bool p_cubic) const {
|
|
|
|
// Assuming p_interval is valid.
|
|
|
|
ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Vector3(), "Invalid interval");
|
|
|
|
|
|
|
|
int idx = p_interval.idx;
|
|
|
|
real_t frac = p_interval.frac;
|
|
|
|
|
|
|
|
const Vector3 *r = baked_point_cache.ptr();
|
|
|
|
int pc = baked_point_cache.size();
|
2021-08-21 09:57:59 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (p_cubic) {
|
|
|
|
Vector3 pre = idx > 0 ? r[idx - 1] : r[idx];
|
2021-09-15 15:40:33 +02:00
|
|
|
Vector3 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
|
2014-02-10 02:10:30 +01:00
|
|
|
return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
|
|
|
|
} else {
|
2020-03-16 10:07:33 +01:00
|
|
|
return r[idx].lerp(r[idx + 1], frac);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
real_t Curve3D::_sample_baked_tilt(Interval p_interval) const {
|
|
|
|
// Assuming that p_interval is valid.
|
|
|
|
ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_tilt_cache.size(), 0.0, "Invalid interval");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
int idx = p_interval.idx;
|
|
|
|
real_t frac = p_interval.frac;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
const real_t *r = baked_tilt_cache.ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
return Math::lerp(r[idx], r[idx + 1], frac);
|
|
|
|
}
|
|
|
|
|
|
|
|
Basis Curve3D::_sample_posture(Interval p_interval, bool p_apply_tilt) const {
|
|
|
|
// Assuming that p_interval is valid.
|
|
|
|
ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Basis(), "Invalid interval");
|
|
|
|
if (up_vector_enabled) {
|
|
|
|
ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_up_vector_cache.size(), Basis(), "Invalid interval");
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2022-08-07 12:29:12 +02:00
|
|
|
|
|
|
|
int idx = p_interval.idx;
|
|
|
|
real_t frac = p_interval.frac;
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
Vector3 forward_begin = baked_forward_vector_cache[idx];
|
|
|
|
Vector3 forward_end = baked_forward_vector_cache[idx + 1];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
Vector3 up_begin;
|
|
|
|
Vector3 up_end;
|
|
|
|
if (up_vector_enabled) {
|
2022-11-23 04:11:58 +01:00
|
|
|
up_begin = baked_up_vector_cache[idx];
|
|
|
|
up_end = baked_up_vector_cache[idx + 1];
|
2022-08-07 12:29:12 +02:00
|
|
|
} else {
|
|
|
|
up_begin = Vector3(0.0, 1.0, 0.0);
|
|
|
|
up_end = Vector3(0.0, 1.0, 0.0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// Build frames at both ends of the interval, then interpolate.
|
|
|
|
const Basis frame_begin = Basis::looking_at(-forward_begin, up_begin);
|
|
|
|
const Basis frame_end = Basis::looking_at(-forward_end, up_end);
|
|
|
|
const Basis frame = frame_begin.slerp(frame_end, frac).orthonormalized();
|
2021-09-15 15:40:33 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
if (!p_apply_tilt) {
|
|
|
|
return frame;
|
|
|
|
}
|
2021-09-15 15:40:33 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// Applying tilt.
|
|
|
|
const real_t tilt = _sample_baked_tilt(p_interval);
|
|
|
|
Vector3 forward = frame.get_column(2);
|
2021-09-15 15:40:33 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
const Basis twist(forward, tilt);
|
|
|
|
return twist * frame;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
Vector3 Curve3D::sample_baked(real_t p_offset, bool p_cubic) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2018-05-19 00:14:25 +02:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// Validate: Curve may not have baked points.
|
|
|
|
int pc = baked_point_cache.size();
|
|
|
|
ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
if (pc == 1) {
|
|
|
|
return baked_point_cache[0];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
Curve3D::Interval interval = _find_interval(p_offset);
|
|
|
|
return _sample_baked(interval, p_cubic);
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform3D Curve3D::sample_baked_with_rotation(real_t p_offset, bool p_cubic, bool p_apply_tilt) const {
|
|
|
|
if (baked_cache_dirty) {
|
|
|
|
_bake();
|
2021-09-15 15:40:33 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// Validate: Curve may not have baked points.
|
|
|
|
const int point_count = baked_point_cache.size();
|
|
|
|
ERR_FAIL_COND_V_MSG(point_count == 0, Transform3D(), "No points in Curve3D.");
|
|
|
|
|
|
|
|
if (point_count == 1) {
|
|
|
|
Transform3D t;
|
|
|
|
t.origin = baked_point_cache.get(0);
|
|
|
|
ERR_FAIL_V_MSG(t, "Only 1 point in Curve3D.");
|
2022-10-06 00:27:46 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
|
2021-09-15 15:40:33 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// 0. Find interval for all sampling steps.
|
|
|
|
Curve3D::Interval interval = _find_interval(p_offset);
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// 1. Sample position.
|
|
|
|
Vector3 pos = _sample_baked(interval, p_cubic);
|
|
|
|
|
|
|
|
// 2. Sample rotation frame.
|
|
|
|
Basis frame = _sample_posture(interval, p_apply_tilt);
|
|
|
|
|
|
|
|
return Transform3D(frame, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t Curve3D::sample_baked_tilt(real_t p_offset) const {
|
|
|
|
if (baked_cache_dirty) {
|
|
|
|
_bake();
|
|
|
|
}
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
// Validate: Curve may not have baked tilts.
|
|
|
|
int pc = baked_tilt_cache.size();
|
|
|
|
ERR_FAIL_COND_V_MSG(pc == 0, 0, "No tilts in Curve3D.");
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
if (pc == 1) {
|
|
|
|
return baked_tilt_cache.get(0);
|
2018-05-19 00:14:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
Curve3D::Interval interval = _find_interval(p_offset);
|
|
|
|
return _sample_baked_tilt(interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Curve3D::sample_baked_up_vector(real_t p_offset, bool p_apply_tilt) const {
|
|
|
|
if (baked_cache_dirty) {
|
|
|
|
_bake();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate: Curve may not have baked up vectors.
|
|
|
|
ERR_FAIL_COND_V_MSG(!up_vector_enabled, Vector3(0, 1, 0), "No up vectors in Curve3D.");
|
|
|
|
|
|
|
|
int count = baked_up_vector_cache.size();
|
|
|
|
if (count == 1) {
|
|
|
|
return baked_up_vector_cache.get(0);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-19 00:14:25 +02:00
|
|
|
|
2022-08-07 12:29:12 +02:00
|
|
|
p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
|
|
|
|
|
|
|
|
Curve3D::Interval interval = _find_interval(p_offset);
|
|
|
|
return _sample_posture(interval, p_apply_tilt).get_column(1);
|
2018-05-19 00:14:25 +02:00
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector3Array Curve3D::get_baked_points() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return baked_point_cache;
|
|
|
|
}
|
|
|
|
|
2021-08-10 00:15:17 +02:00
|
|
|
Vector<real_t> Curve3D::get_baked_tilts() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return baked_tilt_cache;
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector3Array Curve3D::get_baked_up_vectors() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2018-05-19 00:14:25 +02:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-19 00:14:25 +02:00
|
|
|
|
|
|
|
return baked_up_vector_cache;
|
|
|
|
}
|
|
|
|
|
2018-04-25 01:28:17 +02:00
|
|
|
Vector3 Curve3D::get_closest_point(const Vector3 &p_to_point) const {
|
2021-10-07 21:36:31 +02:00
|
|
|
// Brute force method.
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2018-04-25 01:28:17 +02:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2021-10-07 21:36:31 +02:00
|
|
|
// Validate: Curve may not have baked points.
|
2018-04-25 01:28:17 +02:00
|
|
|
int pc = baked_point_cache.size();
|
2019-08-08 22:11:48 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pc == 1) {
|
2018-04-25 01:28:17 +02:00
|
|
|
return baked_point_cache.get(0);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
const Vector3 *r = baked_point_cache.ptr();
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
Vector3 nearest;
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t nearest_dist = -1.0f;
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < pc - 1; i++) {
|
2022-11-27 03:23:27 +01:00
|
|
|
const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
|
2018-04-25 01:28:17 +02:00
|
|
|
Vector3 origin = r[i];
|
2022-11-27 03:23:27 +01:00
|
|
|
Vector3 direction = (r[i + 1] - origin) / interval;
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2022-11-27 03:23:27 +01:00
|
|
|
real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
|
2018-04-25 01:28:17 +02:00
|
|
|
Vector3 proj = origin + direction * d;
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t dist = proj.distance_squared_to(p_to_point);
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
if (nearest_dist < 0.0f || dist < nearest_dist) {
|
|
|
|
nearest = proj;
|
|
|
|
nearest_dist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nearest;
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve3D::get_closest_offset(const Vector3 &p_to_point) const {
|
2021-10-07 21:36:31 +02:00
|
|
|
// Brute force method.
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (baked_cache_dirty) {
|
2018-04-25 01:28:17 +02:00
|
|
|
_bake();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2021-10-07 21:36:31 +02:00
|
|
|
// Validate: Curve may not have baked points.
|
2018-04-25 01:28:17 +02:00
|
|
|
int pc = baked_point_cache.size();
|
2019-08-08 22:11:48 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve3D.");
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pc == 1) {
|
2018-04-25 01:28:17 +02:00
|
|
|
return 0.0f;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
const Vector3 *r = baked_point_cache.ptr();
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t nearest = 0.0f;
|
|
|
|
real_t nearest_dist = -1.0f;
|
2022-11-27 03:23:27 +01:00
|
|
|
real_t offset;
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < pc - 1; i++) {
|
2022-11-27 03:23:27 +01:00
|
|
|
offset = baked_dist_cache[i];
|
|
|
|
|
|
|
|
const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
|
2018-04-25 01:28:17 +02:00
|
|
|
Vector3 origin = r[i];
|
2022-11-27 03:23:27 +01:00
|
|
|
Vector3 direction = (r[i + 1] - origin) / interval;
|
2018-04-25 01:28:17 +02:00
|
|
|
|
2022-11-27 03:23:27 +01:00
|
|
|
real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
|
2018-04-25 01:28:17 +02:00
|
|
|
Vector3 proj = origin + direction * d;
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t dist = proj.distance_squared_to(p_to_point);
|
2018-04-25 01:28:17 +02:00
|
|
|
|
|
|
|
if (nearest_dist < 0.0f || dist < nearest_dist) {
|
|
|
|
nearest = offset + d;
|
|
|
|
nearest_dist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nearest;
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
void Curve3D::set_bake_interval(real_t p_tolerance) {
|
2014-02-10 02:10:30 +01:00
|
|
|
bake_interval = p_tolerance;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
real_t Curve3D::get_bake_interval() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return bake_interval;
|
|
|
|
}
|
|
|
|
|
2018-05-19 00:14:25 +02:00
|
|
|
void Curve3D::set_up_vector_enabled(bool p_enable) {
|
|
|
|
up_vector_enabled = p_enable;
|
2022-02-12 19:11:08 +01:00
|
|
|
mark_dirty();
|
2018-05-19 00:14:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Curve3D::is_up_vector_enabled() const {
|
|
|
|
return up_vector_enabled;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Dictionary Curve3D::_get_data() const {
|
|
|
|
Dictionary dc;
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector3Array d;
|
2014-02-10 02:10:30 +01:00
|
|
|
d.resize(points.size() * 3);
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector3 *w = d.ptrw();
|
2021-08-10 00:15:17 +02:00
|
|
|
Vector<real_t> t;
|
2014-02-10 02:10:30 +01:00
|
|
|
t.resize(points.size());
|
2020-02-17 22:06:54 +01:00
|
|
|
real_t *wt = t.ptrw();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
w[i * 3 + 0] = points[i].in;
|
|
|
|
w[i * 3 + 1] = points[i].out;
|
2021-10-15 22:32:26 +02:00
|
|
|
w[i * 3 + 2] = points[i].position;
|
2014-02-10 02:10:30 +01:00
|
|
|
wt[i] = points[i].tilt;
|
|
|
|
}
|
|
|
|
|
|
|
|
dc["points"] = d;
|
|
|
|
dc["tilts"] = t;
|
|
|
|
|
|
|
|
return dc;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Curve3D::_set_data(const Dictionary &p_data) {
|
|
|
|
ERR_FAIL_COND(!p_data.has("points"));
|
|
|
|
ERR_FAIL_COND(!p_data.has("tilts"));
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector3Array rp = p_data["points"];
|
2014-02-10 02:10:30 +01:00
|
|
|
int pc = rp.size();
|
|
|
|
ERR_FAIL_COND(pc % 3 != 0);
|
|
|
|
points.resize(pc / 3);
|
2020-02-17 22:06:54 +01:00
|
|
|
const Vector3 *r = rp.ptr();
|
2021-08-10 00:15:17 +02:00
|
|
|
Vector<real_t> rtl = p_data["tilts"];
|
2020-02-17 22:06:54 +01:00
|
|
|
const real_t *rt = rtl.ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
points.write[i].in = r[i * 3 + 0];
|
|
|
|
points.write[i].out = r[i * 3 + 1];
|
2021-10-15 22:32:26 +02:00
|
|
|
points.write[i].position = r[i * 3 + 2];
|
2018-07-25 03:11:03 +02:00
|
|
|
points.write[i].tilt = rt[i];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2022-06-23 20:00:05 +02:00
|
|
|
mark_dirty();
|
|
|
|
notify_property_list_changed();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:32:26 +02:00
|
|
|
PackedVector3Array Curve3D::tessellate(int p_max_stages, real_t p_tolerance) const {
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedVector3Array tess;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (points.size() == 0) {
|
|
|
|
return tess;
|
|
|
|
}
|
2022-05-16 18:17:02 +02:00
|
|
|
Vector<RBMap<real_t, Vector3>> midpoints;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
midpoints.resize(points.size() - 1);
|
|
|
|
|
|
|
|
int pc = 1;
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
2021-10-15 22:32:26 +02:00
|
|
|
_bake_segment3d(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_tolerance);
|
2014-02-10 02:10:30 +01:00
|
|
|
pc++;
|
|
|
|
pc += midpoints[i].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
tess.resize(pc);
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector3 *bpw = tess.ptrw();
|
2021-10-15 22:32:26 +02:00
|
|
|
bpw[0] = points[0].position;
|
2014-02-10 02:10:30 +01:00
|
|
|
int pidx = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
2021-10-15 22:32:26 +02:00
|
|
|
for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
|
2014-02-10 02:10:30 +01:00
|
|
|
pidx++;
|
2021-08-09 22:13:42 +02:00
|
|
|
bpw[pidx] = E.value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pidx++;
|
2021-10-15 22:32:26 +02:00
|
|
|
bpw[pidx] = points[i + 1].position;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return tess;
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
Vector<RBMap<real_t, Vector3>> Curve3D::_tessellate_even_length(int p_max_stages, real_t p_length) const {
|
|
|
|
Vector<RBMap<real_t, Vector3>> midpoints;
|
|
|
|
ERR_FAIL_COND_V_MSG(points.size() < 2, midpoints, "Curve must have at least 2 control point");
|
|
|
|
|
|
|
|
midpoints.resize(points.size() - 1);
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
_bake_segment3d_even_length(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_length);
|
|
|
|
}
|
|
|
|
return midpoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
PackedVector3Array Curve3D::tessellate_even_length(int p_max_stages, real_t p_length) const {
|
|
|
|
PackedVector3Array tess;
|
|
|
|
|
|
|
|
Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(p_max_stages, p_length);
|
|
|
|
if (midpoints.size() == 0) {
|
|
|
|
return tess;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pc = 1;
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
pc++;
|
|
|
|
pc += midpoints[i].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
tess.resize(pc);
|
|
|
|
Vector3 *bpw = tess.ptrw();
|
|
|
|
bpw[0] = points[0].position;
|
|
|
|
int pidx = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size() - 1; i++) {
|
|
|
|
for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
|
|
|
|
pidx++;
|
|
|
|
bpw[pidx] = E.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
pidx++;
|
|
|
|
bpw[pidx] = points[i + 1].position;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tess;
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:12:22 +01:00
|
|
|
bool Curve3D::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
Vector<String> components = String(p_name).split("/", true, 2);
|
|
|
|
if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
|
|
|
|
int point_index = components[0].trim_prefix("point_").to_int();
|
|
|
|
String property = components[1];
|
|
|
|
if (property == "position") {
|
|
|
|
set_point_position(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
} else if (property == "in") {
|
|
|
|
set_point_in(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
} else if (property == "out") {
|
|
|
|
set_point_out(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
} else if (property == "tilt") {
|
|
|
|
set_point_tilt(point_index, p_value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Curve3D::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
Vector<String> components = String(p_name).split("/", true, 2);
|
|
|
|
if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
|
|
|
|
int point_index = components[0].trim_prefix("point_").to_int();
|
|
|
|
String property = components[1];
|
|
|
|
if (property == "position") {
|
|
|
|
r_ret = get_point_position(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "in") {
|
|
|
|
r_ret = get_point_in(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "out") {
|
|
|
|
r_ret = get_point_out(point_index);
|
|
|
|
return true;
|
|
|
|
} else if (property == "tilt") {
|
|
|
|
r_ret = get_point_tilt(point_index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curve3D::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
PropertyInfo pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/position", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
|
|
|
|
if (i != 0) {
|
|
|
|
pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/in", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != points.size() - 1) {
|
|
|
|
pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/out", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/tilt", i));
|
|
|
|
pi.usage &= ~PROPERTY_USAGE_STORAGE;
|
|
|
|
p_list->push_back(pi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Curve3D::_bind_methods() {
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_count"), &Curve3D::get_point_count);
|
2022-02-12 19:12:22 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve3D::set_point_count);
|
2022-08-15 16:31:23 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve3D::add_point, DEFVAL(Vector3()), DEFVAL(Vector3()), DEFVAL(-1));
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve3D::set_point_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve3D::get_point_position);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_tilt", "idx", "tilt"), &Curve3D::set_point_tilt);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_point_tilt", "idx"), &Curve3D::get_point_tilt);
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve3D::set_point_in);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve3D::get_point_in);
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve3D::set_point_out);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve3D::get_point_out);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve3D::remove_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_points"), &Curve3D::clear_points);
|
2022-07-24 18:47:57 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("sample", "idx", "t"), &Curve3D::sample);
|
|
|
|
ClassDB::bind_method(D_METHOD("samplef", "fofs"), &Curve3D::samplef);
|
2017-02-13 12:47:24 +01:00
|
|
|
//ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve3D::bake,DEFVAL(10));
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve3D::set_bake_interval);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve3D::get_bake_interval);
|
2018-05-19 00:14:25 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_up_vector_enabled", "enable"), &Curve3D::set_up_vector_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_up_vector_enabled"), &Curve3D::is_up_vector_enabled);
|
2017-02-13 12:47:24 +01:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve3D::get_baked_length);
|
2022-11-24 13:43:34 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("sample_baked", "offset", "cubic"), &Curve3D::sample_baked, DEFVAL(0.0), DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("sample_baked_with_rotation", "offset", "cubic", "apply_tilt"), &Curve3D::sample_baked_with_rotation, DEFVAL(0.0), DEFVAL(false), DEFVAL(false));
|
2022-07-24 18:47:57 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("sample_baked_up_vector", "offset", "apply_tilt"), &Curve3D::sample_baked_up_vector, DEFVAL(false));
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve3D::get_baked_points);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_baked_tilts"), &Curve3D::get_baked_tilts);
|
2018-05-19 00:14:25 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_baked_up_vectors"), &Curve3D::get_baked_up_vectors);
|
2018-04-25 01:28:17 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve3D::get_closest_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve3D::get_closest_offset);
|
2017-07-18 21:03:34 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve3D::tessellate, DEFVAL(5), DEFVAL(4));
|
2022-11-23 04:11:58 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("tessellate_even_length", "max_stages", "tolerance_length"), &Curve3D::tessellate_even_length, DEFVAL(5), DEFVAL(0.2));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_get_data"), &Curve3D::_get_data);
|
2022-08-08 14:18:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve3D::_set_data);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
|
2021-11-03 23:06:17 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
|
2022-02-12 19:12:22 +01:00
|
|
|
ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
|
2018-05-19 00:14:25 +02:00
|
|
|
|
|
|
|
ADD_GROUP("Up Vector", "up_vector_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "up_vector_enabled"), "set_up_vector_enabled", "is_up_vector_enabled");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-07 21:36:31 +02:00
|
|
|
Curve3D::Curve3D() {}
|