Skip to content

Commit

Permalink
Primitive AtlasTexture support for Line2D
Browse files Browse the repository at this point in the history
  • Loading branch information
arkology committed Jan 4, 2025
1 parent bdf625b commit 3f12001
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
51 changes: 44 additions & 7 deletions scene/2d/line_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/math/geometry_2d.h"
#include "line_builder.h"
#include "scene/resources/atlas_texture.h"

Line2D::Line2D() {
}
Expand Down Expand Up @@ -188,7 +189,16 @@ Ref<Gradient> Line2D::get_gradient() const {
}

void Line2D::set_texture(const Ref<Texture2D> &p_texture) {
if (_texture.is_valid()) {
_texture->disconnect_changed(callable_mp(this, &Line2D::_texture_changed));
}

_texture = p_texture;

if (_texture.is_valid()) {
_texture->connect_changed(callable_mp(this, &Line2D::_texture_changed));
}

queue_redraw();
}

Expand Down Expand Up @@ -300,13 +310,36 @@ void Line2D::_draw() {

lb.build();

RS::get_singleton()->canvas_item_add_triangle_array(
get_canvas_item(),
lb.indices,
lb.vertices,
lb.colors,
lb.uvs, Vector<int>(), Vector<float>(),
texture_rid);
const Ref<AtlasTexture> atlas = _texture;
if (atlas.is_valid() && atlas->get_atlas().is_valid()) {
const Ref<Texture2D> &texture = atlas->get_atlas();
const Vector2 atlas_size = texture->get_size();

const Vector2 remap_min = atlas->get_region().position / atlas_size;
const Vector2 remap_max = atlas->get_region().get_end() / atlas_size;

PackedVector2Array uvs = lb.uvs;
for (Vector2 &p : uvs) {
p.x = Math::remap(p.x, 0, 1, remap_min.x, remap_max.x);
p.y = Math::remap(p.y, 0, 1, remap_min.y, remap_max.y);
}

RS::get_singleton()->canvas_item_add_triangle_array(
get_canvas_item(),
lb.indices,
lb.vertices,
lb.colors,
uvs, Vector<int>(), Vector<float>(),
texture_rid);
} else {
RS::get_singleton()->canvas_item_add_triangle_array(
get_canvas_item(),
lb.indices,
lb.vertices,
lb.colors,
lb.uvs, Vector<int>(), Vector<float>(),
texture_rid);
}

// DEBUG: Draw wireframe
// if (lb.indices.size() % 3 == 0) {
Expand Down Expand Up @@ -334,6 +367,10 @@ void Line2D::_curve_changed() {
queue_redraw();
}

void Line2D::_texture_changed() {
queue_redraw();
}

// static
void Line2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_points", "points"), &Line2D::set_points);
Expand Down
1 change: 1 addition & 0 deletions scene/2d/line_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class Line2D : public Node2D {
private:
void _gradient_changed();
void _curve_changed();
void _texture_changed();

private:
Vector<Vector2> _points;
Expand Down

0 comments on commit 3f12001

Please sign in to comment.