noise_texture.cpp 8.2 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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. update_queued = false;
  34. noise_thread = nullptr;
  35. regen_queued = false;
  36. first_time = true;
  37. size = Vector2i(512, 512);
  38. seamless = false;
  39. as_normalmap = false;
  40. bump_strength = 8.0;
  41. noise = Ref<OpenSimplexNoise>();
  42. _queue_update();
  43. }
  44. NoiseTexture::~NoiseTexture() {
  45. if (texture.is_valid()) {
  46. RS::get_singleton()->free(texture);
  47. }
  48. if (noise_thread) {
  49. Thread::wait_to_finish(noise_thread);
  50. memdelete(noise_thread);
  51. }
  52. }
  53. void NoiseTexture::_bind_methods() {
  54. ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture::set_width);
  55. ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture::set_height);
  56. ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture::set_noise);
  57. ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture::get_noise);
  58. ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture::set_seamless);
  59. ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture::get_seamless);
  60. ClassDB::bind_method(D_METHOD("set_as_normalmap", "as_normalmap"), &NoiseTexture::set_as_normalmap);
  61. ClassDB::bind_method(D_METHOD("is_normalmap"), &NoiseTexture::is_normalmap);
  62. ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture::set_bump_strength);
  63. ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture::get_bump_strength);
  64. ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture::_update_texture);
  65. ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture::_generate_texture);
  66. ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture::_thread_done);
  67. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_width", "get_width");
  68. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_height", "get_height");
  69. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
  70. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normalmap"), "set_as_normalmap", "is_normalmap");
  71. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
  72. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise");
  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. if (texture.is_valid()) {
  85. RID new_texture = RS::get_singleton()->texture_2d_create(p_image);
  86. RS::get_singleton()->texture_replace(texture, new_texture);
  87. } else {
  88. texture = RS::get_singleton()->texture_2d_create(p_image);
  89. }
  90. }
  91. emit_changed();
  92. }
  93. void NoiseTexture::_thread_done(const Ref<Image> &p_image) {
  94. _set_texture_data(p_image);
  95. Thread::wait_to_finish(noise_thread);
  96. memdelete(noise_thread);
  97. noise_thread = nullptr;
  98. if (regen_queued) {
  99. noise_thread = Thread::create(_thread_function, this);
  100. regen_queued = false;
  101. }
  102. }
  103. void NoiseTexture::_thread_function(void *p_ud) {
  104. NoiseTexture *tex = (NoiseTexture *)p_ud;
  105. tex->call_deferred("_thread_done", tex->_generate_texture());
  106. }
  107. void NoiseTexture::_queue_update() {
  108. if (update_queued)
  109. return;
  110. update_queued = true;
  111. call_deferred("_update_texture");
  112. }
  113. Ref<Image> NoiseTexture::_generate_texture() {
  114. // Prevent memdelete due to unref() on other thread.
  115. Ref<OpenSimplexNoise> 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);
  122. } else {
  123. image = ref_noise->get_image(size.x, size.y);
  124. }
  125. if (as_normalmap) {
  126. image->bumpmap_to_normalmap(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) {
  141. noise_thread = Thread::create(_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_data(image);
  149. }
  150. update_queued = false;
  151. }
  152. void NoiseTexture::set_noise(Ref<OpenSimplexNoise> p_noise) {
  153. if (p_noise == noise)
  154. return;
  155. if (noise.is_valid()) {
  156. noise->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update));
  157. }
  158. noise = p_noise;
  159. if (noise.is_valid()) {
  160. noise->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update));
  161. }
  162. _queue_update();
  163. }
  164. Ref<OpenSimplexNoise> NoiseTexture::get_noise() {
  165. return noise;
  166. }
  167. void NoiseTexture::set_width(int p_width) {
  168. if (p_width == size.x)
  169. return;
  170. size.x = p_width;
  171. _queue_update();
  172. }
  173. void NoiseTexture::set_height(int p_height) {
  174. if (p_height == size.y)
  175. return;
  176. size.y = p_height;
  177. _queue_update();
  178. }
  179. void NoiseTexture::set_seamless(bool p_seamless) {
  180. if (p_seamless == seamless)
  181. return;
  182. seamless = p_seamless;
  183. _queue_update();
  184. }
  185. bool NoiseTexture::get_seamless() {
  186. return seamless;
  187. }
  188. void NoiseTexture::set_as_normalmap(bool p_as_normalmap) {
  189. if (p_as_normalmap == as_normalmap)
  190. return;
  191. as_normalmap = p_as_normalmap;
  192. _queue_update();
  193. _change_notify();
  194. }
  195. bool NoiseTexture::is_normalmap() {
  196. return as_normalmap;
  197. }
  198. void NoiseTexture::set_bump_strength(float p_bump_strength) {
  199. if (p_bump_strength == bump_strength)
  200. return;
  201. bump_strength = p_bump_strength;
  202. if (as_normalmap)
  203. _queue_update();
  204. }
  205. float NoiseTexture::get_bump_strength() {
  206. return bump_strength;
  207. }
  208. int NoiseTexture::get_width() const {
  209. return size.x;
  210. }
  211. int NoiseTexture::get_height() const {
  212. return size.y;
  213. }
  214. RID NoiseTexture::get_rid() const {
  215. if (!texture.is_valid()) {
  216. texture = RS::get_singleton()->texture_2d_placeholder_create();
  217. }
  218. return texture;
  219. }
  220. Ref<Image> NoiseTexture::get_data() const {
  221. return data;
  222. }