2
0

texture.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  1. /*************************************************************************/
  2. /* texture.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "texture.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/image_loader.h"
  33. #include "core/method_bind_ext.gen.inc"
  34. #include "core/os/os.h"
  35. #include "scene/resources/bit_map.h"
  36. Size2 Texture::get_size() const {
  37. return Size2(get_width(), get_height());
  38. }
  39. bool Texture::is_pixel_opaque(int p_x, int p_y) const {
  40. return true;
  41. }
  42. void Texture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  43. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  44. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, get_size()), get_rid(), false, p_modulate, p_transpose, normal_rid);
  45. }
  46. void Texture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  47. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  48. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_rid(), p_tile, p_modulate, p_transpose, normal_rid);
  49. }
  50. void Texture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map, bool p_clip_uv) const {
  51. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  52. VisualServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, get_rid(), p_src_rect, p_modulate, p_transpose, normal_rid, p_clip_uv);
  53. }
  54. bool Texture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  55. r_rect = p_rect;
  56. r_src_rect = p_src_rect;
  57. return true;
  58. }
  59. void Texture::_bind_methods() {
  60. ClassDB::bind_method(D_METHOD("get_width"), &Texture::get_width);
  61. ClassDB::bind_method(D_METHOD("get_height"), &Texture::get_height);
  62. ClassDB::bind_method(D_METHOD("get_size"), &Texture::get_size);
  63. ClassDB::bind_method(D_METHOD("has_alpha"), &Texture::has_alpha);
  64. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &Texture::set_flags);
  65. ClassDB::bind_method(D_METHOD("get_flags"), &Texture::get_flags);
  66. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "position", "modulate", "transpose", "normal_map"), &Texture::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(Variant()));
  67. ClassDB::bind_method(D_METHOD("draw_rect", "canvas_item", "rect", "tile", "modulate", "transpose", "normal_map"), &Texture::draw_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(Variant()));
  68. ClassDB::bind_method(D_METHOD("draw_rect_region", "canvas_item", "rect", "src_rect", "modulate", "transpose", "normal_map", "clip_uv"), &Texture::draw_rect_region, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(Variant()), DEFVAL(true));
  69. ClassDB::bind_method(D_METHOD("get_data"), &Texture::get_data);
  70. ADD_GROUP("Flags", "");
  71. ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter,Anisotropic Linear,Convert to Linear,Mirrored Repeat,Video Surface"), "set_flags", "get_flags");
  72. ADD_GROUP("", "");
  73. BIND_ENUM_CONSTANT(FLAGS_DEFAULT);
  74. BIND_ENUM_CONSTANT(FLAG_MIPMAPS);
  75. BIND_ENUM_CONSTANT(FLAG_REPEAT);
  76. BIND_ENUM_CONSTANT(FLAG_FILTER);
  77. BIND_ENUM_CONSTANT(FLAG_ANISOTROPIC_FILTER);
  78. BIND_ENUM_CONSTANT(FLAG_CONVERT_TO_LINEAR);
  79. BIND_ENUM_CONSTANT(FLAG_MIRRORED_REPEAT);
  80. BIND_ENUM_CONSTANT(FLAG_VIDEO_SURFACE);
  81. }
  82. Texture::Texture() {
  83. }
  84. /////////////////////
  85. void ImageTexture::reload_from_file() {
  86. String path = ResourceLoader::path_remap(get_path());
  87. if (!path.is_resource_file())
  88. return;
  89. uint32_t flags = get_flags();
  90. Ref<Image> img;
  91. img.instance();
  92. Error err = ImageLoader::load_image(path, img);
  93. ERR_FAIL_COND(err != OK);
  94. create_from_image(img, flags);
  95. }
  96. bool ImageTexture::_set(const StringName &p_name, const Variant &p_value) {
  97. if (p_name == "image")
  98. create_from_image(p_value, flags);
  99. else if (p_name == "flags")
  100. if (w * h == 0)
  101. flags = p_value;
  102. else
  103. set_flags(p_value);
  104. else if (p_name == "size") {
  105. Size2 s = p_value;
  106. w = s.width;
  107. h = s.height;
  108. VisualServer::get_singleton()->texture_set_size_override(texture, w, h, 0);
  109. } else if (p_name == "_data") {
  110. _set_data(p_value);
  111. } else
  112. return false;
  113. return true;
  114. }
  115. bool ImageTexture::_get(const StringName &p_name, Variant &r_ret) const {
  116. if (p_name == "image_data") {
  117. } else if (p_name == "image")
  118. r_ret = get_data();
  119. else if (p_name == "flags")
  120. r_ret = flags;
  121. else if (p_name == "size")
  122. r_ret = Size2(w, h);
  123. else
  124. return false;
  125. return true;
  126. }
  127. void ImageTexture::_get_property_list(List<PropertyInfo> *p_list) const {
  128. p_list->push_back(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat"));
  129. p_list->push_back(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "Image", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT));
  130. p_list->push_back(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, ""));
  131. }
  132. void ImageTexture::_reload_hook(const RID &p_hook) {
  133. String path = get_path();
  134. if (!path.is_resource_file())
  135. return;
  136. Ref<Image> img;
  137. img.instance();
  138. Error err = ImageLoader::load_image(path, img);
  139. ERR_FAIL_COND(err != OK);
  140. VisualServer::get_singleton()->texture_set_data(texture, img);
  141. _change_notify();
  142. }
  143. void ImageTexture::create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags) {
  144. flags = p_flags;
  145. VisualServer::get_singleton()->texture_allocate(texture, p_width, p_height, 0, p_format, VS::TEXTURE_TYPE_2D, p_flags);
  146. format = p_format;
  147. w = p_width;
  148. h = p_height;
  149. _change_notify();
  150. }
  151. void ImageTexture::create_from_image(const Ref<Image> &p_image, uint32_t p_flags) {
  152. ERR_FAIL_COND(p_image.is_null());
  153. flags = p_flags;
  154. w = p_image->get_width();
  155. h = p_image->get_height();
  156. format = p_image->get_format();
  157. VisualServer::get_singleton()->texture_allocate(texture, p_image->get_width(), p_image->get_height(), 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, p_flags);
  158. VisualServer::get_singleton()->texture_set_data(texture, p_image);
  159. _change_notify();
  160. image_stored = true;
  161. }
  162. void ImageTexture::set_flags(uint32_t p_flags) {
  163. /* uint32_t cube = flags & FLAG_CUBEMAP;
  164. if (flags == p_flags&cube)
  165. return;
  166. flags=p_flags|cube; */
  167. flags = p_flags;
  168. if (w == 0 || h == 0) {
  169. return; //uninitialized, do not set to texture
  170. }
  171. VisualServer::get_singleton()->texture_set_flags(texture, p_flags);
  172. _change_notify("flags");
  173. }
  174. uint32_t ImageTexture::get_flags() const {
  175. return ImageTexture::flags;
  176. }
  177. Image::Format ImageTexture::get_format() const {
  178. return format;
  179. }
  180. #ifndef DISABLE_DEPRECATED
  181. Error ImageTexture::load(const String &p_path) {
  182. WARN_DEPRECATED
  183. Ref<Image> img;
  184. img.instance();
  185. Error err = img->load(p_path);
  186. if (err == OK) {
  187. create_from_image(img);
  188. }
  189. return err;
  190. }
  191. #endif
  192. void ImageTexture::set_data(const Ref<Image> &p_image) {
  193. ERR_FAIL_COND(p_image.is_null());
  194. VisualServer::get_singleton()->texture_set_data(texture, p_image);
  195. _change_notify();
  196. alpha_cache.unref();
  197. image_stored = true;
  198. }
  199. void ImageTexture::_resource_path_changed() {
  200. String path = get_path();
  201. }
  202. Ref<Image> ImageTexture::get_data() const {
  203. if (image_stored) {
  204. return VisualServer::get_singleton()->texture_get_data(texture);
  205. } else {
  206. return Ref<Image>();
  207. }
  208. }
  209. int ImageTexture::get_width() const {
  210. return w;
  211. }
  212. int ImageTexture::get_height() const {
  213. return h;
  214. }
  215. RID ImageTexture::get_rid() const {
  216. return texture;
  217. }
  218. bool ImageTexture::has_alpha() const {
  219. return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8);
  220. }
  221. void ImageTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  222. if ((w | h) == 0)
  223. return;
  224. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  225. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, Size2(w, h)), texture, false, p_modulate, p_transpose, normal_rid);
  226. }
  227. void ImageTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  228. if ((w | h) == 0)
  229. return;
  230. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  231. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose, normal_rid);
  232. }
  233. void ImageTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map, bool p_clip_uv) const {
  234. if ((w | h) == 0)
  235. return;
  236. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  237. VisualServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, normal_rid, p_clip_uv);
  238. }
  239. bool ImageTexture::is_pixel_opaque(int p_x, int p_y) const {
  240. if (!alpha_cache.is_valid()) {
  241. Ref<Image> img = get_data();
  242. if (img.is_valid()) {
  243. if (img->is_compressed()) { //must decompress, if compressed
  244. Ref<Image> decom = img->duplicate();
  245. decom->decompress();
  246. img = decom;
  247. }
  248. alpha_cache.instance();
  249. alpha_cache->create_from_image_alpha(img);
  250. }
  251. }
  252. if (alpha_cache.is_valid()) {
  253. int aw = int(alpha_cache->get_size().width);
  254. int ah = int(alpha_cache->get_size().height);
  255. if (aw == 0 || ah == 0) {
  256. return true;
  257. }
  258. int x = p_x * aw / w;
  259. int y = p_y * ah / h;
  260. x = CLAMP(x, 0, aw);
  261. y = CLAMP(y, 0, ah);
  262. return alpha_cache->get_bit(Point2(x, y));
  263. }
  264. return true;
  265. }
  266. void ImageTexture::set_size_override(const Size2 &p_size) {
  267. Size2 s = p_size;
  268. if (s.x != 0)
  269. w = s.x;
  270. if (s.y != 0)
  271. h = s.y;
  272. VisualServer::get_singleton()->texture_set_size_override(texture, w, h, 0);
  273. }
  274. void ImageTexture::set_path(const String &p_path, bool p_take_over) {
  275. if (texture.is_valid()) {
  276. VisualServer::get_singleton()->texture_set_path(texture, p_path);
  277. }
  278. Resource::set_path(p_path, p_take_over);
  279. }
  280. void ImageTexture::set_storage(Storage p_storage) {
  281. storage = p_storage;
  282. }
  283. ImageTexture::Storage ImageTexture::get_storage() const {
  284. return storage;
  285. }
  286. void ImageTexture::set_lossy_storage_quality(float p_lossy_storage_quality) {
  287. lossy_storage_quality = p_lossy_storage_quality;
  288. }
  289. float ImageTexture::get_lossy_storage_quality() const {
  290. return lossy_storage_quality;
  291. }
  292. void ImageTexture::_set_data(Dictionary p_data) {
  293. Ref<Image> img = p_data["image"];
  294. ERR_FAIL_COND(!img.is_valid());
  295. uint32_t flags = p_data["flags"];
  296. create_from_image(img, flags);
  297. set_storage(Storage(p_data["storage"].operator int()));
  298. set_lossy_storage_quality(p_data["lossy_quality"]);
  299. set_size_override(p_data["size"]);
  300. };
  301. void ImageTexture::_bind_methods() {
  302. ClassDB::bind_method(D_METHOD("create", "width", "height", "format", "flags"), &ImageTexture::create, DEFVAL(FLAGS_DEFAULT));
  303. ClassDB::bind_method(D_METHOD("create_from_image", "image", "flags"), &ImageTexture::create_from_image, DEFVAL(FLAGS_DEFAULT));
  304. ClassDB::bind_method(D_METHOD("get_format"), &ImageTexture::get_format);
  305. #ifndef DISABLE_DEPRECATED
  306. ClassDB::bind_method(D_METHOD("load", "path"), &ImageTexture::load);
  307. #endif
  308. ClassDB::bind_method(D_METHOD("set_data", "image"), &ImageTexture::set_data);
  309. ClassDB::bind_method(D_METHOD("set_storage", "mode"), &ImageTexture::set_storage);
  310. ClassDB::bind_method(D_METHOD("get_storage"), &ImageTexture::get_storage);
  311. ClassDB::bind_method(D_METHOD("set_lossy_storage_quality", "quality"), &ImageTexture::set_lossy_storage_quality);
  312. ClassDB::bind_method(D_METHOD("get_lossy_storage_quality"), &ImageTexture::get_lossy_storage_quality);
  313. ClassDB::bind_method(D_METHOD("set_size_override", "size"), &ImageTexture::set_size_override);
  314. ClassDB::bind_method(D_METHOD("_reload_hook", "rid"), &ImageTexture::_reload_hook);
  315. ADD_PROPERTY(PropertyInfo(Variant::INT, "storage", PROPERTY_HINT_ENUM, "Uncompressed,Compress Lossy,Compress Lossless"), "set_storage", "get_storage");
  316. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lossy_quality", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_lossy_storage_quality", "get_lossy_storage_quality");
  317. BIND_ENUM_CONSTANT(STORAGE_RAW);
  318. BIND_ENUM_CONSTANT(STORAGE_COMPRESS_LOSSY);
  319. BIND_ENUM_CONSTANT(STORAGE_COMPRESS_LOSSLESS);
  320. }
  321. ImageTexture::ImageTexture() {
  322. w = h = 0;
  323. flags = FLAGS_DEFAULT;
  324. texture = VisualServer::get_singleton()->texture_create();
  325. storage = STORAGE_RAW;
  326. lossy_storage_quality = 0.7;
  327. image_stored = false;
  328. format = Image::FORMAT_L8;
  329. }
  330. ImageTexture::~ImageTexture() {
  331. VisualServer::get_singleton()->free(texture);
  332. }
  333. //////////////////////////////////////////
  334. void StreamTexture::set_path(const String &p_path, bool p_take_over) {
  335. if (texture.is_valid()) {
  336. VisualServer::get_singleton()->texture_set_path(texture, p_path);
  337. }
  338. Resource::set_path(p_path, p_take_over);
  339. }
  340. void StreamTexture::_requested_3d(void *p_ud) {
  341. StreamTexture *st = (StreamTexture *)p_ud;
  342. Ref<StreamTexture> stex(st);
  343. ERR_FAIL_COND(!request_3d_callback);
  344. request_3d_callback(stex);
  345. }
  346. void StreamTexture::_requested_srgb(void *p_ud) {
  347. StreamTexture *st = (StreamTexture *)p_ud;
  348. Ref<StreamTexture> stex(st);
  349. ERR_FAIL_COND(!request_srgb_callback);
  350. request_srgb_callback(stex);
  351. }
  352. void StreamTexture::_requested_normal(void *p_ud) {
  353. StreamTexture *st = (StreamTexture *)p_ud;
  354. Ref<StreamTexture> stex(st);
  355. ERR_FAIL_COND(!request_normal_callback);
  356. request_normal_callback(stex);
  357. }
  358. StreamTexture::TextureFormatRequestCallback StreamTexture::request_3d_callback = NULL;
  359. StreamTexture::TextureFormatRequestCallback StreamTexture::request_srgb_callback = NULL;
  360. StreamTexture::TextureFormatRequestCallback StreamTexture::request_normal_callback = NULL;
  361. uint32_t StreamTexture::get_flags() const {
  362. return flags;
  363. }
  364. Image::Format StreamTexture::get_format() const {
  365. return format;
  366. }
  367. Error StreamTexture::_load_data(const String &p_path, int &tw, int &th, int &tw_custom, int &th_custom, int &flags, Ref<Image> &image, int p_size_limit) {
  368. alpha_cache.unref();
  369. ERR_FAIL_COND_V(image.is_null(), ERR_INVALID_PARAMETER);
  370. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  371. ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
  372. uint8_t header[4];
  373. f->get_buffer(header, 4);
  374. if (header[0] != 'G' || header[1] != 'D' || header[2] != 'S' || header[3] != 'T') {
  375. memdelete(f);
  376. ERR_FAIL_COND_V(header[0] != 'G' || header[1] != 'D' || header[2] != 'S' || header[3] != 'T', ERR_FILE_CORRUPT);
  377. }
  378. tw = f->get_16();
  379. tw_custom = f->get_16();
  380. th = f->get_16();
  381. th_custom = f->get_16();
  382. flags = f->get_32(); //texture flags!
  383. uint32_t df = f->get_32(); //data format
  384. /*
  385. print_line("width: " + itos(tw));
  386. print_line("height: " + itos(th));
  387. print_line("flags: " + itos(flags));
  388. print_line("df: " + itos(df));
  389. */
  390. #ifdef TOOLS_ENABLED
  391. if (request_3d_callback && df & FORMAT_BIT_DETECT_3D) {
  392. //print_line("request detect 3D at " + p_path);
  393. VS::get_singleton()->texture_set_detect_3d_callback(texture, _requested_3d, this);
  394. } else {
  395. //print_line("not requesting detect 3D at " + p_path);
  396. VS::get_singleton()->texture_set_detect_3d_callback(texture, NULL, NULL);
  397. }
  398. if (request_srgb_callback && df & FORMAT_BIT_DETECT_SRGB) {
  399. //print_line("request detect srgb at " + p_path);
  400. VS::get_singleton()->texture_set_detect_srgb_callback(texture, _requested_srgb, this);
  401. } else {
  402. //print_line("not requesting detect srgb at " + p_path);
  403. VS::get_singleton()->texture_set_detect_srgb_callback(texture, NULL, NULL);
  404. }
  405. if (request_srgb_callback && df & FORMAT_BIT_DETECT_NORMAL) {
  406. //print_line("request detect srgb at " + p_path);
  407. VS::get_singleton()->texture_set_detect_normal_callback(texture, _requested_normal, this);
  408. } else {
  409. //print_line("not requesting detect normal at " + p_path);
  410. VS::get_singleton()->texture_set_detect_normal_callback(texture, NULL, NULL);
  411. }
  412. #endif
  413. if (!(df & FORMAT_BIT_STREAM)) {
  414. p_size_limit = 0;
  415. }
  416. if (df & FORMAT_BIT_LOSSLESS || df & FORMAT_BIT_LOSSY) {
  417. //look for a PNG or WEBP file inside
  418. int sw = tw;
  419. int sh = th;
  420. uint32_t mipmaps = f->get_32();
  421. uint32_t size = f->get_32();
  422. //print_line("mipmaps: " + itos(mipmaps));
  423. while (mipmaps > 1 && p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {
  424. f->seek(f->get_position() + size);
  425. mipmaps = f->get_32();
  426. size = f->get_32();
  427. sw = MAX(sw >> 1, 1);
  428. sh = MAX(sh >> 1, 1);
  429. mipmaps--;
  430. }
  431. //mipmaps need to be read independently, they will be later combined
  432. Vector<Ref<Image> > mipmap_images;
  433. int total_size = 0;
  434. for (uint32_t i = 0; i < mipmaps; i++) {
  435. if (i) {
  436. size = f->get_32();
  437. }
  438. PoolVector<uint8_t> pv;
  439. pv.resize(size);
  440. {
  441. PoolVector<uint8_t>::Write w = pv.write();
  442. f->get_buffer(w.ptr(), size);
  443. }
  444. Ref<Image> img;
  445. if (df & FORMAT_BIT_LOSSLESS) {
  446. img = Image::lossless_unpacker(pv);
  447. } else {
  448. img = Image::lossy_unpacker(pv);
  449. }
  450. if (img.is_null() || img->empty()) {
  451. memdelete(f);
  452. ERR_FAIL_COND_V(img.is_null() || img->empty(), ERR_FILE_CORRUPT);
  453. }
  454. total_size += img->get_data().size();
  455. mipmap_images.push_back(img);
  456. }
  457. //print_line("mipmap read total: " + itos(mipmap_images.size()));
  458. memdelete(f); //no longer needed
  459. if (mipmap_images.size() == 1) {
  460. image = mipmap_images[0];
  461. return OK;
  462. } else {
  463. PoolVector<uint8_t> img_data;
  464. img_data.resize(total_size);
  465. {
  466. PoolVector<uint8_t>::Write w = img_data.write();
  467. int ofs = 0;
  468. for (int i = 0; i < mipmap_images.size(); i++) {
  469. PoolVector<uint8_t> id = mipmap_images[i]->get_data();
  470. int len = id.size();
  471. PoolVector<uint8_t>::Read r = id.read();
  472. copymem(&w[ofs], r.ptr(), len);
  473. ofs += len;
  474. }
  475. }
  476. image->create(sw, sh, true, mipmap_images[0]->get_format(), img_data);
  477. return OK;
  478. }
  479. } else {
  480. //look for regular format
  481. Image::Format format = (Image::Format)(df & FORMAT_MASK_IMAGE_FORMAT);
  482. bool mipmaps = df & FORMAT_BIT_HAS_MIPMAPS;
  483. if (!mipmaps) {
  484. int size = Image::get_image_data_size(tw, th, format, 0);
  485. PoolVector<uint8_t> img_data;
  486. img_data.resize(size);
  487. {
  488. PoolVector<uint8_t>::Write w = img_data.write();
  489. f->get_buffer(w.ptr(), size);
  490. }
  491. memdelete(f);
  492. image->create(tw, th, false, format, img_data);
  493. return OK;
  494. } else {
  495. int sw = tw;
  496. int sh = th;
  497. int mipmaps2 = Image::get_image_required_mipmaps(tw, th, format);
  498. int total_size = Image::get_image_data_size(tw, th, format, true);
  499. int idx = 0;
  500. int ofs = 0;
  501. while (mipmaps2 > 1 && p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {
  502. sw = MAX(sw >> 1, 1);
  503. sh = MAX(sh >> 1, 1);
  504. mipmaps2--;
  505. idx++;
  506. }
  507. if (idx > 0) {
  508. ofs = Image::get_image_data_size(tw, th, format, idx - 1);
  509. }
  510. if (total_size - ofs <= 0) {
  511. memdelete(f);
  512. ERR_FAIL_V(ERR_FILE_CORRUPT);
  513. }
  514. f->seek(f->get_position() + ofs);
  515. PoolVector<uint8_t> img_data;
  516. img_data.resize(total_size - ofs);
  517. {
  518. PoolVector<uint8_t>::Write w = img_data.write();
  519. int bytes = f->get_buffer(w.ptr(), total_size - ofs);
  520. //print_line("requested read: " + itos(total_size - ofs) + " but got: " + itos(bytes));
  521. memdelete(f);
  522. int expected = total_size - ofs;
  523. if (bytes < expected) {
  524. //this is a compatibility workaround for older format, which saved less mipmaps2. It is still recommended the image is reimported.
  525. zeromem(w.ptr() + bytes, (expected - bytes));
  526. } else if (bytes != expected) {
  527. ERR_FAIL_V(ERR_FILE_CORRUPT);
  528. }
  529. }
  530. image->create(sw, sh, true, format, img_data);
  531. return OK;
  532. }
  533. }
  534. return ERR_BUG; //unreachable
  535. }
  536. Error StreamTexture::load(const String &p_path) {
  537. int lw, lh, lwc, lhc, lflags;
  538. Ref<Image> image;
  539. image.instance();
  540. Error err = _load_data(p_path, lw, lh, lwc, lhc, lflags, image);
  541. if (err)
  542. return err;
  543. if (get_path() == String()) {
  544. //temporarily set path if no path set for resource, helps find errors
  545. VisualServer::get_singleton()->texture_set_path(texture, p_path);
  546. }
  547. VS::get_singleton()->texture_allocate(texture, image->get_width(), image->get_height(), 0, image->get_format(), VS::TEXTURE_TYPE_2D, lflags);
  548. VS::get_singleton()->texture_set_data(texture, image);
  549. if (lwc || lhc) {
  550. VS::get_singleton()->texture_set_size_override(texture, lwc, lhc, 0);
  551. } else {
  552. }
  553. w = lwc ? lwc : lw;
  554. h = lhc ? lhc : lh;
  555. flags = lflags;
  556. path_to_file = p_path;
  557. format = image->get_format();
  558. _change_notify();
  559. return OK;
  560. }
  561. String StreamTexture::get_load_path() const {
  562. return path_to_file;
  563. }
  564. int StreamTexture::get_width() const {
  565. return w;
  566. }
  567. int StreamTexture::get_height() const {
  568. return h;
  569. }
  570. RID StreamTexture::get_rid() const {
  571. return texture;
  572. }
  573. void StreamTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  574. if ((w | h) == 0)
  575. return;
  576. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  577. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, Size2(w, h)), texture, false, p_modulate, p_transpose, normal_rid);
  578. }
  579. void StreamTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  580. if ((w | h) == 0)
  581. return;
  582. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  583. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose, normal_rid);
  584. }
  585. void StreamTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map, bool p_clip_uv) const {
  586. if ((w | h) == 0)
  587. return;
  588. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  589. VisualServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, normal_rid, p_clip_uv);
  590. }
  591. bool StreamTexture::has_alpha() const {
  592. return false;
  593. }
  594. Ref<Image> StreamTexture::get_data() const {
  595. return VS::get_singleton()->texture_get_data(texture);
  596. }
  597. bool StreamTexture::is_pixel_opaque(int p_x, int p_y) const {
  598. if (!alpha_cache.is_valid()) {
  599. Ref<Image> img = get_data();
  600. if (img.is_valid()) {
  601. if (img->is_compressed()) { //must decompress, if compressed
  602. Ref<Image> decom = img->duplicate();
  603. decom->decompress();
  604. img = decom;
  605. }
  606. alpha_cache.instance();
  607. alpha_cache->create_from_image_alpha(img);
  608. }
  609. }
  610. if (alpha_cache.is_valid()) {
  611. int aw = int(alpha_cache->get_size().width);
  612. int ah = int(alpha_cache->get_size().height);
  613. if (aw == 0 || ah == 0) {
  614. return true;
  615. }
  616. int x = p_x * aw / w;
  617. int y = p_y * ah / h;
  618. x = CLAMP(x, 0, aw);
  619. y = CLAMP(y, 0, ah);
  620. return alpha_cache->get_bit(Point2(x, y));
  621. }
  622. return true;
  623. }
  624. void StreamTexture::set_flags(uint32_t p_flags) {
  625. flags = p_flags;
  626. VS::get_singleton()->texture_set_flags(texture, flags);
  627. _change_notify("flags");
  628. }
  629. void StreamTexture::reload_from_file() {
  630. String path = get_path();
  631. if (!path.is_resource_file())
  632. return;
  633. path = ResourceLoader::path_remap(path); //remap for translation
  634. path = ResourceLoader::import_remap(path); //remap for import
  635. if (!path.is_resource_file())
  636. return;
  637. load(path);
  638. }
  639. void StreamTexture::_bind_methods() {
  640. ClassDB::bind_method(D_METHOD("load", "path"), &StreamTexture::load);
  641. ClassDB::bind_method(D_METHOD("get_load_path"), &StreamTexture::get_load_path);
  642. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.stex"), "load", "get_load_path");
  643. }
  644. StreamTexture::StreamTexture() {
  645. format = Image::FORMAT_MAX;
  646. flags = 0;
  647. w = 0;
  648. h = 0;
  649. texture = VS::get_singleton()->texture_create();
  650. }
  651. StreamTexture::~StreamTexture() {
  652. VS::get_singleton()->free(texture);
  653. }
  654. RES ResourceFormatLoaderStreamTexture::load(const String &p_path, const String &p_original_path, Error *r_error) {
  655. Ref<StreamTexture> st;
  656. st.instance();
  657. Error err = st->load(p_path);
  658. if (r_error)
  659. *r_error = err;
  660. if (err != OK)
  661. return RES();
  662. return st;
  663. }
  664. void ResourceFormatLoaderStreamTexture::get_recognized_extensions(List<String> *p_extensions) const {
  665. p_extensions->push_back("stex");
  666. }
  667. bool ResourceFormatLoaderStreamTexture::handles_type(const String &p_type) const {
  668. return p_type == "StreamTexture";
  669. }
  670. String ResourceFormatLoaderStreamTexture::get_resource_type(const String &p_path) const {
  671. if (p_path.get_extension().to_lower() == "stex")
  672. return "StreamTexture";
  673. return "";
  674. }
  675. //////////////////////////////////////////
  676. int AtlasTexture::get_width() const {
  677. if (region.size.width == 0) {
  678. if (atlas.is_valid())
  679. return atlas->get_width();
  680. return 1;
  681. } else {
  682. return region.size.width + margin.size.width;
  683. }
  684. }
  685. int AtlasTexture::get_height() const {
  686. if (region.size.height == 0) {
  687. if (atlas.is_valid())
  688. return atlas->get_height();
  689. return 1;
  690. } else {
  691. return region.size.height + margin.size.height;
  692. }
  693. }
  694. RID AtlasTexture::get_rid() const {
  695. if (atlas.is_valid())
  696. return atlas->get_rid();
  697. return RID();
  698. }
  699. bool AtlasTexture::has_alpha() const {
  700. if (atlas.is_valid())
  701. return atlas->has_alpha();
  702. return false;
  703. }
  704. void AtlasTexture::set_flags(uint32_t p_flags) {
  705. if (atlas.is_valid())
  706. atlas->set_flags(p_flags);
  707. }
  708. uint32_t AtlasTexture::get_flags() const {
  709. if (atlas.is_valid())
  710. return atlas->get_flags();
  711. return 0;
  712. }
  713. void AtlasTexture::set_atlas(const Ref<Texture> &p_atlas) {
  714. ERR_FAIL_COND(p_atlas == this);
  715. if (atlas == p_atlas)
  716. return;
  717. atlas = p_atlas;
  718. emit_changed();
  719. _change_notify("atlas");
  720. }
  721. Ref<Texture> AtlasTexture::get_atlas() const {
  722. return atlas;
  723. }
  724. void AtlasTexture::set_region(const Rect2 &p_region) {
  725. if (region == p_region)
  726. return;
  727. region = p_region;
  728. emit_changed();
  729. _change_notify("region");
  730. }
  731. Rect2 AtlasTexture::get_region() const {
  732. return region;
  733. }
  734. void AtlasTexture::set_margin(const Rect2 &p_margin) {
  735. if (margin == p_margin)
  736. return;
  737. margin = p_margin;
  738. emit_changed();
  739. _change_notify("margin");
  740. }
  741. Rect2 AtlasTexture::get_margin() const {
  742. return margin;
  743. }
  744. void AtlasTexture::set_filter_clip(const bool p_enable) {
  745. filter_clip = p_enable;
  746. emit_changed();
  747. _change_notify("filter_clip");
  748. }
  749. bool AtlasTexture::has_filter_clip() const {
  750. return filter_clip;
  751. }
  752. void AtlasTexture::_bind_methods() {
  753. ClassDB::bind_method(D_METHOD("set_atlas", "atlas"), &AtlasTexture::set_atlas);
  754. ClassDB::bind_method(D_METHOD("get_atlas"), &AtlasTexture::get_atlas);
  755. ClassDB::bind_method(D_METHOD("set_region", "region"), &AtlasTexture::set_region);
  756. ClassDB::bind_method(D_METHOD("get_region"), &AtlasTexture::get_region);
  757. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &AtlasTexture::set_margin);
  758. ClassDB::bind_method(D_METHOD("get_margin"), &AtlasTexture::get_margin);
  759. ClassDB::bind_method(D_METHOD("set_filter_clip", "enable"), &AtlasTexture::set_filter_clip);
  760. ClassDB::bind_method(D_METHOD("has_filter_clip"), &AtlasTexture::has_filter_clip);
  761. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "atlas", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_atlas", "get_atlas");
  762. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region"), "set_region", "get_region");
  763. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "margin"), "set_margin", "get_margin");
  764. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_clip"), "set_filter_clip", "has_filter_clip");
  765. }
  766. void AtlasTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  767. if (!atlas.is_valid())
  768. return;
  769. Rect2 rc = region;
  770. if (rc.size.width == 0) {
  771. rc.size.width = atlas->get_width();
  772. }
  773. if (rc.size.height == 0) {
  774. rc.size.height = atlas->get_height();
  775. }
  776. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  777. VS::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, Rect2(p_pos + margin.position, rc.size), atlas->get_rid(), rc, p_modulate, p_transpose, normal_rid, filter_clip);
  778. }
  779. void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  780. if (!atlas.is_valid())
  781. return;
  782. Rect2 rc = region;
  783. if (rc.size.width == 0) {
  784. rc.size.width = atlas->get_width();
  785. }
  786. if (rc.size.height == 0) {
  787. rc.size.height = atlas->get_height();
  788. }
  789. Vector2 scale = p_rect.size / (region.size + margin.size);
  790. Rect2 dr(p_rect.position + margin.position * scale, rc.size * scale);
  791. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  792. VS::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, dr, atlas->get_rid(), rc, p_modulate, p_transpose, normal_rid, filter_clip);
  793. }
  794. void AtlasTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map, bool p_clip_uv) const {
  795. //this might not necessarily work well if using a rect, needs to be fixed properly
  796. if (!atlas.is_valid())
  797. return;
  798. Rect2 dr;
  799. Rect2 src_c;
  800. get_rect_region(p_rect, p_src_rect, dr, src_c);
  801. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  802. VS::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, dr, atlas->get_rid(), src_c, p_modulate, p_transpose, normal_rid, filter_clip);
  803. }
  804. bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  805. if (!atlas.is_valid())
  806. return false;
  807. Rect2 rc = region;
  808. Rect2 src = p_src_rect;
  809. if (src.size == Size2()) {
  810. src.size = rc.size;
  811. }
  812. Vector2 scale = p_rect.size / src.size;
  813. src.position += (rc.position - margin.position);
  814. Rect2 src_c = rc.clip(src);
  815. if (src_c.size == Size2())
  816. return false;
  817. Vector2 ofs = (src_c.position - src.position);
  818. if (scale.x < 0) {
  819. float mx = (margin.size.width - margin.position.x);
  820. mx -= margin.position.x;
  821. ofs.x = -(ofs.x + mx);
  822. }
  823. if (scale.y < 0) {
  824. float my = margin.size.height - margin.position.y;
  825. my -= margin.position.y;
  826. ofs.y = -(ofs.y + my);
  827. }
  828. Rect2 dr(p_rect.position + ofs * scale, src_c.size * scale);
  829. r_rect = dr;
  830. r_src_rect = src_c;
  831. return true;
  832. }
  833. bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
  834. if (!atlas.is_valid())
  835. return true;
  836. int x = p_x + region.position.x - margin.position.x;
  837. int y = p_y + region.position.y - margin.position.y;
  838. // margin edge may outside of atlas
  839. if (x < 0 || x >= atlas->get_width()) return false;
  840. if (y < 0 || y >= atlas->get_height()) return false;
  841. return atlas->is_pixel_opaque(x, y);
  842. }
  843. AtlasTexture::AtlasTexture() {
  844. filter_clip = false;
  845. }
  846. //////////////////////////////////////////
  847. int LargeTexture::get_width() const {
  848. return size.width;
  849. }
  850. int LargeTexture::get_height() const {
  851. return size.height;
  852. }
  853. RID LargeTexture::get_rid() const {
  854. return RID();
  855. }
  856. bool LargeTexture::has_alpha() const {
  857. for (int i = 0; i < pieces.size(); i++) {
  858. if (pieces[i].texture->has_alpha())
  859. return true;
  860. }
  861. return false;
  862. }
  863. void LargeTexture::set_flags(uint32_t p_flags) {
  864. for (int i = 0; i < pieces.size(); i++) {
  865. pieces.write[i].texture->set_flags(p_flags);
  866. }
  867. }
  868. uint32_t LargeTexture::get_flags() const {
  869. if (pieces.size())
  870. return pieces[0].texture->get_flags();
  871. return 0;
  872. }
  873. int LargeTexture::add_piece(const Point2 &p_offset, const Ref<Texture> &p_texture) {
  874. ERR_FAIL_COND_V(p_texture.is_null(), -1);
  875. Piece p;
  876. p.offset = p_offset;
  877. p.texture = p_texture;
  878. pieces.push_back(p);
  879. return pieces.size() - 1;
  880. }
  881. void LargeTexture::set_piece_offset(int p_idx, const Point2 &p_offset) {
  882. ERR_FAIL_INDEX(p_idx, pieces.size());
  883. pieces.write[p_idx].offset = p_offset;
  884. };
  885. void LargeTexture::set_piece_texture(int p_idx, const Ref<Texture> &p_texture) {
  886. ERR_FAIL_COND(p_texture == this);
  887. ERR_FAIL_INDEX(p_idx, pieces.size());
  888. pieces.write[p_idx].texture = p_texture;
  889. };
  890. void LargeTexture::set_size(const Size2 &p_size) {
  891. size = p_size;
  892. }
  893. void LargeTexture::clear() {
  894. pieces.clear();
  895. size = Size2i();
  896. }
  897. Array LargeTexture::_get_data() const {
  898. Array arr;
  899. for (int i = 0; i < pieces.size(); i++) {
  900. arr.push_back(pieces[i].offset);
  901. arr.push_back(pieces[i].texture);
  902. }
  903. arr.push_back(Size2(size));
  904. return arr;
  905. }
  906. void LargeTexture::_set_data(const Array &p_array) {
  907. ERR_FAIL_COND(p_array.size() < 1);
  908. ERR_FAIL_COND(!(p_array.size() & 1));
  909. clear();
  910. for (int i = 0; i < p_array.size() - 1; i += 2) {
  911. add_piece(p_array[i], p_array[i + 1]);
  912. }
  913. size = Size2(p_array[p_array.size() - 1]);
  914. }
  915. int LargeTexture::get_piece_count() const {
  916. return pieces.size();
  917. }
  918. Vector2 LargeTexture::get_piece_offset(int p_idx) const {
  919. ERR_FAIL_INDEX_V(p_idx, pieces.size(), Vector2());
  920. return pieces[p_idx].offset;
  921. }
  922. Ref<Texture> LargeTexture::get_piece_texture(int p_idx) const {
  923. ERR_FAIL_INDEX_V(p_idx, pieces.size(), Ref<Texture>());
  924. return pieces[p_idx].texture;
  925. }
  926. Ref<Image> LargeTexture::to_image() const {
  927. Ref<Image> img = memnew(Image(this->get_width(), this->get_height(), false, Image::FORMAT_RGBA8));
  928. for (int i = 0; i < pieces.size(); i++) {
  929. Ref<Image> src_img = pieces[i].texture->get_data();
  930. img->blit_rect(src_img, Rect2(0, 0, src_img->get_width(), src_img->get_height()), pieces[i].offset);
  931. }
  932. return img;
  933. }
  934. void LargeTexture::_bind_methods() {
  935. ClassDB::bind_method(D_METHOD("add_piece", "ofs", "texture"), &LargeTexture::add_piece);
  936. ClassDB::bind_method(D_METHOD("set_piece_offset", "idx", "ofs"), &LargeTexture::set_piece_offset);
  937. ClassDB::bind_method(D_METHOD("set_piece_texture", "idx", "texture"), &LargeTexture::set_piece_texture);
  938. ClassDB::bind_method(D_METHOD("set_size", "size"), &LargeTexture::set_size);
  939. ClassDB::bind_method(D_METHOD("clear"), &LargeTexture::clear);
  940. ClassDB::bind_method(D_METHOD("get_piece_count"), &LargeTexture::get_piece_count);
  941. ClassDB::bind_method(D_METHOD("get_piece_offset", "idx"), &LargeTexture::get_piece_offset);
  942. ClassDB::bind_method(D_METHOD("get_piece_texture", "idx"), &LargeTexture::get_piece_texture);
  943. ClassDB::bind_method(D_METHOD("_set_data", "data"), &LargeTexture::_set_data);
  944. ClassDB::bind_method(D_METHOD("_get_data"), &LargeTexture::_get_data);
  945. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  946. }
  947. void LargeTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  948. for (int i = 0; i < pieces.size(); i++) {
  949. // TODO
  950. pieces[i].texture->draw(p_canvas_item, pieces[i].offset + p_pos, p_modulate, p_transpose, p_normal_map);
  951. }
  952. }
  953. void LargeTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map) const {
  954. //tiling not supported for this
  955. if (size.x == 0 || size.y == 0)
  956. return;
  957. Size2 scale = p_rect.size / size;
  958. for (int i = 0; i < pieces.size(); i++) {
  959. // TODO
  960. pieces[i].texture->draw_rect(p_canvas_item, Rect2(pieces[i].offset * scale + p_rect.position, pieces[i].texture->get_size() * scale), false, p_modulate, p_transpose, p_normal_map);
  961. }
  962. }
  963. void LargeTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, const Ref<Texture> &p_normal_map, bool p_clip_uv) const {
  964. //tiling not supported for this
  965. if (p_src_rect.size.x == 0 || p_src_rect.size.y == 0)
  966. return;
  967. Size2 scale = p_rect.size / p_src_rect.size;
  968. for (int i = 0; i < pieces.size(); i++) {
  969. // TODO
  970. Rect2 rect(pieces[i].offset, pieces[i].texture->get_size());
  971. if (!p_src_rect.intersects(rect))
  972. continue;
  973. Rect2 local = p_src_rect.clip(rect);
  974. Rect2 target = local;
  975. target.size *= scale;
  976. target.position = p_rect.position + (p_src_rect.position + rect.position) * scale;
  977. local.position -= rect.position;
  978. pieces[i].texture->draw_rect_region(p_canvas_item, target, local, p_modulate, p_transpose, p_normal_map, false);
  979. }
  980. }
  981. bool LargeTexture::is_pixel_opaque(int p_x, int p_y) const {
  982. for (int i = 0; i < pieces.size(); i++) {
  983. // TODO
  984. if (!pieces[i].texture.is_valid())
  985. continue;
  986. Rect2 rect(pieces[i].offset, pieces[i].texture->get_size());
  987. if (rect.has_point(Point2(p_x, p_y))) {
  988. return pieces[i].texture->is_pixel_opaque(p_x - rect.position.x, p_y - rect.position.y);
  989. }
  990. }
  991. return true;
  992. }
  993. LargeTexture::LargeTexture() {
  994. }
  995. ///////////////////////////////////////////////
  996. void CubeMap::set_flags(uint32_t p_flags) {
  997. flags = p_flags;
  998. if (_is_valid())
  999. VS::get_singleton()->texture_set_flags(cubemap, flags);
  1000. }
  1001. uint32_t CubeMap::get_flags() const {
  1002. return flags;
  1003. }
  1004. void CubeMap::set_side(Side p_side, const Ref<Image> &p_image) {
  1005. ERR_FAIL_COND(p_image->empty());
  1006. ERR_FAIL_INDEX(p_side, 6);
  1007. if (!_is_valid()) {
  1008. format = p_image->get_format();
  1009. w = p_image->get_width();
  1010. h = p_image->get_height();
  1011. VS::get_singleton()->texture_allocate(cubemap, w, h, 0, p_image->get_format(), VS::TEXTURE_TYPE_CUBEMAP, flags);
  1012. }
  1013. VS::get_singleton()->texture_set_data(cubemap, p_image, VS::CubeMapSide(p_side));
  1014. valid[p_side] = true;
  1015. }
  1016. Ref<Image> CubeMap::get_side(Side p_side) const {
  1017. if (!valid[p_side])
  1018. return Ref<Image>();
  1019. return VS::get_singleton()->texture_get_data(cubemap, VS::CubeMapSide(p_side));
  1020. }
  1021. Image::Format CubeMap::get_format() const {
  1022. return format;
  1023. }
  1024. int CubeMap::get_width() const {
  1025. return w;
  1026. }
  1027. int CubeMap::get_height() const {
  1028. return h;
  1029. }
  1030. RID CubeMap::get_rid() const {
  1031. return cubemap;
  1032. }
  1033. void CubeMap::set_storage(Storage p_storage) {
  1034. storage = p_storage;
  1035. }
  1036. CubeMap::Storage CubeMap::get_storage() const {
  1037. return storage;
  1038. }
  1039. void CubeMap::set_lossy_storage_quality(float p_lossy_storage_quality) {
  1040. lossy_storage_quality = p_lossy_storage_quality;
  1041. }
  1042. float CubeMap::get_lossy_storage_quality() const {
  1043. return lossy_storage_quality;
  1044. }
  1045. void CubeMap::set_path(const String &p_path, bool p_take_over) {
  1046. if (cubemap.is_valid()) {
  1047. VisualServer::get_singleton()->texture_set_path(cubemap, p_path);
  1048. }
  1049. Resource::set_path(p_path, p_take_over);
  1050. }
  1051. bool CubeMap::_set(const StringName &p_name, const Variant &p_value) {
  1052. if (p_name == "side/left") {
  1053. set_side(SIDE_LEFT, p_value);
  1054. } else if (p_name == "side/right") {
  1055. set_side(SIDE_RIGHT, p_value);
  1056. } else if (p_name == "side/bottom") {
  1057. set_side(SIDE_BOTTOM, p_value);
  1058. } else if (p_name == "side/top") {
  1059. set_side(SIDE_TOP, p_value);
  1060. } else if (p_name == "side/front") {
  1061. set_side(SIDE_FRONT, p_value);
  1062. } else if (p_name == "side/back") {
  1063. set_side(SIDE_BACK, p_value);
  1064. } else if (p_name == "storage") {
  1065. storage = Storage(p_value.operator int());
  1066. } else if (p_name == "lossy_quality") {
  1067. lossy_storage_quality = p_value;
  1068. } else
  1069. return false;
  1070. return true;
  1071. }
  1072. bool CubeMap::_get(const StringName &p_name, Variant &r_ret) const {
  1073. if (p_name == "side/left") {
  1074. r_ret = get_side(SIDE_LEFT);
  1075. } else if (p_name == "side/right") {
  1076. r_ret = get_side(SIDE_RIGHT);
  1077. } else if (p_name == "side/bottom") {
  1078. r_ret = get_side(SIDE_BOTTOM);
  1079. } else if (p_name == "side/top") {
  1080. r_ret = get_side(SIDE_TOP);
  1081. } else if (p_name == "side/front") {
  1082. r_ret = get_side(SIDE_FRONT);
  1083. } else if (p_name == "side/back") {
  1084. r_ret = get_side(SIDE_BACK);
  1085. } else if (p_name == "storage") {
  1086. r_ret = storage;
  1087. } else if (p_name == "lossy_quality") {
  1088. r_ret = lossy_storage_quality;
  1089. } else
  1090. return false;
  1091. return true;
  1092. }
  1093. void CubeMap::_get_property_list(List<PropertyInfo> *p_list) const {
  1094. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/left", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  1095. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/right", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  1096. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/bottom", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  1097. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/top", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  1098. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/front", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  1099. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/back", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  1100. }
  1101. void CubeMap::_bind_methods() {
  1102. ClassDB::bind_method(D_METHOD("get_width"), &CubeMap::get_width);
  1103. ClassDB::bind_method(D_METHOD("get_height"), &CubeMap::get_height);
  1104. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &CubeMap::set_flags);
  1105. ClassDB::bind_method(D_METHOD("get_flags"), &CubeMap::get_flags);
  1106. ClassDB::bind_method(D_METHOD("set_side", "side", "image"), &CubeMap::set_side);
  1107. ClassDB::bind_method(D_METHOD("get_side", "side"), &CubeMap::get_side);
  1108. ClassDB::bind_method(D_METHOD("set_storage", "mode"), &CubeMap::set_storage);
  1109. ClassDB::bind_method(D_METHOD("get_storage"), &CubeMap::get_storage);
  1110. ClassDB::bind_method(D_METHOD("set_lossy_storage_quality", "quality"), &CubeMap::set_lossy_storage_quality);
  1111. ClassDB::bind_method(D_METHOD("get_lossy_storage_quality"), &CubeMap::get_lossy_storage_quality);
  1112. ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter"), "set_flags", "get_flags");
  1113. ADD_PROPERTY(PropertyInfo(Variant::INT, "storage_mode", PROPERTY_HINT_ENUM, "Raw,Lossy Compressed,Lossless Compressed"), "set_storage", "get_storage");
  1114. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lossy_storage_quality"), "set_lossy_storage_quality", "get_lossy_storage_quality");
  1115. BIND_ENUM_CONSTANT(STORAGE_RAW);
  1116. BIND_ENUM_CONSTANT(STORAGE_COMPRESS_LOSSY);
  1117. BIND_ENUM_CONSTANT(STORAGE_COMPRESS_LOSSLESS);
  1118. BIND_ENUM_CONSTANT(SIDE_LEFT);
  1119. BIND_ENUM_CONSTANT(SIDE_RIGHT);
  1120. BIND_ENUM_CONSTANT(SIDE_BOTTOM);
  1121. BIND_ENUM_CONSTANT(SIDE_TOP);
  1122. BIND_ENUM_CONSTANT(SIDE_FRONT);
  1123. BIND_ENUM_CONSTANT(SIDE_BACK);
  1124. BIND_ENUM_CONSTANT(FLAG_MIPMAPS);
  1125. BIND_ENUM_CONSTANT(FLAG_REPEAT);
  1126. BIND_ENUM_CONSTANT(FLAG_FILTER);
  1127. BIND_ENUM_CONSTANT(FLAGS_DEFAULT);
  1128. }
  1129. CubeMap::CubeMap() {
  1130. w = h = 0;
  1131. flags = FLAGS_DEFAULT;
  1132. for (int i = 0; i < 6; i++)
  1133. valid[i] = false;
  1134. cubemap = VisualServer::get_singleton()->texture_create();
  1135. storage = STORAGE_RAW;
  1136. lossy_storage_quality = 0.7;
  1137. format = Image::FORMAT_BPTC_RGBA;
  1138. }
  1139. CubeMap::~CubeMap() {
  1140. VisualServer::get_singleton()->free(cubemap);
  1141. }
  1142. /* BIND_ENUM(CubeMapSize);
  1143. BIND_ENUM_CONSTANT( FLAG_CUBEMAP );
  1144. BIND_ENUM_CONSTANT( CUBEMAP_LEFT );
  1145. BIND_ENUM_CONSTANT( CUBEMAP_RIGHT );
  1146. BIND_ENUM_CONSTANT( CUBEMAP_BOTTOM );
  1147. BIND_ENUM_CONSTANT( CUBEMAP_TOP );
  1148. BIND_ENUM_CONSTANT( CUBEMAP_FRONT );
  1149. BIND_ENUM_CONSTANT( CUBEMAP_BACK );
  1150. */
  1151. ///////////////////////////
  1152. void CurveTexture::_bind_methods() {
  1153. ClassDB::bind_method(D_METHOD("set_width", "width"), &CurveTexture::set_width);
  1154. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &CurveTexture::set_curve);
  1155. ClassDB::bind_method(D_METHOD("get_curve"), &CurveTexture::get_curve);
  1156. ClassDB::bind_method(D_METHOD("_update"), &CurveTexture::_update);
  1157. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "32,4096"), "set_width", "get_width");
  1158. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  1159. }
  1160. void CurveTexture::set_width(int p_width) {
  1161. ERR_FAIL_COND(p_width < 32 || p_width > 4096);
  1162. _width = p_width;
  1163. _update();
  1164. }
  1165. int CurveTexture::get_width() const {
  1166. return _width;
  1167. }
  1168. void CurveTexture::ensure_default_setup(float p_min, float p_max) {
  1169. if (_curve.is_null()) {
  1170. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  1171. curve->add_point(Vector2(0, 1));
  1172. curve->add_point(Vector2(1, 1));
  1173. curve->set_min_value(p_min);
  1174. curve->set_max_value(p_max);
  1175. set_curve(curve);
  1176. // Min and max is 0..1 by default
  1177. }
  1178. }
  1179. void CurveTexture::set_curve(Ref<Curve> p_curve) {
  1180. if (_curve != p_curve) {
  1181. if (_curve.is_valid()) {
  1182. _curve->disconnect(CoreStringNames::get_singleton()->changed, this, "_update");
  1183. }
  1184. _curve = p_curve;
  1185. if (_curve.is_valid()) {
  1186. _curve->connect(CoreStringNames::get_singleton()->changed, this, "_update");
  1187. }
  1188. _update();
  1189. }
  1190. }
  1191. void CurveTexture::_update() {
  1192. PoolVector<uint8_t> data;
  1193. data.resize(_width * sizeof(float));
  1194. // The array is locked in that scope
  1195. {
  1196. PoolVector<uint8_t>::Write wd8 = data.write();
  1197. float *wd = (float *)wd8.ptr();
  1198. if (_curve.is_valid()) {
  1199. Curve &curve = **_curve;
  1200. for (int i = 0; i < _width; ++i) {
  1201. float t = i / static_cast<float>(_width);
  1202. wd[i] = curve.interpolate_baked(t);
  1203. }
  1204. } else {
  1205. for (int i = 0; i < _width; ++i) {
  1206. wd[i] = 0;
  1207. }
  1208. }
  1209. }
  1210. Ref<Image> image = memnew(Image(_width, 1, false, Image::FORMAT_RF, data));
  1211. VS::get_singleton()->texture_allocate(_texture, _width, 1, 0, Image::FORMAT_RF, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_FILTER);
  1212. VS::get_singleton()->texture_set_data(_texture, image);
  1213. emit_changed();
  1214. }
  1215. Ref<Curve> CurveTexture::get_curve() const {
  1216. return _curve;
  1217. }
  1218. RID CurveTexture::get_rid() const {
  1219. return _texture;
  1220. }
  1221. CurveTexture::CurveTexture() {
  1222. _width = 2048;
  1223. _texture = VS::get_singleton()->texture_create();
  1224. }
  1225. CurveTexture::~CurveTexture() {
  1226. VS::get_singleton()->free(_texture);
  1227. }
  1228. //////////////////
  1229. //setter and getter names for property serialization
  1230. #define COLOR_RAMP_GET_OFFSETS "get_offsets"
  1231. #define COLOR_RAMP_GET_COLORS "get_colors"
  1232. #define COLOR_RAMP_SET_OFFSETS "set_offsets"
  1233. #define COLOR_RAMP_SET_COLORS "set_colors"
  1234. GradientTexture::GradientTexture() {
  1235. update_pending = false;
  1236. width = 2048;
  1237. texture = VS::get_singleton()->texture_create();
  1238. _queue_update();
  1239. }
  1240. GradientTexture::~GradientTexture() {
  1241. VS::get_singleton()->free(texture);
  1242. }
  1243. void GradientTexture::_bind_methods() {
  1244. ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture::set_gradient);
  1245. ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture::get_gradient);
  1246. ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture::set_width);
  1247. ClassDB::bind_method(D_METHOD("_update"), &GradientTexture::_update);
  1248. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_gradient", "get_gradient");
  1249. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_width", "get_width");
  1250. }
  1251. void GradientTexture::set_gradient(Ref<Gradient> p_gradient) {
  1252. if (p_gradient == gradient)
  1253. return;
  1254. if (gradient.is_valid()) {
  1255. gradient->disconnect(CoreStringNames::get_singleton()->changed, this, "_update");
  1256. }
  1257. gradient = p_gradient;
  1258. if (gradient.is_valid()) {
  1259. gradient->connect(CoreStringNames::get_singleton()->changed, this, "_update");
  1260. }
  1261. _update();
  1262. emit_changed();
  1263. }
  1264. Ref<Gradient> GradientTexture::get_gradient() const {
  1265. return gradient;
  1266. }
  1267. void GradientTexture::_queue_update() {
  1268. if (update_pending)
  1269. return;
  1270. update_pending = true;
  1271. call_deferred("_update");
  1272. }
  1273. void GradientTexture::_update() {
  1274. update_pending = false;
  1275. if (gradient.is_null())
  1276. return;
  1277. PoolVector<uint8_t> data;
  1278. data.resize(width * 4);
  1279. {
  1280. PoolVector<uint8_t>::Write wd8 = data.write();
  1281. Gradient &g = **gradient;
  1282. for (int i = 0; i < width; i++) {
  1283. float ofs = float(i) / (width - 1);
  1284. Color color = g.get_color_at_offset(ofs);
  1285. wd8[i * 4 + 0] = uint8_t(CLAMP(color.r * 255.0, 0, 255));
  1286. wd8[i * 4 + 1] = uint8_t(CLAMP(color.g * 255.0, 0, 255));
  1287. wd8[i * 4 + 2] = uint8_t(CLAMP(color.b * 255.0, 0, 255));
  1288. wd8[i * 4 + 3] = uint8_t(CLAMP(color.a * 255.0, 0, 255));
  1289. }
  1290. }
  1291. Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data));
  1292. VS::get_singleton()->texture_allocate(texture, width, 1, 0, Image::FORMAT_RGBA8, VS::TEXTURE_TYPE_2D, VS::TEXTURE_FLAG_FILTER);
  1293. VS::get_singleton()->texture_set_data(texture, image);
  1294. emit_changed();
  1295. }
  1296. void GradientTexture::set_width(int p_width) {
  1297. width = p_width;
  1298. _queue_update();
  1299. }
  1300. int GradientTexture::get_width() const {
  1301. return width;
  1302. }
  1303. Ref<Image> GradientTexture::get_data() const {
  1304. return VisualServer::get_singleton()->texture_get_data(texture);
  1305. }
  1306. //////////////////////////////////////
  1307. void ProxyTexture::_bind_methods() {
  1308. ClassDB::bind_method(D_METHOD("set_base", "base"), &ProxyTexture::set_base);
  1309. ClassDB::bind_method(D_METHOD("get_base"), &ProxyTexture::get_base);
  1310. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "base", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_base", "get_base");
  1311. }
  1312. void ProxyTexture::set_base(const Ref<Texture> &p_texture) {
  1313. ERR_FAIL_COND(p_texture == this);
  1314. base = p_texture;
  1315. if (base.is_valid()) {
  1316. VS::get_singleton()->texture_set_proxy(proxy, base->get_rid());
  1317. } else {
  1318. VS::get_singleton()->texture_set_proxy(proxy, RID());
  1319. }
  1320. }
  1321. Ref<Texture> ProxyTexture::get_base() const {
  1322. return base;
  1323. }
  1324. int ProxyTexture::get_width() const {
  1325. if (base.is_valid())
  1326. return base->get_width();
  1327. return 1;
  1328. }
  1329. int ProxyTexture::get_height() const {
  1330. if (base.is_valid())
  1331. return base->get_height();
  1332. return 1;
  1333. }
  1334. RID ProxyTexture::get_rid() const {
  1335. return proxy;
  1336. }
  1337. bool ProxyTexture::has_alpha() const {
  1338. if (base.is_valid())
  1339. return base->has_alpha();
  1340. return false;
  1341. }
  1342. void ProxyTexture::set_flags(uint32_t p_flags) {
  1343. }
  1344. uint32_t ProxyTexture::get_flags() const {
  1345. if (base.is_valid())
  1346. return base->get_flags();
  1347. return 0;
  1348. }
  1349. ProxyTexture::ProxyTexture() {
  1350. proxy = VS::get_singleton()->texture_create();
  1351. }
  1352. ProxyTexture::~ProxyTexture() {
  1353. VS::get_singleton()->free(proxy);
  1354. }
  1355. //////////////////////////////////////////////
  1356. void AnimatedTexture::_update_proxy() {
  1357. RWLockRead r(rw_lock);
  1358. float delta;
  1359. if (prev_ticks == 0) {
  1360. delta = 0;
  1361. prev_ticks = OS::get_singleton()->get_ticks_usec();
  1362. } else {
  1363. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  1364. delta = float(double(ticks - prev_ticks) / 1000000.0);
  1365. prev_ticks = ticks;
  1366. }
  1367. time += delta;
  1368. float limit;
  1369. if (fps == 0) {
  1370. limit = 0;
  1371. } else {
  1372. limit = 1.0 / fps;
  1373. }
  1374. int iter_max = frame_count;
  1375. while (iter_max) {
  1376. float frame_limit = limit + frames[current_frame].delay_sec;
  1377. if (time > frame_limit) {
  1378. current_frame++;
  1379. if (current_frame >= frame_count) {
  1380. current_frame = 0;
  1381. }
  1382. time -= frame_limit;
  1383. } else {
  1384. break;
  1385. }
  1386. iter_max--;
  1387. }
  1388. if (frames[current_frame].texture.is_valid()) {
  1389. VisualServer::get_singleton()->texture_set_proxy(proxy, frames[current_frame].texture->get_rid());
  1390. }
  1391. }
  1392. void AnimatedTexture::set_frames(int p_frames) {
  1393. ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES);
  1394. RWLockWrite r(rw_lock);
  1395. frame_count = p_frames;
  1396. }
  1397. int AnimatedTexture::get_frames() const {
  1398. return frame_count;
  1399. }
  1400. void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture> &p_texture) {
  1401. ERR_FAIL_COND(p_texture == this);
  1402. ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
  1403. RWLockWrite w(rw_lock);
  1404. frames[p_frame].texture = p_texture;
  1405. }
  1406. Ref<Texture> AnimatedTexture::get_frame_texture(int p_frame) const {
  1407. ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture>());
  1408. RWLockRead r(rw_lock);
  1409. return frames[p_frame].texture;
  1410. }
  1411. void AnimatedTexture::set_frame_delay(int p_frame, float p_delay_sec) {
  1412. ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
  1413. RWLockRead r(rw_lock);
  1414. frames[p_frame].delay_sec = p_delay_sec;
  1415. }
  1416. float AnimatedTexture::get_frame_delay(int p_frame) const {
  1417. ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0);
  1418. RWLockRead r(rw_lock);
  1419. return frames[p_frame].delay_sec;
  1420. }
  1421. void AnimatedTexture::set_fps(float p_fps) {
  1422. ERR_FAIL_COND(p_fps < 0 || p_fps >= 1000);
  1423. fps = p_fps;
  1424. }
  1425. float AnimatedTexture::get_fps() const {
  1426. return fps;
  1427. }
  1428. int AnimatedTexture::get_width() const {
  1429. RWLockRead r(rw_lock);
  1430. if (!frames[current_frame].texture.is_valid()) {
  1431. return 1;
  1432. }
  1433. return frames[current_frame].texture->get_width();
  1434. }
  1435. int AnimatedTexture::get_height() const {
  1436. RWLockRead r(rw_lock);
  1437. if (!frames[current_frame].texture.is_valid()) {
  1438. return 1;
  1439. }
  1440. return frames[current_frame].texture->get_height();
  1441. }
  1442. RID AnimatedTexture::get_rid() const {
  1443. return proxy;
  1444. }
  1445. bool AnimatedTexture::has_alpha() const {
  1446. RWLockRead r(rw_lock);
  1447. if (!frames[current_frame].texture.is_valid()) {
  1448. return false;
  1449. }
  1450. return frames[current_frame].texture->has_alpha();
  1451. }
  1452. Ref<Image> AnimatedTexture::get_data() const {
  1453. RWLockRead r(rw_lock);
  1454. if (!frames[current_frame].texture.is_valid()) {
  1455. return Ref<Image>();
  1456. }
  1457. return frames[current_frame].texture->get_data();
  1458. }
  1459. bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const {
  1460. RWLockRead r(rw_lock);
  1461. if (frames[current_frame].texture.is_valid()) {
  1462. return frames[current_frame].texture->is_pixel_opaque(p_x, p_y);
  1463. }
  1464. return true;
  1465. }
  1466. void AnimatedTexture::set_flags(uint32_t p_flags) {
  1467. }
  1468. uint32_t AnimatedTexture::get_flags() const {
  1469. RWLockRead r(rw_lock);
  1470. if (!frames[current_frame].texture.is_valid()) {
  1471. return 0;
  1472. }
  1473. return frames[current_frame].texture->get_flags();
  1474. }
  1475. void AnimatedTexture::_validate_property(PropertyInfo &property) const {
  1476. String prop = property.name;
  1477. if (prop.begins_with("frame_")) {
  1478. int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
  1479. if (frame >= frame_count) {
  1480. property.usage = 0;
  1481. }
  1482. }
  1483. }
  1484. void AnimatedTexture::_bind_methods() {
  1485. ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames);
  1486. ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames);
  1487. ClassDB::bind_method(D_METHOD("set_fps", "fps"), &AnimatedTexture::set_fps);
  1488. ClassDB::bind_method(D_METHOD("get_fps"), &AnimatedTexture::get_fps);
  1489. ClassDB::bind_method(D_METHOD("set_frame_texture", "frame", "texture"), &AnimatedTexture::set_frame_texture);
  1490. ClassDB::bind_method(D_METHOD("get_frame_texture", "frame"), &AnimatedTexture::get_frame_texture);
  1491. ClassDB::bind_method(D_METHOD("set_frame_delay", "frame", "delay"), &AnimatedTexture::set_frame_delay);
  1492. ClassDB::bind_method(D_METHOD("get_frame_delay", "frame"), &AnimatedTexture::get_frame_delay);
  1493. ClassDB::bind_method(D_METHOD("_update_proxy"), &AnimatedTexture::_update_proxy);
  1494. ADD_PROPERTY(PropertyInfo(Variant::INT, "frames", PROPERTY_HINT_RANGE, "1," + itos(MAX_FRAMES), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_frames", "get_frames");
  1495. ADD_PROPERTY(PropertyInfo(Variant::REAL, "fps", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_fps", "get_fps");
  1496. for (int i = 0; i < MAX_FRAMES; i++) {
  1497. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "frame_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_texture", "get_frame_texture", i);
  1498. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "frame_" + itos(i) + "/delay_sec", PROPERTY_HINT_RANGE, "0.0,16.0,0.01", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_delay", "get_frame_delay", i);
  1499. }
  1500. BIND_CONSTANT(MAX_FRAMES);
  1501. }
  1502. AnimatedTexture::AnimatedTexture() {
  1503. proxy = VS::get_singleton()->texture_create();
  1504. VisualServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);
  1505. time = 0;
  1506. frame_count = 1;
  1507. fps = 4;
  1508. prev_ticks = 0;
  1509. current_frame = 0;
  1510. VisualServer::get_singleton()->connect("frame_pre_draw", this, "_update_proxy");
  1511. #ifndef NO_THREADS
  1512. rw_lock = RWLock::create();
  1513. #else
  1514. rw_lock = NULL;
  1515. #endif
  1516. }
  1517. AnimatedTexture::~AnimatedTexture() {
  1518. VS::get_singleton()->free(proxy);
  1519. if (rw_lock) {
  1520. memdelete(rw_lock);
  1521. }
  1522. }
  1523. ///////////////////////////////
  1524. void TextureLayered::set_flags(uint32_t p_flags) {
  1525. flags = p_flags;
  1526. if (texture.is_valid()) {
  1527. VS::get_singleton()->texture_set_flags(texture, flags);
  1528. }
  1529. }
  1530. uint32_t TextureLayered::get_flags() const {
  1531. return flags;
  1532. }
  1533. Image::Format TextureLayered::get_format() const {
  1534. return format;
  1535. }
  1536. uint32_t TextureLayered::get_width() const {
  1537. return width;
  1538. }
  1539. uint32_t TextureLayered::get_height() const {
  1540. return height;
  1541. }
  1542. uint32_t TextureLayered::get_depth() const {
  1543. return depth;
  1544. }
  1545. void TextureLayered::_set_data(const Dictionary &p_data) {
  1546. ERR_FAIL_COND(!p_data.has("width"));
  1547. ERR_FAIL_COND(!p_data.has("height"));
  1548. ERR_FAIL_COND(!p_data.has("depth"));
  1549. ERR_FAIL_COND(!p_data.has("format"));
  1550. ERR_FAIL_COND(!p_data.has("flags"));
  1551. ERR_FAIL_COND(!p_data.has("layers"));
  1552. int w = p_data["width"];
  1553. int h = p_data["height"];
  1554. int d = p_data["depth"];
  1555. Image::Format format = Image::Format(int(p_data["format"]));
  1556. int flags = p_data["flags"];
  1557. Array layers = p_data["layers"];
  1558. ERR_FAIL_COND(layers.size() != d);
  1559. create(w, h, d, format, flags);
  1560. for (int i = 0; i < layers.size(); i++) {
  1561. Ref<Image> img = layers[i];
  1562. ERR_CONTINUE(!img.is_valid());
  1563. ERR_CONTINUE(img->get_format() != format);
  1564. ERR_CONTINUE(img->get_width() != w);
  1565. ERR_CONTINUE(img->get_height() != h);
  1566. set_layer_data(img, i);
  1567. }
  1568. }
  1569. Dictionary TextureLayered::_get_data() const {
  1570. Dictionary d;
  1571. d["width"] = width;
  1572. d["height"] = height;
  1573. d["depth"] = depth;
  1574. d["flags"] = flags;
  1575. d["format"] = format;
  1576. Array layers;
  1577. for (int i = 0; i < depth; i++) {
  1578. layers.push_back(get_layer_data(i));
  1579. }
  1580. d["layers"] = layers;
  1581. return d;
  1582. }
  1583. void TextureLayered::create(uint32_t p_width, uint32_t p_height, uint32_t p_depth, Image::Format p_format, uint32_t p_flags) {
  1584. VS::get_singleton()->texture_allocate(texture, p_width, p_height, p_depth, p_format, is_3d ? VS::TEXTURE_TYPE_3D : VS::TEXTURE_TYPE_2D_ARRAY, p_flags);
  1585. width = p_width;
  1586. height = p_height;
  1587. depth = p_depth;
  1588. format = p_format;
  1589. flags = p_flags;
  1590. }
  1591. void TextureLayered::set_layer_data(const Ref<Image> &p_image, int p_layer) {
  1592. ERR_FAIL_COND(!texture.is_valid());
  1593. VS::get_singleton()->texture_set_data(texture, p_image, p_layer);
  1594. }
  1595. Ref<Image> TextureLayered::get_layer_data(int p_layer) const {
  1596. ERR_FAIL_COND_V(!texture.is_valid(), Ref<Image>());
  1597. return VS::get_singleton()->texture_get_data(texture, p_layer);
  1598. }
  1599. void TextureLayered::set_data_partial(const Ref<Image> &p_image, int p_x_ofs, int p_y_ofs, int p_z, int p_mipmap) {
  1600. ERR_FAIL_COND(!texture.is_valid());
  1601. VS::get_singleton()->texture_set_data_partial(texture, p_image, 0, 0, p_image->get_width(), p_image->get_height(), p_x_ofs, p_y_ofs, p_mipmap, p_z);
  1602. }
  1603. RID TextureLayered::get_rid() const {
  1604. return texture;
  1605. }
  1606. void TextureLayered::set_path(const String &p_path, bool p_take_over) {
  1607. if (texture.is_valid()) {
  1608. VS::get_singleton()->texture_set_path(texture, p_path);
  1609. }
  1610. Resource::set_path(p_path, p_take_over);
  1611. }
  1612. void TextureLayered::_bind_methods() {
  1613. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &TextureLayered::set_flags);
  1614. ClassDB::bind_method(D_METHOD("get_flags"), &TextureLayered::get_flags);
  1615. ClassDB::bind_method(D_METHOD("get_format"), &TextureLayered::get_format);
  1616. ClassDB::bind_method(D_METHOD("get_width"), &TextureLayered::get_width);
  1617. ClassDB::bind_method(D_METHOD("get_height"), &TextureLayered::get_height);
  1618. ClassDB::bind_method(D_METHOD("get_depth"), &TextureLayered::get_depth);
  1619. ClassDB::bind_method(D_METHOD("create", "width", "height", "depth", "format", "flags"), &TextureLayered::create, DEFVAL(FLAGS_DEFAULT));
  1620. ClassDB::bind_method(D_METHOD("set_layer_data", "image", "layer"), &TextureLayered::set_layer_data);
  1621. ClassDB::bind_method(D_METHOD("get_layer_data", "layer"), &TextureLayered::get_layer_data);
  1622. ClassDB::bind_method(D_METHOD("set_data_partial", "image", "x_offset", "y_offset", "layer", "mipmap"), &TextureLayered::set_data_partial, DEFVAL(0));
  1623. ClassDB::bind_method(D_METHOD("_set_data", "data"), &TextureLayered::_set_data);
  1624. ClassDB::bind_method(D_METHOD("_get_data"), &TextureLayered::_get_data);
  1625. ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter"), "set_flags", "get_flags");
  1626. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_data", "_get_data");
  1627. BIND_ENUM_CONSTANT(FLAG_MIPMAPS);
  1628. BIND_ENUM_CONSTANT(FLAG_REPEAT);
  1629. BIND_ENUM_CONSTANT(FLAG_FILTER);
  1630. BIND_ENUM_CONSTANT(FLAGS_DEFAULT);
  1631. }
  1632. TextureLayered::TextureLayered(bool p_3d) {
  1633. is_3d = p_3d;
  1634. format = Image::FORMAT_MAX;
  1635. flags = FLAGS_DEFAULT;
  1636. width = 0;
  1637. height = 0;
  1638. depth = 0;
  1639. texture = VS::get_singleton()->texture_create();
  1640. }
  1641. TextureLayered::~TextureLayered() {
  1642. if (texture.is_valid()) {
  1643. VS::get_singleton()->free(texture);
  1644. }
  1645. }
  1646. RES ResourceFormatLoaderTextureLayered::load(const String &p_path, const String &p_original_path, Error *r_error) {
  1647. if (r_error) {
  1648. *r_error = ERR_CANT_OPEN;
  1649. }
  1650. Ref<TextureLayered> lt;
  1651. Ref<Texture3D> tex3d;
  1652. Ref<TextureArray> texarr;
  1653. if (p_path.ends_with("tex3d")) {
  1654. tex3d.instance();
  1655. lt = tex3d;
  1656. } else if (p_path.ends_with("texarr")) {
  1657. texarr.instance();
  1658. lt = texarr;
  1659. } else {
  1660. ERR_EXPLAIN("Unrecognized layered texture extension");
  1661. ERR_FAIL_V(RES());
  1662. }
  1663. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  1664. ERR_FAIL_COND_V(!f, RES());
  1665. uint8_t header[5] = { 0, 0, 0, 0, 0 };
  1666. f->get_buffer(header, 4);
  1667. if (header[0] == 'G' && header[1] == 'D' && header[2] == '3' && header[3] == 'T') {
  1668. if (tex3d.is_null()) {
  1669. memdelete(f);
  1670. ERR_FAIL_COND_V(tex3d.is_null(), RES())
  1671. }
  1672. } else if (header[0] == 'G' && header[1] == 'D' && header[2] == 'A' && header[3] == 'T') {
  1673. if (texarr.is_null()) {
  1674. memdelete(f);
  1675. ERR_FAIL_COND_V(texarr.is_null(), RES())
  1676. }
  1677. } else {
  1678. ERR_EXPLAIN("Unrecognized layered texture file format: " + String((const char *)header));
  1679. ERR_FAIL_V(RES());
  1680. }
  1681. int tw = f->get_32();
  1682. int th = f->get_32();
  1683. int td = f->get_32();
  1684. int flags = f->get_32(); //texture flags!
  1685. Image::Format format = Image::Format(f->get_32());
  1686. uint32_t compression = f->get_32(); // 0 - lossless (PNG), 1 - vram, 2 - uncompressed
  1687. lt->create(tw, th, td, format, flags);
  1688. for (int layer = 0; layer < td; layer++) {
  1689. Ref<Image> image;
  1690. image.instance();
  1691. if (compression == COMPRESSION_LOSSLESS) {
  1692. //look for a PNG file inside
  1693. int mipmaps = f->get_32();
  1694. Vector<Ref<Image> > mipmap_images;
  1695. for (int i = 0; i < mipmaps; i++) {
  1696. uint32_t size = f->get_32();
  1697. PoolVector<uint8_t> pv;
  1698. pv.resize(size);
  1699. {
  1700. PoolVector<uint8_t>::Write w = pv.write();
  1701. f->get_buffer(w.ptr(), size);
  1702. }
  1703. Ref<Image> img = Image::lossless_unpacker(pv);
  1704. if (img.is_null() || img->empty() || format != img->get_format()) {
  1705. if (r_error) {
  1706. *r_error = ERR_FILE_CORRUPT;
  1707. }
  1708. memdelete(f);
  1709. ERR_FAIL_V(RES());
  1710. }
  1711. mipmap_images.push_back(img);
  1712. }
  1713. if (mipmap_images.size() == 1) {
  1714. image = mipmap_images[0];
  1715. } else {
  1716. int total_size = Image::get_image_data_size(tw, th, format, true);
  1717. PoolVector<uint8_t> img_data;
  1718. img_data.resize(total_size);
  1719. {
  1720. PoolVector<uint8_t>::Write w = img_data.write();
  1721. int ofs = 0;
  1722. for (int i = 0; i < mipmap_images.size(); i++) {
  1723. PoolVector<uint8_t> id = mipmap_images[i]->get_data();
  1724. int len = id.size();
  1725. PoolVector<uint8_t>::Read r = id.read();
  1726. copymem(&w[ofs], r.ptr(), len);
  1727. ofs += len;
  1728. }
  1729. }
  1730. image->create(tw, th, true, format, img_data);
  1731. if (image->empty()) {
  1732. if (r_error) {
  1733. *r_error = ERR_FILE_CORRUPT;
  1734. }
  1735. memdelete(f);
  1736. ERR_FAIL_V(RES());
  1737. }
  1738. }
  1739. } else {
  1740. //look for regular format
  1741. bool mipmaps = (flags & Texture::FLAG_MIPMAPS);
  1742. int total_size = Image::get_image_data_size(tw, th, format, mipmaps);
  1743. PoolVector<uint8_t> img_data;
  1744. img_data.resize(total_size);
  1745. {
  1746. PoolVector<uint8_t>::Write w = img_data.write();
  1747. int bytes = f->get_buffer(w.ptr(), total_size);
  1748. if (bytes != total_size) {
  1749. if (r_error) {
  1750. *r_error = ERR_FILE_CORRUPT;
  1751. memdelete(f);
  1752. }
  1753. ERR_FAIL_V(RES());
  1754. }
  1755. }
  1756. image->create(tw, th, mipmaps, format, img_data);
  1757. }
  1758. lt->set_layer_data(image, layer);
  1759. }
  1760. if (r_error)
  1761. *r_error = OK;
  1762. return lt;
  1763. }
  1764. void ResourceFormatLoaderTextureLayered::get_recognized_extensions(List<String> *p_extensions) const {
  1765. p_extensions->push_back("tex3d");
  1766. p_extensions->push_back("texarr");
  1767. }
  1768. bool ResourceFormatLoaderTextureLayered::handles_type(const String &p_type) const {
  1769. return p_type == "Texture3D" || p_type == "TextureArray";
  1770. }
  1771. String ResourceFormatLoaderTextureLayered::get_resource_type(const String &p_path) const {
  1772. if (p_path.get_extension().to_lower() == "tex3d")
  1773. return "Texture3D";
  1774. if (p_path.get_extension().to_lower() == "texarr")
  1775. return "TextureArray";
  1776. return "";
  1777. }