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