texture.cpp 77 KB

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