noise_texture_2d.cpp 13 KB

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