gradient.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*************************************************************************/
  2. /* gradient.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GRADIENT_H
  31. #define GRADIENT_H
  32. #include "core/io/resource.h"
  33. class Gradient : public Resource {
  34. GDCLASS(Gradient, Resource);
  35. OBJ_SAVE_TYPE(Gradient);
  36. public:
  37. enum InterpolationMode {
  38. GRADIENT_INTERPOLATE_LINEAR,
  39. GRADIENT_INTERPOLATE_CONSTANT,
  40. GRADIENT_INTERPOLATE_CUBIC,
  41. };
  42. struct Point {
  43. float offset = 0.0;
  44. Color color;
  45. bool operator<(const Point &p_ponit) const {
  46. return offset < p_ponit.offset;
  47. }
  48. };
  49. private:
  50. Vector<Point> points;
  51. bool is_sorted = true;
  52. InterpolationMode interpolation_mode = GRADIENT_INTERPOLATE_LINEAR;
  53. _FORCE_INLINE_ void _update_sorting() {
  54. if (!is_sorted) {
  55. points.sort();
  56. is_sorted = true;
  57. }
  58. }
  59. protected:
  60. static void _bind_methods();
  61. public:
  62. Gradient();
  63. virtual ~Gradient();
  64. void add_point(float p_offset, const Color &p_color);
  65. void remove_point(int p_index);
  66. void set_points(const Vector<Point> &p_points);
  67. Vector<Point> &get_points();
  68. void reverse();
  69. void set_offset(int pos, const float offset);
  70. float get_offset(int pos);
  71. void set_color(int pos, const Color &color);
  72. Color get_color(int pos);
  73. void set_offsets(const Vector<float> &p_offsets);
  74. Vector<float> get_offsets() const;
  75. void set_colors(const Vector<Color> &p_colors);
  76. Vector<Color> get_colors() const;
  77. void set_interpolation_mode(InterpolationMode p_interp_mode);
  78. InterpolationMode get_interpolation_mode();
  79. _FORCE_INLINE_ Color get_color_at_offset(float p_offset) {
  80. if (points.is_empty()) {
  81. return Color(0, 0, 0, 1);
  82. }
  83. _update_sorting();
  84. // Binary search.
  85. int low = 0;
  86. int high = points.size() - 1;
  87. int middle = 0;
  88. #ifdef DEBUG_ENABLED
  89. if (low > high) {
  90. ERR_PRINT("low > high, this may be a bug");
  91. }
  92. #endif
  93. while (low <= high) {
  94. middle = (low + high) / 2;
  95. const Point &point = points[middle];
  96. if (point.offset > p_offset) {
  97. high = middle - 1; //search low end of array
  98. } else if (point.offset < p_offset) {
  99. low = middle + 1; //search high end of array
  100. } else {
  101. return point.color;
  102. }
  103. }
  104. // Return sampled value.
  105. if (points[middle].offset > p_offset) {
  106. middle--;
  107. }
  108. int first = middle;
  109. int second = middle + 1;
  110. if (second >= points.size()) {
  111. return points[points.size() - 1].color;
  112. }
  113. if (first < 0) {
  114. return points[0].color;
  115. }
  116. const Point &pointFirst = points[first];
  117. const Point &pointSecond = points[second];
  118. switch (interpolation_mode) {
  119. case GRADIENT_INTERPOLATE_LINEAR: {
  120. return pointFirst.color.lerp(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset));
  121. } break;
  122. case GRADIENT_INTERPOLATE_CONSTANT: {
  123. return pointFirst.color;
  124. } break;
  125. case GRADIENT_INTERPOLATE_CUBIC: {
  126. int p0 = first - 1;
  127. int p3 = second + 1;
  128. if (p3 >= points.size()) {
  129. p3 = second;
  130. }
  131. if (p0 < 0) {
  132. p0 = first;
  133. }
  134. const Point &pointP0 = points[p0];
  135. const Point &pointP3 = points[p3];
  136. float x = (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset);
  137. float r = Math::cubic_interpolate(pointFirst.color.r, pointSecond.color.r, pointP0.color.r, pointP3.color.r, x);
  138. float g = Math::cubic_interpolate(pointFirst.color.g, pointSecond.color.g, pointP0.color.g, pointP3.color.g, x);
  139. float b = Math::cubic_interpolate(pointFirst.color.b, pointSecond.color.b, pointP0.color.b, pointP3.color.b, x);
  140. float a = Math::cubic_interpolate(pointFirst.color.a, pointSecond.color.a, pointP0.color.a, pointP3.color.a, x);
  141. return Color(r, g, b, a);
  142. } break;
  143. default: {
  144. // Fallback to linear interpolation.
  145. return pointFirst.color.lerp(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset));
  146. }
  147. }
  148. }
  149. int get_points_count() const;
  150. };
  151. VARIANT_ENUM_CAST(Gradient::InterpolationMode);
  152. #endif // GRADIENT_H