texture.cpp 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543
  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/io/marshalls.h"
  34. #include "core/math/geometry_2d.h"
  35. #include "core/os/os.h"
  36. #include "scene/resources/bit_map.h"
  37. #include "scene/resources/mesh.h"
  38. #include "servers/camera/camera_feed.h"
  39. int Texture2D::get_width() const {
  40. int ret = 0;
  41. GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
  42. return ret;
  43. }
  44. int Texture2D::get_height() const {
  45. int ret = 0;
  46. GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
  47. return ret;
  48. }
  49. Size2 Texture2D::get_size() const {
  50. return Size2(get_width(), get_height());
  51. }
  52. bool Texture2D::is_pixel_opaque(int p_x, int p_y) const {
  53. bool ret = true;
  54. GDVIRTUAL_CALL(_is_pixel_opaque, p_x, p_y, ret);
  55. return ret;
  56. }
  57. bool Texture2D::has_alpha() const {
  58. bool ret = true;
  59. GDVIRTUAL_CALL(_has_alpha, ret);
  60. return ret;
  61. }
  62. void Texture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  63. if (GDVIRTUAL_CALL(_draw, p_canvas_item, p_pos, p_modulate, p_transpose)) {
  64. return;
  65. }
  66. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, get_size()), get_rid(), false, p_modulate, p_transpose);
  67. }
  68. void Texture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  69. if (GDVIRTUAL_CALL(_draw_rect, p_canvas_item, p_rect, p_tile, p_modulate, p_transpose)) {
  70. return;
  71. }
  72. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_rid(), p_tile, p_modulate, p_transpose);
  73. }
  74. void Texture2D::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
  75. if (GDVIRTUAL_CALL(_draw_rect_region, p_canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_clip_uv)) {
  76. return;
  77. }
  78. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, get_rid(), p_src_rect, p_modulate, p_transpose, p_clip_uv);
  79. }
  80. bool Texture2D::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  81. r_rect = p_rect;
  82. r_src_rect = p_src_rect;
  83. return true;
  84. }
  85. Ref<Resource> Texture2D::create_placeholder() const {
  86. Ref<PlaceholderTexture2D> placeholder;
  87. placeholder.instantiate();
  88. placeholder->set_size(get_size());
  89. return placeholder;
  90. }
  91. void Texture2D::_bind_methods() {
  92. ClassDB::bind_method(D_METHOD("get_width"), &Texture2D::get_width);
  93. ClassDB::bind_method(D_METHOD("get_height"), &Texture2D::get_height);
  94. ClassDB::bind_method(D_METHOD("get_size"), &Texture2D::get_size);
  95. ClassDB::bind_method(D_METHOD("has_alpha"), &Texture2D::has_alpha);
  96. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "position", "modulate", "transpose"), &Texture2D::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(false));
  97. ClassDB::bind_method(D_METHOD("draw_rect", "canvas_item", "rect", "tile", "modulate", "transpose"), &Texture2D::draw_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false));
  98. ClassDB::bind_method(D_METHOD("draw_rect_region", "canvas_item", "rect", "src_rect", "modulate", "transpose", "clip_uv"), &Texture2D::draw_rect_region, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(true));
  99. ClassDB::bind_method(D_METHOD("get_image"), &Texture2D::get_image);
  100. ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2D::create_placeholder);
  101. ADD_GROUP("", "");
  102. GDVIRTUAL_BIND(_get_width);
  103. GDVIRTUAL_BIND(_get_height);
  104. GDVIRTUAL_BIND(_is_pixel_opaque, "x", "y");
  105. GDVIRTUAL_BIND(_has_alpha);
  106. GDVIRTUAL_BIND(_draw, "to_canvas_item", "pos", "modulate", "transpose")
  107. GDVIRTUAL_BIND(_draw_rect, "to_canvas_item", "rect", "tile", "modulate", "transpose")
  108. GDVIRTUAL_BIND(_draw_rect_region, "to_canvas_item", "rect", "src_rect", "modulate", "transpose", "clip_uv");
  109. }
  110. Texture2D::Texture2D() {
  111. }
  112. /////////////////////
  113. void ImageTexture::reload_from_file() {
  114. String path = ResourceLoader::path_remap(get_path());
  115. if (!path.is_resource_file()) {
  116. return;
  117. }
  118. Ref<Image> img;
  119. img.instantiate();
  120. if (ImageLoader::load_image(path, img) == OK) {
  121. set_image(img);
  122. } else {
  123. Resource::reload_from_file();
  124. notify_property_list_changed();
  125. emit_changed();
  126. }
  127. }
  128. bool ImageTexture::_set(const StringName &p_name, const Variant &p_value) {
  129. if (p_name == "image") {
  130. set_image(p_value);
  131. return true;
  132. }
  133. return false;
  134. }
  135. bool ImageTexture::_get(const StringName &p_name, Variant &r_ret) const {
  136. if (p_name == "image") {
  137. r_ret = get_image();
  138. return true;
  139. }
  140. return false;
  141. }
  142. void ImageTexture::_get_property_list(List<PropertyInfo> *p_list) const {
  143. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("image"), PROPERTY_HINT_RESOURCE_TYPE, "Image", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT));
  144. }
  145. Ref<ImageTexture> ImageTexture::create_from_image(const Ref<Image> &p_image) {
  146. ERR_FAIL_COND_V_MSG(p_image.is_null() || p_image->is_empty(), Ref<ImageTexture>(), "Invalid image");
  147. Ref<ImageTexture> image_texture;
  148. image_texture.instantiate();
  149. image_texture->set_image(p_image);
  150. return image_texture;
  151. }
  152. void ImageTexture::set_image(const Ref<Image> &p_image) {
  153. ERR_FAIL_COND_MSG(p_image.is_null() || p_image->is_empty(), "Invalid image");
  154. w = p_image->get_width();
  155. h = p_image->get_height();
  156. format = p_image->get_format();
  157. mipmaps = p_image->has_mipmaps();
  158. if (texture.is_null()) {
  159. texture = RenderingServer::get_singleton()->texture_2d_create(p_image);
  160. } else {
  161. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_image);
  162. RenderingServer::get_singleton()->texture_replace(texture, new_texture);
  163. }
  164. notify_property_list_changed();
  165. emit_changed();
  166. image_stored = true;
  167. }
  168. Image::Format ImageTexture::get_format() const {
  169. return format;
  170. }
  171. void ImageTexture::update(const Ref<Image> &p_image) {
  172. ERR_FAIL_COND_MSG(p_image.is_null(), "Invalid image");
  173. ERR_FAIL_COND_MSG(texture.is_null(), "Texture is not initialized.");
  174. ERR_FAIL_COND_MSG(p_image->get_width() != w || p_image->get_height() != h,
  175. "The new image dimensions must match the texture size.");
  176. ERR_FAIL_COND_MSG(p_image->get_format() != format,
  177. "The new image format must match the texture's image format.");
  178. ERR_FAIL_COND_MSG(mipmaps != p_image->has_mipmaps(),
  179. "The new image mipmaps configuration must match the texture's image mipmaps configuration");
  180. RS::get_singleton()->texture_2d_update(texture, p_image);
  181. notify_property_list_changed();
  182. emit_changed();
  183. alpha_cache.unref();
  184. image_stored = true;
  185. }
  186. Ref<Image> ImageTexture::get_image() const {
  187. if (image_stored) {
  188. return RenderingServer::get_singleton()->texture_2d_get(texture);
  189. } else {
  190. return Ref<Image>();
  191. }
  192. }
  193. int ImageTexture::get_width() const {
  194. return w;
  195. }
  196. int ImageTexture::get_height() const {
  197. return h;
  198. }
  199. RID ImageTexture::get_rid() const {
  200. if (texture.is_null()) {
  201. //we are in trouble, create something temporary
  202. texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  203. }
  204. return texture;
  205. }
  206. bool ImageTexture::has_alpha() const {
  207. return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8);
  208. }
  209. void ImageTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  210. if ((w | h) == 0) {
  211. return;
  212. }
  213. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, Size2(w, h)), texture, false, p_modulate, p_transpose);
  214. }
  215. void ImageTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  216. if ((w | h) == 0) {
  217. return;
  218. }
  219. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose);
  220. }
  221. 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, bool p_clip_uv) const {
  222. if ((w | h) == 0) {
  223. return;
  224. }
  225. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, p_clip_uv);
  226. }
  227. bool ImageTexture::is_pixel_opaque(int p_x, int p_y) const {
  228. if (!alpha_cache.is_valid()) {
  229. Ref<Image> img = get_image();
  230. if (img.is_valid()) {
  231. if (img->is_compressed()) { //must decompress, if compressed
  232. Ref<Image> decom = img->duplicate();
  233. decom->decompress();
  234. img = decom;
  235. }
  236. alpha_cache.instantiate();
  237. alpha_cache->create_from_image_alpha(img);
  238. }
  239. }
  240. if (alpha_cache.is_valid()) {
  241. int aw = int(alpha_cache->get_size().width);
  242. int ah = int(alpha_cache->get_size().height);
  243. if (aw == 0 || ah == 0) {
  244. return true;
  245. }
  246. int x = p_x * aw / w;
  247. int y = p_y * ah / h;
  248. x = CLAMP(x, 0, aw);
  249. y = CLAMP(y, 0, ah);
  250. return alpha_cache->get_bit(x, y);
  251. }
  252. return true;
  253. }
  254. void ImageTexture::set_size_override(const Size2i &p_size) {
  255. Size2i s = p_size;
  256. if (s.x != 0) {
  257. w = s.x;
  258. }
  259. if (s.y != 0) {
  260. h = s.y;
  261. }
  262. RenderingServer::get_singleton()->texture_set_size_override(texture, w, h);
  263. }
  264. void ImageTexture::set_path(const String &p_path, bool p_take_over) {
  265. if (texture.is_valid()) {
  266. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  267. }
  268. Resource::set_path(p_path, p_take_over);
  269. }
  270. void ImageTexture::_bind_methods() {
  271. ClassDB::bind_static_method("ImageTexture", D_METHOD("create_from_image", "image"), &ImageTexture::create_from_image);
  272. ClassDB::bind_method(D_METHOD("get_format"), &ImageTexture::get_format);
  273. ClassDB::bind_method(D_METHOD("set_image", "image"), &ImageTexture::set_image);
  274. ClassDB::bind_method(D_METHOD("update", "image"), &ImageTexture::update);
  275. ClassDB::bind_method(D_METHOD("set_size_override", "size"), &ImageTexture::set_size_override);
  276. }
  277. ImageTexture::ImageTexture() {}
  278. ImageTexture::~ImageTexture() {
  279. if (texture.is_valid()) {
  280. ERR_FAIL_NULL(RenderingServer::get_singleton());
  281. RenderingServer::get_singleton()->free(texture);
  282. }
  283. }
  284. /////////////////////
  285. void PortableCompressedTexture2D::_set_data(const Vector<uint8_t> &p_data) {
  286. if (p_data.size() == 0) {
  287. return; //nothing to do
  288. }
  289. const uint8_t *data = p_data.ptr();
  290. uint32_t data_size = p_data.size();
  291. ERR_FAIL_COND(data_size < 20);
  292. compression_mode = CompressionMode(decode_uint32(data + 0));
  293. format = Image::Format(decode_uint32(data + 4));
  294. uint32_t mipmap_count = decode_uint32(data + 8);
  295. size.width = decode_uint32(data + 12);
  296. size.height = decode_uint32(data + 16);
  297. mipmaps = mipmap_count > 1;
  298. data += 20;
  299. data_size -= 20;
  300. Ref<Image> image;
  301. switch (compression_mode) {
  302. case COMPRESSION_MODE_LOSSLESS:
  303. case COMPRESSION_MODE_LOSSY: {
  304. Vector<uint8_t> image_data;
  305. ERR_FAIL_COND(data_size < 4);
  306. for (uint32_t i = 0; i < mipmap_count; i++) {
  307. uint32_t mipsize = decode_uint32(data);
  308. data += 4;
  309. data_size -= 4;
  310. ERR_FAIL_COND(mipsize < data_size);
  311. Ref<Image> img = memnew(Image(data, data_size));
  312. ERR_FAIL_COND(img->is_empty());
  313. if (img->get_format() != format) { // May happen due to webp/png in the tiny mipmaps.
  314. img->convert(format);
  315. }
  316. image_data.append_array(img->get_data());
  317. data += mipsize;
  318. data_size -= mipsize;
  319. }
  320. image = Ref<Image>(memnew(Image(size.width, size.height, mipmap_count > 1, format, image_data)));
  321. } break;
  322. case COMPRESSION_MODE_BASIS_UNIVERSAL: {
  323. ERR_FAIL_COND(!Image::basis_universal_unpacker_ptr);
  324. image = Image::basis_universal_unpacker_ptr(data, data_size);
  325. } break;
  326. case COMPRESSION_MODE_S3TC:
  327. case COMPRESSION_MODE_ETC2:
  328. case COMPRESSION_MODE_BPTC: {
  329. image = Ref<Image>(memnew(Image(size.width, size.height, mipmap_count > 1, format, p_data.slice(20))));
  330. } break;
  331. }
  332. ERR_FAIL_COND(image.is_null());
  333. if (texture.is_null()) {
  334. texture = RenderingServer::get_singleton()->texture_2d_create(image);
  335. } else {
  336. RID new_texture = RenderingServer::get_singleton()->texture_2d_create(image);
  337. RenderingServer::get_singleton()->texture_replace(texture, new_texture);
  338. }
  339. image_stored = true;
  340. RenderingServer::get_singleton()->texture_set_size_override(texture, size_override.width, size_override.height);
  341. alpha_cache.unref();
  342. if (keep_all_compressed_buffers || keep_compressed_buffer) {
  343. compressed_buffer = p_data;
  344. } else {
  345. compressed_buffer.clear();
  346. }
  347. }
  348. PortableCompressedTexture2D::CompressionMode PortableCompressedTexture2D::get_compression_mode() const {
  349. return compression_mode;
  350. }
  351. Vector<uint8_t> PortableCompressedTexture2D::_get_data() const {
  352. return compressed_buffer;
  353. }
  354. void PortableCompressedTexture2D::create_from_image(const Ref<Image> &p_image, CompressionMode p_compression_mode, bool p_normal_map, float p_lossy_quality) {
  355. ERR_FAIL_COND(p_image.is_null() || p_image->is_empty());
  356. Vector<uint8_t> buffer;
  357. buffer.resize(20);
  358. encode_uint32(p_compression_mode, buffer.ptrw());
  359. encode_uint32(p_image->get_format(), buffer.ptrw() + 4);
  360. encode_uint32(p_image->get_mipmap_count() + 1, buffer.ptrw() + 8);
  361. encode_uint32(p_image->get_width(), buffer.ptrw() + 12);
  362. encode_uint32(p_image->get_height(), buffer.ptrw() + 16);
  363. switch (p_compression_mode) {
  364. case COMPRESSION_MODE_LOSSLESS:
  365. case COMPRESSION_MODE_LOSSY: {
  366. for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {
  367. Vector<uint8_t> data;
  368. if (p_compression_mode == COMPRESSION_MODE_LOSSY) {
  369. data = Image::webp_lossy_packer(p_image->get_image_from_mipmap(i), p_lossy_quality);
  370. } else {
  371. data = Image::webp_lossless_packer(p_image->get_image_from_mipmap(i));
  372. }
  373. int data_len = data.size();
  374. buffer.resize(buffer.size() + 4);
  375. encode_uint32(data_len, buffer.ptrw() + buffer.size() - 4);
  376. buffer.append_array(data);
  377. }
  378. } break;
  379. case COMPRESSION_MODE_BASIS_UNIVERSAL: {
  380. Image::UsedChannels uc = p_image->detect_used_channels(p_normal_map ? Image::COMPRESS_SOURCE_NORMAL : Image::COMPRESS_SOURCE_GENERIC);
  381. Vector<uint8_t> budata = Image::basis_universal_packer(p_image, uc);
  382. buffer.append_array(budata);
  383. } break;
  384. case COMPRESSION_MODE_S3TC:
  385. case COMPRESSION_MODE_ETC2:
  386. case COMPRESSION_MODE_BPTC: {
  387. Ref<Image> copy = p_image->duplicate();
  388. switch (p_compression_mode) {
  389. case COMPRESSION_MODE_S3TC:
  390. copy->compress(Image::COMPRESS_S3TC);
  391. break;
  392. case COMPRESSION_MODE_ETC2:
  393. copy->compress(Image::COMPRESS_ETC2);
  394. break;
  395. case COMPRESSION_MODE_BPTC:
  396. copy->compress(Image::COMPRESS_BPTC);
  397. break;
  398. default: {
  399. };
  400. }
  401. buffer.append_array(copy->get_data());
  402. } break;
  403. }
  404. _set_data(buffer);
  405. }
  406. Image::Format PortableCompressedTexture2D::get_format() const {
  407. return format;
  408. }
  409. Ref<Image> PortableCompressedTexture2D::get_image() const {
  410. if (image_stored) {
  411. return RenderingServer::get_singleton()->texture_2d_get(texture);
  412. } else {
  413. return Ref<Image>();
  414. }
  415. }
  416. int PortableCompressedTexture2D::get_width() const {
  417. return size.width;
  418. }
  419. int PortableCompressedTexture2D::get_height() const {
  420. return size.height;
  421. }
  422. RID PortableCompressedTexture2D::get_rid() const {
  423. if (texture.is_null()) {
  424. //we are in trouble, create something temporary
  425. texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  426. }
  427. return texture;
  428. }
  429. bool PortableCompressedTexture2D::has_alpha() const {
  430. return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8);
  431. }
  432. void PortableCompressedTexture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  433. if (size.width == 0 || size.height == 0) {
  434. return;
  435. }
  436. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), texture, false, p_modulate, p_transpose);
  437. }
  438. void PortableCompressedTexture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  439. if (size.width == 0 || size.height == 0) {
  440. return;
  441. }
  442. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose);
  443. }
  444. void PortableCompressedTexture2D::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
  445. if (size.width == 0 || size.height == 0) {
  446. return;
  447. }
  448. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, p_clip_uv);
  449. }
  450. bool PortableCompressedTexture2D::is_pixel_opaque(int p_x, int p_y) const {
  451. if (!alpha_cache.is_valid()) {
  452. Ref<Image> img = get_image();
  453. if (img.is_valid()) {
  454. if (img->is_compressed()) { //must decompress, if compressed
  455. Ref<Image> decom = img->duplicate();
  456. decom->decompress();
  457. img = decom;
  458. }
  459. alpha_cache.instantiate();
  460. alpha_cache->create_from_image_alpha(img);
  461. }
  462. }
  463. if (alpha_cache.is_valid()) {
  464. int aw = int(alpha_cache->get_size().width);
  465. int ah = int(alpha_cache->get_size().height);
  466. if (aw == 0 || ah == 0) {
  467. return true;
  468. }
  469. int x = p_x * aw / size.width;
  470. int y = p_y * ah / size.height;
  471. x = CLAMP(x, 0, aw);
  472. y = CLAMP(y, 0, ah);
  473. return alpha_cache->get_bit(x, y);
  474. }
  475. return true;
  476. }
  477. void PortableCompressedTexture2D::set_size_override(const Size2 &p_size) {
  478. size_override = p_size;
  479. RenderingServer::get_singleton()->texture_set_size_override(texture, size_override.width, size_override.height);
  480. }
  481. Size2 PortableCompressedTexture2D::get_size_override() const {
  482. return size_override;
  483. }
  484. void PortableCompressedTexture2D::set_path(const String &p_path, bool p_take_over) {
  485. if (texture.is_valid()) {
  486. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  487. }
  488. Resource::set_path(p_path, p_take_over);
  489. }
  490. bool PortableCompressedTexture2D::keep_all_compressed_buffers = false;
  491. void PortableCompressedTexture2D::set_keep_all_compressed_buffers(bool p_keep) {
  492. keep_all_compressed_buffers = p_keep;
  493. }
  494. bool PortableCompressedTexture2D::is_keeping_all_compressed_buffers() {
  495. return keep_all_compressed_buffers;
  496. }
  497. void PortableCompressedTexture2D::set_keep_compressed_buffer(bool p_keep) {
  498. keep_compressed_buffer = p_keep;
  499. if (!p_keep) {
  500. compressed_buffer.clear();
  501. }
  502. }
  503. bool PortableCompressedTexture2D::is_keeping_compressed_buffer() const {
  504. return keep_compressed_buffer;
  505. }
  506. void PortableCompressedTexture2D::_bind_methods() {
  507. ClassDB::bind_method(D_METHOD("create_from_image", "image", "compression_mode", "normal_map", "lossy_quality"), &PortableCompressedTexture2D::create_from_image, DEFVAL(false), DEFVAL(0.8));
  508. ClassDB::bind_method(D_METHOD("get_format"), &PortableCompressedTexture2D::get_format);
  509. ClassDB::bind_method(D_METHOD("get_compression_mode"), &PortableCompressedTexture2D::get_compression_mode);
  510. ClassDB::bind_method(D_METHOD("set_size_override", "size"), &PortableCompressedTexture2D::set_size_override);
  511. ClassDB::bind_method(D_METHOD("get_size_override"), &PortableCompressedTexture2D::get_size_override);
  512. ClassDB::bind_method(D_METHOD("set_keep_compressed_buffer", "keep"), &PortableCompressedTexture2D::set_keep_compressed_buffer);
  513. ClassDB::bind_method(D_METHOD("is_keeping_compressed_buffer"), &PortableCompressedTexture2D::is_keeping_compressed_buffer);
  514. ClassDB::bind_method(D_METHOD("_set_data", "data"), &PortableCompressedTexture2D::_set_data);
  515. ClassDB::bind_method(D_METHOD("_get_data"), &PortableCompressedTexture2D::_get_data);
  516. ClassDB::bind_static_method("PortableCompressedTexture2D", D_METHOD("set_keep_all_compressed_buffers", "keep"), &PortableCompressedTexture2D::set_keep_all_compressed_buffers);
  517. ClassDB::bind_static_method("PortableCompressedTexture2D", D_METHOD("is_keeping_all_compressed_buffers"), &PortableCompressedTexture2D::is_keeping_all_compressed_buffers);
  518. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "_set_data", "_get_data");
  519. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size_override", PROPERTY_HINT_NONE, "suffix:px"), "set_size_override", "get_size_override");
  520. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_compressed_buffer"), "set_keep_compressed_buffer", "is_keeping_compressed_buffer");
  521. BIND_ENUM_CONSTANT(COMPRESSION_MODE_LOSSLESS);
  522. BIND_ENUM_CONSTANT(COMPRESSION_MODE_LOSSY);
  523. BIND_ENUM_CONSTANT(COMPRESSION_MODE_BASIS_UNIVERSAL);
  524. BIND_ENUM_CONSTANT(COMPRESSION_MODE_S3TC);
  525. BIND_ENUM_CONSTANT(COMPRESSION_MODE_ETC2);
  526. BIND_ENUM_CONSTANT(COMPRESSION_MODE_BPTC);
  527. }
  528. PortableCompressedTexture2D::PortableCompressedTexture2D() {}
  529. PortableCompressedTexture2D::~PortableCompressedTexture2D() {
  530. if (texture.is_valid()) {
  531. ERR_FAIL_NULL(RenderingServer::get_singleton());
  532. RenderingServer::get_singleton()->free(texture);
  533. }
  534. }
  535. //////////////////////////////////////////
  536. Ref<Image> CompressedTexture2D::load_image_from_file(Ref<FileAccess> f, int p_size_limit) {
  537. uint32_t data_format = f->get_32();
  538. uint32_t w = f->get_16();
  539. uint32_t h = f->get_16();
  540. uint32_t mipmaps = f->get_32();
  541. Image::Format format = Image::Format(f->get_32());
  542. if (data_format == DATA_FORMAT_PNG || data_format == DATA_FORMAT_WEBP) {
  543. //look for a PNG or WebP file inside
  544. int sw = w;
  545. int sh = h;
  546. //mipmaps need to be read independently, they will be later combined
  547. Vector<Ref<Image>> mipmap_images;
  548. uint64_t total_size = 0;
  549. bool first = true;
  550. for (uint32_t i = 0; i < mipmaps + 1; i++) {
  551. uint32_t size = f->get_32();
  552. if (p_size_limit > 0 && i < (mipmaps - 1) && (sw > p_size_limit || sh > p_size_limit)) {
  553. //can't load this due to size limit
  554. sw = MAX(sw >> 1, 1);
  555. sh = MAX(sh >> 1, 1);
  556. f->seek(f->get_position() + size);
  557. continue;
  558. }
  559. Vector<uint8_t> pv;
  560. pv.resize(size);
  561. {
  562. uint8_t *wr = pv.ptrw();
  563. f->get_buffer(wr, size);
  564. }
  565. Ref<Image> img;
  566. if (data_format == DATA_FORMAT_PNG && Image::png_unpacker) {
  567. img = Image::png_unpacker(pv);
  568. } else if (data_format == DATA_FORMAT_WEBP && Image::webp_unpacker) {
  569. img = Image::webp_unpacker(pv);
  570. }
  571. if (img.is_null() || img->is_empty()) {
  572. ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
  573. }
  574. if (first) {
  575. //format will actually be the format of the first image,
  576. //as it may have changed on compression
  577. format = img->get_format();
  578. first = false;
  579. } else if (img->get_format() != format) {
  580. img->convert(format); //all needs to be the same format
  581. }
  582. total_size += img->get_data().size();
  583. mipmap_images.push_back(img);
  584. sw = MAX(sw >> 1, 1);
  585. sh = MAX(sh >> 1, 1);
  586. }
  587. //print_line("mipmap read total: " + itos(mipmap_images.size()));
  588. Ref<Image> image;
  589. image.instantiate();
  590. if (mipmap_images.size() == 1) {
  591. //only one image (which will most likely be the case anyway for this format)
  592. image = mipmap_images[0];
  593. return image;
  594. } else {
  595. //rarer use case, but needs to be supported
  596. Vector<uint8_t> img_data;
  597. img_data.resize(total_size);
  598. {
  599. uint8_t *wr = img_data.ptrw();
  600. int ofs = 0;
  601. for (int i = 0; i < mipmap_images.size(); i++) {
  602. Vector<uint8_t> id = mipmap_images[i]->get_data();
  603. int len = id.size();
  604. const uint8_t *r = id.ptr();
  605. memcpy(&wr[ofs], r, len);
  606. ofs += len;
  607. }
  608. }
  609. image->set_data(w, h, true, mipmap_images[0]->get_format(), img_data);
  610. return image;
  611. }
  612. } else if (data_format == DATA_FORMAT_BASIS_UNIVERSAL) {
  613. int sw = w;
  614. int sh = h;
  615. uint32_t size = f->get_32();
  616. if (p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {
  617. //can't load this due to size limit
  618. sw = MAX(sw >> 1, 1);
  619. sh = MAX(sh >> 1, 1);
  620. f->seek(f->get_position() + size);
  621. return Ref<Image>();
  622. }
  623. Vector<uint8_t> pv;
  624. pv.resize(size);
  625. {
  626. uint8_t *wr = pv.ptrw();
  627. f->get_buffer(wr, size);
  628. }
  629. Ref<Image> img;
  630. img = Image::basis_universal_unpacker(pv);
  631. if (img.is_null() || img->is_empty()) {
  632. ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
  633. }
  634. format = img->get_format();
  635. sw = MAX(sw >> 1, 1);
  636. sh = MAX(sh >> 1, 1);
  637. return img;
  638. } else if (data_format == DATA_FORMAT_IMAGE) {
  639. int size = Image::get_image_data_size(w, h, format, mipmaps ? true : false);
  640. for (uint32_t i = 0; i < mipmaps + 1; i++) {
  641. int tw, th;
  642. int ofs = Image::get_image_mipmap_offset_and_dimensions(w, h, format, i, tw, th);
  643. if (p_size_limit > 0 && i < mipmaps && (p_size_limit > tw || p_size_limit > th)) {
  644. if (ofs) {
  645. f->seek(f->get_position() + ofs);
  646. }
  647. continue; //oops, size limit enforced, go to next
  648. }
  649. Vector<uint8_t> data;
  650. data.resize(size - ofs);
  651. {
  652. uint8_t *wr = data.ptrw();
  653. f->get_buffer(wr, data.size());
  654. }
  655. Ref<Image> image = Image::create_from_data(tw, th, mipmaps - i ? true : false, format, data);
  656. return image;
  657. }
  658. }
  659. return Ref<Image>();
  660. }
  661. void CompressedTexture2D::set_path(const String &p_path, bool p_take_over) {
  662. if (texture.is_valid()) {
  663. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  664. }
  665. Resource::set_path(p_path, p_take_over);
  666. }
  667. void CompressedTexture2D::_requested_3d(void *p_ud) {
  668. CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
  669. Ref<CompressedTexture2D> ctex(ct);
  670. ERR_FAIL_COND(!request_3d_callback);
  671. request_3d_callback(ctex);
  672. }
  673. void CompressedTexture2D::_requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel) {
  674. CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
  675. Ref<CompressedTexture2D> ctex(ct);
  676. ERR_FAIL_COND(!request_roughness_callback);
  677. request_roughness_callback(ctex, p_normal_path, p_roughness_channel);
  678. }
  679. void CompressedTexture2D::_requested_normal(void *p_ud) {
  680. CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
  681. Ref<CompressedTexture2D> ctex(ct);
  682. ERR_FAIL_COND(!request_normal_callback);
  683. request_normal_callback(ctex);
  684. }
  685. CompressedTexture2D::TextureFormatRequestCallback CompressedTexture2D::request_3d_callback = nullptr;
  686. CompressedTexture2D::TextureFormatRoughnessRequestCallback CompressedTexture2D::request_roughness_callback = nullptr;
  687. CompressedTexture2D::TextureFormatRequestCallback CompressedTexture2D::request_normal_callback = nullptr;
  688. Image::Format CompressedTexture2D::get_format() const {
  689. return format;
  690. }
  691. Error CompressedTexture2D::_load_data(const String &p_path, int &r_width, int &r_height, Ref<Image> &image, bool &r_request_3d, bool &r_request_normal, bool &r_request_roughness, int &mipmap_limit, int p_size_limit) {
  692. alpha_cache.unref();
  693. ERR_FAIL_COND_V(image.is_null(), ERR_INVALID_PARAMETER);
  694. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  695. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  696. uint8_t header[4];
  697. f->get_buffer(header, 4);
  698. if (header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != '2') {
  699. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is corrupt (Bad header).");
  700. }
  701. uint32_t version = f->get_32();
  702. if (version > FORMAT_VERSION) {
  703. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  704. }
  705. r_width = f->get_32();
  706. r_height = f->get_32();
  707. uint32_t df = f->get_32(); //data format
  708. //skip reserved
  709. mipmap_limit = int(f->get_32());
  710. //reserved
  711. f->get_32();
  712. f->get_32();
  713. f->get_32();
  714. #ifdef TOOLS_ENABLED
  715. r_request_3d = request_3d_callback && df & FORMAT_BIT_DETECT_3D;
  716. r_request_roughness = request_roughness_callback && df & FORMAT_BIT_DETECT_ROUGNESS;
  717. r_request_normal = request_normal_callback && df & FORMAT_BIT_DETECT_NORMAL;
  718. #else
  719. r_request_3d = false;
  720. r_request_roughness = false;
  721. r_request_normal = false;
  722. #endif
  723. if (!(df & FORMAT_BIT_STREAM)) {
  724. p_size_limit = 0;
  725. }
  726. image = load_image_from_file(f, p_size_limit);
  727. if (image.is_null() || image->is_empty()) {
  728. return ERR_CANT_OPEN;
  729. }
  730. return OK;
  731. }
  732. Error CompressedTexture2D::load(const String &p_path) {
  733. int lw, lh;
  734. Ref<Image> image;
  735. image.instantiate();
  736. bool request_3d;
  737. bool request_normal;
  738. bool request_roughness;
  739. int mipmap_limit;
  740. Error err = _load_data(p_path, lw, lh, image, request_3d, request_normal, request_roughness, mipmap_limit);
  741. if (err) {
  742. return err;
  743. }
  744. if (texture.is_valid()) {
  745. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  746. RS::get_singleton()->texture_replace(texture, new_texture);
  747. } else {
  748. texture = RS::get_singleton()->texture_2d_create(image);
  749. }
  750. if (lw || lh) {
  751. RS::get_singleton()->texture_set_size_override(texture, lw, lh);
  752. }
  753. w = lw;
  754. h = lh;
  755. path_to_file = p_path;
  756. format = image->get_format();
  757. if (get_path().is_empty()) {
  758. //temporarily set path if no path set for resource, helps find errors
  759. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  760. }
  761. #ifdef TOOLS_ENABLED
  762. if (request_3d) {
  763. //print_line("request detect 3D at " + p_path);
  764. RS::get_singleton()->texture_set_detect_3d_callback(texture, _requested_3d, this);
  765. } else {
  766. //print_line("not requesting detect 3D at " + p_path);
  767. RS::get_singleton()->texture_set_detect_3d_callback(texture, nullptr, nullptr);
  768. }
  769. if (request_roughness) {
  770. //print_line("request detect srgb at " + p_path);
  771. RS::get_singleton()->texture_set_detect_roughness_callback(texture, _requested_roughness, this);
  772. } else {
  773. //print_line("not requesting detect srgb at " + p_path);
  774. RS::get_singleton()->texture_set_detect_roughness_callback(texture, nullptr, nullptr);
  775. }
  776. if (request_normal) {
  777. //print_line("request detect srgb at " + p_path);
  778. RS::get_singleton()->texture_set_detect_normal_callback(texture, _requested_normal, this);
  779. } else {
  780. //print_line("not requesting detect normal at " + p_path);
  781. RS::get_singleton()->texture_set_detect_normal_callback(texture, nullptr, nullptr);
  782. }
  783. #endif
  784. notify_property_list_changed();
  785. emit_changed();
  786. return OK;
  787. }
  788. String CompressedTexture2D::get_load_path() const {
  789. return path_to_file;
  790. }
  791. int CompressedTexture2D::get_width() const {
  792. return w;
  793. }
  794. int CompressedTexture2D::get_height() const {
  795. return h;
  796. }
  797. RID CompressedTexture2D::get_rid() const {
  798. if (!texture.is_valid()) {
  799. texture = RS::get_singleton()->texture_2d_placeholder_create();
  800. }
  801. return texture;
  802. }
  803. void CompressedTexture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  804. if ((w | h) == 0) {
  805. return;
  806. }
  807. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, Size2(w, h)), texture, false, p_modulate, p_transpose);
  808. }
  809. void CompressedTexture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  810. if ((w | h) == 0) {
  811. return;
  812. }
  813. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose);
  814. }
  815. void CompressedTexture2D::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
  816. if ((w | h) == 0) {
  817. return;
  818. }
  819. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, p_clip_uv);
  820. }
  821. bool CompressedTexture2D::has_alpha() const {
  822. return false;
  823. }
  824. Ref<Image> CompressedTexture2D::get_image() const {
  825. if (texture.is_valid()) {
  826. return RS::get_singleton()->texture_2d_get(texture);
  827. } else {
  828. return Ref<Image>();
  829. }
  830. }
  831. bool CompressedTexture2D::is_pixel_opaque(int p_x, int p_y) const {
  832. if (!alpha_cache.is_valid()) {
  833. Ref<Image> img = get_image();
  834. if (img.is_valid()) {
  835. if (img->is_compressed()) { //must decompress, if compressed
  836. Ref<Image> decom = img->duplicate();
  837. decom->decompress();
  838. img = decom;
  839. }
  840. alpha_cache.instantiate();
  841. alpha_cache->create_from_image_alpha(img);
  842. }
  843. }
  844. if (alpha_cache.is_valid()) {
  845. int aw = int(alpha_cache->get_size().width);
  846. int ah = int(alpha_cache->get_size().height);
  847. if (aw == 0 || ah == 0) {
  848. return true;
  849. }
  850. int x = p_x * aw / w;
  851. int y = p_y * ah / h;
  852. x = CLAMP(x, 0, aw);
  853. y = CLAMP(y, 0, ah);
  854. return alpha_cache->get_bit(x, y);
  855. }
  856. return true;
  857. }
  858. void CompressedTexture2D::reload_from_file() {
  859. String path = get_path();
  860. if (!path.is_resource_file()) {
  861. return;
  862. }
  863. path = ResourceLoader::path_remap(path); //remap for translation
  864. path = ResourceLoader::import_remap(path); //remap for import
  865. if (!path.is_resource_file()) {
  866. return;
  867. }
  868. load(path);
  869. }
  870. void CompressedTexture2D::_validate_property(PropertyInfo &p_property) const {
  871. }
  872. void CompressedTexture2D::_bind_methods() {
  873. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture2D::load);
  874. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture2D::get_load_path);
  875. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  876. }
  877. CompressedTexture2D::CompressedTexture2D() {}
  878. CompressedTexture2D::~CompressedTexture2D() {
  879. if (texture.is_valid()) {
  880. ERR_FAIL_NULL(RenderingServer::get_singleton());
  881. RS::get_singleton()->free(texture);
  882. }
  883. }
  884. Ref<Resource> ResourceFormatLoaderCompressedTexture2D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  885. Ref<CompressedTexture2D> st;
  886. st.instantiate();
  887. Error err = st->load(p_path);
  888. if (r_error) {
  889. *r_error = err;
  890. }
  891. if (err != OK) {
  892. return Ref<Resource>();
  893. }
  894. return st;
  895. }
  896. void ResourceFormatLoaderCompressedTexture2D::get_recognized_extensions(List<String> *p_extensions) const {
  897. p_extensions->push_back("ctex");
  898. }
  899. bool ResourceFormatLoaderCompressedTexture2D::handles_type(const String &p_type) const {
  900. return p_type == "CompressedTexture2D";
  901. }
  902. String ResourceFormatLoaderCompressedTexture2D::get_resource_type(const String &p_path) const {
  903. if (p_path.get_extension().to_lower() == "ctex") {
  904. return "CompressedTexture2D";
  905. }
  906. return "";
  907. }
  908. ////////////////////////////////////
  909. TypedArray<Image> Texture3D::_get_datai() const {
  910. Vector<Ref<Image>> data = get_data();
  911. TypedArray<Image> ret;
  912. ret.resize(data.size());
  913. for (int i = 0; i < data.size(); i++) {
  914. ret[i] = data[i];
  915. }
  916. return ret;
  917. }
  918. Image::Format Texture3D::get_format() const {
  919. Image::Format ret = Image::FORMAT_MAX;
  920. GDVIRTUAL_REQUIRED_CALL(_get_format, ret);
  921. return ret;
  922. }
  923. int Texture3D::get_width() const {
  924. int ret = 0;
  925. GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
  926. return ret;
  927. }
  928. int Texture3D::get_height() const {
  929. int ret = 0;
  930. GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
  931. return ret;
  932. }
  933. int Texture3D::get_depth() const {
  934. int ret = 0;
  935. GDVIRTUAL_REQUIRED_CALL(_get_depth, ret);
  936. return ret;
  937. }
  938. bool Texture3D::has_mipmaps() const {
  939. bool ret = false;
  940. GDVIRTUAL_REQUIRED_CALL(_has_mipmaps, ret);
  941. return ret;
  942. }
  943. Vector<Ref<Image>> Texture3D::get_data() const {
  944. TypedArray<Image> ret;
  945. GDVIRTUAL_REQUIRED_CALL(_get_data, ret);
  946. Vector<Ref<Image>> data;
  947. data.resize(ret.size());
  948. for (int i = 0; i < data.size(); i++) {
  949. data.write[i] = ret[i];
  950. }
  951. return data;
  952. }
  953. void Texture3D::_bind_methods() {
  954. ClassDB::bind_method(D_METHOD("get_format"), &Texture3D::get_format);
  955. ClassDB::bind_method(D_METHOD("get_width"), &Texture3D::get_width);
  956. ClassDB::bind_method(D_METHOD("get_height"), &Texture3D::get_height);
  957. ClassDB::bind_method(D_METHOD("get_depth"), &Texture3D::get_depth);
  958. ClassDB::bind_method(D_METHOD("has_mipmaps"), &Texture3D::has_mipmaps);
  959. ClassDB::bind_method(D_METHOD("get_data"), &Texture3D::_get_datai);
  960. ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture3D::create_placeholder);
  961. GDVIRTUAL_BIND(_get_format);
  962. GDVIRTUAL_BIND(_get_width);
  963. GDVIRTUAL_BIND(_get_height);
  964. GDVIRTUAL_BIND(_get_depth);
  965. GDVIRTUAL_BIND(_has_mipmaps);
  966. GDVIRTUAL_BIND(_get_data);
  967. }
  968. Ref<Resource> Texture3D::create_placeholder() const {
  969. Ref<PlaceholderTexture3D> placeholder;
  970. placeholder.instantiate();
  971. placeholder->set_size(Vector3i(get_width(), get_height(), get_depth()));
  972. return placeholder;
  973. }
  974. //////////////////////////////////////////
  975. Image::Format ImageTexture3D::get_format() const {
  976. return format;
  977. }
  978. int ImageTexture3D::get_width() const {
  979. return width;
  980. }
  981. int ImageTexture3D::get_height() const {
  982. return height;
  983. }
  984. int ImageTexture3D::get_depth() const {
  985. return depth;
  986. }
  987. bool ImageTexture3D::has_mipmaps() const {
  988. return mipmaps;
  989. }
  990. Error ImageTexture3D::_create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const TypedArray<Image> &p_data) {
  991. Vector<Ref<Image>> images;
  992. images.resize(p_data.size());
  993. for (int i = 0; i < images.size(); i++) {
  994. images.write[i] = p_data[i];
  995. }
  996. return create(p_format, p_width, p_height, p_depth, p_mipmaps, images);
  997. }
  998. void ImageTexture3D::_update(const TypedArray<Image> &p_data) {
  999. Vector<Ref<Image>> images;
  1000. images.resize(p_data.size());
  1001. for (int i = 0; i < images.size(); i++) {
  1002. images.write[i] = p_data[i];
  1003. }
  1004. return update(images);
  1005. }
  1006. Error ImageTexture3D::create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) {
  1007. RID tex = RenderingServer::get_singleton()->texture_3d_create(p_format, p_width, p_height, p_depth, p_mipmaps, p_data);
  1008. ERR_FAIL_COND_V(tex.is_null(), ERR_CANT_CREATE);
  1009. if (texture.is_valid()) {
  1010. RenderingServer::get_singleton()->texture_replace(texture, tex);
  1011. } else {
  1012. texture = tex;
  1013. }
  1014. format = p_format;
  1015. width = p_width;
  1016. height = p_height;
  1017. depth = p_depth;
  1018. mipmaps = p_mipmaps;
  1019. return OK;
  1020. }
  1021. void ImageTexture3D::update(const Vector<Ref<Image>> &p_data) {
  1022. ERR_FAIL_COND(!texture.is_valid());
  1023. RenderingServer::get_singleton()->texture_3d_update(texture, p_data);
  1024. }
  1025. Vector<Ref<Image>> ImageTexture3D::get_data() const {
  1026. ERR_FAIL_COND_V(!texture.is_valid(), Vector<Ref<Image>>());
  1027. return RS::get_singleton()->texture_3d_get(texture);
  1028. }
  1029. RID ImageTexture3D::get_rid() const {
  1030. if (!texture.is_valid()) {
  1031. texture = RS::get_singleton()->texture_3d_placeholder_create();
  1032. }
  1033. return texture;
  1034. }
  1035. void ImageTexture3D::set_path(const String &p_path, bool p_take_over) {
  1036. if (texture.is_valid()) {
  1037. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  1038. }
  1039. Resource::set_path(p_path, p_take_over);
  1040. }
  1041. void ImageTexture3D::_bind_methods() {
  1042. ClassDB::bind_method(D_METHOD("create", "format", "width", "height", "depth", "use_mipmaps", "data"), &ImageTexture3D::_create);
  1043. ClassDB::bind_method(D_METHOD("update", "data"), &ImageTexture3D::_update);
  1044. }
  1045. ImageTexture3D::ImageTexture3D() {
  1046. }
  1047. ImageTexture3D::~ImageTexture3D() {
  1048. if (texture.is_valid()) {
  1049. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1050. RS::get_singleton()->free(texture);
  1051. }
  1052. }
  1053. ////////////////////////////////////////////
  1054. void CompressedTexture3D::set_path(const String &p_path, bool p_take_over) {
  1055. if (texture.is_valid()) {
  1056. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  1057. }
  1058. Resource::set_path(p_path, p_take_over);
  1059. }
  1060. Image::Format CompressedTexture3D::get_format() const {
  1061. return format;
  1062. }
  1063. Error CompressedTexture3D::_load_data(const String &p_path, Vector<Ref<Image>> &r_data, Image::Format &r_format, int &r_width, int &r_height, int &r_depth, bool &r_mipmaps) {
  1064. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1065. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  1066. uint8_t header[4];
  1067. f->get_buffer(header, 4);
  1068. ERR_FAIL_COND_V(header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L', ERR_FILE_UNRECOGNIZED);
  1069. //stored as compressed textures (used for lossless and lossy compression)
  1070. uint32_t version = f->get_32();
  1071. if (version > FORMAT_VERSION) {
  1072. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  1073. }
  1074. r_depth = f->get_32(); //depth
  1075. f->get_32(); //ignored (mode)
  1076. f->get_32(); // ignored (data format)
  1077. f->get_32(); //ignored
  1078. int mipmap_count = f->get_32();
  1079. f->get_32(); //ignored
  1080. f->get_32(); //ignored
  1081. r_mipmaps = mipmap_count != 0;
  1082. r_data.clear();
  1083. for (int i = 0; i < (r_depth + mipmap_count); i++) {
  1084. Ref<Image> image = CompressedTexture2D::load_image_from_file(f, 0);
  1085. ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
  1086. if (i == 0) {
  1087. r_format = image->get_format();
  1088. r_width = image->get_width();
  1089. r_height = image->get_height();
  1090. }
  1091. r_data.push_back(image);
  1092. }
  1093. return OK;
  1094. }
  1095. Error CompressedTexture3D::load(const String &p_path) {
  1096. Vector<Ref<Image>> data;
  1097. int tw, th, td;
  1098. Image::Format tfmt;
  1099. bool tmm;
  1100. Error err = _load_data(p_path, data, tfmt, tw, th, td, tmm);
  1101. if (err) {
  1102. return err;
  1103. }
  1104. if (texture.is_valid()) {
  1105. RID new_texture = RS::get_singleton()->texture_3d_create(tfmt, tw, th, td, tmm, data);
  1106. RS::get_singleton()->texture_replace(texture, new_texture);
  1107. } else {
  1108. texture = RS::get_singleton()->texture_3d_create(tfmt, tw, th, td, tmm, data);
  1109. }
  1110. w = tw;
  1111. h = th;
  1112. d = td;
  1113. mipmaps = tmm;
  1114. format = tfmt;
  1115. path_to_file = p_path;
  1116. if (get_path().is_empty()) {
  1117. //temporarily set path if no path set for resource, helps find errors
  1118. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  1119. }
  1120. notify_property_list_changed();
  1121. emit_changed();
  1122. return OK;
  1123. }
  1124. String CompressedTexture3D::get_load_path() const {
  1125. return path_to_file;
  1126. }
  1127. int CompressedTexture3D::get_width() const {
  1128. return w;
  1129. }
  1130. int CompressedTexture3D::get_height() const {
  1131. return h;
  1132. }
  1133. int CompressedTexture3D::get_depth() const {
  1134. return d;
  1135. }
  1136. bool CompressedTexture3D::has_mipmaps() const {
  1137. return mipmaps;
  1138. }
  1139. RID CompressedTexture3D::get_rid() const {
  1140. if (!texture.is_valid()) {
  1141. texture = RS::get_singleton()->texture_3d_placeholder_create();
  1142. }
  1143. return texture;
  1144. }
  1145. Vector<Ref<Image>> CompressedTexture3D::get_data() const {
  1146. if (texture.is_valid()) {
  1147. return RS::get_singleton()->texture_3d_get(texture);
  1148. } else {
  1149. return Vector<Ref<Image>>();
  1150. }
  1151. }
  1152. void CompressedTexture3D::reload_from_file() {
  1153. String path = get_path();
  1154. if (!path.is_resource_file()) {
  1155. return;
  1156. }
  1157. path = ResourceLoader::path_remap(path); //remap for translation
  1158. path = ResourceLoader::import_remap(path); //remap for import
  1159. if (!path.is_resource_file()) {
  1160. return;
  1161. }
  1162. load(path);
  1163. }
  1164. void CompressedTexture3D::_validate_property(PropertyInfo &p_property) const {
  1165. }
  1166. void CompressedTexture3D::_bind_methods() {
  1167. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture3D::load);
  1168. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture3D::get_load_path);
  1169. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  1170. }
  1171. CompressedTexture3D::CompressedTexture3D() {}
  1172. CompressedTexture3D::~CompressedTexture3D() {
  1173. if (texture.is_valid()) {
  1174. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1175. RS::get_singleton()->free(texture);
  1176. }
  1177. }
  1178. /////////////////////////////
  1179. Ref<Resource> ResourceFormatLoaderCompressedTexture3D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  1180. Ref<CompressedTexture3D> st;
  1181. st.instantiate();
  1182. Error err = st->load(p_path);
  1183. if (r_error) {
  1184. *r_error = err;
  1185. }
  1186. if (err != OK) {
  1187. return Ref<Resource>();
  1188. }
  1189. return st;
  1190. }
  1191. void ResourceFormatLoaderCompressedTexture3D::get_recognized_extensions(List<String> *p_extensions) const {
  1192. p_extensions->push_back("ctex3d");
  1193. }
  1194. bool ResourceFormatLoaderCompressedTexture3D::handles_type(const String &p_type) const {
  1195. return p_type == "CompressedTexture3D";
  1196. }
  1197. String ResourceFormatLoaderCompressedTexture3D::get_resource_type(const String &p_path) const {
  1198. if (p_path.get_extension().to_lower() == "ctex3d") {
  1199. return "CompressedTexture3D";
  1200. }
  1201. return "";
  1202. }
  1203. ////////////////////////////////////////////
  1204. int AtlasTexture::get_width() const {
  1205. if (region.size.width == 0) {
  1206. if (atlas.is_valid()) {
  1207. return atlas->get_width();
  1208. }
  1209. return 1;
  1210. } else {
  1211. return region.size.width + margin.size.width;
  1212. }
  1213. }
  1214. int AtlasTexture::get_height() const {
  1215. if (region.size.height == 0) {
  1216. if (atlas.is_valid()) {
  1217. return atlas->get_height();
  1218. }
  1219. return 1;
  1220. } else {
  1221. return region.size.height + margin.size.height;
  1222. }
  1223. }
  1224. RID AtlasTexture::get_rid() const {
  1225. if (atlas.is_valid()) {
  1226. return atlas->get_rid();
  1227. }
  1228. return RID();
  1229. }
  1230. bool AtlasTexture::has_alpha() const {
  1231. if (atlas.is_valid()) {
  1232. return atlas->has_alpha();
  1233. }
  1234. return false;
  1235. }
  1236. void AtlasTexture::set_atlas(const Ref<Texture2D> &p_atlas) {
  1237. ERR_FAIL_COND(p_atlas == this);
  1238. if (atlas == p_atlas) {
  1239. return;
  1240. }
  1241. // Support recursive AtlasTextures.
  1242. if (Ref<AtlasTexture>(atlas).is_valid()) {
  1243. atlas->disconnect(CoreStringNames::get_singleton()->changed, callable_mp((Resource *)this, &AtlasTexture::emit_changed));
  1244. }
  1245. atlas = p_atlas;
  1246. if (Ref<AtlasTexture>(atlas).is_valid()) {
  1247. atlas->connect(CoreStringNames::get_singleton()->changed, callable_mp((Resource *)this, &AtlasTexture::emit_changed));
  1248. }
  1249. emit_changed();
  1250. }
  1251. Ref<Texture2D> AtlasTexture::get_atlas() const {
  1252. return atlas;
  1253. }
  1254. void AtlasTexture::set_region(const Rect2 &p_region) {
  1255. if (region == p_region) {
  1256. return;
  1257. }
  1258. region = p_region;
  1259. emit_changed();
  1260. }
  1261. Rect2 AtlasTexture::get_region() const {
  1262. return region;
  1263. }
  1264. void AtlasTexture::set_margin(const Rect2 &p_margin) {
  1265. if (margin == p_margin) {
  1266. return;
  1267. }
  1268. margin = p_margin;
  1269. emit_changed();
  1270. }
  1271. Rect2 AtlasTexture::get_margin() const {
  1272. return margin;
  1273. }
  1274. void AtlasTexture::set_filter_clip(const bool p_enable) {
  1275. filter_clip = p_enable;
  1276. emit_changed();
  1277. }
  1278. bool AtlasTexture::has_filter_clip() const {
  1279. return filter_clip;
  1280. }
  1281. void AtlasTexture::_bind_methods() {
  1282. ClassDB::bind_method(D_METHOD("set_atlas", "atlas"), &AtlasTexture::set_atlas);
  1283. ClassDB::bind_method(D_METHOD("get_atlas"), &AtlasTexture::get_atlas);
  1284. ClassDB::bind_method(D_METHOD("set_region", "region"), &AtlasTexture::set_region);
  1285. ClassDB::bind_method(D_METHOD("get_region"), &AtlasTexture::get_region);
  1286. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &AtlasTexture::set_margin);
  1287. ClassDB::bind_method(D_METHOD("get_margin"), &AtlasTexture::get_margin);
  1288. ClassDB::bind_method(D_METHOD("set_filter_clip", "enable"), &AtlasTexture::set_filter_clip);
  1289. ClassDB::bind_method(D_METHOD("has_filter_clip"), &AtlasTexture::has_filter_clip);
  1290. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "atlas", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_atlas", "get_atlas");
  1291. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region", PROPERTY_HINT_NONE, "suffix:px"), "set_region", "get_region");
  1292. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "margin", PROPERTY_HINT_NONE, "suffix:px"), "set_margin", "get_margin");
  1293. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_clip"), "set_filter_clip", "has_filter_clip");
  1294. }
  1295. void AtlasTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  1296. if (!atlas.is_valid()) {
  1297. return;
  1298. }
  1299. Rect2 rc = region;
  1300. if (rc.size.width == 0) {
  1301. rc.size.width = atlas->get_width();
  1302. }
  1303. if (rc.size.height == 0) {
  1304. rc.size.height = atlas->get_height();
  1305. }
  1306. atlas->draw_rect_region(p_canvas_item, Rect2(p_pos + margin.position, rc.size), rc, p_modulate, p_transpose, filter_clip);
  1307. }
  1308. void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  1309. if (!atlas.is_valid()) {
  1310. return;
  1311. }
  1312. Rect2 rc = region;
  1313. if (rc.size.width == 0) {
  1314. rc.size.width = atlas->get_width();
  1315. }
  1316. if (rc.size.height == 0) {
  1317. rc.size.height = atlas->get_height();
  1318. }
  1319. Vector2 scale = p_rect.size / (region.size + margin.size);
  1320. Rect2 dr(p_rect.position + margin.position * scale, rc.size * scale);
  1321. atlas->draw_rect_region(p_canvas_item, dr, rc, p_modulate, p_transpose, filter_clip);
  1322. }
  1323. 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, bool p_clip_uv) const {
  1324. //this might not necessarily work well if using a rect, needs to be fixed properly
  1325. if (!atlas.is_valid()) {
  1326. return;
  1327. }
  1328. Rect2 dr;
  1329. Rect2 src_c;
  1330. get_rect_region(p_rect, p_src_rect, dr, src_c);
  1331. atlas->draw_rect_region(p_canvas_item, dr, src_c, p_modulate, p_transpose, filter_clip);
  1332. }
  1333. bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  1334. if (!atlas.is_valid()) {
  1335. return false;
  1336. }
  1337. Rect2 src = p_src_rect;
  1338. if (src.size == Size2()) {
  1339. src.size = region.size;
  1340. }
  1341. Vector2 scale = p_rect.size / src.size;
  1342. src.position += (region.position - margin.position);
  1343. Rect2 src_clipped = region.intersection(src);
  1344. if (src_clipped.size == Size2()) {
  1345. return false;
  1346. }
  1347. Vector2 ofs = (src_clipped.position - src.position);
  1348. if (scale.x < 0) {
  1349. ofs.x += (src_clipped.size.x - src.size.x);
  1350. }
  1351. if (scale.y < 0) {
  1352. ofs.y += (src_clipped.size.y - src.size.y);
  1353. }
  1354. r_rect = Rect2(p_rect.position + ofs * scale, src_clipped.size * scale);
  1355. r_src_rect = src_clipped;
  1356. return true;
  1357. }
  1358. bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
  1359. if (!atlas.is_valid()) {
  1360. return true;
  1361. }
  1362. int x = p_x + region.position.x - margin.position.x;
  1363. int y = p_y + region.position.y - margin.position.y;
  1364. // margin edge may outside of atlas
  1365. if (x < 0 || x >= atlas->get_width()) {
  1366. return false;
  1367. }
  1368. if (y < 0 || y >= atlas->get_height()) {
  1369. return false;
  1370. }
  1371. return atlas->is_pixel_opaque(x, y);
  1372. }
  1373. Ref<Image> AtlasTexture::get_image() const {
  1374. if (!atlas.is_valid() || !atlas->get_image().is_valid()) {
  1375. return Ref<Image>();
  1376. }
  1377. return atlas->get_image()->get_region(region);
  1378. }
  1379. AtlasTexture::AtlasTexture() {}
  1380. /////////////////////////////////////////
  1381. int MeshTexture::get_width() const {
  1382. return size.width;
  1383. }
  1384. int MeshTexture::get_height() const {
  1385. return size.height;
  1386. }
  1387. RID MeshTexture::get_rid() const {
  1388. return RID();
  1389. }
  1390. bool MeshTexture::has_alpha() const {
  1391. return false;
  1392. }
  1393. void MeshTexture::set_mesh(const Ref<Mesh> &p_mesh) {
  1394. mesh = p_mesh;
  1395. }
  1396. Ref<Mesh> MeshTexture::get_mesh() const {
  1397. return mesh;
  1398. }
  1399. void MeshTexture::set_image_size(const Size2 &p_size) {
  1400. size = p_size;
  1401. }
  1402. Size2 MeshTexture::get_image_size() const {
  1403. return size;
  1404. }
  1405. void MeshTexture::set_base_texture(const Ref<Texture2D> &p_texture) {
  1406. base_texture = p_texture;
  1407. }
  1408. Ref<Texture2D> MeshTexture::get_base_texture() const {
  1409. return base_texture;
  1410. }
  1411. void MeshTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  1412. if (mesh.is_null() || base_texture.is_null()) {
  1413. return;
  1414. }
  1415. Transform2D xform;
  1416. xform.set_origin(p_pos);
  1417. if (p_transpose) {
  1418. SWAP(xform.columns[0][1], xform.columns[1][0]);
  1419. SWAP(xform.columns[0][0], xform.columns[1][1]);
  1420. }
  1421. RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
  1422. }
  1423. void MeshTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  1424. if (mesh.is_null() || base_texture.is_null()) {
  1425. return;
  1426. }
  1427. Transform2D xform;
  1428. Vector2 origin = p_rect.position;
  1429. if (p_rect.size.x < 0) {
  1430. origin.x += size.x;
  1431. }
  1432. if (p_rect.size.y < 0) {
  1433. origin.y += size.y;
  1434. }
  1435. xform.set_origin(origin);
  1436. xform.set_scale(p_rect.size / size);
  1437. if (p_transpose) {
  1438. SWAP(xform.columns[0][1], xform.columns[1][0]);
  1439. SWAP(xform.columns[0][0], xform.columns[1][1]);
  1440. }
  1441. RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
  1442. }
  1443. 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, bool p_clip_uv) const {
  1444. if (mesh.is_null() || base_texture.is_null()) {
  1445. return;
  1446. }
  1447. Transform2D xform;
  1448. Vector2 origin = p_rect.position;
  1449. if (p_rect.size.x < 0) {
  1450. origin.x += size.x;
  1451. }
  1452. if (p_rect.size.y < 0) {
  1453. origin.y += size.y;
  1454. }
  1455. xform.set_origin(origin);
  1456. xform.set_scale(p_rect.size / size);
  1457. if (p_transpose) {
  1458. SWAP(xform.columns[0][1], xform.columns[1][0]);
  1459. SWAP(xform.columns[0][0], xform.columns[1][1]);
  1460. }
  1461. RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
  1462. }
  1463. bool MeshTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  1464. r_rect = p_rect;
  1465. r_src_rect = p_src_rect;
  1466. return true;
  1467. }
  1468. bool MeshTexture::is_pixel_opaque(int p_x, int p_y) const {
  1469. return true;
  1470. }
  1471. void MeshTexture::_bind_methods() {
  1472. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshTexture::set_mesh);
  1473. ClassDB::bind_method(D_METHOD("get_mesh"), &MeshTexture::get_mesh);
  1474. ClassDB::bind_method(D_METHOD("set_image_size", "size"), &MeshTexture::set_image_size);
  1475. ClassDB::bind_method(D_METHOD("get_image_size"), &MeshTexture::get_image_size);
  1476. ClassDB::bind_method(D_METHOD("set_base_texture", "texture"), &MeshTexture::set_base_texture);
  1477. ClassDB::bind_method(D_METHOD("get_base_texture"), &MeshTexture::get_base_texture);
  1478. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  1479. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "base_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_base_texture", "get_base_texture");
  1480. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "image_size", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_image_size", "get_image_size");
  1481. }
  1482. MeshTexture::MeshTexture() {
  1483. }
  1484. //////////////////////////////////////////
  1485. void CurveTexture::_bind_methods() {
  1486. ClassDB::bind_method(D_METHOD("set_width", "width"), &CurveTexture::set_width);
  1487. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &CurveTexture::set_curve);
  1488. ClassDB::bind_method(D_METHOD("get_curve"), &CurveTexture::get_curve);
  1489. ClassDB::bind_method(D_METHOD("set_texture_mode", "texture_mode"), &CurveTexture::set_texture_mode);
  1490. ClassDB::bind_method(D_METHOD("get_texture_mode"), &CurveTexture::get_texture_mode);
  1491. ClassDB::bind_method(D_METHOD("_update"), &CurveTexture::_update);
  1492. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,4096,suffix:px"), "set_width", "get_width");
  1493. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_mode", PROPERTY_HINT_ENUM, "RGB,Red"), "set_texture_mode", "get_texture_mode");
  1494. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  1495. BIND_ENUM_CONSTANT(TEXTURE_MODE_RGB);
  1496. BIND_ENUM_CONSTANT(TEXTURE_MODE_RED);
  1497. }
  1498. void CurveTexture::set_width(int p_width) {
  1499. ERR_FAIL_COND(p_width < 32 || p_width > 4096);
  1500. if (_width == p_width) {
  1501. return;
  1502. }
  1503. _width = p_width;
  1504. _update();
  1505. }
  1506. int CurveTexture::get_width() const {
  1507. return _width;
  1508. }
  1509. void CurveTexture::ensure_default_setup(float p_min, float p_max) {
  1510. if (_curve.is_null()) {
  1511. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  1512. curve->add_point(Vector2(0, 1));
  1513. curve->add_point(Vector2(1, 1));
  1514. curve->set_min_value(p_min);
  1515. curve->set_max_value(p_max);
  1516. set_curve(curve);
  1517. // Min and max is 0..1 by default
  1518. }
  1519. }
  1520. void CurveTexture::set_curve(Ref<Curve> p_curve) {
  1521. if (_curve != p_curve) {
  1522. if (_curve.is_valid()) {
  1523. _curve->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveTexture::_update));
  1524. }
  1525. _curve = p_curve;
  1526. if (_curve.is_valid()) {
  1527. _curve->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveTexture::_update));
  1528. }
  1529. _update();
  1530. }
  1531. }
  1532. void CurveTexture::_update() {
  1533. Vector<uint8_t> data;
  1534. data.resize(_width * sizeof(float) * (texture_mode == TEXTURE_MODE_RGB ? 3 : 1));
  1535. // The array is locked in that scope
  1536. {
  1537. uint8_t *wd8 = data.ptrw();
  1538. float *wd = (float *)wd8;
  1539. if (_curve.is_valid()) {
  1540. Curve &curve = **_curve;
  1541. for (int i = 0; i < _width; ++i) {
  1542. float t = i / static_cast<float>(_width);
  1543. if (texture_mode == TEXTURE_MODE_RGB) {
  1544. wd[i * 3 + 0] = curve.sample_baked(t);
  1545. wd[i * 3 + 1] = wd[i * 3 + 0];
  1546. wd[i * 3 + 2] = wd[i * 3 + 0];
  1547. } else {
  1548. wd[i] = curve.sample_baked(t);
  1549. }
  1550. }
  1551. } else {
  1552. for (int i = 0; i < _width; ++i) {
  1553. if (texture_mode == TEXTURE_MODE_RGB) {
  1554. wd[i * 3 + 0] = 0;
  1555. wd[i * 3 + 1] = 0;
  1556. wd[i * 3 + 2] = 0;
  1557. } else {
  1558. wd[i] = 0;
  1559. }
  1560. }
  1561. }
  1562. }
  1563. Ref<Image> image = memnew(Image(_width, 1, false, texture_mode == TEXTURE_MODE_RGB ? Image::FORMAT_RGBF : Image::FORMAT_RF, data));
  1564. if (_texture.is_valid()) {
  1565. if (_current_texture_mode != texture_mode || _current_width != _width) {
  1566. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  1567. RS::get_singleton()->texture_replace(_texture, new_texture);
  1568. } else {
  1569. RS::get_singleton()->texture_2d_update(_texture, image);
  1570. }
  1571. } else {
  1572. _texture = RS::get_singleton()->texture_2d_create(image);
  1573. }
  1574. _current_texture_mode = texture_mode;
  1575. _current_width = _width;
  1576. emit_changed();
  1577. }
  1578. Ref<Curve> CurveTexture::get_curve() const {
  1579. return _curve;
  1580. }
  1581. void CurveTexture::set_texture_mode(TextureMode p_mode) {
  1582. ERR_FAIL_COND(p_mode < TEXTURE_MODE_RGB || p_mode > TEXTURE_MODE_RED);
  1583. if (texture_mode == p_mode) {
  1584. return;
  1585. }
  1586. texture_mode = p_mode;
  1587. _update();
  1588. }
  1589. CurveTexture::TextureMode CurveTexture::get_texture_mode() const {
  1590. return texture_mode;
  1591. }
  1592. RID CurveTexture::get_rid() const {
  1593. if (!_texture.is_valid()) {
  1594. _texture = RS::get_singleton()->texture_2d_placeholder_create();
  1595. }
  1596. return _texture;
  1597. }
  1598. CurveTexture::CurveTexture() {}
  1599. CurveTexture::~CurveTexture() {
  1600. if (_texture.is_valid()) {
  1601. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1602. RS::get_singleton()->free(_texture);
  1603. }
  1604. }
  1605. //////////////////
  1606. void CurveXYZTexture::_bind_methods() {
  1607. ClassDB::bind_method(D_METHOD("set_width", "width"), &CurveXYZTexture::set_width);
  1608. ClassDB::bind_method(D_METHOD("set_curve_x", "curve"), &CurveXYZTexture::set_curve_x);
  1609. ClassDB::bind_method(D_METHOD("get_curve_x"), &CurveXYZTexture::get_curve_x);
  1610. ClassDB::bind_method(D_METHOD("set_curve_y", "curve"), &CurveXYZTexture::set_curve_y);
  1611. ClassDB::bind_method(D_METHOD("get_curve_y"), &CurveXYZTexture::get_curve_y);
  1612. ClassDB::bind_method(D_METHOD("set_curve_z", "curve"), &CurveXYZTexture::set_curve_z);
  1613. ClassDB::bind_method(D_METHOD("get_curve_z"), &CurveXYZTexture::get_curve_z);
  1614. ClassDB::bind_method(D_METHOD("_update"), &CurveXYZTexture::_update);
  1615. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,4096,suffix:px"), "set_width", "get_width");
  1616. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_x", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_x", "get_curve_x");
  1617. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_y", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_y", "get_curve_y");
  1618. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_z", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_z", "get_curve_z");
  1619. }
  1620. void CurveXYZTexture::set_width(int p_width) {
  1621. ERR_FAIL_COND(p_width < 32 || p_width > 4096);
  1622. if (_width == p_width) {
  1623. return;
  1624. }
  1625. _width = p_width;
  1626. _update();
  1627. }
  1628. int CurveXYZTexture::get_width() const {
  1629. return _width;
  1630. }
  1631. void CurveXYZTexture::ensure_default_setup(float p_min, float p_max) {
  1632. if (_curve_x.is_null()) {
  1633. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  1634. curve->add_point(Vector2(0, 1));
  1635. curve->add_point(Vector2(1, 1));
  1636. curve->set_min_value(p_min);
  1637. curve->set_max_value(p_max);
  1638. set_curve_x(curve);
  1639. }
  1640. if (_curve_y.is_null()) {
  1641. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  1642. curve->add_point(Vector2(0, 1));
  1643. curve->add_point(Vector2(1, 1));
  1644. curve->set_min_value(p_min);
  1645. curve->set_max_value(p_max);
  1646. set_curve_y(curve);
  1647. }
  1648. if (_curve_z.is_null()) {
  1649. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  1650. curve->add_point(Vector2(0, 1));
  1651. curve->add_point(Vector2(1, 1));
  1652. curve->set_min_value(p_min);
  1653. curve->set_max_value(p_max);
  1654. set_curve_z(curve);
  1655. }
  1656. }
  1657. void CurveXYZTexture::set_curve_x(Ref<Curve> p_curve) {
  1658. if (_curve_x != p_curve) {
  1659. if (_curve_x.is_valid()) {
  1660. _curve_x->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update));
  1661. }
  1662. _curve_x = p_curve;
  1663. if (_curve_x.is_valid()) {
  1664. _curve_x->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
  1665. }
  1666. _update();
  1667. }
  1668. }
  1669. void CurveXYZTexture::set_curve_y(Ref<Curve> p_curve) {
  1670. if (_curve_y != p_curve) {
  1671. if (_curve_y.is_valid()) {
  1672. _curve_y->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update));
  1673. }
  1674. _curve_y = p_curve;
  1675. if (_curve_y.is_valid()) {
  1676. _curve_y->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
  1677. }
  1678. _update();
  1679. }
  1680. }
  1681. void CurveXYZTexture::set_curve_z(Ref<Curve> p_curve) {
  1682. if (_curve_z != p_curve) {
  1683. if (_curve_z.is_valid()) {
  1684. _curve_z->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update));
  1685. }
  1686. _curve_z = p_curve;
  1687. if (_curve_z.is_valid()) {
  1688. _curve_z->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
  1689. }
  1690. _update();
  1691. }
  1692. }
  1693. void CurveXYZTexture::_update() {
  1694. Vector<uint8_t> data;
  1695. data.resize(_width * sizeof(float) * 3);
  1696. // The array is locked in that scope
  1697. {
  1698. uint8_t *wd8 = data.ptrw();
  1699. float *wd = (float *)wd8;
  1700. if (_curve_x.is_valid()) {
  1701. Curve &curve_x = **_curve_x;
  1702. for (int i = 0; i < _width; ++i) {
  1703. float t = i / static_cast<float>(_width);
  1704. wd[i * 3 + 0] = curve_x.sample_baked(t);
  1705. }
  1706. } else {
  1707. for (int i = 0; i < _width; ++i) {
  1708. wd[i * 3 + 0] = 0;
  1709. }
  1710. }
  1711. if (_curve_y.is_valid()) {
  1712. Curve &curve_y = **_curve_y;
  1713. for (int i = 0; i < _width; ++i) {
  1714. float t = i / static_cast<float>(_width);
  1715. wd[i * 3 + 1] = curve_y.sample_baked(t);
  1716. }
  1717. } else {
  1718. for (int i = 0; i < _width; ++i) {
  1719. wd[i * 3 + 1] = 0;
  1720. }
  1721. }
  1722. if (_curve_z.is_valid()) {
  1723. Curve &curve_z = **_curve_z;
  1724. for (int i = 0; i < _width; ++i) {
  1725. float t = i / static_cast<float>(_width);
  1726. wd[i * 3 + 2] = curve_z.sample_baked(t);
  1727. }
  1728. } else {
  1729. for (int i = 0; i < _width; ++i) {
  1730. wd[i * 3 + 2] = 0;
  1731. }
  1732. }
  1733. }
  1734. Ref<Image> image = memnew(Image(_width, 1, false, Image::FORMAT_RGBF, data));
  1735. if (_texture.is_valid()) {
  1736. if (_current_width != _width) {
  1737. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  1738. RS::get_singleton()->texture_replace(_texture, new_texture);
  1739. } else {
  1740. RS::get_singleton()->texture_2d_update(_texture, image);
  1741. }
  1742. } else {
  1743. _texture = RS::get_singleton()->texture_2d_create(image);
  1744. }
  1745. _current_width = _width;
  1746. emit_changed();
  1747. }
  1748. Ref<Curve> CurveXYZTexture::get_curve_x() const {
  1749. return _curve_x;
  1750. }
  1751. Ref<Curve> CurveXYZTexture::get_curve_y() const {
  1752. return _curve_y;
  1753. }
  1754. Ref<Curve> CurveXYZTexture::get_curve_z() const {
  1755. return _curve_z;
  1756. }
  1757. RID CurveXYZTexture::get_rid() const {
  1758. if (!_texture.is_valid()) {
  1759. _texture = RS::get_singleton()->texture_2d_placeholder_create();
  1760. }
  1761. return _texture;
  1762. }
  1763. CurveXYZTexture::CurveXYZTexture() {}
  1764. CurveXYZTexture::~CurveXYZTexture() {
  1765. if (_texture.is_valid()) {
  1766. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1767. RS::get_singleton()->free(_texture);
  1768. }
  1769. }
  1770. //////////////////
  1771. GradientTexture1D::GradientTexture1D() {
  1772. _queue_update();
  1773. }
  1774. GradientTexture1D::~GradientTexture1D() {
  1775. if (texture.is_valid()) {
  1776. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1777. RS::get_singleton()->free(texture);
  1778. }
  1779. }
  1780. void GradientTexture1D::_bind_methods() {
  1781. ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture1D::set_gradient);
  1782. ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture1D::get_gradient);
  1783. ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture1D::set_width);
  1784. // The `get_width()` method is already exposed by the parent class Texture2D.
  1785. ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture1D::set_use_hdr);
  1786. ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture1D::is_using_hdr);
  1787. ClassDB::bind_method(D_METHOD("_update"), &GradientTexture1D::_update);
  1788. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
  1789. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,16384,suffix:px"), "set_width", "get_width");
  1790. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
  1791. }
  1792. void GradientTexture1D::set_gradient(Ref<Gradient> p_gradient) {
  1793. if (p_gradient == gradient) {
  1794. return;
  1795. }
  1796. if (gradient.is_valid()) {
  1797. gradient->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GradientTexture1D::_update));
  1798. }
  1799. gradient = p_gradient;
  1800. if (gradient.is_valid()) {
  1801. gradient->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GradientTexture1D::_update));
  1802. }
  1803. _update();
  1804. emit_changed();
  1805. }
  1806. Ref<Gradient> GradientTexture1D::get_gradient() const {
  1807. return gradient;
  1808. }
  1809. void GradientTexture1D::_queue_update() {
  1810. if (update_pending) {
  1811. return;
  1812. }
  1813. update_pending = true;
  1814. call_deferred(SNAME("_update"));
  1815. }
  1816. void GradientTexture1D::_update() {
  1817. update_pending = false;
  1818. if (gradient.is_null()) {
  1819. return;
  1820. }
  1821. if (use_hdr) {
  1822. // High dynamic range.
  1823. Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBAF));
  1824. Gradient &g = **gradient;
  1825. // `create()` isn't available for non-uint8_t data, so fill in the data manually.
  1826. for (int i = 0; i < width; i++) {
  1827. float ofs = float(i) / (width - 1);
  1828. image->set_pixel(i, 0, g.get_color_at_offset(ofs));
  1829. }
  1830. if (texture.is_valid()) {
  1831. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  1832. RS::get_singleton()->texture_replace(texture, new_texture);
  1833. } else {
  1834. texture = RS::get_singleton()->texture_2d_create(image);
  1835. }
  1836. } else {
  1837. // Low dynamic range. "Overbright" colors will be clamped.
  1838. Vector<uint8_t> data;
  1839. data.resize(width * 4);
  1840. {
  1841. uint8_t *wd8 = data.ptrw();
  1842. Gradient &g = **gradient;
  1843. for (int i = 0; i < width; i++) {
  1844. float ofs = float(i) / (width - 1);
  1845. Color color = g.get_color_at_offset(ofs);
  1846. wd8[i * 4 + 0] = uint8_t(CLAMP(color.r * 255.0, 0, 255));
  1847. wd8[i * 4 + 1] = uint8_t(CLAMP(color.g * 255.0, 0, 255));
  1848. wd8[i * 4 + 2] = uint8_t(CLAMP(color.b * 255.0, 0, 255));
  1849. wd8[i * 4 + 3] = uint8_t(CLAMP(color.a * 255.0, 0, 255));
  1850. }
  1851. }
  1852. Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data));
  1853. if (texture.is_valid()) {
  1854. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  1855. RS::get_singleton()->texture_replace(texture, new_texture);
  1856. } else {
  1857. texture = RS::get_singleton()->texture_2d_create(image);
  1858. }
  1859. }
  1860. emit_changed();
  1861. }
  1862. void GradientTexture1D::set_width(int p_width) {
  1863. ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
  1864. width = p_width;
  1865. _queue_update();
  1866. }
  1867. int GradientTexture1D::get_width() const {
  1868. return width;
  1869. }
  1870. void GradientTexture1D::set_use_hdr(bool p_enabled) {
  1871. if (p_enabled == use_hdr) {
  1872. return;
  1873. }
  1874. use_hdr = p_enabled;
  1875. _queue_update();
  1876. }
  1877. bool GradientTexture1D::is_using_hdr() const {
  1878. return use_hdr;
  1879. }
  1880. Ref<Image> GradientTexture1D::get_image() const {
  1881. if (!texture.is_valid()) {
  1882. return Ref<Image>();
  1883. }
  1884. return RenderingServer::get_singleton()->texture_2d_get(texture);
  1885. }
  1886. //////////////////
  1887. GradientTexture2D::GradientTexture2D() {
  1888. _queue_update();
  1889. }
  1890. GradientTexture2D::~GradientTexture2D() {
  1891. if (texture.is_valid()) {
  1892. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1893. RS::get_singleton()->free(texture);
  1894. }
  1895. }
  1896. void GradientTexture2D::set_gradient(Ref<Gradient> p_gradient) {
  1897. if (gradient == p_gradient) {
  1898. return;
  1899. }
  1900. if (gradient.is_valid()) {
  1901. gradient->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GradientTexture2D::_queue_update));
  1902. }
  1903. gradient = p_gradient;
  1904. if (gradient.is_valid()) {
  1905. gradient->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GradientTexture2D::_queue_update));
  1906. }
  1907. _update();
  1908. emit_changed();
  1909. }
  1910. Ref<Gradient> GradientTexture2D::get_gradient() const {
  1911. return gradient;
  1912. }
  1913. void GradientTexture2D::_queue_update() {
  1914. if (update_pending) {
  1915. return;
  1916. }
  1917. update_pending = true;
  1918. call_deferred(SNAME("_update"));
  1919. }
  1920. void GradientTexture2D::_update() {
  1921. update_pending = false;
  1922. if (gradient.is_null()) {
  1923. return;
  1924. }
  1925. Ref<Image> image;
  1926. image.instantiate();
  1927. if (gradient->get_point_count() <= 1) { // No need to interpolate.
  1928. image->initialize_data(width, height, false, (use_hdr) ? Image::FORMAT_RGBAF : Image::FORMAT_RGBA8);
  1929. image->fill((gradient->get_point_count() == 1) ? gradient->get_color(0) : Color(0, 0, 0, 1));
  1930. } else {
  1931. if (use_hdr) {
  1932. image->initialize_data(width, height, false, Image::FORMAT_RGBAF);
  1933. Gradient &g = **gradient;
  1934. // `create()` isn't available for non-uint8_t data, so fill in the data manually.
  1935. for (int y = 0; y < height; y++) {
  1936. for (int x = 0; x < width; x++) {
  1937. float ofs = _get_gradient_offset_at(x, y);
  1938. image->set_pixel(x, y, g.get_color_at_offset(ofs));
  1939. }
  1940. }
  1941. } else {
  1942. Vector<uint8_t> data;
  1943. data.resize(width * height * 4);
  1944. {
  1945. uint8_t *wd8 = data.ptrw();
  1946. Gradient &g = **gradient;
  1947. for (int y = 0; y < height; y++) {
  1948. for (int x = 0; x < width; x++) {
  1949. float ofs = _get_gradient_offset_at(x, y);
  1950. const Color &c = g.get_color_at_offset(ofs);
  1951. wd8[(x + (y * width)) * 4 + 0] = uint8_t(CLAMP(c.r * 255.0, 0, 255));
  1952. wd8[(x + (y * width)) * 4 + 1] = uint8_t(CLAMP(c.g * 255.0, 0, 255));
  1953. wd8[(x + (y * width)) * 4 + 2] = uint8_t(CLAMP(c.b * 255.0, 0, 255));
  1954. wd8[(x + (y * width)) * 4 + 3] = uint8_t(CLAMP(c.a * 255.0, 0, 255));
  1955. }
  1956. }
  1957. }
  1958. image->set_data(width, height, false, Image::FORMAT_RGBA8, data);
  1959. }
  1960. }
  1961. if (texture.is_valid()) {
  1962. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  1963. RS::get_singleton()->texture_replace(texture, new_texture);
  1964. } else {
  1965. texture = RS::get_singleton()->texture_2d_create(image);
  1966. }
  1967. emit_changed();
  1968. }
  1969. float GradientTexture2D::_get_gradient_offset_at(int x, int y) const {
  1970. if (fill_to == fill_from) {
  1971. return 0;
  1972. }
  1973. float ofs = 0;
  1974. Vector2 pos;
  1975. if (width > 1) {
  1976. pos.x = static_cast<float>(x) / (width - 1);
  1977. }
  1978. if (height > 1) {
  1979. pos.y = static_cast<float>(y) / (height - 1);
  1980. }
  1981. if (fill == Fill::FILL_LINEAR) {
  1982. Vector2 segment[2];
  1983. segment[0] = fill_from;
  1984. segment[1] = fill_to;
  1985. Vector2 closest = Geometry2D::get_closest_point_to_segment_uncapped(pos, &segment[0]);
  1986. ofs = (closest - fill_from).length() / (fill_to - fill_from).length();
  1987. if ((closest - fill_from).dot(fill_to - fill_from) < 0) {
  1988. ofs *= -1;
  1989. }
  1990. } else if (fill == Fill::FILL_RADIAL) {
  1991. ofs = (pos - fill_from).length() / (fill_to - fill_from).length();
  1992. }
  1993. if (repeat == Repeat::REPEAT_NONE) {
  1994. ofs = CLAMP(ofs, 0.0, 1.0);
  1995. } else if (repeat == Repeat::REPEAT) {
  1996. ofs = Math::fmod(ofs, 1.0f);
  1997. if (ofs < 0) {
  1998. ofs = 1 + ofs;
  1999. }
  2000. } else if (repeat == Repeat::REPEAT_MIRROR) {
  2001. ofs = Math::abs(ofs);
  2002. ofs = Math::fmod(ofs, 2.0f);
  2003. if (ofs > 1.0) {
  2004. ofs = 2.0 - ofs;
  2005. }
  2006. }
  2007. return ofs;
  2008. }
  2009. void GradientTexture2D::set_width(int p_width) {
  2010. ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
  2011. width = p_width;
  2012. _queue_update();
  2013. }
  2014. int GradientTexture2D::get_width() const {
  2015. return width;
  2016. }
  2017. void GradientTexture2D::set_height(int p_height) {
  2018. ERR_FAIL_COND_MSG(p_height <= 0 || p_height > 16384, "Texture dimensions have to be within 1 to 16384 range.");
  2019. height = p_height;
  2020. _queue_update();
  2021. }
  2022. int GradientTexture2D::get_height() const {
  2023. return height;
  2024. }
  2025. void GradientTexture2D::set_use_hdr(bool p_enabled) {
  2026. if (p_enabled == use_hdr) {
  2027. return;
  2028. }
  2029. use_hdr = p_enabled;
  2030. _queue_update();
  2031. }
  2032. bool GradientTexture2D::is_using_hdr() const {
  2033. return use_hdr;
  2034. }
  2035. void GradientTexture2D::set_fill_from(Vector2 p_fill_from) {
  2036. fill_from = p_fill_from;
  2037. _queue_update();
  2038. }
  2039. Vector2 GradientTexture2D::get_fill_from() const {
  2040. return fill_from;
  2041. }
  2042. void GradientTexture2D::set_fill_to(Vector2 p_fill_to) {
  2043. fill_to = p_fill_to;
  2044. _queue_update();
  2045. }
  2046. Vector2 GradientTexture2D::get_fill_to() const {
  2047. return fill_to;
  2048. }
  2049. void GradientTexture2D::set_fill(Fill p_fill) {
  2050. fill = p_fill;
  2051. _queue_update();
  2052. }
  2053. GradientTexture2D::Fill GradientTexture2D::get_fill() const {
  2054. return fill;
  2055. }
  2056. void GradientTexture2D::set_repeat(Repeat p_repeat) {
  2057. repeat = p_repeat;
  2058. _queue_update();
  2059. }
  2060. GradientTexture2D::Repeat GradientTexture2D::get_repeat() const {
  2061. return repeat;
  2062. }
  2063. RID GradientTexture2D::get_rid() const {
  2064. if (!texture.is_valid()) {
  2065. texture = RS::get_singleton()->texture_2d_placeholder_create();
  2066. }
  2067. return texture;
  2068. }
  2069. Ref<Image> GradientTexture2D::get_image() const {
  2070. if (!texture.is_valid()) {
  2071. return Ref<Image>();
  2072. }
  2073. return RenderingServer::get_singleton()->texture_2d_get(texture);
  2074. }
  2075. void GradientTexture2D::_bind_methods() {
  2076. ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture2D::set_gradient);
  2077. ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture2D::get_gradient);
  2078. ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture2D::set_width);
  2079. ClassDB::bind_method(D_METHOD("set_height", "height"), &GradientTexture2D::set_height);
  2080. ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture2D::set_use_hdr);
  2081. ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture2D::is_using_hdr);
  2082. ClassDB::bind_method(D_METHOD("set_fill", "fill"), &GradientTexture2D::set_fill);
  2083. ClassDB::bind_method(D_METHOD("get_fill"), &GradientTexture2D::get_fill);
  2084. ClassDB::bind_method(D_METHOD("set_fill_from", "fill_from"), &GradientTexture2D::set_fill_from);
  2085. ClassDB::bind_method(D_METHOD("get_fill_from"), &GradientTexture2D::get_fill_from);
  2086. ClassDB::bind_method(D_METHOD("set_fill_to", "fill_to"), &GradientTexture2D::set_fill_to);
  2087. ClassDB::bind_method(D_METHOD("get_fill_to"), &GradientTexture2D::get_fill_to);
  2088. ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &GradientTexture2D::set_repeat);
  2089. ClassDB::bind_method(D_METHOD("get_repeat"), &GradientTexture2D::get_repeat);
  2090. ClassDB::bind_method(D_METHOD("_update"), &GradientTexture2D::_update);
  2091. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
  2092. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width");
  2093. ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height");
  2094. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
  2095. ADD_GROUP("Fill", "fill_");
  2096. ADD_PROPERTY(PropertyInfo(Variant::INT, "fill", PROPERTY_HINT_ENUM, "Linear,Radial"), "set_fill", "get_fill");
  2097. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_from"), "set_fill_from", "get_fill_from");
  2098. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_to"), "set_fill_to", "get_fill_to");
  2099. ADD_GROUP("Repeat", "repeat_");
  2100. ADD_PROPERTY(PropertyInfo(Variant::INT, "repeat", PROPERTY_HINT_ENUM, "No Repeat,Repeat,Mirror Repeat"), "set_repeat", "get_repeat");
  2101. BIND_ENUM_CONSTANT(FILL_LINEAR);
  2102. BIND_ENUM_CONSTANT(FILL_RADIAL);
  2103. BIND_ENUM_CONSTANT(REPEAT_NONE);
  2104. BIND_ENUM_CONSTANT(REPEAT);
  2105. BIND_ENUM_CONSTANT(REPEAT_MIRROR);
  2106. }
  2107. //////////////////////////////////////
  2108. void AnimatedTexture::_update_proxy() {
  2109. RWLockRead r(rw_lock);
  2110. float delta;
  2111. if (prev_ticks == 0) {
  2112. delta = 0;
  2113. prev_ticks = OS::get_singleton()->get_ticks_usec();
  2114. } else {
  2115. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  2116. delta = float(double(ticks - prev_ticks) / 1000000.0);
  2117. prev_ticks = ticks;
  2118. }
  2119. time += delta;
  2120. float speed = speed_scale == 0 ? 0 : abs(1.0 / speed_scale);
  2121. int iter_max = frame_count;
  2122. while (iter_max && !pause) {
  2123. float frame_limit = frames[current_frame].duration * speed;
  2124. if (time > frame_limit) {
  2125. if (speed_scale > 0.0) {
  2126. current_frame++;
  2127. } else {
  2128. current_frame--;
  2129. }
  2130. if (current_frame >= frame_count) {
  2131. if (one_shot) {
  2132. current_frame = frame_count - 1;
  2133. } else {
  2134. current_frame = 0;
  2135. }
  2136. } else if (current_frame < 0) {
  2137. if (one_shot) {
  2138. current_frame = 0;
  2139. } else {
  2140. current_frame = frame_count - 1;
  2141. }
  2142. }
  2143. time -= frame_limit;
  2144. } else {
  2145. break;
  2146. }
  2147. iter_max--;
  2148. }
  2149. if (frames[current_frame].texture.is_valid()) {
  2150. RenderingServer::get_singleton()->texture_proxy_update(proxy, frames[current_frame].texture->get_rid());
  2151. }
  2152. }
  2153. void AnimatedTexture::set_frames(int p_frames) {
  2154. ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES);
  2155. RWLockWrite r(rw_lock);
  2156. frame_count = p_frames;
  2157. }
  2158. int AnimatedTexture::get_frames() const {
  2159. return frame_count;
  2160. }
  2161. void AnimatedTexture::set_current_frame(int p_frame) {
  2162. ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count);
  2163. RWLockWrite r(rw_lock);
  2164. current_frame = p_frame;
  2165. time = 0;
  2166. }
  2167. int AnimatedTexture::get_current_frame() const {
  2168. return current_frame;
  2169. }
  2170. void AnimatedTexture::set_pause(bool p_pause) {
  2171. RWLockWrite r(rw_lock);
  2172. pause = p_pause;
  2173. }
  2174. bool AnimatedTexture::get_pause() const {
  2175. return pause;
  2176. }
  2177. void AnimatedTexture::set_one_shot(bool p_one_shot) {
  2178. RWLockWrite r(rw_lock);
  2179. one_shot = p_one_shot;
  2180. }
  2181. bool AnimatedTexture::get_one_shot() const {
  2182. return one_shot;
  2183. }
  2184. void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture) {
  2185. ERR_FAIL_COND(p_texture == this);
  2186. ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
  2187. RWLockWrite w(rw_lock);
  2188. frames[p_frame].texture = p_texture;
  2189. }
  2190. Ref<Texture2D> AnimatedTexture::get_frame_texture(int p_frame) const {
  2191. ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture2D>());
  2192. RWLockRead r(rw_lock);
  2193. return frames[p_frame].texture;
  2194. }
  2195. void AnimatedTexture::set_frame_duration(int p_frame, float p_duration) {
  2196. ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
  2197. RWLockRead r(rw_lock);
  2198. frames[p_frame].duration = p_duration;
  2199. }
  2200. float AnimatedTexture::get_frame_duration(int p_frame) const {
  2201. ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0);
  2202. RWLockRead r(rw_lock);
  2203. return frames[p_frame].duration;
  2204. }
  2205. void AnimatedTexture::set_speed_scale(float p_scale) {
  2206. ERR_FAIL_COND(p_scale < -1000 || p_scale >= 1000);
  2207. speed_scale = p_scale;
  2208. }
  2209. float AnimatedTexture::get_speed_scale() const {
  2210. return speed_scale;
  2211. }
  2212. int AnimatedTexture::get_width() const {
  2213. RWLockRead r(rw_lock);
  2214. if (!frames[current_frame].texture.is_valid()) {
  2215. return 1;
  2216. }
  2217. return frames[current_frame].texture->get_width();
  2218. }
  2219. int AnimatedTexture::get_height() const {
  2220. RWLockRead r(rw_lock);
  2221. if (!frames[current_frame].texture.is_valid()) {
  2222. return 1;
  2223. }
  2224. return frames[current_frame].texture->get_height();
  2225. }
  2226. RID AnimatedTexture::get_rid() const {
  2227. return proxy;
  2228. }
  2229. bool AnimatedTexture::has_alpha() const {
  2230. RWLockRead r(rw_lock);
  2231. if (!frames[current_frame].texture.is_valid()) {
  2232. return false;
  2233. }
  2234. return frames[current_frame].texture->has_alpha();
  2235. }
  2236. Ref<Image> AnimatedTexture::get_image() const {
  2237. RWLockRead r(rw_lock);
  2238. if (!frames[current_frame].texture.is_valid()) {
  2239. return Ref<Image>();
  2240. }
  2241. return frames[current_frame].texture->get_image();
  2242. }
  2243. bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const {
  2244. RWLockRead r(rw_lock);
  2245. if (frames[current_frame].texture.is_valid()) {
  2246. return frames[current_frame].texture->is_pixel_opaque(p_x, p_y);
  2247. }
  2248. return true;
  2249. }
  2250. void AnimatedTexture::_validate_property(PropertyInfo &p_property) const {
  2251. String prop = p_property.name;
  2252. if (prop.begins_with("frame_")) {
  2253. int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
  2254. if (frame >= frame_count) {
  2255. p_property.usage = PROPERTY_USAGE_NONE;
  2256. }
  2257. }
  2258. }
  2259. void AnimatedTexture::_bind_methods() {
  2260. ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames);
  2261. ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames);
  2262. ClassDB::bind_method(D_METHOD("set_current_frame", "frame"), &AnimatedTexture::set_current_frame);
  2263. ClassDB::bind_method(D_METHOD("get_current_frame"), &AnimatedTexture::get_current_frame);
  2264. ClassDB::bind_method(D_METHOD("set_pause", "pause"), &AnimatedTexture::set_pause);
  2265. ClassDB::bind_method(D_METHOD("get_pause"), &AnimatedTexture::get_pause);
  2266. ClassDB::bind_method(D_METHOD("set_one_shot", "one_shot"), &AnimatedTexture::set_one_shot);
  2267. ClassDB::bind_method(D_METHOD("get_one_shot"), &AnimatedTexture::get_one_shot);
  2268. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &AnimatedTexture::set_speed_scale);
  2269. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedTexture::get_speed_scale);
  2270. ClassDB::bind_method(D_METHOD("set_frame_texture", "frame", "texture"), &AnimatedTexture::set_frame_texture);
  2271. ClassDB::bind_method(D_METHOD("get_frame_texture", "frame"), &AnimatedTexture::get_frame_texture);
  2272. ClassDB::bind_method(D_METHOD("set_frame_duration", "frame", "duration"), &AnimatedTexture::set_frame_duration);
  2273. ClassDB::bind_method(D_METHOD("get_frame_duration", "frame"), &AnimatedTexture::get_frame_duration);
  2274. 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");
  2275. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_frame", "get_current_frame");
  2276. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause"), "set_pause", "get_pause");
  2277. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  2278. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-60,60,0.1,or_greater,or_lesser"), "set_speed_scale", "get_speed_scale");
  2279. for (int i = 0; i < MAX_FRAMES; i++) {
  2280. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "frame_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_texture", "get_frame_texture", i);
  2281. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "frame_" + itos(i) + "/duration", PROPERTY_HINT_RANGE, "0.0,16.0,0.01,or_greater,suffix:s", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_duration", "get_frame_duration", i);
  2282. }
  2283. BIND_CONSTANT(MAX_FRAMES);
  2284. }
  2285. AnimatedTexture::AnimatedTexture() {
  2286. //proxy = RS::get_singleton()->texture_create();
  2287. proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
  2288. proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);
  2289. RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);
  2290. RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));
  2291. }
  2292. AnimatedTexture::~AnimatedTexture() {
  2293. ERR_FAIL_NULL(RenderingServer::get_singleton());
  2294. RS::get_singleton()->free(proxy);
  2295. RS::get_singleton()->free(proxy_ph);
  2296. }
  2297. ///////////////////////////////
  2298. Image::Format TextureLayered::get_format() const {
  2299. Image::Format ret = Image::FORMAT_MAX;
  2300. GDVIRTUAL_REQUIRED_CALL(_get_format, ret);
  2301. return ret;
  2302. }
  2303. TextureLayered::LayeredType TextureLayered::get_layered_type() const {
  2304. uint32_t ret = LAYERED_TYPE_2D_ARRAY;
  2305. GDVIRTUAL_REQUIRED_CALL(_get_layered_type, ret);
  2306. return (LayeredType)ret;
  2307. }
  2308. int TextureLayered::get_width() const {
  2309. int ret = 0;
  2310. GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
  2311. return ret;
  2312. }
  2313. int TextureLayered::get_height() const {
  2314. int ret = 0;
  2315. GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
  2316. return ret;
  2317. }
  2318. int TextureLayered::get_layers() const {
  2319. int ret = 0;
  2320. GDVIRTUAL_REQUIRED_CALL(_get_layers, ret);
  2321. return ret;
  2322. }
  2323. bool TextureLayered::has_mipmaps() const {
  2324. bool ret = false;
  2325. GDVIRTUAL_REQUIRED_CALL(_has_mipmaps, ret);
  2326. return ret;
  2327. }
  2328. Ref<Image> TextureLayered::get_layer_data(int p_layer) const {
  2329. Ref<Image> ret;
  2330. GDVIRTUAL_REQUIRED_CALL(_get_layer_data, p_layer, ret);
  2331. return ret;
  2332. }
  2333. void TextureLayered::_bind_methods() {
  2334. ClassDB::bind_method(D_METHOD("get_format"), &TextureLayered::get_format);
  2335. ClassDB::bind_method(D_METHOD("get_layered_type"), &TextureLayered::get_layered_type);
  2336. ClassDB::bind_method(D_METHOD("get_width"), &TextureLayered::get_width);
  2337. ClassDB::bind_method(D_METHOD("get_height"), &TextureLayered::get_height);
  2338. ClassDB::bind_method(D_METHOD("get_layers"), &TextureLayered::get_layers);
  2339. ClassDB::bind_method(D_METHOD("has_mipmaps"), &TextureLayered::has_mipmaps);
  2340. ClassDB::bind_method(D_METHOD("get_layer_data", "layer"), &TextureLayered::get_layer_data);
  2341. BIND_ENUM_CONSTANT(LAYERED_TYPE_2D_ARRAY);
  2342. BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP);
  2343. BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP_ARRAY);
  2344. GDVIRTUAL_BIND(_get_format);
  2345. GDVIRTUAL_BIND(_get_layered_type);
  2346. GDVIRTUAL_BIND(_get_width);
  2347. GDVIRTUAL_BIND(_get_height);
  2348. GDVIRTUAL_BIND(_get_layers);
  2349. GDVIRTUAL_BIND(_has_mipmaps);
  2350. GDVIRTUAL_BIND(_get_layer_data, "layer_index");
  2351. }
  2352. ///////////////////////////////
  2353. Image::Format ImageTextureLayered::get_format() const {
  2354. return format;
  2355. }
  2356. int ImageTextureLayered::get_width() const {
  2357. return width;
  2358. }
  2359. int ImageTextureLayered::get_height() const {
  2360. return height;
  2361. }
  2362. int ImageTextureLayered::get_layers() const {
  2363. return layers;
  2364. }
  2365. bool ImageTextureLayered::has_mipmaps() const {
  2366. return mipmaps;
  2367. }
  2368. ImageTextureLayered::LayeredType ImageTextureLayered::get_layered_type() const {
  2369. return layered_type;
  2370. }
  2371. Error ImageTextureLayered::_create_from_images(const TypedArray<Image> &p_images) {
  2372. Vector<Ref<Image>> images;
  2373. for (int i = 0; i < p_images.size(); i++) {
  2374. Ref<Image> img = p_images[i];
  2375. ERR_FAIL_COND_V(img.is_null(), ERR_INVALID_PARAMETER);
  2376. images.push_back(img);
  2377. }
  2378. return create_from_images(images);
  2379. }
  2380. TypedArray<Image> ImageTextureLayered::_get_images() const {
  2381. TypedArray<Image> images;
  2382. for (int i = 0; i < layers; i++) {
  2383. images.push_back(get_layer_data(i));
  2384. }
  2385. return images;
  2386. }
  2387. void ImageTextureLayered::_set_images(const TypedArray<Image> &p_images) {
  2388. ERR_FAIL_COND(_create_from_images(p_images) != OK);
  2389. }
  2390. Error ImageTextureLayered::create_from_images(Vector<Ref<Image>> p_images) {
  2391. int new_layers = p_images.size();
  2392. ERR_FAIL_COND_V(new_layers == 0, ERR_INVALID_PARAMETER);
  2393. if (layered_type == LAYERED_TYPE_CUBEMAP) {
  2394. ERR_FAIL_COND_V_MSG(new_layers != 6, ERR_INVALID_PARAMETER,
  2395. "Cubemaps require exactly 6 layers");
  2396. } else if (layered_type == LAYERED_TYPE_CUBEMAP_ARRAY) {
  2397. ERR_FAIL_COND_V_MSG((new_layers % 6) != 0, ERR_INVALID_PARAMETER,
  2398. "Cubemap array layers must be a multiple of 6");
  2399. }
  2400. ERR_FAIL_COND_V(p_images[0].is_null() || p_images[0]->is_empty(), ERR_INVALID_PARAMETER);
  2401. Image::Format new_format = p_images[0]->get_format();
  2402. int new_width = p_images[0]->get_width();
  2403. int new_height = p_images[0]->get_height();
  2404. bool new_mipmaps = p_images[0]->has_mipmaps();
  2405. for (int i = 1; i < p_images.size(); i++) {
  2406. ERR_FAIL_COND_V_MSG(p_images[i]->get_format() != new_format, ERR_INVALID_PARAMETER,
  2407. "All images must share the same format");
  2408. ERR_FAIL_COND_V_MSG(p_images[i]->get_width() != new_width || p_images[i]->get_height() != new_height, ERR_INVALID_PARAMETER,
  2409. "All images must share the same dimensions");
  2410. ERR_FAIL_COND_V_MSG(p_images[i]->has_mipmaps() != new_mipmaps, ERR_INVALID_PARAMETER,
  2411. "All images must share the usage of mipmaps");
  2412. }
  2413. if (texture.is_valid()) {
  2414. RID new_texture = RS::get_singleton()->texture_2d_layered_create(p_images, RS::TextureLayeredType(layered_type));
  2415. ERR_FAIL_COND_V(!new_texture.is_valid(), ERR_CANT_CREATE);
  2416. RS::get_singleton()->texture_replace(texture, new_texture);
  2417. } else {
  2418. texture = RS::get_singleton()->texture_2d_layered_create(p_images, RS::TextureLayeredType(layered_type));
  2419. ERR_FAIL_COND_V(!texture.is_valid(), ERR_CANT_CREATE);
  2420. }
  2421. format = new_format;
  2422. width = new_width;
  2423. height = new_height;
  2424. layers = new_layers;
  2425. mipmaps = new_mipmaps;
  2426. return OK;
  2427. }
  2428. void ImageTextureLayered::update_layer(const Ref<Image> &p_image, int p_layer) {
  2429. ERR_FAIL_COND_MSG(texture.is_null(), "Texture is not initialized.");
  2430. ERR_FAIL_COND_MSG(p_image.is_null(), "Invalid image.");
  2431. ERR_FAIL_COND_MSG(p_image->get_format() != format, "Image format must match texture's image format.");
  2432. ERR_FAIL_COND_MSG(p_image->get_width() != width || p_image->get_height() != height, "Image size must match texture's image size.");
  2433. ERR_FAIL_COND_MSG(p_image->has_mipmaps() != mipmaps, "Image mipmap configuration must match texture's image mipmap configuration.");
  2434. ERR_FAIL_INDEX_MSG(p_layer, layers, "Layer index is out of bounds.");
  2435. RS::get_singleton()->texture_2d_update(texture, p_image, p_layer);
  2436. }
  2437. Ref<Image> ImageTextureLayered::get_layer_data(int p_layer) const {
  2438. ERR_FAIL_INDEX_V(p_layer, layers, Ref<Image>());
  2439. return RS::get_singleton()->texture_2d_layer_get(texture, p_layer);
  2440. }
  2441. RID ImageTextureLayered::get_rid() const {
  2442. if (texture.is_null()) {
  2443. texture = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
  2444. }
  2445. return texture;
  2446. }
  2447. void ImageTextureLayered::set_path(const String &p_path, bool p_take_over) {
  2448. if (texture.is_valid()) {
  2449. RS::get_singleton()->texture_set_path(texture, p_path);
  2450. }
  2451. Resource::set_path(p_path, p_take_over);
  2452. }
  2453. void ImageTextureLayered::_bind_methods() {
  2454. ClassDB::bind_method(D_METHOD("create_from_images", "images"), &ImageTextureLayered::_create_from_images);
  2455. ClassDB::bind_method(D_METHOD("update_layer", "image", "layer"), &ImageTextureLayered::update_layer);
  2456. ClassDB::bind_method(D_METHOD("_get_images"), &ImageTextureLayered::_get_images);
  2457. ClassDB::bind_method(D_METHOD("_set_images", "images"), &ImageTextureLayered::_set_images);
  2458. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_images", PROPERTY_HINT_ARRAY_TYPE, "Image", PROPERTY_USAGE_INTERNAL), "_set_images", "_get_images");
  2459. }
  2460. ImageTextureLayered::ImageTextureLayered(LayeredType p_layered_type) {
  2461. layered_type = p_layered_type;
  2462. }
  2463. ImageTextureLayered::~ImageTextureLayered() {
  2464. if (texture.is_valid()) {
  2465. ERR_FAIL_NULL(RenderingServer::get_singleton());
  2466. RS::get_singleton()->free(texture);
  2467. }
  2468. }
  2469. void Texture2DArray::_bind_methods() {
  2470. ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2DArray::create_placeholder);
  2471. }
  2472. Ref<Resource> Texture2DArray::create_placeholder() const {
  2473. Ref<PlaceholderTexture2DArray> placeholder;
  2474. placeholder.instantiate();
  2475. placeholder->set_size(Size2i(get_width(), get_height()));
  2476. placeholder->set_layers(get_layers());
  2477. return placeholder;
  2478. }
  2479. void Cubemap::_bind_methods() {
  2480. ClassDB::bind_method(D_METHOD("create_placeholder"), &Cubemap::create_placeholder);
  2481. }
  2482. Ref<Resource> Cubemap::create_placeholder() const {
  2483. Ref<PlaceholderCubemap> placeholder;
  2484. placeholder.instantiate();
  2485. placeholder->set_size(Size2i(get_width(), get_height()));
  2486. placeholder->set_layers(get_layers());
  2487. return placeholder;
  2488. }
  2489. void CubemapArray::_bind_methods() {
  2490. ClassDB::bind_method(D_METHOD("create_placeholder"), &CubemapArray::create_placeholder);
  2491. }
  2492. Ref<Resource> CubemapArray::create_placeholder() const {
  2493. Ref<PlaceholderCubemapArray> placeholder;
  2494. placeholder.instantiate();
  2495. placeholder->set_size(Size2i(get_width(), get_height()));
  2496. placeholder->set_layers(get_layers());
  2497. return placeholder;
  2498. }
  2499. ///////////////////////////////////////////
  2500. void CompressedTextureLayered::set_path(const String &p_path, bool p_take_over) {
  2501. if (texture.is_valid()) {
  2502. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  2503. }
  2504. Resource::set_path(p_path, p_take_over);
  2505. }
  2506. Image::Format CompressedTextureLayered::get_format() const {
  2507. return format;
  2508. }
  2509. Error CompressedTextureLayered::_load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit) {
  2510. ERR_FAIL_COND_V(images.size() != 0, ERR_INVALID_PARAMETER);
  2511. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  2512. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
  2513. uint8_t header[4];
  2514. f->get_buffer(header, 4);
  2515. if (header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L') {
  2516. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture layered file is corrupt (Bad header).");
  2517. }
  2518. uint32_t version = f->get_32();
  2519. if (version > FORMAT_VERSION) {
  2520. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
  2521. }
  2522. uint32_t layer_count = f->get_32(); //layer count
  2523. uint32_t type = f->get_32(); //layer count
  2524. ERR_FAIL_COND_V((int)type != layered_type, ERR_INVALID_DATA);
  2525. uint32_t df = f->get_32(); //data format
  2526. mipmap_limit = int(f->get_32());
  2527. //reserved
  2528. f->get_32();
  2529. f->get_32();
  2530. f->get_32();
  2531. if (!(df & FORMAT_BIT_STREAM)) {
  2532. p_size_limit = 0;
  2533. }
  2534. images.resize(layer_count);
  2535. for (uint32_t i = 0; i < layer_count; i++) {
  2536. Ref<Image> image = CompressedTexture2D::load_image_from_file(f, p_size_limit);
  2537. ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
  2538. images.write[i] = image;
  2539. }
  2540. return OK;
  2541. }
  2542. Error CompressedTextureLayered::load(const String &p_path) {
  2543. Vector<Ref<Image>> images;
  2544. int mipmap_limit;
  2545. Error err = _load_data(p_path, images, mipmap_limit);
  2546. if (err) {
  2547. return err;
  2548. }
  2549. if (texture.is_valid()) {
  2550. RID new_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
  2551. RS::get_singleton()->texture_replace(texture, new_texture);
  2552. } else {
  2553. texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
  2554. }
  2555. w = images[0]->get_width();
  2556. h = images[0]->get_height();
  2557. mipmaps = images[0]->has_mipmaps();
  2558. format = images[0]->get_format();
  2559. layers = images.size();
  2560. path_to_file = p_path;
  2561. if (get_path().is_empty()) {
  2562. //temporarily set path if no path set for resource, helps find errors
  2563. RenderingServer::get_singleton()->texture_set_path(texture, p_path);
  2564. }
  2565. notify_property_list_changed();
  2566. emit_changed();
  2567. return OK;
  2568. }
  2569. String CompressedTextureLayered::get_load_path() const {
  2570. return path_to_file;
  2571. }
  2572. int CompressedTextureLayered::get_width() const {
  2573. return w;
  2574. }
  2575. int CompressedTextureLayered::get_height() const {
  2576. return h;
  2577. }
  2578. int CompressedTextureLayered::get_layers() const {
  2579. return layers;
  2580. }
  2581. bool CompressedTextureLayered::has_mipmaps() const {
  2582. return mipmaps;
  2583. }
  2584. TextureLayered::LayeredType CompressedTextureLayered::get_layered_type() const {
  2585. return layered_type;
  2586. }
  2587. RID CompressedTextureLayered::get_rid() const {
  2588. if (!texture.is_valid()) {
  2589. texture = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
  2590. }
  2591. return texture;
  2592. }
  2593. Ref<Image> CompressedTextureLayered::get_layer_data(int p_layer) const {
  2594. if (texture.is_valid()) {
  2595. return RS::get_singleton()->texture_2d_layer_get(texture, p_layer);
  2596. } else {
  2597. return Ref<Image>();
  2598. }
  2599. }
  2600. void CompressedTextureLayered::reload_from_file() {
  2601. String path = get_path();
  2602. if (!path.is_resource_file()) {
  2603. return;
  2604. }
  2605. path = ResourceLoader::path_remap(path); //remap for translation
  2606. path = ResourceLoader::import_remap(path); //remap for import
  2607. if (!path.is_resource_file()) {
  2608. return;
  2609. }
  2610. load(path);
  2611. }
  2612. void CompressedTextureLayered::_validate_property(PropertyInfo &p_property) const {
  2613. }
  2614. void CompressedTextureLayered::_bind_methods() {
  2615. ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTextureLayered::load);
  2616. ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTextureLayered::get_load_path);
  2617. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
  2618. }
  2619. CompressedTextureLayered::CompressedTextureLayered(LayeredType p_type) {
  2620. layered_type = p_type;
  2621. }
  2622. CompressedTextureLayered::~CompressedTextureLayered() {
  2623. if (texture.is_valid()) {
  2624. ERR_FAIL_NULL(RenderingServer::get_singleton());
  2625. RS::get_singleton()->free(texture);
  2626. }
  2627. }
  2628. /////////////////////////////////////////////////
  2629. Ref<Resource> ResourceFormatLoaderCompressedTextureLayered::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  2630. Ref<CompressedTextureLayered> ct;
  2631. if (p_path.get_extension().to_lower() == "ctexarray") {
  2632. Ref<CompressedTexture2DArray> c;
  2633. c.instantiate();
  2634. ct = c;
  2635. } else if (p_path.get_extension().to_lower() == "ccube") {
  2636. Ref<CompressedCubemap> c;
  2637. c.instantiate();
  2638. ct = c;
  2639. } else if (p_path.get_extension().to_lower() == "ccubearray") {
  2640. Ref<CompressedCubemapArray> c;
  2641. c.instantiate();
  2642. ct = c;
  2643. } else {
  2644. if (r_error) {
  2645. *r_error = ERR_FILE_UNRECOGNIZED;
  2646. }
  2647. return Ref<Resource>();
  2648. }
  2649. Error err = ct->load(p_path);
  2650. if (r_error) {
  2651. *r_error = err;
  2652. }
  2653. if (err != OK) {
  2654. return Ref<Resource>();
  2655. }
  2656. return ct;
  2657. }
  2658. void ResourceFormatLoaderCompressedTextureLayered::get_recognized_extensions(List<String> *p_extensions) const {
  2659. p_extensions->push_back("ctexarray");
  2660. p_extensions->push_back("ccube");
  2661. p_extensions->push_back("ccubearray");
  2662. }
  2663. bool ResourceFormatLoaderCompressedTextureLayered::handles_type(const String &p_type) const {
  2664. return p_type == "CompressedTexture2DArray" || p_type == "CompressedCubemap" || p_type == "CompressedCubemapArray";
  2665. }
  2666. String ResourceFormatLoaderCompressedTextureLayered::get_resource_type(const String &p_path) const {
  2667. if (p_path.get_extension().to_lower() == "ctexarray") {
  2668. return "CompressedTexture2DArray";
  2669. }
  2670. if (p_path.get_extension().to_lower() == "ccube") {
  2671. return "CompressedCubemap";
  2672. }
  2673. if (p_path.get_extension().to_lower() == "ccubearray") {
  2674. return "CompressedCubemapArray";
  2675. }
  2676. return "";
  2677. }
  2678. ///////////////////////////////
  2679. void CameraTexture::_bind_methods() {
  2680. ClassDB::bind_method(D_METHOD("set_camera_feed_id", "feed_id"), &CameraTexture::set_camera_feed_id);
  2681. ClassDB::bind_method(D_METHOD("get_camera_feed_id"), &CameraTexture::get_camera_feed_id);
  2682. ClassDB::bind_method(D_METHOD("set_which_feed", "which_feed"), &CameraTexture::set_which_feed);
  2683. ClassDB::bind_method(D_METHOD("get_which_feed"), &CameraTexture::get_which_feed);
  2684. ClassDB::bind_method(D_METHOD("set_camera_active", "active"), &CameraTexture::set_camera_active);
  2685. ClassDB::bind_method(D_METHOD("get_camera_active"), &CameraTexture::get_camera_active);
  2686. ADD_PROPERTY(PropertyInfo(Variant::INT, "camera_feed_id"), "set_camera_feed_id", "get_camera_feed_id");
  2687. ADD_PROPERTY(PropertyInfo(Variant::INT, "which_feed"), "set_which_feed", "get_which_feed");
  2688. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "camera_is_active"), "set_camera_active", "get_camera_active");
  2689. }
  2690. int CameraTexture::get_width() const {
  2691. Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
  2692. if (feed.is_valid()) {
  2693. return feed->get_base_width();
  2694. } else {
  2695. return 0;
  2696. }
  2697. }
  2698. int CameraTexture::get_height() const {
  2699. Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
  2700. if (feed.is_valid()) {
  2701. return feed->get_base_height();
  2702. } else {
  2703. return 0;
  2704. }
  2705. }
  2706. bool CameraTexture::has_alpha() const {
  2707. return false;
  2708. }
  2709. RID CameraTexture::get_rid() const {
  2710. Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
  2711. if (feed.is_valid()) {
  2712. return feed->get_texture(which_feed);
  2713. } else {
  2714. if (_texture.is_null()) {
  2715. _texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
  2716. }
  2717. return _texture;
  2718. }
  2719. }
  2720. Ref<Image> CameraTexture::get_image() const {
  2721. // not (yet) supported
  2722. return Ref<Image>();
  2723. }
  2724. void CameraTexture::set_camera_feed_id(int p_new_id) {
  2725. camera_feed_id = p_new_id;
  2726. notify_property_list_changed();
  2727. }
  2728. int CameraTexture::get_camera_feed_id() const {
  2729. return camera_feed_id;
  2730. }
  2731. void CameraTexture::set_which_feed(CameraServer::FeedImage p_which) {
  2732. which_feed = p_which;
  2733. notify_property_list_changed();
  2734. }
  2735. CameraServer::FeedImage CameraTexture::get_which_feed() const {
  2736. return which_feed;
  2737. }
  2738. void CameraTexture::set_camera_active(bool p_active) {
  2739. Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
  2740. if (feed.is_valid()) {
  2741. feed->set_active(p_active);
  2742. notify_property_list_changed();
  2743. }
  2744. }
  2745. bool CameraTexture::get_camera_active() const {
  2746. Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
  2747. if (feed.is_valid()) {
  2748. return feed->is_active();
  2749. } else {
  2750. return false;
  2751. }
  2752. }
  2753. CameraTexture::CameraTexture() {}
  2754. CameraTexture::~CameraTexture() {
  2755. if (_texture.is_valid()) {
  2756. ERR_FAIL_NULL(RenderingServer::get_singleton());
  2757. RenderingServer::get_singleton()->free(_texture);
  2758. }
  2759. }
  2760. ///////////////////////////
  2761. void PlaceholderTexture2D::set_size(Size2 p_size) {
  2762. size = p_size;
  2763. }
  2764. int PlaceholderTexture2D::get_width() const {
  2765. return size.width;
  2766. }
  2767. int PlaceholderTexture2D::get_height() const {
  2768. return size.height;
  2769. }
  2770. bool PlaceholderTexture2D::has_alpha() const {
  2771. return false;
  2772. }
  2773. Ref<Image> PlaceholderTexture2D::get_image() const {
  2774. return Ref<Image>();
  2775. }
  2776. RID PlaceholderTexture2D::get_rid() const {
  2777. return rid;
  2778. }
  2779. void PlaceholderTexture2D::_bind_methods() {
  2780. ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaceholderTexture2D::set_size);
  2781. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
  2782. }
  2783. PlaceholderTexture2D::PlaceholderTexture2D() {
  2784. rid = RS::get_singleton()->texture_2d_placeholder_create();
  2785. }
  2786. PlaceholderTexture2D::~PlaceholderTexture2D() {
  2787. ERR_FAIL_NULL(RenderingServer::get_singleton());
  2788. RS::get_singleton()->free(rid);
  2789. }
  2790. ///////////////////////////////////////////////
  2791. void PlaceholderTexture3D::set_size(const Vector3i &p_size) {
  2792. size = p_size;
  2793. }
  2794. Vector3i PlaceholderTexture3D::get_size() const {
  2795. return size;
  2796. }
  2797. Image::Format PlaceholderTexture3D::get_format() const {
  2798. return Image::FORMAT_RGB8;
  2799. }
  2800. int PlaceholderTexture3D::get_width() const {
  2801. return size.x;
  2802. }
  2803. int PlaceholderTexture3D::get_height() const {
  2804. return size.y;
  2805. }
  2806. int PlaceholderTexture3D::get_depth() const {
  2807. return size.z;
  2808. }
  2809. bool PlaceholderTexture3D::has_mipmaps() const {
  2810. return false;
  2811. }
  2812. Vector<Ref<Image>> PlaceholderTexture3D::get_data() const {
  2813. return Vector<Ref<Image>>();
  2814. }
  2815. void PlaceholderTexture3D::_bind_methods() {
  2816. ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaceholderTexture3D::set_size);
  2817. ClassDB::bind_method(D_METHOD("get_size"), &PlaceholderTexture3D::get_size);
  2818. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
  2819. }
  2820. PlaceholderTexture3D::PlaceholderTexture3D() {
  2821. rid = RS::get_singleton()->texture_3d_placeholder_create();
  2822. }
  2823. PlaceholderTexture3D::~PlaceholderTexture3D() {
  2824. ERR_FAIL_NULL(RenderingServer::get_singleton());
  2825. RS::get_singleton()->free(rid);
  2826. }
  2827. /////////////////////////////////////////////////
  2828. void PlaceholderTextureLayered::set_size(const Size2i &p_size) {
  2829. size = p_size;
  2830. }
  2831. Size2i PlaceholderTextureLayered::get_size() const {
  2832. return size;
  2833. }
  2834. void PlaceholderTextureLayered::set_layers(int p_layers) {
  2835. layers = p_layers;
  2836. }
  2837. Image::Format PlaceholderTextureLayered::get_format() const {
  2838. return Image::FORMAT_RGB8;
  2839. }
  2840. TextureLayered::LayeredType PlaceholderTextureLayered::get_layered_type() const {
  2841. return layered_type;
  2842. }
  2843. int PlaceholderTextureLayered::get_width() const {
  2844. return size.x;
  2845. }
  2846. int PlaceholderTextureLayered::get_height() const {
  2847. return size.y;
  2848. }
  2849. int PlaceholderTextureLayered::get_layers() const {
  2850. return layers;
  2851. }
  2852. bool PlaceholderTextureLayered::has_mipmaps() const {
  2853. return false;
  2854. }
  2855. Ref<Image> PlaceholderTextureLayered::get_layer_data(int p_layer) const {
  2856. return Ref<Image>();
  2857. }
  2858. void PlaceholderTextureLayered::_bind_methods() {
  2859. ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaceholderTextureLayered::set_size);
  2860. ClassDB::bind_method(D_METHOD("get_size"), &PlaceholderTextureLayered::get_size);
  2861. ClassDB::bind_method(D_METHOD("set_layers", "layers"), &PlaceholderTextureLayered::set_layers);
  2862. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
  2863. ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_RANGE, "1,4096"), "set_layers", "get_layers");
  2864. }
  2865. PlaceholderTextureLayered::PlaceholderTextureLayered(LayeredType p_type) {
  2866. layered_type = p_type;
  2867. rid = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
  2868. }
  2869. PlaceholderTextureLayered::~PlaceholderTextureLayered() {
  2870. ERR_FAIL_NULL(RenderingServer::get_singleton());
  2871. RS::get_singleton()->free(rid);
  2872. }