noise_texture_2d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #include "core/core_string_names.h"
  33. NoiseTexture2D::NoiseTexture2D() {
  34. noise = Ref<Noise>();
  35. _queue_update();
  36. }
  37. NoiseTexture2D::~NoiseTexture2D() {
  38. ERR_FAIL_NULL(RenderingServer::get_singleton());
  39. if (texture.is_valid()) {
  40. RS::get_singleton()->free(texture);
  41. }
  42. if (noise_thread.is_started()) {
  43. noise_thread.wait_to_finish();
  44. }
  45. }
  46. void NoiseTexture2D::_bind_methods() {
  47. ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture2D::_update_texture);
  48. ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture2D::_generate_texture);
  49. ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture2D::_thread_done);
  50. ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture2D::set_width);
  51. ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture2D::set_height);
  52. ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture2D::set_invert);
  53. ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture2D::get_invert);
  54. ClassDB::bind_method(D_METHOD("set_in_3d_space", "enable"), &NoiseTexture2D::set_in_3d_space);
  55. ClassDB::bind_method(D_METHOD("is_in_3d_space"), &NoiseTexture2D::is_in_3d_space);
  56. ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "invert"), &NoiseTexture2D::set_generate_mipmaps);
  57. ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &NoiseTexture2D::is_generating_mipmaps);
  58. ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture2D::set_seamless);
  59. ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture2D::get_seamless);
  60. ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture2D::set_seamless_blend_skirt);
  61. ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture2D::get_seamless_blend_skirt);
  62. ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture2D::set_as_normal_map);
  63. ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture2D::is_normal_map);
  64. ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture2D::set_bump_strength);
  65. ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture2D::get_bump_strength);
  66. ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture2D::set_normalize);
  67. ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture2D::is_normalized);
  68. ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture2D::set_color_ramp);
  69. ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture2D::get_color_ramp);
  70. ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture2D::set_noise);
  71. ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture2D::get_noise);
  72. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");
  73. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");
  74. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");
  75. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "in_3d_space"), "set_in_3d_space", "is_in_3d_space");
  76. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps");
  77. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
  78. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");
  79. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map");
  80. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
  81. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");
  82. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  83. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");
  84. }
  85. void NoiseTexture2D::_validate_property(PropertyInfo &p_property) const {
  86. if (p_property.name == "bump_strength") {
  87. if (!as_normal_map) {
  88. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  89. }
  90. }
  91. if (p_property.name == "seamless_blend_skirt") {
  92. if (!seamless) {
  93. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  94. }
  95. }
  96. }
  97. void NoiseTexture2D::_set_texture_image(const Ref<Image> &p_image) {
  98. image = p_image;
  99. if (image.is_valid()) {
  100. if (texture.is_valid()) {
  101. RID new_texture = RS::get_singleton()->texture_2d_create(p_image);
  102. RS::get_singleton()->texture_replace(texture, new_texture);
  103. } else {
  104. texture = RS::get_singleton()->texture_2d_create(p_image);
  105. }
  106. }
  107. emit_changed();
  108. }
  109. void NoiseTexture2D::_thread_done(const Ref<Image> &p_image) {
  110. _set_texture_image(p_image);
  111. noise_thread.wait_to_finish();
  112. if (regen_queued) {
  113. noise_thread.start(_thread_function, this);
  114. regen_queued = false;
  115. }
  116. }
  117. void NoiseTexture2D::_thread_function(void *p_ud) {
  118. NoiseTexture2D *tex = static_cast<NoiseTexture2D *>(p_ud);
  119. tex->call_deferred(SNAME("_thread_done"), tex->_generate_texture());
  120. }
  121. void NoiseTexture2D::_queue_update() {
  122. if (update_queued) {
  123. return;
  124. }
  125. update_queued = true;
  126. call_deferred(SNAME("_update_texture"));
  127. }
  128. Ref<Image> NoiseTexture2D::_generate_texture() {
  129. // Prevent memdelete due to unref() on other thread.
  130. Ref<Noise> ref_noise = noise;
  131. if (ref_noise.is_null()) {
  132. return Ref<Image>();
  133. }
  134. Ref<Image> new_image;
  135. if (seamless) {
  136. new_image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt, normalize);
  137. } else {
  138. new_image = ref_noise->get_image(size.x, size.y, invert, in_3d_space, normalize);
  139. }
  140. if (color_ramp.is_valid()) {
  141. new_image = _modulate_with_gradient(new_image, color_ramp);
  142. }
  143. if (as_normal_map) {
  144. new_image->bump_map_to_normal_map(bump_strength);
  145. }
  146. if (generate_mipmaps) {
  147. new_image->generate_mipmaps();
  148. }
  149. return new_image;
  150. }
  151. Ref<Image> NoiseTexture2D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {
  152. int width = p_image->get_width();
  153. int height = p_image->get_height();
  154. Ref<Image> new_image = Image::create_empty(width, height, false, Image::FORMAT_RGBA8);
  155. for (int row = 0; row < height; row++) {
  156. for (int col = 0; col < width; col++) {
  157. Color pixel_color = p_image->get_pixel(col, row);
  158. Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());
  159. new_image->set_pixel(col, row, ramp_color);
  160. }
  161. }
  162. return new_image;
  163. }
  164. void NoiseTexture2D::_update_texture() {
  165. bool use_thread = true;
  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(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture2D::_queue_update));
  189. }
  190. noise = p_noise;
  191. if (noise.is_valid()) {
  192. noise->connect(CoreStringNames::get_singleton()->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(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture2D::_queue_update));
  296. }
  297. color_ramp = p_gradient;
  298. if (color_ramp.is_valid()) {
  299. color_ramp->connect(CoreStringNames::get_singleton()->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. }