noise_texture.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. 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 = 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_seamless", "seamless"), &NoiseTexture::set_seamless);
  55. ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture::get_seamless);
  56. ClassDB::bind_method(D_METHOD("set_as_normalmap", "as_normalmap"), &NoiseTexture::set_as_normalmap);
  57. ClassDB::bind_method(D_METHOD("is_normalmap"), &NoiseTexture::is_normalmap);
  58. ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture::set_bump_strength);
  59. ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture::get_bump_strength);
  60. ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture::_update_texture);
  61. ClassDB::bind_method(D_METHOD("_queue_update"), &NoiseTexture::_queue_update);
  62. ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture::_generate_texture);
  63. ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture::_thread_done);
  64. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_width", "get_width");
  65. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_height", "get_height");
  66. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
  67. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normalmap"), "set_as_normalmap", "is_normalmap");
  68. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
  69. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise");
  70. }
  71. void NoiseTexture::_validate_property(PropertyInfo &property) const {
  72. if (property.name == "bump_strength") {
  73. if (!as_normalmap) {
  74. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  75. }
  76. }
  77. }
  78. void NoiseTexture::_set_texture_data(const Ref<Image> &p_image) {
  79. data = p_image;
  80. if (data.is_valid()) {
  81. VS::get_singleton()->texture_allocate(texture, size.x, size.y, 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, flags);
  82. VS::get_singleton()->texture_set_data(texture, p_image);
  83. }
  84. emit_changed();
  85. }
  86. void NoiseTexture::_thread_done(const Ref<Image> &p_image) {
  87. _set_texture_data(p_image);
  88. noise_thread.wait_to_finish();
  89. if (regen_queued) {
  90. noise_thread.start(_thread_function, this);
  91. regen_queued = false;
  92. }
  93. }
  94. void NoiseTexture::_thread_function(void *p_ud) {
  95. NoiseTexture *tex = (NoiseTexture *)p_ud;
  96. tex->call_deferred("_thread_done", tex->_generate_texture());
  97. }
  98. void NoiseTexture::_queue_update() {
  99. if (update_queued)
  100. return;
  101. update_queued = true;
  102. call_deferred("_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);
  115. }
  116. if (as_normalmap) {
  117. image->bumpmap_to_normalmap(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_data(image);
  140. }
  141. update_queued = false;
  142. }
  143. void NoiseTexture::set_noise(Ref<OpenSimplexNoise> p_noise) {
  144. if (p_noise == noise)
  145. return;
  146. if (noise.is_valid()) {
  147. noise->disconnect(CoreStringNames::get_singleton()->changed, this, "_queue_update");
  148. }
  149. noise = p_noise;
  150. if (noise.is_valid()) {
  151. noise->connect(CoreStringNames::get_singleton()->changed, this, "_queue_update");
  152. }
  153. _queue_update();
  154. }
  155. Ref<OpenSimplexNoise> NoiseTexture::get_noise() {
  156. return noise;
  157. }
  158. void NoiseTexture::set_width(int p_width) {
  159. if (p_width == size.x) return;
  160. size.x = p_width;
  161. _queue_update();
  162. }
  163. void NoiseTexture::set_height(int p_height) {
  164. if (p_height == size.y) return;
  165. size.y = p_height;
  166. _queue_update();
  167. }
  168. void NoiseTexture::set_seamless(bool p_seamless) {
  169. if (p_seamless == seamless) return;
  170. seamless = p_seamless;
  171. _queue_update();
  172. }
  173. bool NoiseTexture::get_seamless() {
  174. return seamless;
  175. }
  176. void NoiseTexture::set_as_normalmap(bool p_as_normalmap) {
  177. if (p_as_normalmap == as_normalmap) return;
  178. as_normalmap = p_as_normalmap;
  179. _queue_update();
  180. _change_notify();
  181. }
  182. bool NoiseTexture::is_normalmap() {
  183. return as_normalmap;
  184. }
  185. void NoiseTexture::set_bump_strength(float p_bump_strength) {
  186. if (p_bump_strength == bump_strength) return;
  187. bump_strength = p_bump_strength;
  188. if (as_normalmap)
  189. _queue_update();
  190. }
  191. float NoiseTexture::get_bump_strength() {
  192. return bump_strength;
  193. }
  194. int NoiseTexture::get_width() const {
  195. return size.x;
  196. }
  197. int NoiseTexture::get_height() const {
  198. return size.y;
  199. }
  200. void NoiseTexture::set_flags(uint32_t p_flags) {
  201. flags = p_flags;
  202. VS::get_singleton()->texture_set_flags(texture, flags);
  203. }
  204. uint32_t NoiseTexture::get_flags() const {
  205. return flags;
  206. }
  207. Ref<Image> NoiseTexture::get_data() const {
  208. return data;
  209. }