noise_texture_3d.cpp 11 KB

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