texture.cpp 84 KB

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