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