noise_texture.cpp 8.2 KB

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