texture.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. /*************************************************************************/
  2. /* texture.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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/method_bind_ext.gen.inc"
  32. #include "core/os/os.h"
  33. #include "core_string_names.h"
  34. #include "io/image_loader.h"
  35. Size2 Texture::get_size() const {
  36. return Size2(get_width(), get_height());
  37. }
  38. 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 {
  39. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  40. 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);
  41. }
  42. 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 {
  43. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  44. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_rid(), p_tile, p_modulate, p_transpose, normal_rid);
  45. }
  46. 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 {
  47. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  48. 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);
  49. }
  50. bool Texture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  51. r_rect = p_rect;
  52. r_src_rect = p_src_rect;
  53. return true;
  54. }
  55. void Texture::_bind_methods() {
  56. ClassDB::bind_method(D_METHOD("get_width"), &Texture::get_width);
  57. ClassDB::bind_method(D_METHOD("get_height"), &Texture::get_height);
  58. ClassDB::bind_method(D_METHOD("get_size"), &Texture::get_size);
  59. ClassDB::bind_method(D_METHOD("has_alpha"), &Texture::has_alpha);
  60. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &Texture::set_flags);
  61. ClassDB::bind_method(D_METHOD("get_flags"), &Texture::get_flags);
  62. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "position", "modulate", "transpose", "normal_map"), &Texture::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(Variant()));
  63. 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()));
  64. 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));
  65. ClassDB::bind_method(D_METHOD("get_data"), &Texture::get_data);
  66. BIND_ENUM_CONSTANT(FLAG_MIPMAPS);
  67. BIND_ENUM_CONSTANT(FLAG_REPEAT);
  68. BIND_ENUM_CONSTANT(FLAG_FILTER);
  69. BIND_ENUM_CONSTANT(FLAGS_DEFAULT);
  70. BIND_ENUM_CONSTANT(FLAG_ANISOTROPIC_FILTER);
  71. BIND_ENUM_CONSTANT(FLAG_CONVERT_TO_LINEAR);
  72. BIND_ENUM_CONSTANT(FLAG_MIRRORED_REPEAT);
  73. BIND_ENUM_CONSTANT(FLAG_VIDEO_SURFACE);
  74. }
  75. Texture::Texture() {
  76. }
  77. /////////////////////
  78. void ImageTexture::reload_from_file() {
  79. String path = ResourceLoader::path_remap(get_path());
  80. if (!path.is_resource_file())
  81. return;
  82. uint32_t flags = get_flags();
  83. Ref<Image> img;
  84. img.instance();
  85. Error err = ImageLoader::load_image(path, img);
  86. ERR_FAIL_COND(err != OK);
  87. create_from_image(img, flags);
  88. }
  89. bool ImageTexture::_set(const StringName &p_name, const Variant &p_value) {
  90. if (p_name == "image")
  91. create_from_image(p_value, flags);
  92. else if (p_name == "flags")
  93. if (w * h == 0)
  94. flags = p_value;
  95. else
  96. set_flags(p_value);
  97. else if (p_name == "size") {
  98. Size2 s = p_value;
  99. w = s.width;
  100. h = s.height;
  101. VisualServer::get_singleton()->texture_set_size_override(texture, w, h);
  102. } else if (p_name == "storage") {
  103. storage = Storage(p_value.operator int());
  104. } else if (p_name == "lossy_quality") {
  105. lossy_storage_quality = p_value;
  106. } else if (p_name == "_data") {
  107. _set_data(p_value);
  108. } else
  109. return false;
  110. return true;
  111. }
  112. bool ImageTexture::_get(const StringName &p_name, Variant &r_ret) const {
  113. if (p_name == "image_data") {
  114. } else if (p_name == "image")
  115. r_ret = get_data();
  116. else if (p_name == "flags")
  117. r_ret = flags;
  118. else if (p_name == "size")
  119. r_ret = Size2(w, h);
  120. else if (p_name == "storage")
  121. r_ret = storage;
  122. else if (p_name == "lossy_quality")
  123. r_ret = lossy_storage_quality;
  124. else
  125. return false;
  126. return true;
  127. }
  128. void ImageTexture::_get_property_list(List<PropertyInfo> *p_list) const {
  129. PropertyHint img_hint = PROPERTY_HINT_NONE;
  130. if (storage == STORAGE_COMPRESS_LOSSY) {
  131. img_hint = PROPERTY_HINT_IMAGE_COMPRESS_LOSSY;
  132. } else if (storage == STORAGE_COMPRESS_LOSSLESS) {
  133. img_hint = PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS;
  134. }
  135. p_list->push_back(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter,Anisotropic,sRGB,Mirrored Repeat"));
  136. p_list->push_back(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  137. p_list->push_back(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, ""));
  138. p_list->push_back(PropertyInfo(Variant::INT, "storage", PROPERTY_HINT_ENUM, "Uncompressed,Compress Lossy,Compress Lossless"));
  139. p_list->push_back(PropertyInfo(Variant::REAL, "lossy_quality", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"));
  140. }
  141. void ImageTexture::_reload_hook(const RID &p_hook) {
  142. String path = get_path();
  143. if (!path.is_resource_file())
  144. return;
  145. Ref<Image> img;
  146. img.instance();
  147. Error err = ImageLoader::load_image(path, img);
  148. ERR_FAIL_COND(err != OK);
  149. VisualServer::get_singleton()->texture_set_data(texture, img);
  150. _change_notify();
  151. }
  152. void ImageTexture::create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags) {
  153. flags = p_flags;
  154. VisualServer::get_singleton()->texture_allocate(texture, p_width, p_height, p_format, p_flags);
  155. format = p_format;
  156. w = p_width;
  157. h = p_height;
  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(), p_image->get_format(), p_flags);
  166. VisualServer::get_singleton()->texture_set_data(texture, p_image);
  167. _change_notify();
  168. }
  169. void ImageTexture::set_flags(uint32_t p_flags) {
  170. /* uint32_t cube = flags & FLAG_CUBEMAP;
  171. if (flags == p_flags&cube)
  172. return;
  173. flags=p_flags|cube; */
  174. flags = p_flags;
  175. VisualServer::get_singleton()->texture_set_flags(texture, p_flags);
  176. }
  177. uint32_t ImageTexture::get_flags() const {
  178. return ImageTexture::flags;
  179. }
  180. Image::Format ImageTexture::get_format() const {
  181. return format;
  182. }
  183. void ImageTexture::load(const String &p_path) {
  184. Ref<Image> img;
  185. img.instance();
  186. img->load(p_path);
  187. create_from_image(img);
  188. }
  189. void ImageTexture::set_data(const Ref<Image> &p_image) {
  190. VisualServer::get_singleton()->texture_set_data(texture, p_image);
  191. _change_notify();
  192. }
  193. void ImageTexture::_resource_path_changed() {
  194. String path = get_path();
  195. }
  196. Ref<Image> ImageTexture::get_data() const {
  197. return VisualServer::get_singleton()->texture_get_data(texture);
  198. }
  199. int ImageTexture::get_width() const {
  200. return w;
  201. }
  202. int ImageTexture::get_height() const {
  203. return h;
  204. }
  205. RID ImageTexture::get_rid() const {
  206. return texture;
  207. }
  208. bool ImageTexture::has_alpha() const {
  209. return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8);
  210. }
  211. 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 {
  212. if ((w | h) == 0)
  213. return;
  214. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  215. 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);
  216. }
  217. 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 {
  218. if ((w | h) == 0)
  219. return;
  220. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  221. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose, normal_rid);
  222. }
  223. 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 {
  224. if ((w | h) == 0)
  225. return;
  226. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  227. 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);
  228. }
  229. void ImageTexture::set_size_override(const Size2 &p_size) {
  230. Size2 s = p_size;
  231. if (s.x != 0)
  232. w = s.x;
  233. if (s.y != 0)
  234. h = s.y;
  235. VisualServer::get_singleton()->texture_set_size_override(texture, w, h);
  236. }
  237. void ImageTexture::set_path(const String &p_path, bool p_take_over) {
  238. if (texture.is_valid()) {
  239. VisualServer::get_singleton()->texture_set_path(texture, p_path);
  240. }
  241. Resource::set_path(p_path, p_take_over);
  242. }
  243. void ImageTexture::set_storage(Storage p_storage) {
  244. storage = p_storage;
  245. }
  246. ImageTexture::Storage ImageTexture::get_storage() const {
  247. return storage;
  248. }
  249. void ImageTexture::set_lossy_storage_quality(float p_lossy_storage_quality) {
  250. lossy_storage_quality = p_lossy_storage_quality;
  251. }
  252. float ImageTexture::get_lossy_storage_quality() const {
  253. return lossy_storage_quality;
  254. }
  255. void ImageTexture::_set_data(Dictionary p_data) {
  256. Ref<Image> img = p_data["image"];
  257. ERR_FAIL_COND(!img.is_valid());
  258. uint32_t flags = p_data["flags"];
  259. create_from_image(img, flags);
  260. set_storage(Storage(p_data["storage"].operator int()));
  261. set_lossy_storage_quality(p_data["lossy_quality"]);
  262. set_size_override(p_data["size"]);
  263. };
  264. void ImageTexture::_bind_methods() {
  265. ClassDB::bind_method(D_METHOD("create", "width", "height", "format", "flags"), &ImageTexture::create, DEFVAL(FLAGS_DEFAULT));
  266. ClassDB::bind_method(D_METHOD("create_from_image", "image", "flags"), &ImageTexture::create_from_image, DEFVAL(FLAGS_DEFAULT));
  267. ClassDB::bind_method(D_METHOD("get_format"), &ImageTexture::get_format);
  268. ClassDB::bind_method(D_METHOD("load", "path"), &ImageTexture::load);
  269. ClassDB::bind_method(D_METHOD("set_data", "image"), &ImageTexture::set_data);
  270. ClassDB::bind_method(D_METHOD("set_storage", "mode"), &ImageTexture::set_storage);
  271. ClassDB::bind_method(D_METHOD("get_storage"), &ImageTexture::get_storage);
  272. ClassDB::bind_method(D_METHOD("set_lossy_storage_quality", "quality"), &ImageTexture::set_lossy_storage_quality);
  273. ClassDB::bind_method(D_METHOD("get_lossy_storage_quality"), &ImageTexture::get_lossy_storage_quality);
  274. ClassDB::bind_method(D_METHOD("set_size_override", "size"), &ImageTexture::set_size_override);
  275. ClassDB::bind_method(D_METHOD("_reload_hook", "rid"), &ImageTexture::_reload_hook);
  276. BIND_ENUM_CONSTANT(STORAGE_RAW);
  277. BIND_ENUM_CONSTANT(STORAGE_COMPRESS_LOSSY);
  278. BIND_ENUM_CONSTANT(STORAGE_COMPRESS_LOSSLESS);
  279. }
  280. ImageTexture::ImageTexture() {
  281. w = h = 0;
  282. flags = FLAGS_DEFAULT;
  283. texture = VisualServer::get_singleton()->texture_create();
  284. storage = STORAGE_RAW;
  285. lossy_storage_quality = 0.7;
  286. }
  287. ImageTexture::~ImageTexture() {
  288. VisualServer::get_singleton()->free(texture);
  289. }
  290. //////////////////////////////////////////
  291. void StreamTexture::_requested_3d(void *p_ud) {
  292. StreamTexture *st = (StreamTexture *)p_ud;
  293. Ref<StreamTexture> stex(st);
  294. ERR_FAIL_COND(!request_3d_callback);
  295. request_3d_callback(stex);
  296. }
  297. void StreamTexture::_requested_srgb(void *p_ud) {
  298. StreamTexture *st = (StreamTexture *)p_ud;
  299. Ref<StreamTexture> stex(st);
  300. ERR_FAIL_COND(!request_srgb_callback);
  301. request_srgb_callback(stex);
  302. }
  303. void StreamTexture::_requested_normal(void *p_ud) {
  304. StreamTexture *st = (StreamTexture *)p_ud;
  305. Ref<StreamTexture> stex(st);
  306. ERR_FAIL_COND(!request_normal_callback);
  307. request_normal_callback(stex);
  308. }
  309. StreamTexture::TextureFormatRequestCallback StreamTexture::request_3d_callback = NULL;
  310. StreamTexture::TextureFormatRequestCallback StreamTexture::request_srgb_callback = NULL;
  311. StreamTexture::TextureFormatRequestCallback StreamTexture::request_normal_callback = NULL;
  312. uint32_t StreamTexture::get_flags() const {
  313. return flags;
  314. }
  315. Image::Format StreamTexture::get_format() const {
  316. return format;
  317. }
  318. Error StreamTexture::_load_data(const String &p_path, int &tw, int &th, int &flags, Ref<Image> &image, int p_size_limit) {
  319. ERR_FAIL_COND_V(image.is_null(), ERR_INVALID_PARAMETER);
  320. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  321. ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
  322. uint8_t header[4];
  323. f->get_buffer(header, 4);
  324. if (header[0] != 'G' || header[1] != 'D' || header[2] != 'S' || header[3] != 'T') {
  325. memdelete(f);
  326. ERR_FAIL_COND_V(header[0] != 'G' || header[1] != 'D' || header[2] != 'S' || header[3] != 'T', ERR_FILE_CORRUPT);
  327. }
  328. tw = f->get_32();
  329. th = f->get_32();
  330. flags = f->get_32(); //texture flags!
  331. uint32_t df = f->get_32(); //data format
  332. /*
  333. print_line("width: " + itos(tw));
  334. print_line("height: " + itos(th));
  335. print_line("flags: " + itos(flags));
  336. print_line("df: " + itos(df));
  337. */
  338. #ifdef TOOLS_ENABLED
  339. if (request_3d_callback && df & FORMAT_BIT_DETECT_3D) {
  340. //print_line("request detect 3D at " + p_path);
  341. VS::get_singleton()->texture_set_detect_3d_callback(texture, _requested_3d, this);
  342. } else {
  343. //print_line("not requesting detect 3D at " + p_path);
  344. VS::get_singleton()->texture_set_detect_3d_callback(texture, NULL, NULL);
  345. }
  346. if (request_srgb_callback && df & FORMAT_BIT_DETECT_SRGB) {
  347. //print_line("request detect srgb at " + p_path);
  348. VS::get_singleton()->texture_set_detect_srgb_callback(texture, _requested_srgb, this);
  349. } else {
  350. //print_line("not requesting detect srgb at " + p_path);
  351. VS::get_singleton()->texture_set_detect_srgb_callback(texture, NULL, NULL);
  352. }
  353. if (request_srgb_callback && df & FORMAT_BIT_DETECT_NORMAL) {
  354. //print_line("request detect srgb at " + p_path);
  355. VS::get_singleton()->texture_set_detect_normal_callback(texture, _requested_normal, this);
  356. } else {
  357. //print_line("not requesting detect normal at " + p_path);
  358. VS::get_singleton()->texture_set_detect_normal_callback(texture, NULL, NULL);
  359. }
  360. #endif
  361. if (!(df & FORMAT_BIT_STREAM)) {
  362. p_size_limit = 0;
  363. }
  364. if (df & FORMAT_BIT_LOSSLESS || df & FORMAT_BIT_LOSSY) {
  365. //look for a PNG or WEBP file inside
  366. int sw = tw;
  367. int sh = th;
  368. uint32_t mipmaps = f->get_32();
  369. uint32_t size = f->get_32();
  370. //print_line("mipmaps: " + itos(mipmaps));
  371. while (mipmaps > 1 && p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {
  372. f->seek(f->get_position() + size);
  373. mipmaps = f->get_32();
  374. size = f->get_32();
  375. sw = MAX(sw >> 1, 1);
  376. sh = MAX(sh >> 1, 1);
  377. mipmaps--;
  378. }
  379. //mipmaps need to be read independently, they will be later combined
  380. Vector<Ref<Image> > mipmap_images;
  381. int total_size = 0;
  382. for (uint32_t i = 0; i < mipmaps; i++) {
  383. if (i) {
  384. size = f->get_32();
  385. }
  386. PoolVector<uint8_t> pv;
  387. pv.resize(size);
  388. {
  389. PoolVector<uint8_t>::Write w = pv.write();
  390. f->get_buffer(w.ptr(), size);
  391. }
  392. Ref<Image> img;
  393. if (df & FORMAT_BIT_LOSSLESS) {
  394. img = Image::lossless_unpacker(pv);
  395. } else {
  396. img = Image::lossy_unpacker(pv);
  397. }
  398. if (img.is_null() || img->empty()) {
  399. memdelete(f);
  400. ERR_FAIL_COND_V(img.is_null() || img->empty(), ERR_FILE_CORRUPT);
  401. }
  402. total_size += img->get_data().size();
  403. mipmap_images.push_back(img);
  404. }
  405. //print_line("mipmap read total: " + itos(mipmap_images.size()));
  406. memdelete(f); //no longer needed
  407. if (mipmap_images.size() == 1) {
  408. image = mipmap_images[0];
  409. return OK;
  410. } else {
  411. PoolVector<uint8_t> img_data;
  412. img_data.resize(total_size);
  413. {
  414. PoolVector<uint8_t>::Write w = img_data.write();
  415. int ofs = 0;
  416. for (int i = 0; i < mipmap_images.size(); i++) {
  417. PoolVector<uint8_t> id = mipmap_images[i]->get_data();
  418. int len = id.size();
  419. PoolVector<uint8_t>::Read r = id.read();
  420. copymem(&w[ofs], r.ptr(), len);
  421. ofs += len;
  422. }
  423. }
  424. image->create(sw, sh, true, mipmap_images[0]->get_format(), img_data);
  425. return OK;
  426. }
  427. } else {
  428. //look for regular format
  429. Image::Format format = (Image::Format)(df & FORMAT_MASK_IMAGE_FORMAT);
  430. bool mipmaps = df & FORMAT_BIT_HAS_MIPMAPS;
  431. if (!mipmaps) {
  432. int size = Image::get_image_data_size(tw, th, format, 0);
  433. PoolVector<uint8_t> img_data;
  434. img_data.resize(size);
  435. {
  436. PoolVector<uint8_t>::Write w = img_data.write();
  437. f->get_buffer(w.ptr(), size);
  438. }
  439. memdelete(f);
  440. image->create(tw, th, false, format, img_data);
  441. return OK;
  442. } else {
  443. int sw = tw;
  444. int sh = th;
  445. int mipmaps = Image::get_image_required_mipmaps(tw, th, format);
  446. int total_size = Image::get_image_data_size(tw, th, format, mipmaps);
  447. int idx = 0;
  448. int ofs = 0;
  449. while (mipmaps > 1 && p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {
  450. sw = MAX(sw >> 1, 1);
  451. sh = MAX(sh >> 1, 1);
  452. mipmaps--;
  453. idx++;
  454. }
  455. if (idx > 0) {
  456. ofs = Image::get_image_data_size(tw, th, format, idx - 1);
  457. }
  458. if (total_size - ofs <= 0) {
  459. memdelete(f);
  460. ERR_FAIL_V(ERR_FILE_CORRUPT);
  461. }
  462. f->seek(f->get_position() + ofs);
  463. PoolVector<uint8_t> img_data;
  464. img_data.resize(total_size - ofs);
  465. {
  466. PoolVector<uint8_t>::Write w = img_data.write();
  467. int bytes = f->get_buffer(w.ptr(), total_size - ofs);
  468. //print_line("requested read: " + itos(total_size - ofs) + " but got: " + itos(bytes));
  469. memdelete(f);
  470. if (bytes != total_size - ofs) {
  471. ERR_FAIL_V(ERR_FILE_CORRUPT);
  472. }
  473. }
  474. image->create(sw, sh, true, format, img_data);
  475. return OK;
  476. }
  477. }
  478. return ERR_BUG; //unreachable
  479. }
  480. Error StreamTexture::load(const String &p_path) {
  481. int lw, lh, lflags;
  482. Ref<Image> image;
  483. image.instance();
  484. Error err = _load_data(p_path, lw, lh, lflags, image);
  485. if (err)
  486. return err;
  487. VS::get_singleton()->texture_allocate(texture, image->get_width(), image->get_height(), image->get_format(), lflags);
  488. VS::get_singleton()->texture_set_data(texture, image);
  489. w = lw;
  490. h = lh;
  491. flags = lflags;
  492. path_to_file = p_path;
  493. format = image->get_format();
  494. return OK;
  495. }
  496. String StreamTexture::get_load_path() const {
  497. return path_to_file;
  498. }
  499. int StreamTexture::get_width() const {
  500. return w;
  501. }
  502. int StreamTexture::get_height() const {
  503. return h;
  504. }
  505. RID StreamTexture::get_rid() const {
  506. return texture;
  507. }
  508. 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 {
  509. if ((w | h) == 0)
  510. return;
  511. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  512. 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);
  513. }
  514. 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 {
  515. if ((w | h) == 0)
  516. return;
  517. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  518. VisualServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose, normal_rid);
  519. }
  520. 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 {
  521. if ((w | h) == 0)
  522. return;
  523. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  524. 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);
  525. }
  526. bool StreamTexture::has_alpha() const {
  527. return false;
  528. }
  529. Ref<Image> StreamTexture::get_data() const {
  530. return VS::get_singleton()->texture_get_data(texture);
  531. }
  532. void StreamTexture::set_flags(uint32_t p_flags) {
  533. }
  534. void StreamTexture::reload_from_file() {
  535. String path = get_path();
  536. if (!path.is_resource_file())
  537. return;
  538. path = ResourceLoader::path_remap(path); //remap for translation
  539. path = ResourceLoader::import_remap(path); //remap for import
  540. if (!path.is_resource_file())
  541. return;
  542. load(path);
  543. }
  544. void StreamTexture::_bind_methods() {
  545. ClassDB::bind_method(D_METHOD("load", "path"), &StreamTexture::load);
  546. ClassDB::bind_method(D_METHOD("get_load_path"), &StreamTexture::get_load_path);
  547. ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.stex"), "load", "get_load_path");
  548. }
  549. StreamTexture::StreamTexture() {
  550. format = Image::FORMAT_MAX;
  551. flags = 0;
  552. w = 0;
  553. h = 0;
  554. texture = VS::get_singleton()->texture_create();
  555. }
  556. StreamTexture::~StreamTexture() {
  557. VS::get_singleton()->free(texture);
  558. }
  559. RES ResourceFormatLoaderStreamTexture::load(const String &p_path, const String &p_original_path, Error *r_error) {
  560. Ref<StreamTexture> st;
  561. st.instance();
  562. Error err = st->load(p_path);
  563. if (r_error)
  564. *r_error = err;
  565. if (err != OK)
  566. return RES();
  567. return st;
  568. }
  569. void ResourceFormatLoaderStreamTexture::get_recognized_extensions(List<String> *p_extensions) const {
  570. p_extensions->push_back("stex");
  571. }
  572. bool ResourceFormatLoaderStreamTexture::handles_type(const String &p_type) const {
  573. return p_type == "StreamTexture";
  574. }
  575. String ResourceFormatLoaderStreamTexture::get_resource_type(const String &p_path) const {
  576. if (p_path.get_extension().to_lower() == "stex")
  577. return "StreamTexture";
  578. return "";
  579. }
  580. //////////////////////////////////////////
  581. int AtlasTexture::get_width() const {
  582. if (region.size.width == 0) {
  583. if (atlas.is_valid())
  584. return atlas->get_width();
  585. return 1;
  586. } else {
  587. return region.size.width + margin.size.width;
  588. }
  589. }
  590. int AtlasTexture::get_height() const {
  591. if (region.size.height == 0) {
  592. if (atlas.is_valid())
  593. return atlas->get_height();
  594. return 1;
  595. } else {
  596. return region.size.height + margin.size.height;
  597. }
  598. }
  599. RID AtlasTexture::get_rid() const {
  600. if (atlas.is_valid())
  601. return atlas->get_rid();
  602. return RID();
  603. }
  604. bool AtlasTexture::has_alpha() const {
  605. if (atlas.is_valid())
  606. return atlas->has_alpha();
  607. return false;
  608. }
  609. void AtlasTexture::set_flags(uint32_t p_flags) {
  610. if (atlas.is_valid())
  611. atlas->set_flags(p_flags);
  612. }
  613. uint32_t AtlasTexture::get_flags() const {
  614. if (atlas.is_valid())
  615. return atlas->get_flags();
  616. return 0;
  617. }
  618. void AtlasTexture::set_atlas(const Ref<Texture> &p_atlas) {
  619. if (atlas == p_atlas)
  620. return;
  621. atlas = p_atlas;
  622. emit_changed();
  623. _change_notify("atlas");
  624. }
  625. Ref<Texture> AtlasTexture::get_atlas() const {
  626. return atlas;
  627. }
  628. void AtlasTexture::set_region(const Rect2 &p_region) {
  629. if (region == p_region)
  630. return;
  631. region = p_region;
  632. emit_changed();
  633. _change_notify("region");
  634. }
  635. Rect2 AtlasTexture::get_region() const {
  636. return region;
  637. }
  638. void AtlasTexture::set_margin(const Rect2 &p_margin) {
  639. if (margin == p_margin)
  640. return;
  641. margin = p_margin;
  642. emit_changed();
  643. _change_notify("margin");
  644. }
  645. Rect2 AtlasTexture::get_margin() const {
  646. return margin;
  647. }
  648. void AtlasTexture::set_filter_clip(const bool p_enable) {
  649. filter_clip = p_enable;
  650. emit_changed();
  651. _change_notify("filter_clip");
  652. }
  653. bool AtlasTexture::has_filter_clip() const {
  654. return filter_clip;
  655. }
  656. void AtlasTexture::_bind_methods() {
  657. ClassDB::bind_method(D_METHOD("set_atlas", "atlas"), &AtlasTexture::set_atlas);
  658. ClassDB::bind_method(D_METHOD("get_atlas"), &AtlasTexture::get_atlas);
  659. ClassDB::bind_method(D_METHOD("set_region", "region"), &AtlasTexture::set_region);
  660. ClassDB::bind_method(D_METHOD("get_region"), &AtlasTexture::get_region);
  661. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &AtlasTexture::set_margin);
  662. ClassDB::bind_method(D_METHOD("get_margin"), &AtlasTexture::get_margin);
  663. ClassDB::bind_method(D_METHOD("set_filter_clip", "enable"), &AtlasTexture::set_filter_clip);
  664. ClassDB::bind_method(D_METHOD("has_filter_clip"), &AtlasTexture::has_filter_clip);
  665. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "atlas", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_atlas", "get_atlas");
  666. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region"), "set_region", "get_region");
  667. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "margin"), "set_margin", "get_margin");
  668. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_clip"), "set_filter_clip", "has_filter_clip");
  669. }
  670. 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 {
  671. Rect2 rc = region;
  672. if (!atlas.is_valid())
  673. return;
  674. if (rc.size.width == 0) {
  675. rc.size.width = atlas->get_width();
  676. }
  677. if (rc.size.height == 0) {
  678. rc.size.height = atlas->get_height();
  679. }
  680. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  681. 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);
  682. }
  683. 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 {
  684. Rect2 rc = region;
  685. if (!atlas.is_valid())
  686. return;
  687. if (rc.size.width == 0) {
  688. rc.size.width = atlas->get_width();
  689. }
  690. if (rc.size.height == 0) {
  691. rc.size.height = atlas->get_height();
  692. }
  693. Vector2 scale = p_rect.size / (region.size + margin.size);
  694. Rect2 dr(p_rect.position + margin.position * scale, rc.size * scale);
  695. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  696. 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);
  697. }
  698. 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 {
  699. //this might not necessarily work well if using a rect, needs to be fixed properly
  700. Rect2 rc = region;
  701. if (!atlas.is_valid())
  702. return;
  703. Rect2 src = p_src_rect;
  704. src.position += (rc.position - margin.position);
  705. Rect2 src_c = rc.clip(src);
  706. if (src_c.size == Size2())
  707. return;
  708. Vector2 ofs = (src_c.position - src.position);
  709. Vector2 scale = p_rect.size / p_src_rect.size;
  710. if (scale.x < 0) {
  711. float mx = (margin.size.width - margin.position.x);
  712. mx -= margin.position.x;
  713. ofs.x = -(ofs.x + mx);
  714. }
  715. if (scale.y < 0) {
  716. float my = margin.size.height - margin.position.y;
  717. my -= margin.position.y;
  718. ofs.y = -(ofs.y + my);
  719. }
  720. Rect2 dr(p_rect.position + ofs * scale, src_c.size * scale);
  721. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  722. 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);
  723. }
  724. bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  725. Rect2 rc = region;
  726. if (!atlas.is_valid())
  727. return false;
  728. Rect2 src = p_src_rect;
  729. src.position += (rc.position - margin.position);
  730. Rect2 src_c = rc.clip(src);
  731. if (src_c.size == Size2())
  732. return false;
  733. Vector2 ofs = (src_c.position - src.position);
  734. Vector2 scale = p_rect.size / p_src_rect.size;
  735. if (scale.x < 0) {
  736. float mx = (margin.size.width - margin.position.x);
  737. mx -= margin.position.x;
  738. ofs.x = -(ofs.x + mx);
  739. }
  740. if (scale.y < 0) {
  741. float my = margin.size.height - margin.position.y;
  742. my -= margin.position.y;
  743. ofs.y = -(ofs.y + my);
  744. }
  745. Rect2 dr(p_rect.position + ofs * scale, src_c.size * scale);
  746. r_rect = dr;
  747. r_src_rect = src_c;
  748. return true;
  749. }
  750. AtlasTexture::AtlasTexture() {
  751. filter_clip = false;
  752. }
  753. //////////////////////////////////////////
  754. int LargeTexture::get_width() const {
  755. return size.width;
  756. }
  757. int LargeTexture::get_height() const {
  758. return size.height;
  759. }
  760. RID LargeTexture::get_rid() const {
  761. return RID();
  762. }
  763. bool LargeTexture::has_alpha() const {
  764. for (int i = 0; i < pieces.size(); i++) {
  765. if (pieces[i].texture->has_alpha())
  766. return true;
  767. }
  768. return false;
  769. }
  770. void LargeTexture::set_flags(uint32_t p_flags) {
  771. for (int i = 0; i < pieces.size(); i++) {
  772. pieces[i].texture->set_flags(p_flags);
  773. }
  774. }
  775. uint32_t LargeTexture::get_flags() const {
  776. if (pieces.size())
  777. return pieces[0].texture->get_flags();
  778. return 0;
  779. }
  780. int LargeTexture::add_piece(const Point2 &p_offset, const Ref<Texture> &p_texture) {
  781. ERR_FAIL_COND_V(p_texture.is_null(), -1);
  782. Piece p;
  783. p.offset = p_offset;
  784. p.texture = p_texture;
  785. pieces.push_back(p);
  786. return pieces.size() - 1;
  787. }
  788. void LargeTexture::set_piece_offset(int p_idx, const Point2 &p_offset) {
  789. ERR_FAIL_INDEX(p_idx, pieces.size());
  790. pieces[p_idx].offset = p_offset;
  791. };
  792. void LargeTexture::set_piece_texture(int p_idx, const Ref<Texture> &p_texture) {
  793. ERR_FAIL_INDEX(p_idx, pieces.size());
  794. pieces[p_idx].texture = p_texture;
  795. };
  796. void LargeTexture::set_size(const Size2 &p_size) {
  797. size = p_size;
  798. }
  799. void LargeTexture::clear() {
  800. pieces.clear();
  801. size = Size2i();
  802. }
  803. Array LargeTexture::_get_data() const {
  804. Array arr;
  805. for (int i = 0; i < pieces.size(); i++) {
  806. arr.push_back(pieces[i].offset);
  807. arr.push_back(pieces[i].texture);
  808. }
  809. arr.push_back(Size2(size));
  810. return arr;
  811. }
  812. void LargeTexture::_set_data(const Array &p_array) {
  813. ERR_FAIL_COND(p_array.size() < 1);
  814. ERR_FAIL_COND(!(p_array.size() & 1));
  815. clear();
  816. for (int i = 0; i < p_array.size() - 1; i += 2) {
  817. add_piece(p_array[i], p_array[i + 1]);
  818. }
  819. size = Size2(p_array[p_array.size() - 1]);
  820. }
  821. int LargeTexture::get_piece_count() const {
  822. return pieces.size();
  823. }
  824. Vector2 LargeTexture::get_piece_offset(int p_idx) const {
  825. ERR_FAIL_INDEX_V(p_idx, pieces.size(), Vector2());
  826. return pieces[p_idx].offset;
  827. }
  828. Ref<Texture> LargeTexture::get_piece_texture(int p_idx) const {
  829. ERR_FAIL_INDEX_V(p_idx, pieces.size(), Ref<Texture>());
  830. return pieces[p_idx].texture;
  831. }
  832. void LargeTexture::_bind_methods() {
  833. ClassDB::bind_method(D_METHOD("add_piece", "ofs", "texture"), &LargeTexture::add_piece);
  834. ClassDB::bind_method(D_METHOD("set_piece_offset", "idx", "ofs"), &LargeTexture::set_piece_offset);
  835. ClassDB::bind_method(D_METHOD("set_piece_texture", "idx", "texture"), &LargeTexture::set_piece_texture);
  836. ClassDB::bind_method(D_METHOD("set_size", "size"), &LargeTexture::set_size);
  837. ClassDB::bind_method(D_METHOD("clear"), &LargeTexture::clear);
  838. ClassDB::bind_method(D_METHOD("get_piece_count"), &LargeTexture::get_piece_count);
  839. ClassDB::bind_method(D_METHOD("get_piece_offset", "idx"), &LargeTexture::get_piece_offset);
  840. ClassDB::bind_method(D_METHOD("get_piece_texture", "idx"), &LargeTexture::get_piece_texture);
  841. ClassDB::bind_method(D_METHOD("_set_data", "data"), &LargeTexture::_set_data);
  842. ClassDB::bind_method(D_METHOD("_get_data"), &LargeTexture::_get_data);
  843. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_data", "_get_data");
  844. }
  845. 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 {
  846. for (int i = 0; i < pieces.size(); i++) {
  847. // TODO
  848. pieces[i].texture->draw(p_canvas_item, pieces[i].offset + p_pos, p_modulate, p_transpose, p_normal_map);
  849. }
  850. }
  851. 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 {
  852. //tiling not supported for this
  853. if (size.x == 0 || size.y == 0)
  854. return;
  855. Size2 scale = p_rect.size / size;
  856. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  857. for (int i = 0; i < pieces.size(); i++) {
  858. // TODO
  859. 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);
  860. }
  861. }
  862. 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 {
  863. //tiling not supported for this
  864. if (p_src_rect.size.x == 0 || p_src_rect.size.y == 0)
  865. return;
  866. Size2 scale = p_rect.size / p_src_rect.size;
  867. RID normal_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
  868. for (int i = 0; i < pieces.size(); i++) {
  869. // TODO
  870. Rect2 rect(pieces[i].offset, pieces[i].texture->get_size());
  871. if (!p_src_rect.intersects(rect))
  872. continue;
  873. Rect2 local = p_src_rect.clip(rect);
  874. Rect2 target = local;
  875. target.size *= scale;
  876. target.position = p_rect.position + (p_src_rect.position + rect.position) * scale;
  877. local.position -= rect.position;
  878. pieces[i].texture->draw_rect_region(p_canvas_item, target, local, p_modulate, p_transpose, p_normal_map, false);
  879. }
  880. }
  881. LargeTexture::LargeTexture() {
  882. }
  883. ///////////////////////////////////////////////
  884. void CubeMap::set_flags(uint32_t p_flags) {
  885. flags = p_flags;
  886. if (_is_valid())
  887. VS::get_singleton()->texture_set_flags(cubemap, flags | VS::TEXTURE_FLAG_CUBEMAP);
  888. }
  889. uint32_t CubeMap::get_flags() const {
  890. return flags;
  891. }
  892. void CubeMap::set_side(Side p_side, const Ref<Image> &p_image) {
  893. ERR_FAIL_COND(p_image->empty());
  894. ERR_FAIL_INDEX(p_side, 6);
  895. if (!_is_valid()) {
  896. format = p_image->get_format();
  897. w = p_image->get_width();
  898. h = p_image->get_height();
  899. VS::get_singleton()->texture_allocate(cubemap, w, h, p_image->get_format(), flags | VS::TEXTURE_FLAG_CUBEMAP);
  900. }
  901. VS::get_singleton()->texture_set_data(cubemap, p_image, VS::CubeMapSide(p_side));
  902. valid[p_side] = true;
  903. }
  904. Ref<Image> CubeMap::get_side(Side p_side) const {
  905. if (!valid[p_side])
  906. return Ref<Image>();
  907. return VS::get_singleton()->texture_get_data(cubemap, VS::CubeMapSide(p_side));
  908. }
  909. Image::Format CubeMap::get_format() const {
  910. return format;
  911. }
  912. int CubeMap::get_width() const {
  913. return w;
  914. }
  915. int CubeMap::get_height() const {
  916. return h;
  917. }
  918. RID CubeMap::get_rid() const {
  919. return cubemap;
  920. }
  921. void CubeMap::set_storage(Storage p_storage) {
  922. storage = p_storage;
  923. }
  924. CubeMap::Storage CubeMap::get_storage() const {
  925. return storage;
  926. }
  927. void CubeMap::set_lossy_storage_quality(float p_lossy_storage_quality) {
  928. lossy_storage_quality = p_lossy_storage_quality;
  929. }
  930. float CubeMap::get_lossy_storage_quality() const {
  931. return lossy_storage_quality;
  932. }
  933. void CubeMap::set_path(const String &p_path, bool p_take_over) {
  934. if (cubemap.is_valid()) {
  935. VisualServer::get_singleton()->texture_set_path(cubemap, p_path);
  936. }
  937. Resource::set_path(p_path, p_take_over);
  938. }
  939. bool CubeMap::_set(const StringName &p_name, const Variant &p_value) {
  940. if (p_name == "side/left") {
  941. set_side(SIDE_LEFT, p_value);
  942. } else if (p_name == "side/right") {
  943. set_side(SIDE_RIGHT, p_value);
  944. } else if (p_name == "side/bottom") {
  945. set_side(SIDE_BOTTOM, p_value);
  946. } else if (p_name == "side/top") {
  947. set_side(SIDE_TOP, p_value);
  948. } else if (p_name == "side/front") {
  949. set_side(SIDE_FRONT, p_value);
  950. } else if (p_name == "side/back") {
  951. set_side(SIDE_BACK, p_value);
  952. } else if (p_name == "flags") {
  953. set_flags(p_value);
  954. } else if (p_name == "storage") {
  955. storage = Storage(p_value.operator int());
  956. } else if (p_name == "lossy_quality") {
  957. lossy_storage_quality = p_value;
  958. } else
  959. return false;
  960. return true;
  961. }
  962. bool CubeMap::_get(const StringName &p_name, Variant &r_ret) const {
  963. if (p_name == "side/left") {
  964. r_ret = get_side(SIDE_LEFT);
  965. } else if (p_name == "side/right") {
  966. r_ret = get_side(SIDE_RIGHT);
  967. } else if (p_name == "side/bottom") {
  968. r_ret = get_side(SIDE_BOTTOM);
  969. } else if (p_name == "side/top") {
  970. r_ret = get_side(SIDE_TOP);
  971. } else if (p_name == "side/front") {
  972. r_ret = get_side(SIDE_FRONT);
  973. } else if (p_name == "side/back") {
  974. r_ret = get_side(SIDE_BACK);
  975. } else if (p_name == "flags") {
  976. r_ret = flags;
  977. } else if (p_name == "storage") {
  978. r_ret = storage;
  979. } else if (p_name == "lossy_quality") {
  980. r_ret = lossy_storage_quality;
  981. } else
  982. return false;
  983. return true;
  984. }
  985. void CubeMap::_get_property_list(List<PropertyInfo> *p_list) const {
  986. PropertyHint img_hint = PROPERTY_HINT_NONE;
  987. if (storage == STORAGE_COMPRESS_LOSSY) {
  988. img_hint = PROPERTY_HINT_IMAGE_COMPRESS_LOSSY;
  989. } else if (storage == STORAGE_COMPRESS_LOSSLESS) {
  990. img_hint = PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS;
  991. }
  992. p_list->push_back(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter"));
  993. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/left", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  994. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/right", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  995. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/bottom", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  996. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/top", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  997. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/front", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  998. p_list->push_back(PropertyInfo(Variant::OBJECT, "side/back", PROPERTY_HINT_RESOURCE_TYPE, "Image"));
  999. }
  1000. void CubeMap::_bind_methods() {
  1001. ClassDB::bind_method(D_METHOD("get_width"), &CubeMap::get_width);
  1002. ClassDB::bind_method(D_METHOD("get_height"), &CubeMap::get_height);
  1003. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &CubeMap::set_flags);
  1004. ClassDB::bind_method(D_METHOD("get_flags"), &CubeMap::get_flags);
  1005. ClassDB::bind_method(D_METHOD("set_side", "side", "image"), &CubeMap::set_side);
  1006. ClassDB::bind_method(D_METHOD("get_side", "side"), &CubeMap::get_side);
  1007. ClassDB::bind_method(D_METHOD("set_storage", "mode"), &CubeMap::set_storage);
  1008. ClassDB::bind_method(D_METHOD("get_storage"), &CubeMap::get_storage);
  1009. ClassDB::bind_method(D_METHOD("set_lossy_storage_quality", "quality"), &CubeMap::set_lossy_storage_quality);
  1010. ClassDB::bind_method(D_METHOD("get_lossy_storage_quality"), &CubeMap::get_lossy_storage_quality);
  1011. ADD_PROPERTY(PropertyInfo(Variant::INT, "storage_mode", PROPERTY_HINT_ENUM, "Raw,Lossy Compressed,Lossless Compressed"), "set_storage", "get_storage");
  1012. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lossy_storage_quality"), "set_lossy_storage_quality", "get_lossy_storage_quality");
  1013. BIND_ENUM_CONSTANT(STORAGE_RAW);
  1014. BIND_ENUM_CONSTANT(STORAGE_COMPRESS_LOSSY);
  1015. BIND_ENUM_CONSTANT(STORAGE_COMPRESS_LOSSLESS);
  1016. BIND_ENUM_CONSTANT(SIDE_LEFT);
  1017. BIND_ENUM_CONSTANT(SIDE_RIGHT);
  1018. BIND_ENUM_CONSTANT(SIDE_BOTTOM);
  1019. BIND_ENUM_CONSTANT(SIDE_TOP);
  1020. BIND_ENUM_CONSTANT(SIDE_FRONT);
  1021. BIND_ENUM_CONSTANT(SIDE_BACK);
  1022. BIND_ENUM_CONSTANT(FLAG_MIPMAPS);
  1023. BIND_ENUM_CONSTANT(FLAG_REPEAT);
  1024. BIND_ENUM_CONSTANT(FLAG_FILTER);
  1025. BIND_ENUM_CONSTANT(FLAGS_DEFAULT);
  1026. }
  1027. CubeMap::CubeMap() {
  1028. w = h = 0;
  1029. flags = FLAGS_DEFAULT;
  1030. for (int i = 0; i < 6; i++)
  1031. valid[i] = false;
  1032. cubemap = VisualServer::get_singleton()->texture_create();
  1033. storage = STORAGE_RAW;
  1034. lossy_storage_quality = 0.7;
  1035. }
  1036. CubeMap::~CubeMap() {
  1037. VisualServer::get_singleton()->free(cubemap);
  1038. }
  1039. /* BIND_ENUM(CubeMapSize);
  1040. BIND_ENUM_CONSTANT( FLAG_CUBEMAP );
  1041. BIND_ENUM_CONSTANT( CUBEMAP_LEFT );
  1042. BIND_ENUM_CONSTANT( CUBEMAP_RIGHT );
  1043. BIND_ENUM_CONSTANT( CUBEMAP_BOTTOM );
  1044. BIND_ENUM_CONSTANT( CUBEMAP_TOP );
  1045. BIND_ENUM_CONSTANT( CUBEMAP_FRONT );
  1046. BIND_ENUM_CONSTANT( CUBEMAP_BACK );
  1047. */
  1048. ///////////////////////////
  1049. void CurveTexture::_bind_methods() {
  1050. ClassDB::bind_method(D_METHOD("set_width", "width"), &CurveTexture::set_width);
  1051. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &CurveTexture::set_curve);
  1052. ClassDB::bind_method(D_METHOD("get_curve"), &CurveTexture::get_curve);
  1053. ClassDB::bind_method(D_METHOD("_update"), &CurveTexture::_update);
  1054. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "32,4096"), "set_width", "get_width");
  1055. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  1056. }
  1057. void CurveTexture::set_width(int p_width) {
  1058. ERR_FAIL_COND(p_width < 32 || p_width > 4096);
  1059. _width = p_width;
  1060. _update();
  1061. }
  1062. int CurveTexture::get_width() const {
  1063. return _width;
  1064. }
  1065. void CurveTexture::ensure_default_setup(float p_min, float p_max) {
  1066. if (_curve.is_null()) {
  1067. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  1068. curve->add_point(Vector2(0, 1));
  1069. curve->add_point(Vector2(1, 1));
  1070. curve->set_min_value(p_min);
  1071. curve->set_max_value(p_max);
  1072. set_curve(curve);
  1073. // Min and max is 0..1 by default
  1074. }
  1075. }
  1076. void CurveTexture::set_curve(Ref<Curve> p_curve) {
  1077. if (_curve != p_curve) {
  1078. if (_curve.is_valid()) {
  1079. _curve->disconnect(CoreStringNames::get_singleton()->changed, this, "_update");
  1080. }
  1081. _curve = p_curve;
  1082. if (_curve.is_valid()) {
  1083. _curve->connect(CoreStringNames::get_singleton()->changed, this, "_update");
  1084. }
  1085. _update();
  1086. }
  1087. }
  1088. void CurveTexture::_update() {
  1089. PoolVector<uint8_t> data;
  1090. data.resize(_width * sizeof(float));
  1091. // The array is locked in that scope
  1092. {
  1093. PoolVector<uint8_t>::Write wd8 = data.write();
  1094. float *wd = (float *)wd8.ptr();
  1095. if (_curve.is_valid()) {
  1096. Curve &curve = **_curve;
  1097. for (int i = 0; i < _width; ++i) {
  1098. float t = i / static_cast<float>(_width);
  1099. wd[i] = curve.interpolate_baked(t);
  1100. }
  1101. } else {
  1102. for (int i = 0; i < _width; ++i) {
  1103. wd[i] = 0;
  1104. }
  1105. }
  1106. }
  1107. Ref<Image> image = memnew(Image(_width, 1, false, Image::FORMAT_RF, data));
  1108. VS::get_singleton()->texture_allocate(_texture, _width, 1, Image::FORMAT_RF, VS::TEXTURE_FLAG_FILTER);
  1109. VS::get_singleton()->texture_set_data(_texture, image);
  1110. emit_changed();
  1111. }
  1112. Ref<Curve> CurveTexture::get_curve() const {
  1113. return _curve;
  1114. }
  1115. RID CurveTexture::get_rid() const {
  1116. return _texture;
  1117. }
  1118. CurveTexture::CurveTexture() {
  1119. _width = 2048;
  1120. _texture = VS::get_singleton()->texture_create();
  1121. }
  1122. CurveTexture::~CurveTexture() {
  1123. VS::get_singleton()->free(_texture);
  1124. }
  1125. //////////////////
  1126. //setter and getter names for property serialization
  1127. #define COLOR_RAMP_GET_OFFSETS "get_offsets"
  1128. #define COLOR_RAMP_GET_COLORS "get_colors"
  1129. #define COLOR_RAMP_SET_OFFSETS "set_offsets"
  1130. #define COLOR_RAMP_SET_COLORS "set_colors"
  1131. GradientTexture::GradientTexture() {
  1132. update_pending = false;
  1133. width = 2048;
  1134. texture = VS::get_singleton()->texture_create();
  1135. _queue_update();
  1136. }
  1137. GradientTexture::~GradientTexture() {
  1138. VS::get_singleton()->free(texture);
  1139. }
  1140. void GradientTexture::_bind_methods() {
  1141. ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture::set_gradient);
  1142. ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture::get_gradient);
  1143. ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture::set_width);
  1144. ClassDB::bind_method(D_METHOD("_update"), &GradientTexture::_update);
  1145. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_gradient", "get_gradient");
  1146. ADD_PROPERTY(PropertyInfo(Variant::INT, "width"), "set_width", "get_width");
  1147. }
  1148. void GradientTexture::set_gradient(Ref<Gradient> p_gradient) {
  1149. if (p_gradient == gradient)
  1150. return;
  1151. if (gradient.is_valid()) {
  1152. gradient->disconnect(CoreStringNames::get_singleton()->changed, this, "_update");
  1153. }
  1154. gradient = p_gradient;
  1155. if (gradient.is_valid()) {
  1156. gradient->connect(CoreStringNames::get_singleton()->changed, this, "_update");
  1157. }
  1158. _update();
  1159. emit_changed();
  1160. }
  1161. Ref<Gradient> GradientTexture::get_gradient() const {
  1162. return gradient;
  1163. }
  1164. void GradientTexture::_queue_update() {
  1165. if (update_pending)
  1166. return;
  1167. call_deferred("_update");
  1168. }
  1169. void GradientTexture::_update() {
  1170. if (gradient.is_null())
  1171. return;
  1172. update_pending = false;
  1173. PoolVector<uint8_t> data;
  1174. data.resize(width * 4);
  1175. {
  1176. PoolVector<uint8_t>::Write wd8 = data.write();
  1177. Gradient &g = **gradient;
  1178. for (int i = 0; i < width; i++) {
  1179. float ofs = float(i) / (width - 1);
  1180. Color color = g.get_color_at_offset(ofs);
  1181. wd8[i * 4 + 0] = uint8_t(CLAMP(color.r * 255.0, 0, 255));
  1182. wd8[i * 4 + 1] = uint8_t(CLAMP(color.g * 255.0, 0, 255));
  1183. wd8[i * 4 + 2] = uint8_t(CLAMP(color.b * 255.0, 0, 255));
  1184. wd8[i * 4 + 3] = uint8_t(CLAMP(color.a * 255.0, 0, 255));
  1185. }
  1186. }
  1187. Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data));
  1188. VS::get_singleton()->texture_allocate(texture, width, 1, Image::FORMAT_RGBA8, VS::TEXTURE_FLAG_FILTER);
  1189. VS::get_singleton()->texture_set_data(texture, image);
  1190. emit_changed();
  1191. }
  1192. void GradientTexture::set_width(int p_width) {
  1193. width = p_width;
  1194. _queue_update();
  1195. }
  1196. int GradientTexture::get_width() const {
  1197. return width;
  1198. }
  1199. Ref<Image> GradientTexture::get_data() const {
  1200. return VisualServer::get_singleton()->texture_get_data(texture);
  1201. }