noise_texture_2d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 (!Engine::get_singleton()->is_editor_hint()) {
  83. return;
  84. }
  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. RS::get_singleton()->texture_set_path(texture, get_path());
  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. callable_mp(tex, &NoiseTexture2D::_thread_done).call_deferred(tex->_generate_texture());
  120. }
  121. void NoiseTexture2D::_queue_update() {
  122. if (update_queued) {
  123. return;
  124. }
  125. update_queued = true;
  126. callable_mp(this, &NoiseTexture2D::_update_texture).call_deferred();
  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. #ifndef THREADS_ENABLED
  167. use_thread = false;
  168. #endif
  169. if (first_time) {
  170. use_thread = false;
  171. first_time = false;
  172. }
  173. if (use_thread) {
  174. if (!noise_thread.is_started()) {
  175. noise_thread.start(_thread_function, this);
  176. regen_queued = false;
  177. } else {
  178. regen_queued = true;
  179. }
  180. } else {
  181. Ref<Image> new_image = _generate_texture();
  182. _set_texture_image(new_image);
  183. }
  184. update_queued = false;
  185. }
  186. void NoiseTexture2D::set_noise(Ref<Noise> p_noise) {
  187. if (p_noise == noise) {
  188. return;
  189. }
  190. if (noise.is_valid()) {
  191. noise->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
  192. }
  193. noise = p_noise;
  194. if (noise.is_valid()) {
  195. noise->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
  196. }
  197. _queue_update();
  198. }
  199. Ref<Noise> NoiseTexture2D::get_noise() {
  200. return noise;
  201. }
  202. void NoiseTexture2D::set_width(int p_width) {
  203. ERR_FAIL_COND(p_width <= 0);
  204. if (p_width == size.x) {
  205. return;
  206. }
  207. size.x = p_width;
  208. _queue_update();
  209. }
  210. void NoiseTexture2D::set_height(int p_height) {
  211. ERR_FAIL_COND(p_height <= 0);
  212. if (p_height == size.y) {
  213. return;
  214. }
  215. size.y = p_height;
  216. _queue_update();
  217. }
  218. void NoiseTexture2D::set_invert(bool p_invert) {
  219. if (p_invert == invert) {
  220. return;
  221. }
  222. invert = p_invert;
  223. _queue_update();
  224. }
  225. bool NoiseTexture2D::get_invert() const {
  226. return invert;
  227. }
  228. void NoiseTexture2D::set_in_3d_space(bool p_enable) {
  229. if (p_enable == in_3d_space) {
  230. return;
  231. }
  232. in_3d_space = p_enable;
  233. _queue_update();
  234. }
  235. bool NoiseTexture2D::is_in_3d_space() const {
  236. return in_3d_space;
  237. }
  238. void NoiseTexture2D::set_generate_mipmaps(bool p_enable) {
  239. if (p_enable == generate_mipmaps) {
  240. return;
  241. }
  242. generate_mipmaps = p_enable;
  243. _queue_update();
  244. }
  245. bool NoiseTexture2D::is_generating_mipmaps() const {
  246. return generate_mipmaps;
  247. }
  248. void NoiseTexture2D::set_seamless(bool p_seamless) {
  249. if (p_seamless == seamless) {
  250. return;
  251. }
  252. seamless = p_seamless;
  253. _queue_update();
  254. notify_property_list_changed();
  255. }
  256. bool NoiseTexture2D::get_seamless() {
  257. return seamless;
  258. }
  259. void NoiseTexture2D::set_seamless_blend_skirt(real_t p_blend_skirt) {
  260. ERR_FAIL_COND(p_blend_skirt < 0 || p_blend_skirt > 1);
  261. if (p_blend_skirt == seamless_blend_skirt) {
  262. return;
  263. }
  264. seamless_blend_skirt = p_blend_skirt;
  265. _queue_update();
  266. }
  267. real_t NoiseTexture2D::get_seamless_blend_skirt() {
  268. return seamless_blend_skirt;
  269. }
  270. void NoiseTexture2D::set_as_normal_map(bool p_as_normal_map) {
  271. if (p_as_normal_map == as_normal_map) {
  272. return;
  273. }
  274. as_normal_map = p_as_normal_map;
  275. _queue_update();
  276. notify_property_list_changed();
  277. }
  278. bool NoiseTexture2D::is_normal_map() {
  279. return as_normal_map;
  280. }
  281. void NoiseTexture2D::set_bump_strength(float p_bump_strength) {
  282. if (p_bump_strength == bump_strength) {
  283. return;
  284. }
  285. bump_strength = p_bump_strength;
  286. if (as_normal_map) {
  287. _queue_update();
  288. }
  289. }
  290. float NoiseTexture2D::get_bump_strength() {
  291. return bump_strength;
  292. }
  293. void NoiseTexture2D::set_color_ramp(const Ref<Gradient> &p_gradient) {
  294. if (p_gradient == color_ramp) {
  295. return;
  296. }
  297. if (color_ramp.is_valid()) {
  298. color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
  299. }
  300. color_ramp = p_gradient;
  301. if (color_ramp.is_valid()) {
  302. color_ramp->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
  303. }
  304. _queue_update();
  305. }
  306. void NoiseTexture2D::set_normalize(bool p_normalize) {
  307. if (normalize == p_normalize) {
  308. return;
  309. }
  310. normalize = p_normalize;
  311. _queue_update();
  312. }
  313. bool NoiseTexture2D::is_normalized() const {
  314. return normalize;
  315. }
  316. Ref<Gradient> NoiseTexture2D::get_color_ramp() const {
  317. return color_ramp;
  318. }
  319. int NoiseTexture2D::get_width() const {
  320. return size.x;
  321. }
  322. int NoiseTexture2D::get_height() const {
  323. return size.y;
  324. }
  325. RID NoiseTexture2D::get_rid() const {
  326. if (!texture.is_valid()) {
  327. texture = RS::get_singleton()->texture_2d_placeholder_create();
  328. }
  329. return texture;
  330. }
  331. Ref<Image> NoiseTexture2D::get_image() const {
  332. return image;
  333. }