noise_texture.cpp 8.0 KB

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