texture.cpp 64 KB

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