2
0

texture.cpp 74 KB

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