gradient_texture.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /**************************************************************************/
  2. /* gradient_texture.cpp */
  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. #include "gradient_texture.h"
  31. #include "core/math/geometry_2d.h"
  32. GradientTexture1D::GradientTexture1D() {
  33. _queue_update();
  34. }
  35. GradientTexture1D::~GradientTexture1D() {
  36. if (texture.is_valid()) {
  37. ERR_FAIL_NULL(RenderingServer::get_singleton());
  38. RS::get_singleton()->free(texture);
  39. }
  40. }
  41. void GradientTexture1D::_bind_methods() {
  42. ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture1D::set_gradient);
  43. ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture1D::get_gradient);
  44. ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture1D::set_width);
  45. // The `get_width()` method is already exposed by the parent class Texture2D.
  46. ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture1D::set_use_hdr);
  47. ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture1D::is_using_hdr);
  48. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
  49. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,16384,suffix:px"), "set_width", "get_width");
  50. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
  51. }
  52. void GradientTexture1D::set_gradient(Ref<Gradient> p_gradient) {
  53. if (p_gradient == gradient) {
  54. return;
  55. }
  56. if (gradient.is_valid()) {
  57. gradient->disconnect_changed(callable_mp(this, &GradientTexture1D::_queue_update));
  58. }
  59. gradient = p_gradient;
  60. if (gradient.is_valid()) {
  61. gradient->connect_changed(callable_mp(this, &GradientTexture1D::_queue_update));
  62. }
  63. _queue_update();
  64. emit_changed();
  65. }
  66. Ref<Gradient> GradientTexture1D::get_gradient() const {
  67. return gradient;
  68. }
  69. void GradientTexture1D::_queue_update() {
  70. if (update_pending) {
  71. return;
  72. }
  73. update_pending = true;
  74. callable_mp(this, &GradientTexture1D::update_now).call_deferred();
  75. }
  76. void GradientTexture1D::_update() const {
  77. update_pending = false;
  78. if (gradient.is_null()) {
  79. return;
  80. }
  81. if (use_hdr) {
  82. // High dynamic range.
  83. Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBAF));
  84. Gradient &g = **gradient;
  85. // `create()` isn't available for non-uint8_t data, so fill in the data manually.
  86. for (int i = 0; i < width; i++) {
  87. float ofs = float(i) / (width - 1);
  88. image->set_pixel(i, 0, g.get_color_at_offset(ofs));
  89. }
  90. if (texture.is_valid()) {
  91. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  92. RS::get_singleton()->texture_replace(texture, new_texture);
  93. } else {
  94. texture = RS::get_singleton()->texture_2d_create(image);
  95. }
  96. } else {
  97. // Low dynamic range. "Overbright" colors will be clamped.
  98. Vector<uint8_t> data;
  99. data.resize(width * 4);
  100. {
  101. uint8_t *wd8 = data.ptrw();
  102. Gradient &g = **gradient;
  103. for (int i = 0; i < width; i++) {
  104. float ofs = float(i) / (width - 1);
  105. Color color = g.get_color_at_offset(ofs);
  106. wd8[i * 4 + 0] = uint8_t(CLAMP(color.r * 255.0, 0, 255));
  107. wd8[i * 4 + 1] = uint8_t(CLAMP(color.g * 255.0, 0, 255));
  108. wd8[i * 4 + 2] = uint8_t(CLAMP(color.b * 255.0, 0, 255));
  109. wd8[i * 4 + 3] = uint8_t(CLAMP(color.a * 255.0, 0, 255));
  110. }
  111. }
  112. Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data));
  113. if (texture.is_valid()) {
  114. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  115. RS::get_singleton()->texture_replace(texture, new_texture);
  116. } else {
  117. texture = RS::get_singleton()->texture_2d_create(image);
  118. }
  119. }
  120. RS::get_singleton()->texture_set_path(texture, get_path());
  121. }
  122. void GradientTexture1D::set_width(int p_width) {
  123. ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
  124. width = p_width;
  125. _queue_update();
  126. emit_changed();
  127. }
  128. int GradientTexture1D::get_width() const {
  129. return width;
  130. }
  131. void GradientTexture1D::set_use_hdr(bool p_enabled) {
  132. if (p_enabled == use_hdr) {
  133. return;
  134. }
  135. use_hdr = p_enabled;
  136. _queue_update();
  137. emit_changed();
  138. }
  139. bool GradientTexture1D::is_using_hdr() const {
  140. return use_hdr;
  141. }
  142. RID GradientTexture1D::get_rid() const {
  143. if (!texture.is_valid()) {
  144. texture = RS::get_singleton()->texture_2d_placeholder_create();
  145. }
  146. return texture;
  147. }
  148. Ref<Image> GradientTexture1D::get_image() const {
  149. update_now();
  150. if (!texture.is_valid()) {
  151. return Ref<Image>();
  152. }
  153. return RenderingServer::get_singleton()->texture_2d_get(texture);
  154. }
  155. void GradientTexture1D::update_now() const {
  156. if (update_pending) {
  157. _update();
  158. }
  159. }
  160. //////////////////
  161. GradientTexture2D::GradientTexture2D() {
  162. _queue_update();
  163. }
  164. GradientTexture2D::~GradientTexture2D() {
  165. if (texture.is_valid()) {
  166. ERR_FAIL_NULL(RenderingServer::get_singleton());
  167. RS::get_singleton()->free(texture);
  168. }
  169. }
  170. void GradientTexture2D::set_gradient(Ref<Gradient> p_gradient) {
  171. if (gradient == p_gradient) {
  172. return;
  173. }
  174. if (gradient.is_valid()) {
  175. gradient->disconnect_changed(callable_mp(this, &GradientTexture2D::_queue_update));
  176. }
  177. gradient = p_gradient;
  178. if (gradient.is_valid()) {
  179. gradient->connect_changed(callable_mp(this, &GradientTexture2D::_queue_update));
  180. }
  181. _queue_update();
  182. emit_changed();
  183. }
  184. Ref<Gradient> GradientTexture2D::get_gradient() const {
  185. return gradient;
  186. }
  187. void GradientTexture2D::_queue_update() {
  188. if (update_pending) {
  189. return;
  190. }
  191. update_pending = true;
  192. callable_mp(this, &GradientTexture2D::update_now).call_deferred();
  193. }
  194. void GradientTexture2D::_update() const {
  195. update_pending = false;
  196. if (gradient.is_null()) {
  197. return;
  198. }
  199. Ref<Image> image;
  200. image.instantiate();
  201. if (gradient->get_point_count() <= 1) { // No need to interpolate.
  202. image->initialize_data(width, height, false, (use_hdr) ? Image::FORMAT_RGBAF : Image::FORMAT_RGBA8);
  203. image->fill((gradient->get_point_count() == 1) ? gradient->get_color(0) : Color(0, 0, 0, 1));
  204. } else {
  205. if (use_hdr) {
  206. image->initialize_data(width, height, false, Image::FORMAT_RGBAF);
  207. Gradient &g = **gradient;
  208. // `create()` isn't available for non-uint8_t data, so fill in the data manually.
  209. for (int y = 0; y < height; y++) {
  210. for (int x = 0; x < width; x++) {
  211. float ofs = _get_gradient_offset_at(x, y);
  212. image->set_pixel(x, y, g.get_color_at_offset(ofs));
  213. }
  214. }
  215. } else {
  216. Vector<uint8_t> data;
  217. data.resize(width * height * 4);
  218. {
  219. uint8_t *wd8 = data.ptrw();
  220. Gradient &g = **gradient;
  221. for (int y = 0; y < height; y++) {
  222. for (int x = 0; x < width; x++) {
  223. float ofs = _get_gradient_offset_at(x, y);
  224. const Color &c = g.get_color_at_offset(ofs);
  225. wd8[(x + (y * width)) * 4 + 0] = uint8_t(CLAMP(c.r * 255.0, 0, 255));
  226. wd8[(x + (y * width)) * 4 + 1] = uint8_t(CLAMP(c.g * 255.0, 0, 255));
  227. wd8[(x + (y * width)) * 4 + 2] = uint8_t(CLAMP(c.b * 255.0, 0, 255));
  228. wd8[(x + (y * width)) * 4 + 3] = uint8_t(CLAMP(c.a * 255.0, 0, 255));
  229. }
  230. }
  231. }
  232. image->set_data(width, height, false, Image::FORMAT_RGBA8, data);
  233. }
  234. }
  235. if (texture.is_valid()) {
  236. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  237. RS::get_singleton()->texture_replace(texture, new_texture);
  238. } else {
  239. texture = RS::get_singleton()->texture_2d_create(image);
  240. }
  241. RS::get_singleton()->texture_set_path(texture, get_path());
  242. }
  243. float GradientTexture2D::_get_gradient_offset_at(int x, int y) const {
  244. if (fill_to == fill_from) {
  245. return 0;
  246. }
  247. float ofs = 0;
  248. Vector2 pos;
  249. if (width > 1) {
  250. pos.x = static_cast<float>(x) / (width - 1);
  251. }
  252. if (height > 1) {
  253. pos.y = static_cast<float>(y) / (height - 1);
  254. }
  255. if (fill == Fill::FILL_LINEAR) {
  256. Vector2 segment[2];
  257. segment[0] = fill_from;
  258. segment[1] = fill_to;
  259. Vector2 closest = Geometry2D::get_closest_point_to_segment_uncapped(pos, &segment[0]);
  260. ofs = (closest - fill_from).length() / (fill_to - fill_from).length();
  261. if ((closest - fill_from).dot(fill_to - fill_from) < 0) {
  262. ofs *= -1;
  263. }
  264. } else if (fill == Fill::FILL_RADIAL) {
  265. ofs = (pos - fill_from).length() / (fill_to - fill_from).length();
  266. } else if (fill == Fill::FILL_SQUARE) {
  267. 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));
  268. }
  269. if (repeat == Repeat::REPEAT_NONE) {
  270. ofs = CLAMP(ofs, 0.0, 1.0);
  271. } else if (repeat == Repeat::REPEAT) {
  272. ofs = Math::fmod(ofs, 1.0f);
  273. if (ofs < 0) {
  274. ofs = 1 + ofs;
  275. }
  276. } else if (repeat == Repeat::REPEAT_MIRROR) {
  277. ofs = Math::abs(ofs);
  278. ofs = Math::fmod(ofs, 2.0f);
  279. if (ofs > 1.0) {
  280. ofs = 2.0 - ofs;
  281. }
  282. }
  283. return ofs;
  284. }
  285. void GradientTexture2D::set_width(int p_width) {
  286. ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
  287. width = p_width;
  288. _queue_update();
  289. emit_changed();
  290. }
  291. int GradientTexture2D::get_width() const {
  292. return width;
  293. }
  294. void GradientTexture2D::set_height(int p_height) {
  295. ERR_FAIL_COND_MSG(p_height <= 0 || p_height > 16384, "Texture dimensions have to be within 1 to 16384 range.");
  296. height = p_height;
  297. _queue_update();
  298. emit_changed();
  299. }
  300. int GradientTexture2D::get_height() const {
  301. return height;
  302. }
  303. void GradientTexture2D::set_use_hdr(bool p_enabled) {
  304. if (p_enabled == use_hdr) {
  305. return;
  306. }
  307. use_hdr = p_enabled;
  308. _queue_update();
  309. emit_changed();
  310. }
  311. bool GradientTexture2D::is_using_hdr() const {
  312. return use_hdr;
  313. }
  314. void GradientTexture2D::set_fill_from(Vector2 p_fill_from) {
  315. fill_from = p_fill_from;
  316. _queue_update();
  317. emit_changed();
  318. }
  319. Vector2 GradientTexture2D::get_fill_from() const {
  320. return fill_from;
  321. }
  322. void GradientTexture2D::set_fill_to(Vector2 p_fill_to) {
  323. fill_to = p_fill_to;
  324. _queue_update();
  325. emit_changed();
  326. }
  327. Vector2 GradientTexture2D::get_fill_to() const {
  328. return fill_to;
  329. }
  330. void GradientTexture2D::set_fill(Fill p_fill) {
  331. fill = p_fill;
  332. _queue_update();
  333. emit_changed();
  334. }
  335. GradientTexture2D::Fill GradientTexture2D::get_fill() const {
  336. return fill;
  337. }
  338. void GradientTexture2D::set_repeat(Repeat p_repeat) {
  339. repeat = p_repeat;
  340. _queue_update();
  341. emit_changed();
  342. }
  343. GradientTexture2D::Repeat GradientTexture2D::get_repeat() const {
  344. return repeat;
  345. }
  346. RID GradientTexture2D::get_rid() const {
  347. if (!texture.is_valid()) {
  348. texture = RS::get_singleton()->texture_2d_placeholder_create();
  349. }
  350. return texture;
  351. }
  352. Ref<Image> GradientTexture2D::get_image() const {
  353. update_now();
  354. if (!texture.is_valid()) {
  355. return Ref<Image>();
  356. }
  357. return RenderingServer::get_singleton()->texture_2d_get(texture);
  358. }
  359. void GradientTexture2D::update_now() const {
  360. if (update_pending) {
  361. _update();
  362. }
  363. }
  364. void GradientTexture2D::_bind_methods() {
  365. ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture2D::set_gradient);
  366. ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture2D::get_gradient);
  367. ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture2D::set_width);
  368. ClassDB::bind_method(D_METHOD("set_height", "height"), &GradientTexture2D::set_height);
  369. ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture2D::set_use_hdr);
  370. ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture2D::is_using_hdr);
  371. ClassDB::bind_method(D_METHOD("set_fill", "fill"), &GradientTexture2D::set_fill);
  372. ClassDB::bind_method(D_METHOD("get_fill"), &GradientTexture2D::get_fill);
  373. ClassDB::bind_method(D_METHOD("set_fill_from", "fill_from"), &GradientTexture2D::set_fill_from);
  374. ClassDB::bind_method(D_METHOD("get_fill_from"), &GradientTexture2D::get_fill_from);
  375. ClassDB::bind_method(D_METHOD("set_fill_to", "fill_to"), &GradientTexture2D::set_fill_to);
  376. ClassDB::bind_method(D_METHOD("get_fill_to"), &GradientTexture2D::get_fill_to);
  377. ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &GradientTexture2D::set_repeat);
  378. ClassDB::bind_method(D_METHOD("get_repeat"), &GradientTexture2D::get_repeat);
  379. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
  380. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width");
  381. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height");
  382. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
  383. ADD_GROUP("Fill", "fill_");
  384. ADD_PROPERTY(PropertyInfo(Variant::INT, "fill", PROPERTY_HINT_ENUM, "Linear,Radial,Square"), "set_fill", "get_fill");
  385. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_from"), "set_fill_from", "get_fill_from");
  386. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_to"), "set_fill_to", "get_fill_to");
  387. ADD_GROUP("Repeat", "repeat_");
  388. ADD_PROPERTY(PropertyInfo(Variant::INT, "repeat", PROPERTY_HINT_ENUM, "No Repeat,Repeat,Mirror Repeat"), "set_repeat", "get_repeat");
  389. BIND_ENUM_CONSTANT(FILL_LINEAR);
  390. BIND_ENUM_CONSTANT(FILL_RADIAL);
  391. BIND_ENUM_CONSTANT(FILL_SQUARE);
  392. BIND_ENUM_CONSTANT(REPEAT_NONE);
  393. BIND_ENUM_CONSTANT(REPEAT);
  394. BIND_ENUM_CONSTANT(REPEAT_MIRROR);
  395. }