texture.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /**************************************************************************/
  2. /* texture.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #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 FormatBits {
  135. FORMAT_MASK_IMAGE_FORMAT = (1 << 20) - 1,
  136. FORMAT_BIT_PNG = 1 << 20,
  137. FORMAT_BIT_WEBP = 1 << 21,
  138. FORMAT_BIT_STREAM = 1 << 22,
  139. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  140. FORMAT_BIT_DETECT_3D = 1 << 24,
  141. FORMAT_BIT_DETECT_SRGB = 1 << 25,
  142. FORMAT_BIT_DETECT_NORMAL = 1 << 26,
  143. };
  144. private:
  145. 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);
  146. String path_to_file;
  147. RID texture;
  148. Image::Format format;
  149. uint32_t flags;
  150. int w, h;
  151. mutable Ref<BitMap> alpha_cache;
  152. virtual void reload_from_file();
  153. static void _requested_3d(void *p_ud);
  154. static void _requested_srgb(void *p_ud);
  155. static void _requested_normal(void *p_ud);
  156. protected:
  157. static void _bind_methods();
  158. void _validate_property(PropertyInfo &property) const;
  159. public:
  160. typedef void (*TextureFormatRequestCallback)(const Ref<StreamTexture> &);
  161. static TextureFormatRequestCallback request_3d_callback;
  162. static TextureFormatRequestCallback request_srgb_callback;
  163. static TextureFormatRequestCallback request_normal_callback;
  164. uint32_t get_flags() const;
  165. Image::Format get_format() const;
  166. Error load(const String &p_path);
  167. String get_load_path() const;
  168. int get_width() const;
  169. int get_height() const;
  170. virtual RID get_rid() const;
  171. virtual void set_path(const String &p_path, bool p_take_over);
  172. 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;
  173. 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;
  174. 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;
  175. virtual bool has_alpha() const;
  176. virtual void set_flags(uint32_t p_flags);
  177. bool is_pixel_opaque(int p_x, int p_y) const;
  178. virtual Ref<Image> get_data() const;
  179. StreamTexture();
  180. ~StreamTexture();
  181. };
  182. class ResourceFormatLoaderStreamTexture : public ResourceFormatLoader {
  183. public:
  184. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr);
  185. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  186. virtual bool handles_type(const String &p_type) const;
  187. virtual String get_resource_type(const String &p_path) const;
  188. };
  189. VARIANT_ENUM_CAST(ImageTexture::Storage);
  190. class AtlasTexture : public Texture {
  191. GDCLASS(AtlasTexture, Texture);
  192. RES_BASE_EXTENSION("atlastex");
  193. protected:
  194. Ref<Texture> atlas;
  195. Rect2 region;
  196. Rect2 margin;
  197. bool filter_clip;
  198. static void _bind_methods();
  199. public:
  200. virtual int get_width() const;
  201. virtual int get_height() const;
  202. virtual RID get_rid() const;
  203. virtual bool has_alpha() const;
  204. virtual void set_flags(uint32_t p_flags);
  205. virtual uint32_t get_flags() const;
  206. void set_atlas(const Ref<Texture> &p_atlas);
  207. Ref<Texture> get_atlas() const;
  208. void set_region(const Rect2 &p_region);
  209. Rect2 get_region() const;
  210. void set_margin(const Rect2 &p_margin);
  211. Rect2 get_margin() const;
  212. void set_filter_clip(const bool p_enable);
  213. bool has_filter_clip() const;
  214. virtual Ref<Image> get_data() const;
  215. 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;
  216. 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;
  217. 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;
  218. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  219. bool is_pixel_opaque(int p_x, int p_y) const;
  220. AtlasTexture();
  221. };
  222. class Mesh;
  223. class MeshTexture : public Texture {
  224. GDCLASS(MeshTexture, Texture);
  225. RES_BASE_EXTENSION("meshtex");
  226. Ref<Texture> base_texture;
  227. Ref<Mesh> mesh;
  228. Size2i size;
  229. protected:
  230. static void _bind_methods();
  231. public:
  232. virtual int get_width() const;
  233. virtual int get_height() const;
  234. virtual RID get_rid() const;
  235. virtual bool has_alpha() const;
  236. virtual void set_flags(uint32_t p_flags);
  237. virtual uint32_t get_flags() const;
  238. void set_mesh(const Ref<Mesh> &p_mesh);
  239. Ref<Mesh> get_mesh() const;
  240. void set_image_size(const Size2 &p_size);
  241. Size2 get_image_size() const;
  242. void set_base_texture(const Ref<Texture> &p_texture);
  243. Ref<Texture> get_base_texture() const;
  244. 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;
  245. 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;
  246. 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;
  247. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  248. bool is_pixel_opaque(int p_x, int p_y) const;
  249. MeshTexture();
  250. };
  251. class LargeTexture : public Texture {
  252. GDCLASS(LargeTexture, Texture);
  253. RES_BASE_EXTENSION("largetex");
  254. protected:
  255. struct Piece {
  256. Point2 offset;
  257. Ref<Texture> texture;
  258. };
  259. Vector<Piece> pieces;
  260. Size2i size;
  261. Array _get_data() const;
  262. void _set_data(const Array &p_array);
  263. static void _bind_methods();
  264. public:
  265. virtual int get_width() const;
  266. virtual int get_height() const;
  267. virtual RID get_rid() const;
  268. virtual bool has_alpha() const;
  269. virtual void set_flags(uint32_t p_flags);
  270. virtual uint32_t get_flags() const;
  271. int add_piece(const Point2 &p_offset, const Ref<Texture> &p_texture);
  272. void set_piece_offset(int p_idx, const Point2 &p_offset);
  273. void set_piece_texture(int p_idx, const Ref<Texture> &p_texture);
  274. void set_size(const Size2 &p_size);
  275. void clear();
  276. int get_piece_count() const;
  277. Vector2 get_piece_offset(int p_idx) const;
  278. Ref<Texture> get_piece_texture(int p_idx) const;
  279. Ref<Image> to_image() const;
  280. 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;
  281. 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;
  282. 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;
  283. bool is_pixel_opaque(int p_x, int p_y) const;
  284. LargeTexture();
  285. };
  286. class CubeMap : public Resource {
  287. GDCLASS(CubeMap, Resource);
  288. RES_BASE_EXTENSION("cubemap");
  289. public:
  290. enum Storage {
  291. STORAGE_RAW,
  292. STORAGE_COMPRESS_LOSSY,
  293. STORAGE_COMPRESS_LOSSLESS
  294. };
  295. enum Side {
  296. SIDE_LEFT,
  297. SIDE_RIGHT,
  298. SIDE_BOTTOM,
  299. SIDE_TOP,
  300. SIDE_FRONT,
  301. SIDE_BACK
  302. };
  303. enum Flags {
  304. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  305. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  306. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  307. FLAGS_DEFAULT = FLAG_MIPMAPS | FLAG_REPEAT | FLAG_FILTER,
  308. };
  309. private:
  310. bool valid[6];
  311. RID cubemap;
  312. Image::Format format;
  313. uint32_t flags;
  314. int w, h;
  315. Storage storage;
  316. Size2 size_override;
  317. float lossy_storage_quality;
  318. _FORCE_INLINE_ bool _is_valid() const {
  319. for (int i = 0; i < 6; i++) {
  320. if (valid[i]) {
  321. return true;
  322. }
  323. }
  324. return false;
  325. }
  326. protected:
  327. bool _set(const StringName &p_name, const Variant &p_value);
  328. bool _get(const StringName &p_name, Variant &r_ret) const;
  329. void _get_property_list(List<PropertyInfo> *p_list) const;
  330. static void _bind_methods();
  331. public:
  332. void set_flags(uint32_t p_flags);
  333. uint32_t get_flags() const;
  334. void set_side(Side p_side, const Ref<Image> &p_image);
  335. Ref<Image> get_side(Side p_side) const;
  336. Image::Format get_format() const;
  337. int get_width() const;
  338. int get_height() const;
  339. virtual RID get_rid() const;
  340. void set_storage(Storage p_storage);
  341. Storage get_storage() const;
  342. void set_lossy_storage_quality(float p_lossy_storage_quality);
  343. float get_lossy_storage_quality() const;
  344. virtual void set_path(const String &p_path, bool p_take_over = false);
  345. CubeMap();
  346. ~CubeMap();
  347. };
  348. VARIANT_ENUM_CAST(CubeMap::Flags)
  349. VARIANT_ENUM_CAST(CubeMap::Side)
  350. VARIANT_ENUM_CAST(CubeMap::Storage)
  351. class TextureLayered : public Resource {
  352. GDCLASS(TextureLayered, Resource);
  353. public:
  354. enum Flags {
  355. FLAG_MIPMAPS = VisualServer::TEXTURE_FLAG_MIPMAPS,
  356. FLAG_REPEAT = VisualServer::TEXTURE_FLAG_REPEAT,
  357. FLAG_FILTER = VisualServer::TEXTURE_FLAG_FILTER,
  358. FLAG_ANISOTROPIC_FILTER = VisualServer::TEXTURE_FLAG_ANISOTROPIC_FILTER,
  359. FLAG_CONVERT_TO_LINEAR = VisualServer::TEXTURE_FLAG_CONVERT_TO_LINEAR,
  360. FLAGS_DEFAULT_TEXTURE_ARRAY = FLAG_MIPMAPS | FLAG_REPEAT | FLAG_FILTER,
  361. FLAGS_DEFAULT_TEXTURE_3D = FLAG_FILTER,
  362. };
  363. enum CompressMode {
  364. COMPRESS_LOSSLESS,
  365. COMPRESS_VIDEO_RAM,
  366. COMPRESS_UNCOMPRESSED
  367. };
  368. private:
  369. String path_to_file;
  370. bool is_3d;
  371. RID texture;
  372. Image::Format format;
  373. uint32_t flags;
  374. int width;
  375. int height;
  376. int depth;
  377. virtual void reload_from_file();
  378. void _set_data(const Dictionary &p_data);
  379. Dictionary _get_data() const;
  380. protected:
  381. static void _bind_methods();
  382. public:
  383. void set_flags(uint32_t p_flags);
  384. uint32_t get_flags() const;
  385. Image::Format get_format() const;
  386. Error load(const String &p_path);
  387. String get_load_path() const;
  388. uint32_t get_width() const;
  389. uint32_t get_height() const;
  390. uint32_t get_depth() const;
  391. void create(uint32_t p_width, uint32_t p_height, uint32_t p_depth, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT_TEXTURE_ARRAY);
  392. void set_layer_data(const Ref<Image> &p_image, int p_layer);
  393. Ref<Image> get_layer_data(int p_layer) const;
  394. void set_data_partial(const Ref<Image> &p_image, int p_x_ofs, int p_y_ofs, int p_z, int p_mipmap = 0);
  395. virtual RID get_rid() const;
  396. virtual void set_path(const String &p_path, bool p_take_over = false);
  397. TextureLayered(bool p_3d = false);
  398. ~TextureLayered();
  399. };
  400. VARIANT_ENUM_CAST(TextureLayered::Flags)
  401. class Texture3D : public TextureLayered {
  402. GDCLASS(Texture3D, TextureLayered);
  403. protected:
  404. static void _bind_methods();
  405. public:
  406. void create(uint32_t p_width, uint32_t p_height, uint32_t p_depth, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT_TEXTURE_3D) {
  407. TextureLayered::create(p_width, p_height, p_depth, p_format, p_flags);
  408. }
  409. Texture3D() :
  410. TextureLayered(true) {}
  411. };
  412. class TextureArray : public TextureLayered {
  413. GDCLASS(TextureArray, TextureLayered);
  414. protected:
  415. static void _bind_methods();
  416. public:
  417. void create(uint32_t p_width, uint32_t p_height, uint32_t p_depth, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT_TEXTURE_ARRAY) {
  418. TextureLayered::create(p_width, p_height, p_depth, p_format, p_flags);
  419. }
  420. TextureArray() :
  421. TextureLayered(false) {}
  422. };
  423. class ResourceFormatLoaderTextureLayered : public ResourceFormatLoader {
  424. public:
  425. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr);
  426. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  427. virtual bool handles_type(const String &p_type) const;
  428. virtual String get_resource_type(const String &p_path) const;
  429. };
  430. class CurveTexture : public Texture {
  431. GDCLASS(CurveTexture, Texture);
  432. RES_BASE_EXTENSION("curvetex")
  433. private:
  434. RID _texture;
  435. Ref<Curve> _curve;
  436. int _width;
  437. void _update();
  438. protected:
  439. static void _bind_methods();
  440. public:
  441. void set_width(int p_width);
  442. int get_width() const;
  443. void ensure_default_setup(float p_min = 0, float p_max = 1);
  444. void set_curve(Ref<Curve> p_curve);
  445. Ref<Curve> get_curve() const;
  446. virtual RID get_rid() const;
  447. virtual int get_height() const { return 1; }
  448. virtual bool has_alpha() const { return false; }
  449. virtual void set_flags(uint32_t p_flags) {}
  450. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  451. CurveTexture();
  452. ~CurveTexture();
  453. };
  454. /*
  455. enum CubeMapSide {
  456. CUBEMAP_LEFT,
  457. CUBEMAP_RIGHT,
  458. CUBEMAP_BOTTOM,
  459. CUBEMAP_TOP,
  460. CUBEMAP_FRONT,
  461. CUBEMAP_BACK,
  462. };
  463. */
  464. //VARIANT_ENUM_CAST( Texture::CubeMapSide );
  465. class GradientTexture : public Texture {
  466. GDCLASS(GradientTexture, Texture);
  467. public:
  468. struct Point {
  469. float offset;
  470. Color color;
  471. bool operator<(const Point &p_ponit) const {
  472. return offset < p_ponit.offset;
  473. }
  474. };
  475. private:
  476. Ref<Gradient> gradient;
  477. bool update_pending;
  478. RID texture;
  479. int width;
  480. void _queue_update();
  481. void _update();
  482. protected:
  483. static void _bind_methods();
  484. public:
  485. void set_gradient(Ref<Gradient> p_gradient);
  486. Ref<Gradient> get_gradient() const;
  487. void set_width(int p_width);
  488. int get_width() const;
  489. virtual RID get_rid() const { return texture; }
  490. virtual int get_height() const { return 1; }
  491. virtual bool has_alpha() const { return true; }
  492. virtual void set_flags(uint32_t p_flags) {}
  493. virtual uint32_t get_flags() const { return FLAG_FILTER; }
  494. virtual Ref<Image> get_data() const;
  495. GradientTexture();
  496. virtual ~GradientTexture();
  497. };
  498. class GradientTexture2D : public Texture {
  499. GDCLASS(GradientTexture2D, Texture);
  500. public:
  501. enum Fill {
  502. FILL_LINEAR,
  503. FILL_RADIAL,
  504. };
  505. enum Repeat {
  506. REPEAT_NONE,
  507. REPEAT,
  508. REPEAT_MIRROR,
  509. };
  510. private:
  511. Ref<Gradient> gradient;
  512. mutable RID texture;
  513. int width = 64;
  514. int height = 64;
  515. uint32_t flags = FLAGS_DEFAULT;
  516. bool use_hdr = false;
  517. Vector2 fill_from;
  518. Vector2 fill_to = Vector2(1, 0);
  519. Fill fill = FILL_LINEAR;
  520. Repeat repeat = REPEAT_NONE;
  521. float _get_gradient_offset_at(int x, int y) const;
  522. bool update_pending = false;
  523. void _queue_update();
  524. void _update();
  525. protected:
  526. static void _bind_methods();
  527. public:
  528. void set_gradient(Ref<Gradient> p_gradient);
  529. Ref<Gradient> get_gradient() const;
  530. void set_width(int p_width);
  531. virtual int get_width() const;
  532. void set_height(int p_height);
  533. virtual int get_height() const;
  534. virtual void set_flags(uint32_t p_flags);
  535. virtual uint32_t get_flags() const;
  536. void set_use_hdr(bool p_enabled);
  537. bool is_using_hdr() const;
  538. void set_fill(Fill p_fill);
  539. Fill get_fill() const;
  540. void set_fill_from(Vector2 p_fill_from);
  541. Vector2 get_fill_from() const;
  542. void set_fill_to(Vector2 p_fill_to);
  543. Vector2 get_fill_to() const;
  544. void set_repeat(Repeat p_repeat);
  545. Repeat get_repeat() const;
  546. virtual RID get_rid() const;
  547. virtual bool has_alpha() const { return true; }
  548. virtual Ref<Image> get_data() const;
  549. GradientTexture2D();
  550. virtual ~GradientTexture2D();
  551. };
  552. VARIANT_ENUM_CAST(GradientTexture2D::Fill);
  553. VARIANT_ENUM_CAST(GradientTexture2D::Repeat);
  554. class ProxyTexture : public Texture {
  555. GDCLASS(ProxyTexture, Texture);
  556. private:
  557. RID proxy;
  558. Ref<Texture> base;
  559. protected:
  560. static void _bind_methods();
  561. public:
  562. void set_base(const Ref<Texture> &p_texture);
  563. Ref<Texture> get_base() const;
  564. virtual int get_width() const;
  565. virtual int get_height() const;
  566. virtual RID get_rid() const;
  567. virtual bool has_alpha() const;
  568. virtual void set_flags(uint32_t p_flags);
  569. virtual uint32_t get_flags() const;
  570. ProxyTexture();
  571. ~ProxyTexture();
  572. };
  573. class AnimatedTexture : public Texture {
  574. GDCLASS(AnimatedTexture, Texture);
  575. //use readers writers lock for this, since its far more times read than written to
  576. RWLock rw_lock;
  577. public:
  578. enum {
  579. MAX_FRAMES = 256
  580. };
  581. private:
  582. RID proxy;
  583. struct Frame {
  584. Ref<Texture> texture;
  585. float delay_sec;
  586. Frame() {
  587. delay_sec = 0;
  588. }
  589. };
  590. Frame frames[MAX_FRAMES];
  591. int frame_count;
  592. int current_frame;
  593. bool pause;
  594. bool oneshot;
  595. float fps;
  596. float time;
  597. uint64_t prev_ticks;
  598. void _update_proxy();
  599. protected:
  600. static void _bind_methods();
  601. void _validate_property(PropertyInfo &property) const;
  602. public:
  603. void set_frames(int p_frames);
  604. int get_frames() const;
  605. void set_current_frame(int p_frame);
  606. int get_current_frame() const;
  607. void set_pause(bool p_pause);
  608. bool get_pause() const;
  609. void set_oneshot(bool p_oneshot);
  610. bool get_oneshot() const;
  611. void set_frame_texture(int p_frame, const Ref<Texture> &p_texture);
  612. Ref<Texture> get_frame_texture(int p_frame) const;
  613. void set_frame_delay(int p_frame, float p_delay_sec);
  614. float get_frame_delay(int p_frame) const;
  615. void set_fps(float p_fps);
  616. float get_fps() const;
  617. virtual int get_width() const;
  618. virtual int get_height() const;
  619. virtual RID get_rid() const;
  620. virtual bool has_alpha() const;
  621. virtual void set_flags(uint32_t p_flags);
  622. virtual uint32_t get_flags() const;
  623. virtual Ref<Image> get_data() const;
  624. bool is_pixel_opaque(int p_x, int p_y) const;
  625. AnimatedTexture();
  626. ~AnimatedTexture();
  627. };
  628. class CameraTexture : public Texture {
  629. GDCLASS(CameraTexture, Texture);
  630. private:
  631. int camera_feed_id;
  632. CameraServer::FeedImage which_feed;
  633. protected:
  634. static void _bind_methods();
  635. public:
  636. virtual int get_width() const;
  637. virtual int get_height() const;
  638. virtual RID get_rid() const;
  639. virtual bool has_alpha() const;
  640. virtual void set_flags(uint32_t p_flags);
  641. virtual uint32_t get_flags() const;
  642. virtual Ref<Image> get_data() const;
  643. void set_camera_feed_id(int p_new_id);
  644. int get_camera_feed_id() const;
  645. void set_which_feed(CameraServer::FeedImage p_which);
  646. CameraServer::FeedImage get_which_feed() const;
  647. void set_camera_active(bool p_active);
  648. bool get_camera_active() const;
  649. CameraTexture();
  650. ~CameraTexture();
  651. };
  652. // External textures as defined by https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external.txt
  653. class ExternalTexture : public Texture {
  654. GDCLASS(ExternalTexture, Texture);
  655. private:
  656. RID texture;
  657. Size2 size;
  658. protected:
  659. static void _bind_methods();
  660. public:
  661. uint32_t get_external_texture_id();
  662. virtual Size2 get_size() const;
  663. void set_size(const Size2 &p_size);
  664. virtual int get_width() const;
  665. virtual int get_height() const;
  666. virtual RID get_rid() const;
  667. virtual bool has_alpha() const;
  668. virtual void set_flags(uint32_t p_flags);
  669. virtual uint32_t get_flags() const;
  670. ExternalTexture();
  671. ~ExternalTexture();
  672. };
  673. #endif // TEXTURE_H