gradient.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**************************************************************************/
  2. /* gradient.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/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;
  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;
  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(Vector<Point> &p_points);
  67. Vector<Point> &get_points();
  68. void set_offset(int pos, const float offset);
  69. float get_offset(int pos);
  70. void set_color(int pos, const Color &color);
  71. Color get_color(int pos);
  72. void set_offsets(const Vector<float> &p_offsets);
  73. Vector<float> get_offsets() const;
  74. void set_colors(const Vector<Color> &p_colors);
  75. Vector<Color> get_colors() const;
  76. void set_interpolation_mode(InterpolationMode p_interp_mode);
  77. InterpolationMode get_interpolation_mode();
  78. _FORCE_INLINE_ float cubic_interpolate(float p0, float p1, float p2, float p3, float x) {
  79. return p1 + 0.5 * x * (p2 - p0 + x * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3 + x * (3.0 * (p1 - p2) + p3 - p0)));
  80. }
  81. _FORCE_INLINE_ Color get_color_at_offset(float p_offset) {
  82. if (points.empty()) {
  83. return Color(0, 0, 0, 1);
  84. }
  85. _update_sorting();
  86. //binary search
  87. int low = 0;
  88. int high = points.size() - 1;
  89. int middle = 0;
  90. #ifdef DEBUG_ENABLED
  91. if (low > high)
  92. ERR_PRINT("low > high, this may be a bug");
  93. #endif
  94. while (low <= high) {
  95. middle = (low + high) / 2;
  96. const Point &point = points[middle];
  97. if (point.offset > p_offset) {
  98. high = middle - 1; //search low end of array
  99. } else if (point.offset < p_offset) {
  100. low = middle + 1; //search high end of array
  101. } else {
  102. return point.color;
  103. }
  104. }
  105. //return interpolated value
  106. if (points[middle].offset > p_offset) {
  107. middle--;
  108. }
  109. int first = middle;
  110. int second = middle + 1;
  111. if (second >= points.size()) {
  112. return points[points.size() - 1].color;
  113. }
  114. if (first < 0) {
  115. return points[0].color;
  116. }
  117. const Point &pointFirst = points[first];
  118. const Point &pointSecond = points[second];
  119. switch (interpolation_mode) {
  120. case GRADIENT_INTERPOLATE_LINEAR: {
  121. return pointFirst.color.linear_interpolate(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset));
  122. } break;
  123. case GRADIENT_INTERPOLATE_CONSTANT: {
  124. return pointFirst.color;
  125. } break;
  126. case GRADIENT_INTERPOLATE_CUBIC: {
  127. int p0 = first - 1;
  128. int p3 = second + 1;
  129. if (p3 >= points.size()) {
  130. p3 = second;
  131. }
  132. if (p0 < 0) {
  133. p0 = first;
  134. }
  135. const Point &pointP0 = points[p0];
  136. const Point &pointP3 = points[p3];
  137. float x = (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset);
  138. float r = cubic_interpolate(pointP0.color.r, pointFirst.color.r, pointSecond.color.r, pointP3.color.r, x);
  139. float g = cubic_interpolate(pointP0.color.g, pointFirst.color.g, pointSecond.color.g, pointP3.color.g, x);
  140. float b = cubic_interpolate(pointP0.color.b, pointFirst.color.b, pointSecond.color.b, pointP3.color.b, x);
  141. float a = cubic_interpolate(pointP0.color.a, pointFirst.color.a, pointSecond.color.a, pointP3.color.a, x);
  142. return Color(r, g, b, a);
  143. } break;
  144. default: {
  145. // Fallback to linear interpolation.
  146. return pointFirst.color.linear_interpolate(pointSecond.color, (p_offset - pointFirst.offset) / (pointSecond.offset - pointFirst.offset));
  147. }
  148. }
  149. }
  150. int get_points_count() const;
  151. };
  152. VARIANT_ENUM_CAST(Gradient::InterpolationMode);
  153. #endif // GRADIENT_H