noise_texture.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*************************************************************************/
  2. /* noise_texture.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 = NULL;
  35. regen_queued = false;
  36. first_time = true;
  37. size = Vector2i(512, 512);
  38. seamless = false;
  39. as_normalmap = false;
  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. }
  48. void NoiseTexture::_bind_methods() {
  49. ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture::set_width);
  50. ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture::set_height);
  51. ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture::set_noise);
  52. ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture::get_noise);
  53. ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture::set_seamless);
  54. ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture::get_seamless);
  55. ClassDB::bind_method(D_METHOD("set_as_normalmap", "as_normalmap"), &NoiseTexture::set_as_normalmap);
  56. ClassDB::bind_method(D_METHOD("is_normalmap"), &NoiseTexture::is_normalmap);
  57. ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture::_update_texture);
  58. ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture::_generate_texture);
  59. ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture::_thread_done);
  60. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_width", "get_width");
  61. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_height", "get_height");
  62. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
  63. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normalmap"), "set_as_normalmap", "is_normalmap");
  64. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise");
  65. }
  66. void NoiseTexture::_set_texture_data(const Ref<Image> &p_image) {
  67. data = p_image;
  68. if (data.is_valid()) {
  69. VS::get_singleton()->texture_allocate(texture, size.x, size.y, 0, Image::FORMAT_RGBA8, VS::TEXTURE_TYPE_2D, flags);
  70. VS::get_singleton()->texture_set_data(texture, p_image);
  71. }
  72. emit_changed();
  73. }
  74. void NoiseTexture::_thread_done(const Ref<Image> &p_image) {
  75. _set_texture_data(p_image);
  76. Thread::wait_to_finish(noise_thread);
  77. memdelete(noise_thread);
  78. noise_thread = NULL;
  79. if (regen_queued) {
  80. noise_thread = Thread::create(_thread_function, this);
  81. regen_queued = false;
  82. }
  83. }
  84. void NoiseTexture::_thread_function(void *p_ud) {
  85. NoiseTexture *tex = (NoiseTexture *)p_ud;
  86. tex->call_deferred("_thread_done", tex->_generate_texture());
  87. }
  88. void NoiseTexture::_queue_update() {
  89. if (update_queued)
  90. return;
  91. update_queued = true;
  92. call_deferred("_update_texture");
  93. }
  94. Ref<Image> NoiseTexture::_generate_texture() {
  95. update_queued = false;
  96. if (noise.is_null()) return Ref<Image>();
  97. Ref<Image> image;
  98. if (seamless) {
  99. image = noise->get_seamless_image(size.x);
  100. } else {
  101. image = noise->get_image(size.x, size.y);
  102. }
  103. if (as_normalmap) {
  104. image->bumpmap_to_normalmap();
  105. }
  106. return image;
  107. }
  108. void NoiseTexture::_update_texture() {
  109. bool use_thread = true;
  110. if (first_time) {
  111. use_thread = false;
  112. first_time = false;
  113. }
  114. #ifdef NO_THREADS
  115. use_thread = false;
  116. #endif
  117. if (use_thread) {
  118. if (!noise_thread) {
  119. noise_thread = Thread::create(_thread_function, this);
  120. regen_queued = false;
  121. } else {
  122. regen_queued = true;
  123. }
  124. } else {
  125. Ref<Image> image = _generate_texture();
  126. _set_texture_data(image);
  127. }
  128. }
  129. void NoiseTexture::set_noise(Ref<OpenSimplexNoise> p_noise) {
  130. if (p_noise == noise)
  131. return;
  132. if (noise.is_valid()) {
  133. noise->disconnect(CoreStringNames::get_singleton()->changed, this, "_update_texture");
  134. }
  135. noise = p_noise;
  136. if (noise.is_valid()) {
  137. noise->connect(CoreStringNames::get_singleton()->changed, this, "_update_texture");
  138. }
  139. _queue_update();
  140. }
  141. Ref<OpenSimplexNoise> NoiseTexture::get_noise() {
  142. return noise;
  143. }
  144. void NoiseTexture::set_width(int p_width) {
  145. if (p_width == size.x) return;
  146. size.x = p_width;
  147. _queue_update();
  148. }
  149. void NoiseTexture::set_height(int p_height) {
  150. if (p_height == size.y) return;
  151. size.y = p_height;
  152. _queue_update();
  153. }
  154. void NoiseTexture::set_seamless(bool p_seamless) {
  155. if (p_seamless == seamless) return;
  156. seamless = p_seamless;
  157. _queue_update();
  158. }
  159. bool NoiseTexture::get_seamless() {
  160. return seamless;
  161. }
  162. void NoiseTexture::set_as_normalmap(bool p_as_normalmap) {
  163. if (p_as_normalmap == as_normalmap) return;
  164. as_normalmap = p_as_normalmap;
  165. _queue_update();
  166. }
  167. bool NoiseTexture::is_normalmap() {
  168. return as_normalmap;
  169. }
  170. int NoiseTexture::get_width() const {
  171. return size.x;
  172. }
  173. int NoiseTexture::get_height() const {
  174. return size.y;
  175. }
  176. void NoiseTexture::set_flags(uint32_t p_flags) {
  177. flags = p_flags;
  178. VS::get_singleton()->texture_set_flags(texture, flags);
  179. }
  180. uint32_t NoiseTexture::get_flags() const {
  181. return flags;
  182. }
  183. Ref<Image> NoiseTexture::get_data() const {
  184. return data;
  185. }