noise_texture.cpp 8.0 KB

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