noise_texture_2d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**************************************************************************/
  2. /* noise_texture_2d.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 "noise_texture_2d.h"
  31. #include "noise.h"
  32. NoiseTexture2D::NoiseTexture2D() {
  33. noise = Ref<Noise>();
  34. _queue_update();
  35. }
  36. NoiseTexture2D::~NoiseTexture2D() {
  37. ERR_FAIL_NULL(RenderingServer::get_singleton());
  38. if (texture.is_valid()) {
  39. RS::get_singleton()->free(texture);
  40. }
  41. if (noise_thread.is_started()) {
  42. noise_thread.wait_to_finish();
  43. }
  44. }
  45. void NoiseTexture2D::_bind_methods() {
  46. ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture2D::_update_texture);
  47. ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture2D::_generate_texture);
  48. ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture2D::_thread_done);
  49. ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture2D::set_width);
  50. ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture2D::set_height);
  51. ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture2D::set_invert);
  52. ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture2D::get_invert);
  53. ClassDB::bind_method(D_METHOD("set_in_3d_space", "enable"), &NoiseTexture2D::set_in_3d_space);
  54. ClassDB::bind_method(D_METHOD("is_in_3d_space"), &NoiseTexture2D::is_in_3d_space);
  55. ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "invert"), &NoiseTexture2D::set_generate_mipmaps);
  56. ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &NoiseTexture2D::is_generating_mipmaps);
  57. ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture2D::set_seamless);
  58. ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture2D::get_seamless);
  59. ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture2D::set_seamless_blend_skirt);
  60. ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture2D::get_seamless_blend_skirt);
  61. ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture2D::set_as_normal_map);
  62. ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture2D::is_normal_map);
  63. ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture2D::set_bump_strength);
  64. ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture2D::get_bump_strength);
  65. ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture2D::set_normalize);
  66. ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture2D::is_normalized);
  67. ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture2D::set_color_ramp);
  68. ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture2D::get_color_ramp);
  69. ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture2D::set_noise);
  70. ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture2D::get_noise);
  71. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");
  72. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");
  73. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");
  74. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "in_3d_space"), "set_in_3d_space", "is_in_3d_space");
  75. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps");
  76. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
  77. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");
  78. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map");
  79. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
  80. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");
  81. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  82. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");
  83. }
  84. void NoiseTexture2D::_validate_property(PropertyInfo &p_property) const {
  85. if (p_property.name == "bump_strength") {
  86. if (!as_normal_map) {
  87. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  88. }
  89. }
  90. if (p_property.name == "seamless_blend_skirt") {
  91. if (!seamless) {
  92. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  93. }
  94. }
  95. }
  96. void NoiseTexture2D::_set_texture_image(const Ref<Image> &p_image) {
  97. image = p_image;
  98. if (image.is_valid()) {
  99. if (texture.is_valid()) {
  100. RID new_texture = RS::get_singleton()->texture_2d_create(p_image);
  101. RS::get_singleton()->texture_replace(texture, new_texture);
  102. } else {
  103. texture = RS::get_singleton()->texture_2d_create(p_image);
  104. }
  105. }
  106. emit_changed();
  107. }
  108. void NoiseTexture2D::_thread_done(const Ref<Image> &p_image) {
  109. _set_texture_image(p_image);
  110. noise_thread.wait_to_finish();
  111. if (regen_queued) {
  112. noise_thread.start(_thread_function, this);
  113. regen_queued = false;
  114. }
  115. }
  116. void NoiseTexture2D::_thread_function(void *p_ud) {
  117. NoiseTexture2D *tex = static_cast<NoiseTexture2D *>(p_ud);
  118. tex->call_deferred(SNAME("_thread_done"), tex->_generate_texture());
  119. }
  120. void NoiseTexture2D::_queue_update() {
  121. if (update_queued) {
  122. return;
  123. }
  124. update_queued = true;
  125. call_deferred(SNAME("_update_texture"));
  126. }
  127. Ref<Image> NoiseTexture2D::_generate_texture() {
  128. // Prevent memdelete due to unref() on other thread.
  129. Ref<Noise> ref_noise = noise;
  130. if (ref_noise.is_null()) {
  131. return Ref<Image>();
  132. }
  133. Ref<Image> new_image;
  134. if (seamless) {
  135. new_image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt, normalize);
  136. } else {
  137. new_image = ref_noise->get_image(size.x, size.y, invert, in_3d_space, normalize);
  138. }
  139. if (color_ramp.is_valid()) {
  140. new_image = _modulate_with_gradient(new_image, color_ramp);
  141. }
  142. if (as_normal_map) {
  143. new_image->bump_map_to_normal_map(bump_strength);
  144. }
  145. if (generate_mipmaps) {
  146. new_image->generate_mipmaps();
  147. }
  148. return new_image;
  149. }
  150. Ref<Image> NoiseTexture2D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {
  151. int width = p_image->get_width();
  152. int height = p_image->get_height();
  153. Ref<Image> new_image = Image::create_empty(width, height, false, Image::FORMAT_RGBA8);
  154. for (int row = 0; row < height; row++) {
  155. for (int col = 0; col < width; col++) {
  156. Color pixel_color = p_image->get_pixel(col, row);
  157. Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());
  158. new_image->set_pixel(col, row, ramp_color);
  159. }
  160. }
  161. return new_image;
  162. }
  163. void NoiseTexture2D::_update_texture() {
  164. bool use_thread = true;
  165. if (first_time) {
  166. use_thread = false;
  167. first_time = false;
  168. }
  169. if (use_thread) {
  170. if (!noise_thread.is_started()) {
  171. noise_thread.start(_thread_function, this);
  172. regen_queued = false;
  173. } else {
  174. regen_queued = true;
  175. }
  176. } else {
  177. Ref<Image> new_image = _generate_texture();
  178. _set_texture_image(new_image);
  179. }
  180. update_queued = false;
  181. }
  182. void NoiseTexture2D::set_noise(Ref<Noise> p_noise) {
  183. if (p_noise == noise) {
  184. return;
  185. }
  186. if (noise.is_valid()) {
  187. noise->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
  188. }
  189. noise = p_noise;
  190. if (noise.is_valid()) {
  191. noise->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
  192. }
  193. _queue_update();
  194. }
  195. Ref<Noise> NoiseTexture2D::get_noise() {
  196. return noise;
  197. }
  198. void NoiseTexture2D::set_width(int p_width) {
  199. ERR_FAIL_COND(p_width <= 0);
  200. if (p_width == size.x) {
  201. return;
  202. }
  203. size.x = p_width;
  204. _queue_update();
  205. }
  206. void NoiseTexture2D::set_height(int p_height) {
  207. ERR_FAIL_COND(p_height <= 0);
  208. if (p_height == size.y) {
  209. return;
  210. }
  211. size.y = p_height;
  212. _queue_update();
  213. }
  214. void NoiseTexture2D::set_invert(bool p_invert) {
  215. if (p_invert == invert) {
  216. return;
  217. }
  218. invert = p_invert;
  219. _queue_update();
  220. }
  221. bool NoiseTexture2D::get_invert() const {
  222. return invert;
  223. }
  224. void NoiseTexture2D::set_in_3d_space(bool p_enable) {
  225. if (p_enable == in_3d_space) {
  226. return;
  227. }
  228. in_3d_space = p_enable;
  229. _queue_update();
  230. }
  231. bool NoiseTexture2D::is_in_3d_space() const {
  232. return in_3d_space;
  233. }
  234. void NoiseTexture2D::set_generate_mipmaps(bool p_enable) {
  235. if (p_enable == generate_mipmaps) {
  236. return;
  237. }
  238. generate_mipmaps = p_enable;
  239. _queue_update();
  240. }
  241. bool NoiseTexture2D::is_generating_mipmaps() const {
  242. return generate_mipmaps;
  243. }
  244. void NoiseTexture2D::set_seamless(bool p_seamless) {
  245. if (p_seamless == seamless) {
  246. return;
  247. }
  248. seamless = p_seamless;
  249. _queue_update();
  250. notify_property_list_changed();
  251. }
  252. bool NoiseTexture2D::get_seamless() {
  253. return seamless;
  254. }
  255. void NoiseTexture2D::set_seamless_blend_skirt(real_t p_blend_skirt) {
  256. ERR_FAIL_COND(p_blend_skirt < 0 || p_blend_skirt > 1);
  257. if (p_blend_skirt == seamless_blend_skirt) {
  258. return;
  259. }
  260. seamless_blend_skirt = p_blend_skirt;
  261. _queue_update();
  262. }
  263. real_t NoiseTexture2D::get_seamless_blend_skirt() {
  264. return seamless_blend_skirt;
  265. }
  266. void NoiseTexture2D::set_as_normal_map(bool p_as_normal_map) {
  267. if (p_as_normal_map == as_normal_map) {
  268. return;
  269. }
  270. as_normal_map = p_as_normal_map;
  271. _queue_update();
  272. notify_property_list_changed();
  273. }
  274. bool NoiseTexture2D::is_normal_map() {
  275. return as_normal_map;
  276. }
  277. void NoiseTexture2D::set_bump_strength(float p_bump_strength) {
  278. if (p_bump_strength == bump_strength) {
  279. return;
  280. }
  281. bump_strength = p_bump_strength;
  282. if (as_normal_map) {
  283. _queue_update();
  284. }
  285. }
  286. float NoiseTexture2D::get_bump_strength() {
  287. return bump_strength;
  288. }
  289. void NoiseTexture2D::set_color_ramp(const Ref<Gradient> &p_gradient) {
  290. if (p_gradient == color_ramp) {
  291. return;
  292. }
  293. if (color_ramp.is_valid()) {
  294. color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
  295. }
  296. color_ramp = p_gradient;
  297. if (color_ramp.is_valid()) {
  298. color_ramp->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
  299. }
  300. _queue_update();
  301. }
  302. void NoiseTexture2D::set_normalize(bool p_normalize) {
  303. if (normalize == p_normalize) {
  304. return;
  305. }
  306. normalize = p_normalize;
  307. _queue_update();
  308. }
  309. bool NoiseTexture2D::is_normalized() const {
  310. return normalize;
  311. }
  312. Ref<Gradient> NoiseTexture2D::get_color_ramp() const {
  313. return color_ramp;
  314. }
  315. int NoiseTexture2D::get_width() const {
  316. return size.x;
  317. }
  318. int NoiseTexture2D::get_height() const {
  319. return size.y;
  320. }
  321. RID NoiseTexture2D::get_rid() const {
  322. if (!texture.is_valid()) {
  323. texture = RS::get_singleton()->texture_2d_placeholder_create();
  324. }
  325. return texture;
  326. }
  327. Ref<Image> NoiseTexture2D::get_image() const {
  328. return image;
  329. }