summaryrefslogtreecommitdiff
path: root/scene
diff options
context:
space:
mode:
Diffstat (limited to 'scene')
-rw-r--r--scene/2d/tile_map.cpp37
-rw-r--r--scene/2d/tile_map.h2
-rw-r--r--scene/animation/animation_tree.cpp16
-rw-r--r--scene/animation/animation_tree.h1
-rw-r--r--scene/gui/rich_text_label.cpp4
-rw-r--r--scene/gui/subviewport_container.cpp12
-rw-r--r--scene/gui/subviewport_container.h3
-rw-r--r--scene/gui/text_edit.cpp2
-rw-r--r--scene/main/node.cpp14
-rw-r--r--scene/main/node.h1
-rw-r--r--scene/main/viewport.cpp1
-rw-r--r--scene/resources/bit_map.cpp101
-rw-r--r--scene/resources/curve.cpp245
-rw-r--r--scene/resources/curve.h7
-rw-r--r--scene/resources/default_theme/default_theme.cpp3
-rw-r--r--scene/resources/primitive_meshes.cpp8
16 files changed, 217 insertions, 240 deletions
diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp
index 40e8818262..0159e9f313 100644
--- a/scene/2d/tile_map.cpp
+++ b/scene/2d/tile_map.cpp
@@ -1859,11 +1859,13 @@ void TileMap::_scenes_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_
while (q_list_element) {
TileMapQuadrant &q = *q_list_element->self();
- // Clear the scenes.
- for (const KeyValue<Vector2i, String> &E : q.scenes) {
- Node *node = get_node_or_null(E.value);
- if (node) {
- node->queue_free();
+ // Clear the scenes if instance cache was cleared.
+ if (instantiated_scenes.is_empty()) {
+ for (const KeyValue<Vector2i, String> &E : q.scenes) {
+ Node *node = get_node_or_null(E.value);
+ if (node) {
+ node->queue_free();
+ }
}
}
@@ -1871,6 +1873,15 @@ void TileMap::_scenes_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_
// Recreate the scenes.
for (const Vector2i &E_cell : q.cells) {
+ Vector3i cell_coords = Vector3i(q.layer, E_cell.x, E_cell.y);
+ if (instantiated_scenes.has(cell_coords)) {
+ // Skip scene if the instance was cached (to avoid recreating scenes unnecessarily).
+ continue;
+ }
+ if (!Engine::get_singleton()->is_editor_hint()) {
+ instantiated_scenes.insert(cell_coords);
+ }
+
const TileMapCell &c = get_cell(q.layer, E_cell, true);
TileSetSource *source;
@@ -1907,15 +1918,16 @@ void TileMap::_scenes_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_
}
void TileMap::_scenes_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
- // Clear the scenes.
- for (const KeyValue<Vector2i, String> &E : p_quadrant->scenes) {
- Node *node = get_node_or_null(E.value);
- if (node) {
- node->queue_free();
+ // Clear the scenes if instance cache was cleared.
+ if (instantiated_scenes.is_empty()) {
+ for (const KeyValue<Vector2i, String> &E : p_quadrant->scenes) {
+ Node *node = get_node_or_null(E.value);
+ if (node) {
+ node->queue_free();
+ }
}
+ p_quadrant->scenes.clear();
}
-
- p_quadrant->scenes.clear();
}
void TileMap::_scenes_draw_quadrant_debug(TileMapQuadrant *p_quadrant) {
@@ -4037,6 +4049,7 @@ void TileMap::_bind_methods() {
void TileMap::_tile_set_changed() {
emit_signal(SNAME("changed"));
_tile_set_changed_deferred_update_needed = true;
+ instantiated_scenes.clear();
call_deferred(SNAME("_tile_set_changed_deferred_update"));
}
diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h
index eaf100631e..68a5d3c80b 100644
--- a/scene/2d/tile_map.h
+++ b/scene/2d/tile_map.h
@@ -236,6 +236,8 @@ private:
void _clear_layer_internals(int p_layer);
void _clear_internals();
+ HashSet<Vector3i> instantiated_scenes;
+
// Rect caching.
void _recompute_rect_cache();
diff --git a/scene/animation/animation_tree.cpp b/scene/animation/animation_tree.cpp
index e148512963..99d450fa5b 100644
--- a/scene/animation/animation_tree.cpp
+++ b/scene/animation/animation_tree.cpp
@@ -221,7 +221,7 @@ double AnimationNode::_blend_node(const StringName &p_subpath, const Vector<Stri
}
blendw[i] = blendr[i] * p_blend;
- if (blendw[i] > CMP_EPSILON) {
+ if (!Math::is_zero_approx(blendw[i])) {
any_valid = true;
}
}
@@ -236,7 +236,7 @@ double AnimationNode::_blend_node(const StringName &p_subpath, const Vector<Stri
}
blendw[i] = blendr[i] * p_blend;
- if (blendw[i] > CMP_EPSILON) {
+ if (!Math::is_zero_approx(blendw[i])) {
any_valid = true;
}
}
@@ -252,7 +252,7 @@ double AnimationNode::_blend_node(const StringName &p_subpath, const Vector<Stri
blendw[i] = blendr[i]; //not filtered, do not blend
}
- if (blendw[i] > CMP_EPSILON) {
+ if (!Math::is_zero_approx(blendw[i])) {
any_valid = true;
}
}
@@ -263,7 +263,7 @@ double AnimationNode::_blend_node(const StringName &p_subpath, const Vector<Stri
for (int i = 0; i < blend_count; i++) {
//regular blend
blendw[i] = blendr[i] * p_blend;
- if (blendw[i] > CMP_EPSILON) {
+ if (!Math::is_zero_approx(blendw[i])) {
any_valid = true;
}
}
@@ -586,6 +586,7 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) {
track_value->object = child;
}
+ track_value->is_discrete = anim->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE || anim->value_track_get_update_mode(i) == Animation::UPDATE_TRIGGER;
track_value->is_using_angle = anim->track_get_interpolation_type(i) == Animation::INTERPOLATION_LINEAR_ANGLE || anim->track_get_interpolation_type(i) == Animation::INTERPOLATION_CUBIC_ANGLE;
track_value->subpath = leftover_path;
@@ -800,6 +801,7 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) {
} else if (track_cache_type == Animation::TYPE_VALUE) {
// If it has at least one angle interpolation, it also uses angle interpolation for blending.
TrackCacheValue *track_value = memnew(TrackCacheValue);
+ track_value->is_discrete |= anim->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE || anim->value_track_get_update_mode(i) == Animation::UPDATE_TRIGGER;
track_value->is_using_angle |= anim->track_get_interpolation_type(i) == Animation::INTERPOLATION_LINEAR_ANGLE || anim->track_get_interpolation_type(i) == Animation::INTERPOLATION_CUBIC_ANGLE;
}
@@ -1025,7 +1027,7 @@ void AnimationTree::_process_graph(double p_delta) {
int blend_idx = state.track_map[path];
ERR_CONTINUE(blend_idx < 0 || blend_idx >= state.track_count);
real_t blend = (*as.track_blends)[blend_idx] * weight;
- if (blend < CMP_EPSILON) {
+ if (Math::is_zero_approx(blend)) {
continue; // Nothing to blend.
}
@@ -1658,6 +1660,10 @@ void AnimationTree::_process_graph(double p_delta) {
case Animation::TYPE_VALUE: {
TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
+ if (t->is_discrete) {
+ break; // Don't overwrite the value set by UPDATE_DISCRETE or UPDATE_TRIGGER.
+ }
+
if (t->init_value.get_type() == Variant::BOOL) {
t->object->set_indexed(t->subpath, t->value.operator real_t() >= 0.5);
} else {
diff --git a/scene/animation/animation_tree.h b/scene/animation/animation_tree.h
index 493cd894d0..84d0a8190a 100644
--- a/scene/animation/animation_tree.h
+++ b/scene/animation/animation_tree.h
@@ -232,6 +232,7 @@ private:
Variant init_value;
Variant value;
Vector<StringName> subpath;
+ bool is_discrete = false;
bool is_using_angle = false;
TrackCacheValue() { type = Animation::TYPE_VALUE; }
};
diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp
index 889610e071..a54805ce56 100644
--- a/scene/gui/rich_text_label.cpp
+++ b/scene/gui/rich_text_label.cpp
@@ -3860,6 +3860,10 @@ void RichTextLabel::append_text(const String &p_bbcode) {
Color color2 = Color::from_string(subtag_b[1], fallback_color);
set_cell_row_background_color(color1, color2);
}
+ if (subtag_b.size() == 1) {
+ Color color1 = Color::from_string(subtag_a[1], fallback_color);
+ set_cell_row_background_color(color1, color1);
+ }
}
}
}
diff --git a/scene/gui/subviewport_container.cpp b/scene/gui/subviewport_container.cpp
index 3ad84cbc6d..f3d9a22342 100644
--- a/scene/gui/subviewport_container.cpp
+++ b/scene/gui/subviewport_container.cpp
@@ -227,6 +227,18 @@ void SubViewportContainer::unhandled_input(const Ref<InputEvent> &p_event) {
}
}
+void SubViewportContainer::add_child_notify(Node *p_child) {
+ if (Object::cast_to<SubViewport>(p_child)) {
+ queue_redraw();
+ }
+}
+
+void SubViewportContainer::remove_child_notify(Node *p_child) {
+ if (Object::cast_to<SubViewport>(p_child)) {
+ queue_redraw();
+ }
+}
+
PackedStringArray SubViewportContainer::get_configuration_warnings() const {
PackedStringArray warnings = Node::get_configuration_warnings();
diff --git a/scene/gui/subviewport_container.h b/scene/gui/subviewport_container.h
index 63a58b5f07..fdd8fe9486 100644
--- a/scene/gui/subviewport_container.h
+++ b/scene/gui/subviewport_container.h
@@ -44,6 +44,9 @@ protected:
void _notification(int p_what);
static void _bind_methods();
+ virtual void add_child_notify(Node *p_child) override;
+ virtual void remove_child_notify(Node *p_child) override;
+
public:
void set_stretch(bool p_enable);
bool is_stretch_enabled() const;
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index 56f7281721..cce9fa4f34 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -4234,7 +4234,7 @@ Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_
if (!p_allow_out_of_bounds) {
return Point2i(-1, -1);
}
- return Point2i(text[row].size(), row);
+ return Point2i(text[row].length(), row);
}
int col = 0;
diff --git a/scene/main/node.cpp b/scene/main/node.cpp
index f88fdb3ac1..90b996e06b 100644
--- a/scene/main/node.cpp
+++ b/scene/main/node.cpp
@@ -349,7 +349,7 @@ void Node::move_child(Node *p_child, int p_index) {
}
void Node::_move_child(Node *p_child, int p_index, bool p_ignore_end) {
- ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, move_child() failed. Consider using call_deferred(\"move_child\") instead (or \"popup\" if this is from a popup).");
+ ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `move_child()` failed. Consider using `move_child.call_deferred(child, index)` instead (or `popup.call_deferred()` if this is from a popup).");
// Specifying one place beyond the end
// means the same as moving to the last index
@@ -1130,7 +1130,7 @@ void Node::add_child(Node *p_child, bool p_force_readable_name, InternalMode p_i
#ifdef DEBUG_ENABLED
ERR_FAIL_COND_MSG(p_child->is_ancestor_of(this), vformat("Can't add child '%s' to '%s' as it would result in a cyclic dependency since '%s' is already a parent of '%s'.", p_child->get_name(), get_name(), p_child->get_name(), get_name()));
#endif
- ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, add_node() failed. Consider using call_deferred(\"add_child\", child) instead.");
+ ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `add_child()` failed. Consider using `add_child.call_deferred(child)` instead.");
_validate_child_name(p_child, p_force_readable_name);
_add_child_nocheck(p_child, p_child->data.name);
@@ -1150,7 +1150,7 @@ void Node::add_sibling(Node *p_sibling, bool p_force_readable_name) {
ERR_FAIL_NULL(p_sibling);
ERR_FAIL_NULL(data.parent);
ERR_FAIL_COND_MSG(p_sibling == this, vformat("Can't add sibling '%s' to itself.", p_sibling->get_name())); // adding to itself!
- ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, add_sibling() failed. Consider using call_deferred(\"add_sibling\", sibling) instead.");
+ ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `add_sibling()` failed. Consider using `add_sibling.call_deferred(sibling)` instead.");
InternalMode internal = INTERNAL_MODE_DISABLED;
if (_is_internal_front()) { // The sibling will have the same internal status.
@@ -1165,7 +1165,7 @@ void Node::add_sibling(Node *p_sibling, bool p_force_readable_name) {
void Node::remove_child(Node *p_child) {
ERR_FAIL_NULL(p_child);
- ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, remove_node() failed. Consider using call_deferred(\"remove_child\", child) instead.");
+ ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `remove_child()` failed. Consider using `remove_child.call_deferred(child)` instead.");
int child_count = data.children.size();
Node **children = data.children.ptrw();
@@ -2570,10 +2570,6 @@ static void _Node_debug_sn(Object *p_obj) {
}
#endif // DEBUG_ENABLED
-void Node::_print_orphan_nodes() {
- print_orphan_nodes();
-}
-
void Node::print_orphan_nodes() {
#ifdef DEBUG_ENABLED
ObjectDB::debug_objects(_Node_debug_sn);
@@ -2745,6 +2741,7 @@ void Node::_bind_methods() {
GLOBAL_DEF("editor/node_naming/name_casing", NAME_CASING_PASCAL_CASE);
ProjectSettings::get_singleton()->set_custom_property_info("editor/node_naming/name_casing", PropertyInfo(Variant::INT, "editor/node_naming/name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case"));
+ ClassDB::bind_static_method("Node", D_METHOD("print_orphan_nodes"), &Node::print_orphan_nodes);
ClassDB::bind_method(D_METHOD("add_sibling", "sibling", "force_readable_name"), &Node::add_sibling, DEFVAL(false));
ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name);
@@ -2802,7 +2799,6 @@ void Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Node::set_process_mode);
ClassDB::bind_method(D_METHOD("get_process_mode"), &Node::get_process_mode);
ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process);
- ClassDB::bind_method(D_METHOD("print_orphan_nodes"), &Node::_print_orphan_nodes);
ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded);
ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded);
diff --git a/scene/main/node.h b/scene/main/node.h
index 5e51ec6b0e..e07fb003d4 100644
--- a/scene/main/node.h
+++ b/scene/main/node.h
@@ -173,7 +173,6 @@ private:
void _propagate_ready();
void _propagate_exit_tree();
void _propagate_after_exit_tree();
- void _print_orphan_nodes();
void _propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification);
void _propagate_groups_dirty();
Array _get_node_and_resource(const NodePath &p_path);
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp
index 9e440405af..345d5de937 100644
--- a/scene/main/viewport.cpp
+++ b/scene/main/viewport.cpp
@@ -362,6 +362,7 @@ void Viewport::_notification(int p_what) {
current_canvas = find_world_2d()->get_canvas();
RenderingServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
+ RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, current_canvas, canvas_transform);
RenderingServer::get_singleton()->viewport_set_canvas_cull_mask(viewport, canvas_cull_mask);
_update_audio_listener_2d();
#ifndef _3D_DISABLED
diff --git a/scene/resources/bit_map.cpp b/scene/resources/bit_map.cpp
index 4afc82576d..0df61871d8 100644
--- a/scene/resources/bit_map.cpp
+++ b/scene/resources/bit_map.cpp
@@ -169,14 +169,6 @@ Dictionary BitMap::_get_data() const {
return d;
}
-struct CrossStackEntry {
- Point2i cross;
- Vector<int> ranges;
-
- _FORCE_INLINE_ bool operator==(const CrossStackEntry &p_other) const { return cross == p_other.cross; }
- _FORCE_INLINE_ bool operator!=(const CrossStackEntry &p_other) const { return cross != p_other.cross; }
-};
-
Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_start) const {
int stepx = 0;
int stepy = 0;
@@ -188,14 +180,11 @@ Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2
int cury = starty;
unsigned int count = 0;
- Vector<CrossStackEntry> cross_stack;
- int cross_stack_size = 0;
+ HashMap<Point2i, int> cross_map;
- // Add starting point to stack as the default entry.
- cross_stack.push_back({ Point2i(-1, -1), Vector<int>({ 0 }) });
- cross_stack_size++;
+ Vector<Vector2> _points;
+ int points_size = 0;
- Vector<Point2i> _points;
Vector<Vector<Vector2>> ret;
// Add starting entry at start of return.
@@ -326,52 +315,25 @@ Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2
// Handle crossing points.
if (sv == 6 || sv == 9) {
- const int new_index = _points.size() - 1;
-
- // Add previous point to last stack entry.
- cross_stack.write[cross_stack_size - 1].ranges.push_back(new_index);
-
- // Create temporary entry to maybe insert, for searching.
- const CrossStackEntry new_entry = { _points[new_index], Vector<int>({ new_index }) };
-
- // Attempt to find matching entry.
- const int found = cross_stack.rfind(new_entry, cross_stack_size - 1);
+ const Point2i cur_pos(curx, cury);
- if (found != -1) {
- Vector<Vector2> tmp;
+ // Find if this point has occured before.
+ if (HashMap<Point2i, int>::Iterator found = cross_map.find(cur_pos)) {
+ // Add points after the previous crossing to the result.
+ ret.push_back(_points.slice(found->value + 1, points_size));
- // Iterate over entries between end of stack and found, adding ranges to result.
- for (int i = found; i < cross_stack_size; i++) {
- const Vector<int> &ranges = cross_stack[i].ranges;
+ // Remove points after crossing point.
+ points_size = found->value + 1;
- for (int j = 0; j < ranges.size() / 2; j++) {
- int first = ranges[2 * j];
- const int last = ranges[2 * j + 1];
-
- int new_pos = tmp.size();
-
- tmp.resize(tmp.size() + (last - first));
-
- Vector2 *tmp_ptrw = tmp.ptrw();
-
- for (; first < last; first++, new_pos++) {
- tmp_ptrw[new_pos].x = (float)(_points[first].x - p_rect.position.x);
- tmp_ptrw[new_pos].y = (float)(_points[first].y - p_rect.position.y);
- }
- }
+ // Erase trailing map elements.
+ while (cross_map.last() != found) {
+ cross_map.remove(cross_map.last());
}
- ret.push_back(tmp);
-
- // Shrink stack.
- cross_stack_size = found;
-
- // Add previous point to last stack entry.
- cross_stack.write[cross_stack_size - 1].ranges.push_back(new_index);
+ cross_map.erase(cur_pos);
} else {
- cross_stack.resize(MAX(cross_stack_size + 1, cross_stack.size()));
- cross_stack.set(cross_stack_size, new_entry);
- cross_stack_size++;
+ // Add crossing point to map.
+ cross_map.insert(cur_pos, points_size - 1);
}
}
@@ -381,10 +343,11 @@ Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2
curx += stepx;
cury += stepy;
if (stepx == prevx && stepy == prevy) {
- _points.write[_points.size() - 1].x = curx;
- _points.write[_points.size() - 1].y = cury;
+ _points.set(points_size - 1, Vector2(curx, cury) - p_rect.position);
} else {
- _points.push_back(Point2i(curx, cury));
+ _points.resize(MAX(points_size + 1, _points.size()));
+ _points.set(points_size, Vector2(curx, cury) - p_rect.position);
+ points_size++;
}
count++;
@@ -394,28 +357,10 @@ Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2
ERR_FAIL_COND_V((int)count > width * height, Vector<Vector<Vector2>>());
} while (curx != startx || cury != starty);
- // Add last position to last stack entry.
- cross_stack.write[cross_stack_size - 1].ranges.push_back(_points.size());
-
- for (int i = 0; i < cross_stack_size; i++) {
- const Vector<int> &ranges = cross_stack[i].ranges;
-
- for (int j = 0; j < ranges.size() / 2; j++) {
- int first = ranges[2 * j];
- const int last = ranges[2 * j + 1];
-
- int new_pos = ret[0].size();
+ // Add remaining points to result.
+ _points.resize(points_size);
- ret.write[0].resize(ret[0].size() + (last - first));
-
- Vector2 *tmp_ptrw = ret.write[0].ptrw();
-
- for (; first < last; first++, new_pos++) {
- tmp_ptrw[new_pos].x = (float)(_points[first].x - p_rect.position.x);
- tmp_ptrw[new_pos].y = (float)(_points[first].y - p_rect.position.y);
- }
- }
- }
+ ret.set(0, _points);
return ret;
}
diff --git a/scene/resources/curve.cpp b/scene/resources/curve.cpp
index 0c36abc148..9289c5da4a 100644
--- a/scene/resources/curve.cpp
+++ b/scene/resources/curve.cpp
@@ -1403,6 +1403,22 @@ void Curve3D::_bake_segment3d(RBMap<real_t, Vector3> &r_bake, real_t p_begin, re
}
}
+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);
+
+ size_t length = beg.distance_to(end);
+
+ 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;
+
+ _bake_segment3d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
+ _bake_segment3d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
+ }
+}
+
void Curve3D::_bake() const {
if (!baked_cache_dirty) {
return;
@@ -1416,6 +1432,7 @@ void Curve3D::_bake() const {
baked_tilt_cache.clear();
baked_dist_cache.clear();
+ baked_forward_vector_cache.clear();
baked_up_vector_cache.clear();
return;
}
@@ -1427,10 +1444,12 @@ void Curve3D::_bake() const {
baked_tilt_cache.set(0, points[0].tilt);
baked_dist_cache.resize(1);
baked_dist_cache.set(0, 0.0);
+ baked_forward_vector_cache.resize(1);
+ baked_forward_vector_cache.set(0, Vector3(0.0, 0.0, 1.0));
if (up_vector_enabled) {
baked_up_vector_cache.resize(1);
- baked_up_vector_cache.set(0, Vector3(0, 1, 0));
+ baked_up_vector_cache.set(0, Vector3(0.0, 1.0, 0.0));
} else {
baked_up_vector_cache.clear();
}
@@ -1438,101 +1457,52 @@ void Curve3D::_bake() const {
return;
}
- Vector3 position = points[0].position;
- real_t dist = 0.0;
- List<Plane> pointlist; // Abuse Plane for (position, dist)
- List<real_t> distlist;
-
- // Start always from origin.
- pointlist.push_back(Plane(position, points[0].tilt));
- distlist.push_back(0.0);
-
- // Step 1: Sample points
- const real_t step = 0.1; // At least 10 substeps ought to be enough?
- for (int i = 0; i < points.size() - 1; i++) {
- real_t p = 0.0;
-
- while (p < 1.0) {
- real_t np = p + step;
- if (np > 1.0) {
- np = 1.0;
- }
-
- Vector3 npp = points[i].position.bezier_interpolate(points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, np);
- real_t d = position.distance_to(npp);
-
- if (d > bake_interval) {
- // OK! between P and NP there _has_ to be Something, let's go searching!
-
- const int iterations = 10; // Lots of detail!
-
- real_t low = p;
- real_t hi = np;
- real_t mid = low + (hi - low) * 0.5;
-
- for (int j = 0; j < iterations; j++) {
- npp = points[i].position.bezier_interpolate(points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, mid);
- d = position.distance_to(npp);
-
- if (bake_interval < d) {
- hi = mid;
- } else {
- low = mid;
- }
- mid = low + (hi - low) * 0.5;
- }
-
- position = npp;
- p = mid;
- Plane post;
- post.normal = position;
- post.d = Math::lerp(points[i].tilt, points[i + 1].tilt, mid);
- dist += d;
+ // Step 1: Tesselate curve to (almost) even length segments
+ {
+ Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(10, bake_interval);
- pointlist.push_back(post);
- distlist.push_back(dist);
- } else {
- p = np;
- }
+ int pc = 1;
+ for (int i = 0; i < points.size() - 1; i++) {
+ pc++;
+ pc += midpoints[i].size();
}
- Vector3 npp = points[i + 1].position;
- real_t d = position.distance_to(npp);
-
- if (d > CMP_EPSILON) { // Avoid the degenerate case of two very close points.
- position = npp;
- Plane post;
- post.normal = position;
- post.d = points[i + 1].tilt;
-
- dist += d;
+ 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);
+ }
- pointlist.push_back(post);
- distlist.push_back(dist);
+ 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;
}
- }
-
- baked_max_ofs = dist;
-
- const int point_count = pointlist.size();
- {
- baked_point_cache.resize(point_count);
- Vector3 *w = baked_point_cache.ptrw();
-
- baked_tilt_cache.resize(point_count);
- real_t *wt = baked_tilt_cache.ptrw();
-
- baked_dist_cache.resize(point_count);
- real_t *wd = baked_dist_cache.ptrw();
-
- int idx = 0;
- for (const Plane &E : pointlist) {
- w[idx] = E.normal;
- wt[idx] = E.d;
- wd[idx] = distlist[idx];
- idx++;
+ // 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];
}
if (!up_vector_enabled) {
@@ -1545,14 +1515,12 @@ void Curve3D::_bake() const {
// See Dougan, Carl. "The parallel transport frame." Game Programming Gems 2 (2001): 215-219.
// for an example discussing about why not the Frenet frame.
{
- PackedVector3Array forward_vectors;
+ int point_count = baked_point_cache.size();
baked_up_vector_cache.resize(point_count);
- forward_vectors.resize(point_count);
-
Vector3 *up_write = baked_up_vector_cache.ptrw();
- Vector3 *forward_write = forward_vectors.ptrw();
+ const Vector3 *forward_ptr = baked_forward_vector_cache.ptr();
const Vector3 *points_ptr = baked_point_cache.ptr();
Basis frame; // X-right, Y-up, Z-forward.
@@ -1560,28 +1528,20 @@ void Curve3D::_bake() const {
// Set the initial frame based on Y-up rule.
{
- Vector3 up(0, 1, 0);
- Vector3 forward = (points_ptr[1] - points_ptr[0]).normalized();
- if (forward.is_equal_approx(Vector3())) {
- forward = Vector3(1, 0, 0);
- }
+ Vector3 forward = forward_ptr[0];
- if (abs(forward.dot(up)) > 1.0 - UNIT_EPSILON) {
- frame_prev = Basis::looking_at(-forward, up);
- } else {
+ if (abs(forward.dot(Vector3(0, 1, 0))) > 1.0 - UNIT_EPSILON) {
frame_prev = Basis::looking_at(-forward, Vector3(1, 0, 0));
+ } else {
+ frame_prev = Basis::looking_at(-forward, Vector3(0, 1, 0));
}
up_write[0] = frame_prev.get_column(1);
- forward_write[0] = frame_prev.get_column(2);
}
// Calculate the Parallel Transport Frame.
for (int idx = 1; idx < point_count; idx++) {
- Vector3 forward = (points_ptr[idx] - points_ptr[idx - 1]).normalized();
- if (forward.is_equal_approx(Vector3())) {
- forward = frame_prev.get_column(2);
- }
+ Vector3 forward = forward_ptr[idx];
Basis rotate;
rotate.rotate_to_align(frame_prev.get_column(2), forward);
@@ -1589,8 +1549,6 @@ void Curve3D::_bake() const {
frame.orthonormalize(); // guard against float error accumulation
up_write[idx] = frame.get_column(1);
- forward_write[idx] = frame.get_column(2);
-
frame_prev = frame;
}
@@ -1601,8 +1559,8 @@ void Curve3D::_bake() const {
is_loop = false;
}
- real_t dot = forward_write[0].dot(forward_write[point_count - 1]);
- if (dot < 1.0 - 0.01) { // Alignment should not be too tight, or it dosen't work for coarse bake interval
+ 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.
is_loop = false;
}
}
@@ -1612,17 +1570,17 @@ void Curve3D::_bake() const {
const Vector3 up_start = up_write[0];
const Vector3 up_end = up_write[point_count - 1];
- real_t sign = SIGN(up_end.cross(up_start).dot(forward_write[0]));
+ real_t sign = SIGN(up_end.cross(up_start).dot(forward_ptr[0]));
real_t full_angle = Quaternion(up_end, up_start).get_angle();
- if (abs(full_angle) < UNIT_EPSILON) {
+ if (abs(full_angle) < CMP_EPSILON) {
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);
- Basis twist(forward_write[idx] * sign, angle);
+ Basis twist(forward_ptr[idx] * sign, angle);
up_write[idx] = twist.xform(up_write[idx]);
}
@@ -1720,22 +1678,14 @@ Basis Curve3D::_sample_posture(Interval p_interval, bool p_apply_tilt) const {
int idx = p_interval.idx;
real_t frac = p_interval.frac;
- Vector3 forward_begin;
- Vector3 forward_end;
- if (idx == 0) {
- forward_begin = (baked_point_cache[1] - baked_point_cache[0]).normalized();
- forward_end = (baked_point_cache[1] - baked_point_cache[0]).normalized();
- } else {
- forward_begin = (baked_point_cache[idx] - baked_point_cache[idx - 1]).normalized();
- forward_end = (baked_point_cache[idx + 1] - baked_point_cache[idx]).normalized();
- }
+ Vector3 forward_begin = baked_forward_vector_cache[idx];
+ Vector3 forward_end = baked_forward_vector_cache[idx + 1];
Vector3 up_begin;
Vector3 up_end;
if (up_vector_enabled) {
- const Vector3 *up_ptr = baked_up_vector_cache.ptr();
- up_begin = up_ptr[idx];
- up_end = up_ptr[idx + 1];
+ up_begin = baked_up_vector_cache[idx];
+ up_end = baked_up_vector_cache[idx + 1];
} else {
up_begin = Vector3(0.0, 1.0, 0.0);
up_end = Vector3(0.0, 1.0, 0.0);
@@ -2046,6 +1996,50 @@ PackedVector3Array Curve3D::tessellate(int p_max_stages, real_t p_tolerance) con
return tess;
}
+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;
+}
+
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()) {
@@ -2146,6 +2140,7 @@ void Curve3D::_bind_methods() {
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);
ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve3D::tessellate, DEFVAL(5), DEFVAL(4));
+ ClassDB::bind_method(D_METHOD("tessellate_even_length", "max_stages", "tolerance_length"), &Curve3D::tessellate_even_length, DEFVAL(5), DEFVAL(0.2));
ClassDB::bind_method(D_METHOD("_get_data"), &Curve3D::_get_data);
ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve3D::_set_data);
diff --git a/scene/resources/curve.h b/scene/resources/curve.h
index d0c813f262..88c3cf3ae6 100644
--- a/scene/resources/curve.h
+++ b/scene/resources/curve.h
@@ -242,6 +242,7 @@ class Curve3D : public Resource {
mutable PackedVector3Array baked_point_cache;
mutable Vector<real_t> baked_tilt_cache;
mutable PackedVector3Array baked_up_vector_cache;
+ mutable PackedVector3Array baked_forward_vector_cache;
mutable Vector<real_t> baked_dist_cache;
mutable real_t baked_max_ofs = 0.0;
@@ -262,6 +263,7 @@ class Curve3D : public Resource {
bool up_vector_enabled = true;
void _bake_segment3d(RBMap<real_t, Vector3> &r_bake, real_t p_begin, real_t p_end, const Vector3 &p_a, const Vector3 &p_out, const Vector3 &p_b, const Vector3 &p_in, int p_depth, int p_max_depth, real_t p_tol) const;
+ void _bake_segment3d_even_length(RBMap<real_t, Vector3> &r_bake, real_t p_begin, real_t p_end, const Vector3 &p_a, const Vector3 &p_out, const Vector3 &p_b, const Vector3 &p_in, int p_depth, int p_max_depth, real_t p_length) const;
Dictionary _get_data() const;
void _set_data(const Dictionary &p_data);
@@ -272,6 +274,8 @@ class Curve3D : public Resource {
void _add_point(const Vector3 &p_position, const Vector3 &p_in = Vector3(), const Vector3 &p_out = Vector3(), int p_atpos = -1);
void _remove_point(int p_index);
+ Vector<RBMap<real_t, Vector3>> _tessellate_even_length(int p_max_stages = 5, real_t p_length = 0.2) const;
+
protected:
static void _bind_methods();
@@ -309,7 +313,8 @@ public:
Vector3 get_closest_point(const Vector3 &p_to_point) const;
real_t get_closest_offset(const Vector3 &p_to_point) const;
- PackedVector3Array tessellate(int p_max_stages = 5, real_t p_tolerance = 4) const; //useful for display
+ PackedVector3Array tessellate(int p_max_stages = 5, real_t p_tolerance = 4) const; // Useful for display.
+ PackedVector3Array tessellate_even_length(int p_max_stages = 5, real_t p_length = 0.2) const; // Useful for baking.
Curve3D();
};
diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp
index 894936acd7..f179b4b818 100644
--- a/scene/resources/default_theme/default_theme.cpp
+++ b/scene/resources/default_theme/default_theme.cpp
@@ -84,7 +84,8 @@ static Ref<ImageTexture> generate_icon(int p_index) {
// with integer scales.
const bool upsample = !Math::is_equal_approx(Math::round(scale), scale);
ImageLoaderSVG img_loader;
- img_loader.create_image_from_string(img, default_theme_icons_sources[p_index], scale, upsample, HashMap<Color, Color>());
+ Error err = img_loader.create_image_from_string(img, default_theme_icons_sources[p_index], scale, upsample, HashMap<Color, Color>());
+ ERR_FAIL_COND_V_MSG(err != OK, Ref<ImageTexture>(), "Failed generating icon, unsupported or invalid SVG data in default theme.");
#endif
return ImageTexture::create_from_image(img);
diff --git a/scene/resources/primitive_meshes.cpp b/scene/resources/primitive_meshes.cpp
index 4c6d533c72..2e8fcb3717 100644
--- a/scene/resources/primitive_meshes.cpp
+++ b/scene/resources/primitive_meshes.cpp
@@ -2779,13 +2779,7 @@ void TextMesh::_generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph
real_t step = CLAMP(curve_step / (p0 - p3).length(), 0.01, 0.5);
real_t t = step;
while (t < 1.0) {
- real_t omt = (1.0 - t);
- real_t omt2 = omt * omt;
- real_t omt3 = omt2 * omt;
- real_t t2 = t * t;
- real_t t3 = t2 * t;
-
- Vector2 point = p0 * omt3 + p1 * omt2 * t * 3.0 + p2 * omt * t2 * 3.0 + p3 * t3;
+ Vector2 point = p0.bezier_interpolate(p1, p2, p3, t);
Vector2 p = point * pixel_size + origin;
polygon.push_back(ContourPoint(p, false));
t += step;