noise_texture.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*************************************************************************/
  2. /* noise_texture.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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.h"
  31. #include "core/core_string_names.h"
  32. #include "noise.h"
  33. NoiseTexture::NoiseTexture() {
  34. noise = Ref<Noise>();
  35. _queue_update();
  36. }
  37. NoiseTexture::~NoiseTexture() {
  38. if (texture.is_valid()) {
  39. RS::get_singleton()->free(texture);
  40. }
  41. noise_thread.wait_to_finish();
  42. }
  43. void NoiseTexture::_bind_methods() {
  44. ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture::set_width);
  45. ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture::set_height);
  46. ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture::set_noise);
  47. ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture::get_noise);
  48. ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture::set_invert);
  49. ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture::get_invert);
  50. ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture::set_seamless);
  51. ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture::get_seamless);
  52. ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture::set_seamless_blend_skirt);
  53. ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture::get_seamless_blend_skirt);
  54. ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture::set_as_normal_map);
  55. ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture::is_normal_map);
  56. ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture::set_bump_strength);
  57. ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture::get_bump_strength);
  58. ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture::_update_texture);
  59. ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture::_generate_texture);
  60. ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture::_thread_done);
  61. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_width", "get_width");
  62. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_height", "get_height");
  63. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");
  64. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
  65. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0.05,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");
  66. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map");
  67. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
  68. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");
  69. }
  70. void NoiseTexture::_validate_property(PropertyInfo &property) const {
  71. if (property.name == "bump_strength") {
  72. if (!as_normal_map) {
  73. property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  74. }
  75. }
  76. if (property.name == "seamless_blend_skirt") {
  77. if (!seamless) {
  78. property.usage = PROPERTY_USAGE_NO_EDITOR;
  79. }
  80. }
  81. }
  82. void NoiseTexture::_set_texture_image(const Ref<Image> &p_image) {
  83. image = p_image;
  84. if (image.is_valid()) {
  85. if (texture.is_valid()) {
  86. RID new_texture = RS::get_singleton()->texture_2d_create(p_image);
  87. RS::get_singleton()->texture_replace(texture, new_texture);
  88. } else {
  89. texture = RS::get_singleton()->texture_2d_create(p_image);
  90. }
  91. }
  92. emit_changed();
  93. }
  94. void NoiseTexture::_thread_done(const Ref<Image> &p_image) {
  95. _set_texture_image(p_image);
  96. noise_thread.wait_to_finish();
  97. if (regen_queued) {
  98. noise_thread.start(_thread_function, this);
  99. regen_queued = false;
  100. }
  101. }
  102. void NoiseTexture::_thread_function(void *p_ud) {
  103. NoiseTexture *tex = static_cast<NoiseTexture *>(p_ud);
  104. tex->call_deferred(SNAME("_thread_done"), tex->_generate_texture());
  105. }
  106. void NoiseTexture::_queue_update() {
  107. if (update_queued) {
  108. return;
  109. }
  110. update_queued = true;
  111. call_deferred(SNAME("_update_texture"));
  112. }
  113. Ref<Image> NoiseTexture::_generate_texture() {
  114. // Prevent memdelete due to unref() on other thread.
  115. Ref<Noise> ref_noise = noise;
  116. if (ref_noise.is_null()) {
  117. return Ref<Image>();
  118. }
  119. Ref<Image> image;
  120. if (seamless) {
  121. image = ref_noise->get_seamless_image(size.x, size.y, invert, seamless_blend_skirt);
  122. } else {
  123. image = ref_noise->get_image(size.x, size.y, invert);
  124. }
  125. if (as_normal_map) {
  126. image->bump_map_to_normal_map(bump_strength);
  127. }
  128. return image;
  129. }
  130. void NoiseTexture::_update_texture() {
  131. bool use_thread = true;
  132. if (first_time) {
  133. use_thread = false;
  134. first_time = false;
  135. }
  136. #ifdef NO_THREADS
  137. use_thread = false;
  138. #endif
  139. if (use_thread) {
  140. if (!noise_thread.is_started()) {
  141. noise_thread.start(_thread_function, this);
  142. regen_queued = false;
  143. } else {
  144. regen_queued = true;
  145. }
  146. } else {
  147. Ref<Image> image = _generate_texture();
  148. _set_texture_image(image);
  149. }
  150. update_queued = false;
  151. }
  152. void NoiseTexture::set_noise(Ref<Noise> p_noise) {
  153. if (p_noise == noise) {
  154. return;
  155. }
  156. if (noise.is_valid()) {
  157. noise->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update));
  158. }
  159. noise = p_noise;
  160. if (noise.is_valid()) {
  161. noise->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update));
  162. }
  163. _queue_update();
  164. }
  165. Ref<Noise> NoiseTexture::get_noise() {
  166. return noise;
  167. }
  168. void NoiseTexture::set_width(int p_width) {
  169. ERR_FAIL_COND(p_width <= 0);
  170. if (p_width == size.x) {
  171. return;
  172. }
  173. size.x = p_width;
  174. _queue_update();
  175. }
  176. void NoiseTexture::set_height(int p_height) {
  177. ERR_FAIL_COND(p_height <= 0);
  178. if (p_height == size.y) {
  179. return;
  180. }
  181. size.y = p_height;
  182. _queue_update();
  183. }
  184. void NoiseTexture::set_invert(bool p_invert) {
  185. if (p_invert == invert) {
  186. return;
  187. }
  188. invert = p_invert;
  189. _queue_update();
  190. }
  191. bool NoiseTexture::get_invert() const {
  192. return invert;
  193. }
  194. void NoiseTexture::set_seamless(bool p_seamless) {
  195. if (p_seamless == seamless) {
  196. return;
  197. }
  198. seamless = p_seamless;
  199. _queue_update();
  200. notify_property_list_changed();
  201. }
  202. bool NoiseTexture::get_seamless() {
  203. return seamless;
  204. }
  205. void NoiseTexture::set_seamless_blend_skirt(real_t p_blend_skirt) {
  206. if (p_blend_skirt == seamless_blend_skirt) {
  207. return;
  208. }
  209. seamless_blend_skirt = p_blend_skirt;
  210. _queue_update();
  211. }
  212. real_t NoiseTexture::get_seamless_blend_skirt() {
  213. return seamless_blend_skirt;
  214. }
  215. void NoiseTexture::set_as_normal_map(bool p_as_normal_map) {
  216. if (p_as_normal_map == as_normal_map) {
  217. return;
  218. }
  219. as_normal_map = p_as_normal_map;
  220. _queue_update();
  221. notify_property_list_changed();
  222. }
  223. bool NoiseTexture::is_normal_map() {
  224. return as_normal_map;
  225. }
  226. void NoiseTexture::set_bump_strength(float p_bump_strength) {
  227. if (p_bump_strength == bump_strength) {
  228. return;
  229. }
  230. bump_strength = p_bump_strength;
  231. if (as_normal_map) {
  232. _queue_update();
  233. }
  234. }
  235. float NoiseTexture::get_bump_strength() {
  236. return bump_strength;
  237. }
  238. int NoiseTexture::get_width() const {
  239. return size.x;
  240. }
  241. int NoiseTexture::get_height() const {
  242. return size.y;
  243. }
  244. RID NoiseTexture::get_rid() const {
  245. if (!texture.is_valid()) {
  246. texture = RS::get_singleton()->texture_2d_placeholder_create();
  247. }
  248. return texture;
  249. }
  250. Ref<Image> NoiseTexture::get_image() const {
  251. return image;
  252. }