Skip to content

Commit

Permalink
Merge pull request #76151 from MewPurPur/square-gradient2d
Browse files Browse the repository at this point in the history
Add a square fill mode to GradientTexture2D
  • Loading branch information
YuriSizov authored May 29, 2023
2 parents b986d30 + 0a4a1bc commit c5e9892
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/classes/GradientTexture2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<constant name="FILL_RADIAL" value="1" enum="Fill">
The colors are linearly interpolated in a circular pattern.
</constant>
<constant name="FILL_SQUARE" value="2" enum="Fill">
The colors are linearly interpolated in a square pattern.
</constant>
<constant name="REPEAT_NONE" value="0" enum="Repeat">
The gradient fill is restricted to the range defined by [member fill_from] to [member fill_to] offsets.
</constant>
Expand Down
5 changes: 4 additions & 1 deletion scene/resources/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,8 @@ float GradientTexture2D::_get_gradient_offset_at(int x, int y) const {
}
} else if (fill == Fill::FILL_RADIAL) {
ofs = (pos - fill_from).length() / (fill_to - fill_from).length();
} else if (fill == Fill::FILL_SQUARE) {
ofs = MAX(Math::abs(pos.x - fill_from.x), Math::abs(pos.y - fill_from.y)) / MAX(Math::abs(fill_to.x - fill_from.x), Math::abs(fill_to.y - fill_from.y));
}
if (repeat == Repeat::REPEAT_NONE) {
ofs = CLAMP(ofs, 0.0, 1.0);
Expand Down Expand Up @@ -2556,7 +2558,7 @@ void GradientTexture2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");

ADD_GROUP("Fill", "fill_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "fill", PROPERTY_HINT_ENUM, "Linear,Radial"), "set_fill", "get_fill");
ADD_PROPERTY(PropertyInfo(Variant::INT, "fill", PROPERTY_HINT_ENUM, "Linear,Radial,Square"), "set_fill", "get_fill");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_from"), "set_fill_from", "get_fill_from");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_to"), "set_fill_to", "get_fill_to");

Expand All @@ -2565,6 +2567,7 @@ void GradientTexture2D::_bind_methods() {

BIND_ENUM_CONSTANT(FILL_LINEAR);
BIND_ENUM_CONSTANT(FILL_RADIAL);
BIND_ENUM_CONSTANT(FILL_SQUARE);

BIND_ENUM_CONSTANT(REPEAT_NONE);
BIND_ENUM_CONSTANT(REPEAT);
Expand Down
1 change: 1 addition & 0 deletions scene/resources/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ class GradientTexture2D : public Texture2D {
enum Fill {
FILL_LINEAR,
FILL_RADIAL,
FILL_SQUARE,
};
enum Repeat {
REPEAT_NONE,
Expand Down

0 comments on commit c5e9892

Please sign in to comment.