summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/math/a_star.cpp195
-rw-r--r--core/math/a_star.h10
-rw-r--r--doc/classes/AStar2D.xml24
-rw-r--r--doc/classes/InputEventMouseMotion.xml3
-rw-r--r--editor/editor_inspector.cpp12
-rw-r--r--editor/plugins/canvas_item_editor_plugin.cpp18
-rw-r--r--servers/visual/shader_language.cpp6
-rw-r--r--servers/visual/shader_types.cpp5
8 files changed, 255 insertions, 18 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp
index 847d4d8681..bfff12ac45 100644
--- a/core/math/a_star.cpp
+++ b/core/math/a_star.cpp
@@ -399,7 +399,7 @@ bool AStar::_solve(Point *begin_point, Point *end_point) {
return found_route;
}
-float AStar::_estimate_cost(int p_from_id, int p_to_id) {
+real_t AStar::_estimate_cost(int p_from_id, int p_to_id) {
if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost))
return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
@@ -415,7 +415,7 @@ float AStar::_estimate_cost(int p_from_id, int p_to_id) {
return from_point->pos.distance_to(to_point->pos);
}
-float AStar::_compute_cost(int p_from_id, int p_to_id) {
+real_t AStar::_compute_cost(int p_from_id, int p_to_id) {
if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost))
return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
@@ -677,25 +677,195 @@ Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const {
return Vector2(p.x, p.y);
}
+real_t AStar2D::_estimate_cost(int p_from_id, int p_to_id) {
+
+ if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost))
+ return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
+
+ AStar::Point *from_point;
+ bool from_exists = astar.points.lookup(p_from_id, from_point);
+ ERR_FAIL_COND_V(!from_exists, 0);
+
+ AStar::Point *to_point;
+ bool to_exists = astar.points.lookup(p_to_id, to_point);
+ ERR_FAIL_COND_V(!to_exists, 0);
+
+ return from_point->pos.distance_to(to_point->pos);
+}
+
+real_t AStar2D::_compute_cost(int p_from_id, int p_to_id) {
+
+ if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost))
+ return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
+
+ AStar::Point *from_point;
+ bool from_exists = astar.points.lookup(p_from_id, from_point);
+ ERR_FAIL_COND_V(!from_exists, 0);
+
+ AStar::Point *to_point;
+ bool to_exists = astar.points.lookup(p_to_id, to_point);
+ ERR_FAIL_COND_V(!to_exists, 0);
+
+ return from_point->pos.distance_to(to_point->pos);
+}
+
Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) {
- PackedVector3Array pv = astar.get_point_path(p_from_id, p_to_id);
- int size = pv.size();
- PackedVector2Array path;
- path.resize(size);
+ AStar::Point *a;
+ bool from_exists = astar.points.lookup(p_from_id, a);
+ ERR_FAIL_COND_V(!from_exists, Vector<Vector2>());
+
+ AStar::Point *b;
+ bool to_exists = astar.points.lookup(p_to_id, b);
+ ERR_FAIL_COND_V(!to_exists, Vector<Vector2>());
+
+ if (a == b) {
+ Vector<Vector2> ret;
+ ret.push_back(Vector2(a->pos.x, a->pos.y));
+ return ret;
+ }
+
+ AStar::Point *begin_point = a;
+ AStar::Point *end_point = b;
+
+ bool found_route = _solve(begin_point, end_point);
+ if (!found_route) return Vector<Vector2>();
+
+ AStar::Point *p = end_point;
+ int pc = 1; // Begin point
+ while (p != begin_point) {
+ pc++;
+ p = p->prev_point;
+ }
+
+ Vector<Vector2> path;
+ path.resize(pc);
+
{
- const Vector3 *r = pv.ptr();
Vector2 *w = path.ptrw();
- for (int i = 0; i < size; i++) {
- Vector3 p = r[i];
- w[i] = Vector2(p.x, p.y);
+
+ AStar::Point *p2 = end_point;
+ int idx = pc - 1;
+ while (p2 != begin_point) {
+ w[idx--] = Vector2(p2->pos.x, p2->pos.y);
+ p2 = p2->prev_point;
}
+
+ w[0] = Vector2(p2->pos.x, p2->pos.y); // Assign first
}
+
return path;
}
Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) {
- return astar.get_id_path(p_from_id, p_to_id);
+
+ AStar::Point *a;
+ bool from_exists = astar.points.lookup(p_from_id, a);
+ ERR_FAIL_COND_V(!from_exists, Vector<int>());
+
+ AStar::Point *b;
+ bool to_exists = astar.points.lookup(p_to_id, b);
+ ERR_FAIL_COND_V(!to_exists, Vector<int>());
+
+ if (a == b) {
+ Vector<int> ret;
+ ret.push_back(a->id);
+ return ret;
+ }
+
+ AStar::Point *begin_point = a;
+ AStar::Point *end_point = b;
+
+ bool found_route = _solve(begin_point, end_point);
+ if (!found_route) return Vector<int>();
+
+ AStar::Point *p = end_point;
+ int pc = 1; // Begin point
+ while (p != begin_point) {
+ pc++;
+ p = p->prev_point;
+ }
+
+ Vector<int> path;
+ path.resize(pc);
+
+ {
+ int *w = path.ptrw();
+
+ p = end_point;
+ int idx = pc - 1;
+ while (p != begin_point) {
+ w[idx--] = p->id;
+ p = p->prev_point;
+ }
+
+ w[0] = p->id; // Assign first
+ }
+
+ return path;
+}
+
+bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) {
+
+ astar.pass++;
+
+ if (!end_point->enabled) return false;
+
+ bool found_route = false;
+
+ Vector<AStar::Point *> open_list;
+ SortArray<AStar::Point *, AStar::SortPoints> sorter;
+
+ begin_point->g_score = 0;
+ begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
+ open_list.push_back(begin_point);
+
+ while (!open_list.empty()) {
+
+ AStar::Point *p = open_list[0]; // The currently processed point
+
+ if (p == end_point) {
+ found_route = true;
+ break;
+ }
+
+ sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list
+ open_list.remove(open_list.size() - 1);
+ p->closed_pass = astar.pass; // Mark the point as closed
+
+ for (OAHashMap<int, AStar::Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
+
+ AStar::Point *e = *(it.value); // The neighbour point
+
+ if (!e->enabled || e->closed_pass == astar.pass) {
+ continue;
+ }
+
+ real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
+
+ bool new_point = false;
+
+ if (e->open_pass != astar.pass) { // The point wasn't inside the open list.
+ e->open_pass = astar.pass;
+ open_list.push_back(e);
+ new_point = true;
+ } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
+ continue;
+ }
+
+ e->prev_point = p;
+ e->g_score = tentative_g_score;
+ e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
+
+ if (new_point) { // The position of the new points is already known.
+ sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw());
+ } else {
+ sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw());
+ }
+ }
+ }
+
+ return found_route;
}
void AStar2D::_bind_methods() {
@@ -728,6 +898,9 @@ void AStar2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar2D::get_point_path);
ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar2D::get_id_path);
+
+ BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
+ BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
}
AStar2D::AStar2D() {
diff --git a/core/math/a_star.h b/core/math/a_star.h
index bfcf0c09d3..cc6c4619e9 100644
--- a/core/math/a_star.h
+++ b/core/math/a_star.h
@@ -43,6 +43,7 @@
class AStar : public Reference {
GDCLASS(AStar, Reference);
+ friend class AStar2D;
struct Point {
@@ -124,8 +125,8 @@ class AStar : public Reference {
protected:
static void _bind_methods();
- virtual float _estimate_cost(int p_from_id, int p_to_id);
- virtual float _compute_cost(int p_from_id, int p_to_id);
+ virtual real_t _estimate_cost(int p_from_id, int p_to_id);
+ virtual real_t _compute_cost(int p_from_id, int p_to_id);
public:
int get_available_point_id() const;
@@ -166,9 +167,14 @@ class AStar2D : public Reference {
GDCLASS(AStar2D, Reference);
AStar astar;
+ bool _solve(AStar::Point *begin_point, AStar::Point *end_point);
+
protected:
static void _bind_methods();
+ virtual real_t _estimate_cost(int p_from_id, int p_to_id);
+ virtual real_t _compute_cost(int p_from_id, int p_to_id);
+
public:
int get_available_point_id() const;
diff --git a/doc/classes/AStar2D.xml b/doc/classes/AStar2D.xml
index 2639f62552..16fa05041e 100644
--- a/doc/classes/AStar2D.xml
+++ b/doc/classes/AStar2D.xml
@@ -9,6 +9,30 @@
<tutorials>
</tutorials>
<methods>
+ <method name="_compute_cost" qualifiers="virtual">
+ <return type="float">
+ </return>
+ <argument index="0" name="from_id" type="int">
+ </argument>
+ <argument index="1" name="to_id" type="int">
+ </argument>
+ <description>
+ Called when computing the cost between two connected points.
+ Note that this function is hidden in the default [code]AStar2D[/code] class.
+ </description>
+ </method>
+ <method name="_estimate_cost" qualifiers="virtual">
+ <return type="float">
+ </return>
+ <argument index="0" name="from_id" type="int">
+ </argument>
+ <argument index="1" name="to_id" type="int">
+ </argument>
+ <description>
+ Called when estimating the cost between a point and the path's ending point.
+ Note that this function is hidden in the default [code]AStar2D[/code] class.
+ </description>
+ </method>
<method name="add_point">
<return type="void">
</return>
diff --git a/doc/classes/InputEventMouseMotion.xml b/doc/classes/InputEventMouseMotion.xml
index 1549353d45..97b9d5247a 100644
--- a/doc/classes/InputEventMouseMotion.xml
+++ b/doc/classes/InputEventMouseMotion.xml
@@ -16,7 +16,8 @@
Represents the pressure the user puts on the pen. Ranges from [code]0.0[/code] to [code]1.0[/code].
</member>
<member name="relative" type="Vector2" setter="set_relative" getter="get_relative" default="Vector2( 0, 0 )">
- The mouse position relative to the previous position (position at the last frame).
+ The mouse position relative to the previous position (position at the last frame).
+ [b]Note:[/b] Since [InputEventMouseMotion] is only emitted when the mouse moves, the last event won't have a relative position of [code]Vector2(0, 0)[/code] when the user stops moving the mouse.
</member>
<member name="speed" type="Vector2" setter="set_speed" getter="get_speed" default="Vector2( 0, 0 )">
The mouse speed in pixels per second.
diff --git a/editor/editor_inspector.cpp b/editor/editor_inspector.cpp
index a7540bad10..fe3ed23dee 100644
--- a/editor/editor_inspector.cpp
+++ b/editor/editor_inspector.cpp
@@ -1707,6 +1707,18 @@ void EditorInspector::update_tree() {
break;
}
}
+
+ Vector<String> slices = propname.operator String().split("/");
+ if (slices.size() == 2 && slices[0].begins_with("custom_")) {
+ // Likely a theme property.
+ for (int i = 0; i < F->get().theme_properties.size(); i++) {
+ if (F->get().theme_properties[i].name == slices[1]) {
+ descr = F->get().theme_properties[i].description.strip_edges();
+ break;
+ }
+ }
+ }
+
if (!F->get().inherits.empty()) {
F = dd->class_list.find(F->get().inherits);
} else {
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp
index 6e1166bb37..9f66672f4a 100644
--- a/editor/plugins/canvas_item_editor_plugin.cpp
+++ b/editor/plugins/canvas_item_editor_plugin.cpp
@@ -4306,10 +4306,20 @@ void CanvasItemEditor::_zoom_on_position(float p_zoom, Point2 p_position) {
float prev_zoom = zoom;
zoom = p_zoom;
- Point2 ofs = p_position;
- ofs = ofs / prev_zoom - ofs / zoom;
- view_offset.x = Math::round(view_offset.x + ofs.x);
- view_offset.y = Math::round(view_offset.y + ofs.y);
+
+ view_offset += p_position / prev_zoom - p_position / zoom;
+
+ // We want to align in-scene pixels to screen pixels, this prevents blurry rendering
+ // in small details (texts, lines).
+ // This correction adds a jitter movement when zooming, so we correct only when the
+ // zoom factor is an integer. (in the other cases, all pixels won't be aligned anyway)
+ float closest_zoom_factor = Math::round(zoom);
+ if (Math::is_zero_approx(zoom - closest_zoom_factor)) {
+ // make sure scene pixel at view_offset is aligned on a screen pixel
+ Vector2 view_offset_int = view_offset.floor();
+ Vector2 view_offset_frac = view_offset - view_offset_int;
+ view_offset = view_offset_int + (view_offset_frac * closest_zoom_factor).round() / closest_zoom_factor;
+ }
_update_zoom_label();
update_viewport();
diff --git a/servers/visual/shader_language.cpp b/servers/visual/shader_language.cpp
index e73f8b8fc4..713b54e1ba 100644
--- a/servers/visual/shader_language.cpp
+++ b/servers/visual/shader_language.cpp
@@ -6284,6 +6284,12 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
builtin_types = p_functions[name].built_ins;
}
+ if (p_functions.has("global")) { // Adds global variables: 'TIME'
+ for (Map<StringName, BuiltInInfo>::Element *E = p_functions["global"].built_ins.front(); E; E = E->next()) {
+ builtin_types.insert(E->key(), E->value());
+ }
+ }
+
ShaderNode::Function function;
function.callable = !p_functions.has(name);
diff --git a/servers/visual/shader_types.cpp b/servers/visual/shader_types.cpp
index 5cd5f0b7bb..dd798f2d54 100644
--- a/servers/visual/shader_types.cpp
+++ b/servers/visual/shader_types.cpp
@@ -56,6 +56,8 @@ ShaderTypes::ShaderTypes() {
/*************** SPATIAL ***********************/
+ shader_modes[VS::SHADER_SPATIAL].functions["global"].built_ins["TIME"] = constt(ShaderLanguage::TYPE_FLOAT);
+
shader_modes[VS::SHADER_SPATIAL].functions["vertex"].built_ins["VERTEX"] = ShaderLanguage::TYPE_VEC3;
shader_modes[VS::SHADER_SPATIAL].functions["vertex"].built_ins["NORMAL"] = ShaderLanguage::TYPE_VEC3;
shader_modes[VS::SHADER_SPATIAL].functions["vertex"].built_ins["TANGENT"] = ShaderLanguage::TYPE_VEC3;
@@ -200,6 +202,8 @@ ShaderTypes::ShaderTypes() {
/************ CANVAS ITEM **************************/
+ shader_modes[VS::SHADER_CANVAS_ITEM].functions["global"].built_ins["TIME"] = constt(ShaderLanguage::TYPE_FLOAT);
+
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["VERTEX"] = ShaderLanguage::TYPE_VEC2;
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["UV"] = ShaderLanguage::TYPE_VEC2;
shader_modes[VS::SHADER_CANVAS_ITEM].functions["vertex"].built_ins["COLOR"] = ShaderLanguage::TYPE_VEC4;
@@ -267,6 +271,7 @@ ShaderTypes::ShaderTypes() {
/************ PARTICLES **************************/
+ shader_modes[VS::SHADER_PARTICLES].functions["global"].built_ins["TIME"] = constt(ShaderLanguage::TYPE_FLOAT);
shader_modes[VS::SHADER_PARTICLES].functions["vertex"].built_ins["COLOR"] = ShaderLanguage::TYPE_VEC4;
shader_modes[VS::SHADER_PARTICLES].functions["vertex"].built_ins["VELOCITY"] = ShaderLanguage::TYPE_VEC3;
shader_modes[VS::SHADER_PARTICLES].functions["vertex"].built_ins["MASS"] = ShaderLanguage::TYPE_FLOAT;