texture.cpp 65 KB

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