2
0

noise_texture.cpp 8.6 KB

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