From 0a4a1bc4b82581b64735ca76544f71e87a6bf435 Mon Sep 17 00:00:00 2001 From: VolTer Date: Mon, 17 Apr 2023 02:56:25 +0200 Subject: [PATCH] Add a square fill mode to GradientTexture2D --- doc/classes/GradientTexture2D.xml | 3 +++ scene/resources/texture.cpp | 5 ++++- scene/resources/texture.h | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/classes/GradientTexture2D.xml b/doc/classes/GradientTexture2D.xml index 2d425035ca7..9522de3528e 100644 --- a/doc/classes/GradientTexture2D.xml +++ b/doc/classes/GradientTexture2D.xml @@ -42,6 +42,9 @@ The colors are linearly interpolated in a circular pattern. + + The colors are linearly interpolated in a square pattern. + The gradient fill is restricted to the range defined by [member fill_from] to [member fill_to] offsets. diff --git a/scene/resources/texture.cpp b/scene/resources/texture.cpp index 282c531555f..5edb31b1a7d 100644 --- a/scene/resources/texture.cpp +++ b/scene/resources/texture.cpp @@ -2427,6 +2427,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); @@ -2555,7 +2557,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"); @@ -2564,6 +2566,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); diff --git a/scene/resources/texture.h b/scene/resources/texture.h index 50bcec58f6e..6b5e87ae218 100644 --- a/scene/resources/texture.h +++ b/scene/resources/texture.h @@ -829,6 +829,7 @@ public: enum Fill { FILL_LINEAR, FILL_RADIAL, + FILL_SQUARE, }; enum Repeat { REPEAT_NONE,