noise_texture_3d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_noise", "noise"), &NoiseTexture3D::set_noise);
  50. ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture3D::get_noise);
  51. ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture3D::set_color_ramp);
  52. ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture3D::get_color_ramp);
  53. ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture3D::set_seamless);
  54. ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture3D::get_seamless);
  55. ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture3D::set_invert);
  56. ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture3D::get_invert);
  57. ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture3D::set_normalize);
  58. ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture3D::is_normalized);
  59. ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture3D::set_seamless_blend_skirt);
  60. ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture3D::get_seamless_blend_skirt);
  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::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");
  65. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  66. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
  67. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");
  68. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");
  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. }
  71. void NoiseTexture3D::_validate_property(PropertyInfo &p_property) const {
  72. if (!Engine::get_singleton()->is_editor_hint()) {
  73. return;
  74. }
  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. callable_mp(tex, &NoiseTexture3D::_thread_done).call_deferred(tex->_generate_texture());
  109. }
  110. void NoiseTexture3D::_queue_update() {
  111. if (update_queued) {
  112. return;
  113. }
  114. update_queued = true;
  115. callable_mp(this, &NoiseTexture3D::_update_texture).call_deferred();
  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. 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.");
  124. Vector<Ref<Image>> images;
  125. if (seamless) {
  126. images = ref_noise->_get_seamless_image(width, height, depth, invert, true, seamless_blend_skirt, normalize);
  127. } else {
  128. images = ref_noise->_get_image(width, height, depth, invert, true, normalize);
  129. }
  130. if (color_ramp.is_valid()) {
  131. for (int i = 0; i < images.size(); i++) {
  132. images.write[i] = _modulate_with_gradient(images[i], color_ramp);
  133. }
  134. }
  135. TypedArray<Image> new_data;
  136. new_data.resize(images.size());
  137. for (int i = 0; i < new_data.size(); i++) {
  138. new_data[i] = images[i];
  139. }
  140. return new_data;
  141. }
  142. Ref<Image> NoiseTexture3D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {
  143. int w = p_image->get_width();
  144. int h = p_image->get_height();
  145. Ref<Image> new_image = Image::create_empty(w, h, false, Image::FORMAT_RGBA8);
  146. for (int row = 0; row < h; row++) {
  147. for (int col = 0; col < w; col++) {
  148. Color pixel_color = p_image->get_pixel(col, row);
  149. Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());
  150. new_image->set_pixel(col, row, ramp_color);
  151. }
  152. }
  153. return new_image;
  154. }
  155. void NoiseTexture3D::_update_texture() {
  156. bool use_thread = true;
  157. #ifndef THREADS_ENABLED
  158. use_thread = false;
  159. #endif
  160. if (first_time) {
  161. use_thread = false;
  162. first_time = false;
  163. }
  164. if (use_thread) {
  165. if (!noise_thread.is_started()) {
  166. noise_thread.start(_thread_function, this);
  167. regen_queued = false;
  168. } else {
  169. regen_queued = true;
  170. }
  171. } else {
  172. TypedArray<Image> new_data = _generate_texture();
  173. _set_texture_data(new_data);
  174. }
  175. update_queued = false;
  176. }
  177. void NoiseTexture3D::set_noise(Ref<Noise> p_noise) {
  178. if (p_noise == noise) {
  179. return;
  180. }
  181. if (noise.is_valid()) {
  182. noise->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
  183. }
  184. noise = p_noise;
  185. if (noise.is_valid()) {
  186. noise->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
  187. }
  188. _queue_update();
  189. }
  190. Ref<Noise> NoiseTexture3D::get_noise() {
  191. return noise;
  192. }
  193. void NoiseTexture3D::set_width(int p_width) {
  194. ERR_FAIL_COND(p_width <= 0);
  195. if (p_width == width) {
  196. return;
  197. }
  198. width = p_width;
  199. _queue_update();
  200. }
  201. void NoiseTexture3D::set_height(int p_height) {
  202. ERR_FAIL_COND(p_height <= 0);
  203. if (p_height == height) {
  204. return;
  205. }
  206. height = p_height;
  207. _queue_update();
  208. }
  209. void NoiseTexture3D::set_depth(int p_depth) {
  210. ERR_FAIL_COND(p_depth <= 0);
  211. if (p_depth == depth) {
  212. return;
  213. }
  214. depth = p_depth;
  215. _queue_update();
  216. }
  217. void NoiseTexture3D::set_invert(bool p_invert) {
  218. if (p_invert == invert) {
  219. return;
  220. }
  221. invert = p_invert;
  222. _queue_update();
  223. }
  224. bool NoiseTexture3D::get_invert() const {
  225. return invert;
  226. }
  227. void NoiseTexture3D::set_seamless(bool p_seamless) {
  228. if (p_seamless == seamless) {
  229. return;
  230. }
  231. seamless = p_seamless;
  232. _queue_update();
  233. notify_property_list_changed();
  234. }
  235. bool NoiseTexture3D::get_seamless() {
  236. return seamless;
  237. }
  238. void NoiseTexture3D::set_seamless_blend_skirt(real_t p_blend_skirt) {
  239. ERR_FAIL_COND(p_blend_skirt < 0.05 || p_blend_skirt > 1);
  240. if (p_blend_skirt == seamless_blend_skirt) {
  241. return;
  242. }
  243. seamless_blend_skirt = p_blend_skirt;
  244. _queue_update();
  245. }
  246. real_t NoiseTexture3D::get_seamless_blend_skirt() {
  247. return seamless_blend_skirt;
  248. }
  249. void NoiseTexture3D::set_color_ramp(const Ref<Gradient> &p_gradient) {
  250. if (p_gradient == color_ramp) {
  251. return;
  252. }
  253. if (color_ramp.is_valid()) {
  254. color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
  255. }
  256. color_ramp = p_gradient;
  257. if (color_ramp.is_valid()) {
  258. color_ramp->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
  259. }
  260. _queue_update();
  261. }
  262. void NoiseTexture3D::set_normalize(bool p_normalize) {
  263. if (normalize == p_normalize) {
  264. return;
  265. }
  266. normalize = p_normalize;
  267. _queue_update();
  268. }
  269. bool NoiseTexture3D::is_normalized() const {
  270. return normalize;
  271. }
  272. Ref<Gradient> NoiseTexture3D::get_color_ramp() const {
  273. return color_ramp;
  274. }
  275. int NoiseTexture3D::get_width() const {
  276. return width;
  277. }
  278. int NoiseTexture3D::get_height() const {
  279. return height;
  280. }
  281. int NoiseTexture3D::get_depth() const {
  282. return depth;
  283. }
  284. bool NoiseTexture3D::has_mipmaps() const {
  285. return false;
  286. }
  287. RID NoiseTexture3D::get_rid() const {
  288. if (!texture.is_valid()) {
  289. texture = RS::get_singleton()->texture_3d_placeholder_create();
  290. }
  291. return texture;
  292. }
  293. Vector<Ref<Image>> NoiseTexture3D::get_data() const {
  294. ERR_FAIL_COND_V(!texture.is_valid(), Vector<Ref<Image>>());
  295. return RS::get_singleton()->texture_3d_get(texture);
  296. }
  297. Image::Format NoiseTexture3D::get_format() const {
  298. return format;
  299. }