noise_texture_3d.cpp 11 KB

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