noise_texture.cpp 8.7 KB

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