noise_texture_3d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**************************************************************************/
  2. /* noise_texture_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_3d.h"
  31. #include "noise.h"
  32. NoiseTexture3D::NoiseTexture3D() {
  33. noise = Ref<Noise>();
  34. _queue_update();
  35. }
  36. NoiseTexture3D::~NoiseTexture3D() {
  37. ERR_FAIL_NULL(RenderingServer::get_singleton());
  38. if (texture.is_valid()) {
  39. RS::get_singleton()->free(texture);
  40. }
  41. if (noise_thread.is_started()) {
  42. noise_thread.wait_to_finish();
  43. }
  44. }
  45. void NoiseTexture3D::_bind_methods() {
  46. ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture3D::_update_texture);
  47. ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture3D::_generate_texture);
  48. ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture3D::_thread_done);
  49. ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture3D::set_width);
  50. ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture3D::set_height);
  51. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &NoiseTexture3D::set_depth);
  52. ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture3D::set_invert);
  53. ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture3D::get_invert);
  54. ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture3D::set_seamless);
  55. ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture3D::get_seamless);
  56. ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture3D::set_seamless_blend_skirt);
  57. ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture3D::get_seamless_blend_skirt);
  58. ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture3D::set_normalize);
  59. ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture3D::is_normalized);
  60. ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture3D::set_color_ramp);
  61. ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture3D::get_color_ramp);
  62. ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture3D::set_noise);
  63. ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture3D::get_noise);
  64. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");
  65. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");
  66. ADD_PROPERTY(PropertyInfo(Variant::INT, "depth", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_depth", "get_depth");
  67. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");
  68. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
  69. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0.05,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");
  70. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");
  71. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  72. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");
  73. }
  74. void NoiseTexture3D::_validate_property(PropertyInfo &p_property) const {
  75. if (p_property.name == "seamless_blend_skirt") {
  76. if (!seamless) {
  77. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  78. }
  79. }
  80. }
  81. void NoiseTexture3D::_set_texture_data(const TypedArray<Image> &p_data) {
  82. if (!p_data.is_empty()) {
  83. Vector<Ref<Image>> data;
  84. data.resize(p_data.size());
  85. for (int i = 0; i < data.size(); i++) {
  86. data.write[i] = p_data[i];
  87. }
  88. if (texture.is_valid()) {
  89. RID new_texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);
  90. RS::get_singleton()->texture_replace(texture, new_texture);
  91. } else {
  92. texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);
  93. }
  94. format = data[0]->get_format();
  95. }
  96. emit_changed();
  97. }
  98. void NoiseTexture3D::_thread_done(const TypedArray<Image> &p_data) {
  99. _set_texture_data(p_data);
  100. noise_thread.wait_to_finish();
  101. if (regen_queued) {
  102. noise_thread.start(_thread_function, this);
  103. regen_queued = false;
  104. }
  105. }
  106. void NoiseTexture3D::_thread_function(void *p_ud) {
  107. NoiseTexture3D *tex = static_cast<NoiseTexture3D *>(p_ud);
  108. tex->call_deferred(SNAME("_thread_done"), tex->_generate_texture());
  109. }
  110. void NoiseTexture3D::_queue_update() {
  111. if (update_queued) {
  112. return;
  113. }
  114. update_queued = true;
  115. call_deferred(SNAME("_update_texture"));
  116. }
  117. TypedArray<Image> NoiseTexture3D::_generate_texture() {
  118. // Prevent memdelete due to unref() on other thread.
  119. Ref<Noise> ref_noise = noise;
  120. if (ref_noise.is_null()) {
  121. return TypedArray<Image>();
  122. }
  123. Vector<Ref<Image>> images;
  124. if (seamless) {
  125. images = ref_noise->_get_seamless_image(width, height, depth, invert, true, seamless_blend_skirt, normalize);
  126. } else {
  127. images = ref_noise->_get_image(width, height, depth, invert, true, normalize);
  128. }
  129. if (color_ramp.is_valid()) {
  130. for (int i = 0; i < images.size(); i++) {
  131. images.write[i] = _modulate_with_gradient(images[i], color_ramp);
  132. }
  133. }
  134. TypedArray<Image> new_data;
  135. new_data.resize(images.size());
  136. for (int i = 0; i < new_data.size(); i++) {
  137. new_data[i] = images[i];
  138. }
  139. return new_data;
  140. }
  141. Ref<Image> NoiseTexture3D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {
  142. int w = p_image->get_width();
  143. int h = p_image->get_height();
  144. Ref<Image> new_image = Image::create_empty(w, h, false, Image::FORMAT_RGBA8);
  145. for (int row = 0; row < h; row++) {
  146. for (int col = 0; col < w; col++) {
  147. Color pixel_color = p_image->get_pixel(col, row);
  148. Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());
  149. new_image->set_pixel(col, row, ramp_color);
  150. }
  151. }
  152. return new_image;
  153. }
  154. void NoiseTexture3D::_update_texture() {
  155. bool use_thread = true;
  156. if (first_time) {
  157. use_thread = false;
  158. first_time = false;
  159. }
  160. if (use_thread) {
  161. if (!noise_thread.is_started()) {
  162. noise_thread.start(_thread_function, this);
  163. regen_queued = false;
  164. } else {
  165. regen_queued = true;
  166. }
  167. } else {
  168. TypedArray<Image> new_data = _generate_texture();
  169. _set_texture_data(new_data);
  170. }
  171. update_queued = false;
  172. }
  173. void NoiseTexture3D::set_noise(Ref<Noise> p_noise) {
  174. if (p_noise == noise) {
  175. return;
  176. }
  177. if (noise.is_valid()) {
  178. noise->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
  179. }
  180. noise = p_noise;
  181. if (noise.is_valid()) {
  182. noise->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
  183. }
  184. _queue_update();
  185. }
  186. Ref<Noise> NoiseTexture3D::get_noise() {
  187. return noise;
  188. }
  189. void NoiseTexture3D::set_width(int p_width) {
  190. ERR_FAIL_COND(p_width <= 0);
  191. if (p_width == width) {
  192. return;
  193. }
  194. width = p_width;
  195. _queue_update();
  196. }
  197. void NoiseTexture3D::set_height(int p_height) {
  198. ERR_FAIL_COND(p_height <= 0);
  199. if (p_height == height) {
  200. return;
  201. }
  202. height = p_height;
  203. _queue_update();
  204. }
  205. void NoiseTexture3D::set_depth(int p_depth) {
  206. ERR_FAIL_COND(p_depth <= 0);
  207. if (p_depth == depth) {
  208. return;
  209. }
  210. depth = p_depth;
  211. _queue_update();
  212. }
  213. void NoiseTexture3D::set_invert(bool p_invert) {
  214. if (p_invert == invert) {
  215. return;
  216. }
  217. invert = p_invert;
  218. _queue_update();
  219. }
  220. bool NoiseTexture3D::get_invert() const {
  221. return invert;
  222. }
  223. void NoiseTexture3D::set_seamless(bool p_seamless) {
  224. if (p_seamless == seamless) {
  225. return;
  226. }
  227. seamless = p_seamless;
  228. _queue_update();
  229. notify_property_list_changed();
  230. }
  231. bool NoiseTexture3D::get_seamless() {
  232. return seamless;
  233. }
  234. void NoiseTexture3D::set_seamless_blend_skirt(real_t p_blend_skirt) {
  235. ERR_FAIL_COND(p_blend_skirt < 0.05 || p_blend_skirt > 1);
  236. if (p_blend_skirt == seamless_blend_skirt) {
  237. return;
  238. }
  239. seamless_blend_skirt = p_blend_skirt;
  240. _queue_update();
  241. }
  242. real_t NoiseTexture3D::get_seamless_blend_skirt() {
  243. return seamless_blend_skirt;
  244. }
  245. void NoiseTexture3D::set_color_ramp(const Ref<Gradient> &p_gradient) {
  246. if (p_gradient == color_ramp) {
  247. return;
  248. }
  249. if (color_ramp.is_valid()) {
  250. color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
  251. }
  252. color_ramp = p_gradient;
  253. if (color_ramp.is_valid()) {
  254. color_ramp->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
  255. }
  256. _queue_update();
  257. }
  258. void NoiseTexture3D::set_normalize(bool p_normalize) {
  259. if (normalize == p_normalize) {
  260. return;
  261. }
  262. normalize = p_normalize;
  263. _queue_update();
  264. }
  265. bool NoiseTexture3D::is_normalized() const {
  266. return normalize;
  267. }
  268. Ref<Gradient> NoiseTexture3D::get_color_ramp() const {
  269. return color_ramp;
  270. }
  271. int NoiseTexture3D::get_width() const {
  272. return width;
  273. }
  274. int NoiseTexture3D::get_height() const {
  275. return height;
  276. }
  277. int NoiseTexture3D::get_depth() const {
  278. return depth;
  279. }
  280. RID NoiseTexture3D::get_rid() const {
  281. if (!texture.is_valid()) {
  282. texture = RS::get_singleton()->texture_3d_placeholder_create();
  283. }
  284. return texture;
  285. }
  286. Vector<Ref<Image>> NoiseTexture3D::get_data() const {
  287. ERR_FAIL_COND_V(!texture.is_valid(), Vector<Ref<Image>>());
  288. return RS::get_singleton()->texture_3d_get(texture);
  289. }
  290. Image::Format NoiseTexture3D::get_format() const {
  291. return format;
  292. }