texture.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*************************************************************************/
  2. /* texture.h */
  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. #ifndef TEXTURE_H
  31. #define TEXTURE_H
  32. #include "core/io/resource_loader.h"
  33. #include "core/math/rect2.h"
  34. #include "core/os/mutex.h"
  35. #include "core/os/rw_lock.h"
  36. #include "core/os/thread_safe.h"
  37. #include "core/resource.h"
  38. #include "scene/resources/curve.h"
  39. #include "scene/resources/gradient.h"
  40. #include "servers/camera_server.h"
  41. #include "servers/visual_server.h"
  42. class Texture : public Resource {
  43. GDCLASS(Texture, Resource);
  44. OBJ_SAVE_TYPE(Texture); // Saves derived classes with common type so they can be interchanged.
  45. protected:
  46. static void _bind_methods();
  47. public:
  48. enum Flags {
  49. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  50. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  51. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  52. FLAG_ANISOTROPIC_FILTER = VisualServer::TEXTURE_FLAG_ANISOTROPIC_FILTER,
  53. FLAG_CONVERT_TO_LINEAR = VisualServer::TEXTURE_FLAG_CONVERT_TO_LINEAR,
  54. FLAG_VIDEO_SURFACE = VisualServer::TEXTURE_FLAG_USED_FOR_STREAMING,
  55. FLAGS_DEFAULT = FLAG_MIPMAPS | FLAG_REPEAT | FLAG_FILTER,
  56. FLAG_MIRRORED_REPEAT = VisualServer::TEXTURE_FLAG_MIRRORED_REPEAT
  57. };
  58. virtual int get_width() const = 0;
  59. virtual int get_height() const = 0;
  60. virtual Size2 get_size() const;
  61. virtual RID get_rid() const = 0;
  62. virtual bool is_pixel_opaque(int p_x, int p_y) const;
  63. virtual bool has_alpha() const = 0;
  64. virtual void set_flags(uint32_t p_flags) = 0;
  65. virtual uint32_t get_flags() const = 0;
  66. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  67. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  68. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  69. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  70. virtual Ref<Image> get_data() const { return Ref<Image>(); }
  71. Texture();
  72. };
  73. VARIANT_ENUM_CAST(Texture::Flags);
  74. class BitMap;
  75. class ImageTexture : public Texture {
  76. GDCLASS(ImageTexture, Texture);
  77. RES_BASE_EXTENSION("tex");
  78. public:
  79. enum Storage {
  80. STORAGE_RAW,
  81. STORAGE_COMPRESS_LOSSY,
  82. STORAGE_COMPRESS_LOSSLESS
  83. };
  84. private:
  85. RID texture;
  86. Image::Format format;
  87. uint32_t flags;
  88. int w, h;
  89. Storage storage;
  90. Size2 size_override;
  91. float lossy_storage_quality;
  92. mutable Ref<BitMap> alpha_cache;
  93. bool image_stored;
  94. protected:
  95. virtual void reload_from_file();
  96. bool _set(const StringName &p_name, const Variant &p_value);
  97. bool _get(const StringName &p_name, Variant &r_ret) const;
  98. void _get_property_list(List<PropertyInfo> *p_list) const;
  99. void _reload_hook(const RID &p_hook);
  100. virtual void _resource_path_changed();
  101. static void _bind_methods();
  102. void _set_data(Dictionary p_data);
  103. public:
  104. void create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT);
  105. void create_from_image(const Ref<Image> &p_image, uint32_t p_flags = FLAGS_DEFAULT);
  106. void set_flags(uint32_t p_flags);
  107. uint32_t get_flags() const;
  108. Image::Format get_format() const;
  109. #ifndef DISABLE_DEPRECATED
  110. Error load(const String &p_path);
  111. #endif
  112. void set_data(const Ref<Image> &p_image);
  113. Ref<Image> get_data() const;
  114. int get_width() const;
  115. int get_height() const;
  116. virtual RID get_rid() const;
  117. bool has_alpha() const;
  118. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  119. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  120. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  121. void set_storage(Storage p_storage);
  122. Storage get_storage() const;
  123. bool is_pixel_opaque(int p_x, int p_y) const;
  124. void set_lossy_storage_quality(float p_lossy_storage_quality);
  125. float get_lossy_storage_quality() const;
  126. void set_size_override(const Size2 &p_size);
  127. virtual void set_path(const String &p_path, bool p_take_over = false);
  128. ImageTexture();
  129. ~ImageTexture();
  130. };
  131. class StreamTexture : public Texture {
  132. GDCLASS(StreamTexture, Texture);
  133. public:
  134. enum DataFormat {
  135. DATA_FORMAT_IMAGE,
  136. DATA_FORMAT_LOSSLESS,
  137. DATA_FORMAT_LOSSY
  138. };
  139. enum FormatBits {
  140. FORMAT_MASK_IMAGE_FORMAT = (1 << 20) - 1,
  141. FORMAT_BIT_LOSSLESS = 1 << 20,
  142. FORMAT_BIT_LOSSY = 1 << 21,
  143. FORMAT_BIT_STREAM = 1 << 22,
  144. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  145. FORMAT_BIT_DETECT_3D = 1 << 24,
  146. FORMAT_BIT_DETECT_SRGB = 1 << 25,
  147. FORMAT_BIT_DETECT_NORMAL = 1 << 26,
  148. };
  149. private:
  150. Error _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 = 0);
  151. String path_to_file;
  152. RID texture;
  153. Image::Format format;
  154. uint32_t flags;
  155. int w, h;
  156. mutable Ref<BitMap> alpha_cache;
  157. virtual void reload_from_file();
  158. static void _requested_3d(void *p_ud);
  159. static void _requested_srgb(void *p_ud);
  160. static void _requested_normal(void *p_ud);
  161. protected:
  162. static void _bind_methods();
  163. void _validate_property(PropertyInfo &property) const;
  164. public:
  165. typedef void (*TextureFormatRequestCallback)(const Ref<StreamTexture> &);
  166. static TextureFormatRequestCallback request_3d_callback;
  167. static TextureFormatRequestCallback request_srgb_callback;
  168. static TextureFormatRequestCallback request_normal_callback;
  169. uint32_t get_flags() const;
  170. Image::Format get_format() const;
  171. Error load(const String &p_path);
  172. String get_load_path() const;
  173. int get_width() const;
  174. int get_height() const;
  175. virtual RID get_rid() const;
  176. virtual void set_path(const String &p_path, bool p_take_over);
  177. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  178. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  179. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  180. virtual bool has_alpha() const;
  181. virtual void set_flags(uint32_t p_flags);
  182. bool is_pixel_opaque(int p_x, int p_y) const;
  183. virtual Ref<Image> get_data() const;
  184. StreamTexture();
  185. ~StreamTexture();
  186. };
  187. class ResourceFormatLoaderStreamTexture : public ResourceFormatLoader {
  188. public:
  189. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  190. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  191. virtual bool handles_type(const String &p_type) const;
  192. virtual String get_resource_type(const String &p_path) const;
  193. };
  194. VARIANT_ENUM_CAST(ImageTexture::Storage);
  195. class AtlasTexture : public Texture {
  196. GDCLASS(AtlasTexture, Texture);
  197. RES_BASE_EXTENSION("atlastex");
  198. protected:
  199. Ref<Texture> atlas;
  200. Rect2 region;
  201. Rect2 margin;
  202. bool filter_clip;
  203. static void _bind_methods();
  204. public:
  205. virtual int get_width() const;
  206. virtual int get_height() const;
  207. virtual RID get_rid() const;
  208. virtual bool has_alpha() const;
  209. virtual void set_flags(uint32_t p_flags);
  210. virtual uint32_t get_flags() const;
  211. void set_atlas(const Ref<Texture> &p_atlas);
  212. Ref<Texture> get_atlas() const;
  213. void set_region(const Rect2 &p_region);
  214. Rect2 get_region() const;
  215. void set_margin(const Rect2 &p_margin);
  216. Rect2 get_margin() const;
  217. void set_filter_clip(const bool p_enable);
  218. bool has_filter_clip() const;
  219. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  220. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  221. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  222. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  223. bool is_pixel_opaque(int p_x, int p_y) const;
  224. AtlasTexture();
  225. };
  226. class Mesh;
  227. class MeshTexture : public Texture {
  228. GDCLASS(MeshTexture, Texture);
  229. RES_BASE_EXTENSION("meshtex");
  230. Ref<Texture> base_texture;
  231. Ref<Mesh> mesh;
  232. Size2i size;
  233. protected:
  234. static void _bind_methods();
  235. public:
  236. virtual int get_width() const;
  237. virtual int get_height() const;
  238. virtual RID get_rid() const;
  239. virtual bool has_alpha() const;
  240. virtual void set_flags(uint32_t p_flags);
  241. virtual uint32_t get_flags() const;
  242. void set_mesh(const Ref<Mesh> &p_mesh);
  243. Ref<Mesh> get_mesh() const;
  244. void set_image_size(const Size2 &p_size);
  245. Size2 get_image_size() const;
  246. void set_base_texture(const Ref<Texture> &p_texture);
  247. Ref<Texture> get_base_texture() const;
  248. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  249. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  250. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  251. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  252. bool is_pixel_opaque(int p_x, int p_y) const;
  253. MeshTexture();
  254. };
  255. class LargeTexture : public Texture {
  256. GDCLASS(LargeTexture, Texture);
  257. RES_BASE_EXTENSION("largetex");
  258. protected:
  259. struct Piece {
  260. Point2 offset;
  261. Ref<Texture> texture;
  262. };
  263. Vector<Piece> pieces;
  264. Size2i size;
  265. Array _get_data() const;
  266. void _set_data(const Array &p_array);
  267. static void _bind_methods();
  268. public:
  269. virtual int get_width() const;
  270. virtual int get_height() const;
  271. virtual RID get_rid() const;
  272. virtual bool has_alpha() const;
  273. virtual void set_flags(uint32_t p_flags);
  274. virtual uint32_t get_flags() const;
  275. int add_piece(const Point2 &p_offset, const Ref<Texture> &p_texture);
  276. void set_piece_offset(int p_idx, const Point2 &p_offset);
  277. void set_piece_texture(int p_idx, const Ref<Texture> &p_texture);
  278. void set_size(const Size2 &p_size);
  279. void clear();
  280. int get_piece_count() const;
  281. Vector2 get_piece_offset(int p_idx) const;
  282. Ref<Texture> get_piece_texture(int p_idx) const;
  283. Ref<Image> to_image() const;
  284. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  285. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>()) const;
  286. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_clip_uv = true) const;
  287. bool is_pixel_opaque(int p_x, int p_y) const;
  288. LargeTexture();
  289. };
  290. class CubeMap : public Resource {
  291. GDCLASS(CubeMap, Resource);
  292. RES_BASE_EXTENSION("cubemap");
  293. public:
  294. enum Storage {
  295. STORAGE_RAW,
  296. STORAGE_COMPRESS_LOSSY,
  297. STORAGE_COMPRESS_LOSSLESS
  298. };
  299. enum Side {
  300. SIDE_LEFT,
  301. SIDE_RIGHT,
  302. SIDE_BOTTOM,
  303. SIDE_TOP,
  304. SIDE_FRONT,
  305. SIDE_BACK
  306. };
  307. enum Flags {
  308. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  309. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  310. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  311. FLAGS_DEFAULT = FLAG_MIPMAPS | FLAG_REPEAT | FLAG_FILTER,
  312. };
  313. private:
  314. bool valid[6];
  315. RID cubemap;
  316. Image::Format format;
  317. uint32_t flags;
  318. int w, h;
  319. Storage storage;
  320. Size2 size_override;
  321. float lossy_storage_quality;
  322. _FORCE_INLINE_ bool _is_valid() const {
  323. for (int i = 0; i < 6; i++) {
  324. if (valid[i]) return true;
  325. }
  326. return false;
  327. }
  328. protected:
  329. bool _set(const StringName &p_name, const Variant &p_value);
  330. bool _get(const StringName &p_name, Variant &r_ret) const;
  331. void _get_property_list(List<PropertyInfo> *p_list) const;
  332. static void _bind_methods();
  333. public:
  334. void set_flags(uint32_t p_flags);
  335. uint32_t get_flags() const;
  336. void set_side(Side p_side, const Ref<Image> &p_image);
  337. Ref<Image> get_side(Side p_side) const;
  338. Image::Format get_format() const;
  339. int get_width() const;
  340. int get_height() const;
  341. virtual RID get_rid() const;
  342. void set_storage(Storage p_storage);
  343. Storage get_storage() const;
  344. void set_lossy_storage_quality(float p_lossy_storage_quality);
  345. float get_lossy_storage_quality() const;
  346. virtual void set_path(const String &p_path, bool p_take_over = false);
  347. CubeMap();
  348. ~CubeMap();
  349. };
  350. VARIANT_ENUM_CAST(CubeMap::Flags)
  351. VARIANT_ENUM_CAST(CubeMap::Side)
  352. VARIANT_ENUM_CAST(CubeMap::Storage)
  353. class TextureLayered : public Resource {
  354. GDCLASS(TextureLayered, Resource);
  355. public:
  356. enum Flags {
  357. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  358. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  359. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  360. FLAG_CONVERT_TO_LINEAR = VisualServer::TEXTURE_FLAG_CONVERT_TO_LINEAR,
  361. FLAGS_DEFAULT = FLAG_FILTER,
  362. };
  363. private:
  364. bool is_3d;
  365. RID texture;
  366. Image::Format format;
  367. uint32_t flags;
  368. int width;
  369. int height;
  370. int depth;
  371. void _set_data(const Dictionary &p_data);
  372. Dictionary _get_data() const;
  373. protected:
  374. static void _bind_methods();
  375. public:
  376. void set_flags(uint32_t p_flags);
  377. uint32_t get_flags() const;
  378. Image::Format get_format() const;
  379. uint32_t get_width() const;
  380. uint32_t get_height() const;
  381. uint32_t get_depth() const;
  382. void create(uint32_t p_width, uint32_t p_height, uint32_t p_depth, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT);
  383. void set_layer_data(const Ref<Image> &p_image, int p_layer);
  384. Ref<Image> get_layer_data(int p_layer) const;
  385. void set_data_partial(const Ref<Image> &p_image, int p_x_ofs, int p_y_ofs, int p_z, int p_mipmap = 0);
  386. virtual RID get_rid() const;
  387. virtual void set_path(const String &p_path, bool p_take_over = false);
  388. TextureLayered(bool p_3d = false);
  389. ~TextureLayered();
  390. };
  391. VARIANT_ENUM_CAST(TextureLayered::Flags)
  392. class Texture3D : public TextureLayered {
  393. GDCLASS(Texture3D, TextureLayered);
  394. public:
  395. Texture3D() :
  396. TextureLayered(true) {}
  397. };
  398. class TextureArray : public TextureLayered {
  399. GDCLASS(TextureArray, TextureLayered);
  400. public:
  401. TextureArray() :
  402. TextureLayered(false) {}
  403. };
  404. class ResourceFormatLoaderTextureLayered : public ResourceFormatLoader {
  405. public:
  406. enum Compression {
  407. COMPRESSION_LOSSLESS,
  408. COMPRESSION_VRAM,
  409. COMPRESSION_UNCOMPRESSED
  410. };
  411. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  412. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  413. virtual bool handles_type(const String &p_type) const;
  414. virtual String get_resource_type(const String &p_path) const;
  415. };
  416. class CurveTexture : public Texture {
  417. GDCLASS(CurveTexture, Texture);
  418. RES_BASE_EXTENSION("curvetex")
  419. private:
  420. RID _texture;
  421. Ref<Curve> _curve;
  422. int _width;
  423. void _update();
  424. protected:
  425. static void _bind_methods();
  426. public:
  427. void set_width(int p_width);
  428. int get_width() const;
  429. void ensure_default_setup(float p_min = 0, float p_max = 1);
  430. void set_curve(Ref<Curve> p_curve);
  431. Ref<Curve> get_curve() const;
  432. virtual RID get_rid() const;
  433. virtual int get_height() const { return 1; }
  434. virtual bool has_alpha() const { return false; }
  435. virtual void set_flags(uint32_t p_flags) {}
  436. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  437. CurveTexture();
  438. ~CurveTexture();
  439. };
  440. /*
  441. enum CubeMapSide {
  442. CUBEMAP_LEFT,
  443. CUBEMAP_RIGHT,
  444. CUBEMAP_BOTTOM,
  445. CUBEMAP_TOP,
  446. CUBEMAP_FRONT,
  447. CUBEMAP_BACK,
  448. };
  449. */
  450. //VARIANT_ENUM_CAST( Texture::CubeMapSide );
  451. class GradientTexture : public Texture {
  452. GDCLASS(GradientTexture, Texture);
  453. public:
  454. struct Point {
  455. float offset;
  456. Color color;
  457. bool operator<(const Point &p_ponit) const {
  458. return offset < p_ponit.offset;
  459. }
  460. };
  461. private:
  462. Ref<Gradient> gradient;
  463. bool update_pending;
  464. RID texture;
  465. int width;
  466. void _queue_update();
  467. void _update();
  468. protected:
  469. static void _bind_methods();
  470. public:
  471. void set_gradient(Ref<Gradient> p_gradient);
  472. Ref<Gradient> get_gradient() const;
  473. void set_width(int p_width);
  474. int get_width() const;
  475. virtual RID get_rid() const { return texture; }
  476. virtual int get_height() const { return 1; }
  477. virtual bool has_alpha() const { return true; }
  478. virtual void set_flags(uint32_t p_flags) {}
  479. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  480. virtual Ref<Image> get_data() const;
  481. GradientTexture();
  482. virtual ~GradientTexture();
  483. };
  484. class ProxyTexture : public Texture {
  485. GDCLASS(ProxyTexture, Texture);
  486. private:
  487. RID proxy;
  488. Ref<Texture> base;
  489. protected:
  490. static void _bind_methods();
  491. public:
  492. void set_base(const Ref<Texture> &p_texture);
  493. Ref<Texture> get_base() const;
  494. virtual int get_width() const;
  495. virtual int get_height() const;
  496. virtual RID get_rid() const;
  497. virtual bool has_alpha() const;
  498. virtual void set_flags(uint32_t p_flags);
  499. virtual uint32_t get_flags() const;
  500. ProxyTexture();
  501. ~ProxyTexture();
  502. };
  503. class AnimatedTexture : public Texture {
  504. GDCLASS(AnimatedTexture, Texture);
  505. //use readers writers lock for this, since its far more times read than written to
  506. RWLock *rw_lock;
  507. private:
  508. enum {
  509. MAX_FRAMES = 256
  510. };
  511. RID proxy;
  512. struct Frame {
  513. Ref<Texture> texture;
  514. float delay_sec;
  515. Frame() {
  516. delay_sec = 0;
  517. }
  518. };
  519. Frame frames[MAX_FRAMES];
  520. int frame_count;
  521. int current_frame;
  522. float fps;
  523. float time;
  524. uint64_t prev_ticks;
  525. void _update_proxy();
  526. protected:
  527. static void _bind_methods();
  528. void _validate_property(PropertyInfo &property) const;
  529. public:
  530. void set_frames(int p_frames);
  531. int get_frames() const;
  532. void set_frame_texture(int p_frame, const Ref<Texture> &p_texture);
  533. Ref<Texture> get_frame_texture(int p_frame) const;
  534. void set_frame_delay(int p_frame, float p_delay_sec);
  535. float get_frame_delay(int p_frame) const;
  536. void set_fps(float p_fps);
  537. float get_fps() const;
  538. virtual int get_width() const;
  539. virtual int get_height() const;
  540. virtual RID get_rid() const;
  541. virtual bool has_alpha() const;
  542. virtual void set_flags(uint32_t p_flags);
  543. virtual uint32_t get_flags() const;
  544. virtual Ref<Image> get_data() const;
  545. bool is_pixel_opaque(int p_x, int p_y) const;
  546. AnimatedTexture();
  547. ~AnimatedTexture();
  548. };
  549. class CameraTexture : public Texture {
  550. GDCLASS(CameraTexture, Texture);
  551. private:
  552. int camera_feed_id;
  553. CameraServer::FeedImage which_feed;
  554. protected:
  555. static void _bind_methods();
  556. public:
  557. virtual int get_width() const;
  558. virtual int get_height() const;
  559. virtual RID get_rid() const;
  560. virtual bool has_alpha() const;
  561. virtual void set_flags(uint32_t p_flags);
  562. virtual uint32_t get_flags() const;
  563. virtual Ref<Image> get_data() const;
  564. void set_camera_feed_id(int p_new_id);
  565. int get_camera_feed_id() const;
  566. void set_which_feed(CameraServer::FeedImage p_which);
  567. CameraServer::FeedImage get_which_feed() const;
  568. void set_camera_active(bool p_active);
  569. bool get_camera_active() const;
  570. CameraTexture();
  571. ~CameraTexture();
  572. };
  573. // External textures as defined by https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external.txt
  574. class ExternalTexture : public Texture {
  575. GDCLASS(ExternalTexture, Texture);
  576. private:
  577. RID texture;
  578. Size2 size;
  579. protected:
  580. static void _bind_methods();
  581. public:
  582. uint32_t get_external_texture_id();
  583. virtual Size2 get_size() const;
  584. void set_size(const Size2 &p_size);
  585. virtual int get_width() const;
  586. virtual int get_height() const;
  587. virtual RID get_rid() const;
  588. virtual bool has_alpha() const;
  589. virtual void set_flags(uint32_t p_flags);
  590. virtual uint32_t get_flags() const;
  591. ExternalTexture();
  592. ~ExternalTexture();
  593. };
  594. #endif