texture.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*************************************************************************/
  2. /* texture.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/file_access.h"
  33. #include "core/io/resource.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/math/rect2.h"
  36. #include "core/os/mutex.h"
  37. #include "core/os/rw_lock.h"
  38. #include "core/os/thread_safe.h"
  39. #include "scene/resources/curve.h"
  40. #include "scene/resources/gradient.h"
  41. #include "servers/camera_server.h"
  42. #include "servers/rendering_server.h"
  43. class Texture : public Resource {
  44. GDCLASS(Texture, Resource);
  45. public:
  46. Texture() {}
  47. };
  48. class Texture2D : public Texture {
  49. GDCLASS(Texture2D, Texture);
  50. OBJ_SAVE_TYPE(Texture2D); // Saves derived classes with common type so they can be interchanged.
  51. protected:
  52. static void _bind_methods();
  53. GDVIRTUAL0RC(int, _get_width)
  54. GDVIRTUAL0RC(int, _get_height)
  55. GDVIRTUAL2RC(bool, _is_pixel_opaque, int, int)
  56. GDVIRTUAL0RC(bool, _has_alpha)
  57. GDVIRTUAL4C(_draw, RID, Point2, Color, bool)
  58. GDVIRTUAL5C(_draw_rect, RID, Rect2, bool, Color, bool)
  59. GDVIRTUAL6C(_draw_rect_region, RID, Rect2, Rect2, Color, bool, bool)
  60. public:
  61. virtual int get_width() const;
  62. virtual int get_height() const;
  63. virtual Size2 get_size() const;
  64. virtual bool is_pixel_opaque(int p_x, int p_y) const;
  65. virtual bool has_alpha() const;
  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;
  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;
  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, 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_image() const { return Ref<Image>(); }
  71. Texture2D();
  72. };
  73. class BitMap;
  74. class ImageTexture : public Texture2D {
  75. GDCLASS(ImageTexture, Texture2D);
  76. RES_BASE_EXTENSION("tex");
  77. mutable RID texture;
  78. Image::Format format = Image::FORMAT_L8;
  79. bool mipmaps = false;
  80. int w = 0;
  81. int h = 0;
  82. Size2 size_override;
  83. mutable Ref<BitMap> alpha_cache;
  84. bool image_stored = false;
  85. protected:
  86. virtual void reload_from_file() override;
  87. bool _set(const StringName &p_name, const Variant &p_value);
  88. bool _get(const StringName &p_name, Variant &r_ret) const;
  89. void _get_property_list(List<PropertyInfo> *p_list) const;
  90. static void _bind_methods();
  91. public:
  92. void set_image(const Ref<Image> &p_image);
  93. static Ref<ImageTexture> create_from_image(const Ref<Image> &p_image);
  94. Image::Format get_format() const;
  95. void update(const Ref<Image> &p_image);
  96. Ref<Image> get_image() const override;
  97. int get_width() const override;
  98. int get_height() const override;
  99. virtual RID get_rid() const override;
  100. bool has_alpha() const override;
  101. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  102. 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 override;
  103. 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, bool p_clip_uv = true) const override;
  104. bool is_pixel_opaque(int p_x, int p_y) const override;
  105. void set_size_override(const Size2i &p_size);
  106. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  107. ImageTexture();
  108. ~ImageTexture();
  109. };
  110. class PortableCompressedTexture2D : public Texture2D {
  111. GDCLASS(PortableCompressedTexture2D, Texture2D);
  112. public:
  113. enum CompressionMode {
  114. COMPRESSION_MODE_LOSSLESS,
  115. COMPRESSION_MODE_LOSSY,
  116. COMPRESSION_MODE_BASIS_UNIVERSAL,
  117. COMPRESSION_MODE_S3TC,
  118. COMPRESSION_MODE_ETC2,
  119. COMPRESSION_MODE_BPTC,
  120. };
  121. private:
  122. CompressionMode compression_mode = COMPRESSION_MODE_LOSSLESS;
  123. static bool keep_all_compressed_buffers;
  124. bool keep_compressed_buffer = false;
  125. Vector<uint8_t> compressed_buffer;
  126. Size2 size;
  127. Size2 size_override;
  128. bool mipmaps = false;
  129. Image::Format format = Image::FORMAT_L8;
  130. mutable RID texture;
  131. mutable Ref<BitMap> alpha_cache;
  132. bool image_stored = false;
  133. protected:
  134. Vector<uint8_t> _get_data() const;
  135. void _set_data(const Vector<uint8_t> &p_data);
  136. static void _bind_methods();
  137. public:
  138. CompressionMode get_compression_mode() const;
  139. void create_from_image(const Ref<Image> &p_image, CompressionMode p_compression_mode, bool p_normal_map = false, float p_lossy_quality = 0.8);
  140. Image::Format get_format() const;
  141. void update(const Ref<Image> &p_image);
  142. Ref<Image> get_image() const override;
  143. int get_width() const override;
  144. int get_height() const override;
  145. virtual RID get_rid() const override;
  146. bool has_alpha() const override;
  147. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  148. 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 override;
  149. 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, bool p_clip_uv = true) const override;
  150. bool is_pixel_opaque(int p_x, int p_y) const override;
  151. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  152. void set_size_override(const Size2 &p_size);
  153. Size2 get_size_override() const;
  154. void set_keep_compressed_buffer(bool p_keep);
  155. bool is_keeping_compressed_buffer() const;
  156. static void set_keep_all_compressed_buffers(bool p_keep);
  157. static bool is_keeping_all_compressed_buffers();
  158. PortableCompressedTexture2D();
  159. ~PortableCompressedTexture2D();
  160. };
  161. VARIANT_ENUM_CAST(PortableCompressedTexture2D::CompressionMode)
  162. class CompressedTexture2D : public Texture2D {
  163. GDCLASS(CompressedTexture2D, Texture2D);
  164. public:
  165. enum DataFormat {
  166. DATA_FORMAT_IMAGE,
  167. DATA_FORMAT_PNG,
  168. DATA_FORMAT_WEBP,
  169. DATA_FORMAT_BASIS_UNIVERSAL,
  170. };
  171. enum {
  172. FORMAT_VERSION = 1
  173. };
  174. enum FormatBits {
  175. FORMAT_BIT_STREAM = 1 << 22,
  176. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  177. FORMAT_BIT_DETECT_3D = 1 << 24,
  178. //FORMAT_BIT_DETECT_SRGB = 1 << 25,
  179. FORMAT_BIT_DETECT_NORMAL = 1 << 26,
  180. FORMAT_BIT_DETECT_ROUGNESS = 1 << 27,
  181. };
  182. private:
  183. Error _load_data(const String &p_path, int &r_width, int &r_height, Ref<Image> &image, bool &r_request_3d, bool &r_request_normal, bool &r_request_roughness, int &mipmap_limit, int p_size_limit = 0);
  184. String path_to_file;
  185. mutable RID texture;
  186. Image::Format format = Image::FORMAT_MAX;
  187. int w = 0;
  188. int h = 0;
  189. mutable Ref<BitMap> alpha_cache;
  190. virtual void reload_from_file() override;
  191. static void _requested_3d(void *p_ud);
  192. static void _requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
  193. static void _requested_normal(void *p_ud);
  194. protected:
  195. static void _bind_methods();
  196. void _validate_property(PropertyInfo &p_property) const;
  197. public:
  198. static Ref<Image> load_image_from_file(Ref<FileAccess> p_file, int p_size_limit);
  199. typedef void (*TextureFormatRequestCallback)(const Ref<CompressedTexture2D> &);
  200. typedef void (*TextureFormatRoughnessRequestCallback)(const Ref<CompressedTexture2D> &, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
  201. static TextureFormatRequestCallback request_3d_callback;
  202. static TextureFormatRoughnessRequestCallback request_roughness_callback;
  203. static TextureFormatRequestCallback request_normal_callback;
  204. Image::Format get_format() const;
  205. Error load(const String &p_path);
  206. String get_load_path() const;
  207. int get_width() const override;
  208. int get_height() const override;
  209. virtual RID get_rid() const override;
  210. virtual void set_path(const String &p_path, bool p_take_over) override;
  211. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  212. 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 override;
  213. 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, bool p_clip_uv = true) const override;
  214. virtual bool has_alpha() const override;
  215. bool is_pixel_opaque(int p_x, int p_y) const override;
  216. virtual Ref<Image> get_image() const override;
  217. CompressedTexture2D();
  218. ~CompressedTexture2D();
  219. };
  220. class ResourceFormatLoaderCompressedTexture2D : public ResourceFormatLoader {
  221. public:
  222. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  223. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  224. virtual bool handles_type(const String &p_type) const;
  225. virtual String get_resource_type(const String &p_path) const;
  226. };
  227. class AtlasTexture : public Texture2D {
  228. GDCLASS(AtlasTexture, Texture2D);
  229. RES_BASE_EXTENSION("atlastex");
  230. protected:
  231. Ref<Texture2D> atlas;
  232. Rect2 region;
  233. Rect2 margin;
  234. bool filter_clip = false;
  235. static void _bind_methods();
  236. public:
  237. virtual int get_width() const override;
  238. virtual int get_height() const override;
  239. virtual RID get_rid() const override;
  240. virtual bool has_alpha() const override;
  241. void set_atlas(const Ref<Texture2D> &p_atlas);
  242. Ref<Texture2D> get_atlas() const;
  243. void set_region(const Rect2 &p_region);
  244. Rect2 get_region() const;
  245. void set_margin(const Rect2 &p_margin);
  246. Rect2 get_margin() const;
  247. void set_filter_clip(const bool p_enable);
  248. bool has_filter_clip() const;
  249. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  250. 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 override;
  251. 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, bool p_clip_uv = true) const override;
  252. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const override;
  253. bool is_pixel_opaque(int p_x, int p_y) const override;
  254. virtual Ref<Image> get_image() const override;
  255. AtlasTexture();
  256. };
  257. class Mesh;
  258. class MeshTexture : public Texture2D {
  259. GDCLASS(MeshTexture, Texture2D);
  260. RES_BASE_EXTENSION("meshtex");
  261. Ref<Texture2D> base_texture;
  262. Ref<Mesh> mesh;
  263. Size2i size;
  264. protected:
  265. static void _bind_methods();
  266. public:
  267. virtual int get_width() const override;
  268. virtual int get_height() const override;
  269. virtual RID get_rid() const override;
  270. virtual bool has_alpha() const override;
  271. void set_mesh(const Ref<Mesh> &p_mesh);
  272. Ref<Mesh> get_mesh() const;
  273. void set_image_size(const Size2 &p_size);
  274. Size2 get_image_size() const;
  275. void set_base_texture(const Ref<Texture2D> &p_texture);
  276. Ref<Texture2D> get_base_texture() const;
  277. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  278. 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 override;
  279. 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, bool p_clip_uv = true) const override;
  280. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const override;
  281. bool is_pixel_opaque(int p_x, int p_y) const override;
  282. MeshTexture();
  283. };
  284. class TextureLayered : public Texture {
  285. GDCLASS(TextureLayered, Texture);
  286. protected:
  287. static void _bind_methods();
  288. GDVIRTUAL0RC(Image::Format, _get_format)
  289. GDVIRTUAL0RC(uint32_t, _get_layered_type)
  290. GDVIRTUAL0RC(int, _get_width)
  291. GDVIRTUAL0RC(int, _get_height)
  292. GDVIRTUAL0RC(int, _get_layers)
  293. GDVIRTUAL0RC(bool, _has_mipmaps)
  294. GDVIRTUAL1RC(Ref<Image>, _get_layer_data, int)
  295. public:
  296. enum LayeredType {
  297. LAYERED_TYPE_2D_ARRAY,
  298. LAYERED_TYPE_CUBEMAP,
  299. LAYERED_TYPE_CUBEMAP_ARRAY
  300. };
  301. virtual Image::Format get_format() const;
  302. virtual LayeredType get_layered_type() const;
  303. virtual int get_width() const;
  304. virtual int get_height() const;
  305. virtual int get_layers() const;
  306. virtual bool has_mipmaps() const;
  307. virtual Ref<Image> get_layer_data(int p_layer) const;
  308. TextureLayered() {}
  309. };
  310. VARIANT_ENUM_CAST(TextureLayered::LayeredType)
  311. class ImageTextureLayered : public TextureLayered {
  312. GDCLASS(ImageTextureLayered, TextureLayered);
  313. LayeredType layered_type;
  314. mutable RID texture;
  315. Image::Format format = Image::FORMAT_MAX;
  316. int width = 0;
  317. int height = 0;
  318. int layers = 0;
  319. bool mipmaps = false;
  320. Error _create_from_images(const Array &p_images);
  321. Array _get_images() const;
  322. protected:
  323. static void _bind_methods();
  324. public:
  325. virtual Image::Format get_format() const override;
  326. virtual int get_width() const override;
  327. virtual int get_height() const override;
  328. virtual int get_layers() const override;
  329. virtual bool has_mipmaps() const override;
  330. virtual LayeredType get_layered_type() const override;
  331. Error create_from_images(Vector<Ref<Image>> p_images);
  332. void update_layer(const Ref<Image> &p_image, int p_layer);
  333. virtual Ref<Image> get_layer_data(int p_layer) const override;
  334. virtual RID get_rid() const override;
  335. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  336. ImageTextureLayered(LayeredType p_layered_type);
  337. ~ImageTextureLayered();
  338. };
  339. class Texture2DArray : public ImageTextureLayered {
  340. GDCLASS(Texture2DArray, ImageTextureLayered)
  341. public:
  342. Texture2DArray() :
  343. ImageTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  344. };
  345. class Cubemap : public ImageTextureLayered {
  346. GDCLASS(Cubemap, ImageTextureLayered);
  347. public:
  348. Cubemap() :
  349. ImageTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  350. };
  351. class CubemapArray : public ImageTextureLayered {
  352. GDCLASS(CubemapArray, ImageTextureLayered);
  353. public:
  354. CubemapArray() :
  355. ImageTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  356. };
  357. class CompressedTextureLayered : public TextureLayered {
  358. GDCLASS(CompressedTextureLayered, TextureLayered);
  359. public:
  360. enum DataFormat {
  361. DATA_FORMAT_IMAGE,
  362. DATA_FORMAT_PNG,
  363. DATA_FORMAT_WEBP,
  364. DATA_FORMAT_BASIS_UNIVERSAL,
  365. };
  366. enum {
  367. FORMAT_VERSION = 1
  368. };
  369. enum FormatBits {
  370. FORMAT_BIT_STREAM = 1 << 22,
  371. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  372. };
  373. private:
  374. Error _load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit = 0);
  375. String path_to_file;
  376. mutable RID texture;
  377. Image::Format format = Image::FORMAT_MAX;
  378. int w = 0;
  379. int h = 0;
  380. int layers = 0;
  381. bool mipmaps = false;
  382. LayeredType layered_type = LayeredType::LAYERED_TYPE_2D_ARRAY;
  383. virtual void reload_from_file() override;
  384. protected:
  385. static void _bind_methods();
  386. void _validate_property(PropertyInfo &p_property) const;
  387. public:
  388. Image::Format get_format() const override;
  389. Error load(const String &p_path);
  390. String get_load_path() const;
  391. virtual LayeredType get_layered_type() const override;
  392. int get_width() const override;
  393. int get_height() const override;
  394. int get_layers() const override;
  395. virtual bool has_mipmaps() const override;
  396. virtual RID get_rid() const override;
  397. virtual void set_path(const String &p_path, bool p_take_over) override;
  398. virtual Ref<Image> get_layer_data(int p_layer) const override;
  399. CompressedTextureLayered(LayeredType p_layered_type);
  400. ~CompressedTextureLayered();
  401. };
  402. class CompressedTexture2DArray : public CompressedTextureLayered {
  403. GDCLASS(CompressedTexture2DArray, CompressedTextureLayered)
  404. public:
  405. CompressedTexture2DArray() :
  406. CompressedTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  407. };
  408. class CompressedCubemap : public CompressedTextureLayered {
  409. GDCLASS(CompressedCubemap, CompressedTextureLayered);
  410. public:
  411. CompressedCubemap() :
  412. CompressedTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  413. };
  414. class CompressedCubemapArray : public CompressedTextureLayered {
  415. GDCLASS(CompressedCubemapArray, CompressedTextureLayered);
  416. public:
  417. CompressedCubemapArray() :
  418. CompressedTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  419. };
  420. class ResourceFormatLoaderCompressedTextureLayered : public ResourceFormatLoader {
  421. public:
  422. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  423. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  424. virtual bool handles_type(const String &p_type) const;
  425. virtual String get_resource_type(const String &p_path) const;
  426. };
  427. class Texture3D : public Texture {
  428. GDCLASS(Texture3D, Texture);
  429. protected:
  430. static void _bind_methods();
  431. TypedArray<Image> _get_datai() const;
  432. GDVIRTUAL0RC(Image::Format, _get_format)
  433. GDVIRTUAL0RC(int, _get_width)
  434. GDVIRTUAL0RC(int, _get_height)
  435. GDVIRTUAL0RC(int, _get_depth)
  436. GDVIRTUAL0RC(bool, _has_mipmaps)
  437. GDVIRTUAL0RC(TypedArray<Image>, _get_data)
  438. public:
  439. virtual Image::Format get_format() const;
  440. virtual int get_width() const;
  441. virtual int get_height() const;
  442. virtual int get_depth() const;
  443. virtual bool has_mipmaps() const;
  444. virtual Vector<Ref<Image>> get_data() const;
  445. };
  446. class ImageTexture3D : public Texture3D {
  447. GDCLASS(ImageTexture3D, Texture3D);
  448. mutable RID texture;
  449. Image::Format format = Image::FORMAT_MAX;
  450. int width = 1;
  451. int height = 1;
  452. int depth = 1;
  453. bool mipmaps = false;
  454. protected:
  455. static void _bind_methods();
  456. Error _create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const TypedArray<Image> &p_data);
  457. void _update(const TypedArray<Image> &p_data);
  458. public:
  459. virtual Image::Format get_format() const override;
  460. virtual int get_width() const override;
  461. virtual int get_height() const override;
  462. virtual int get_depth() const override;
  463. virtual bool has_mipmaps() const override;
  464. Error create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data);
  465. void update(const Vector<Ref<Image>> &p_data);
  466. virtual Vector<Ref<Image>> get_data() const override;
  467. virtual RID get_rid() const override;
  468. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  469. ImageTexture3D();
  470. ~ImageTexture3D();
  471. };
  472. class CompressedTexture3D : public Texture3D {
  473. GDCLASS(CompressedTexture3D, Texture3D);
  474. public:
  475. enum DataFormat {
  476. DATA_FORMAT_IMAGE,
  477. DATA_FORMAT_PNG,
  478. DATA_FORMAT_WEBP,
  479. DATA_FORMAT_BASIS_UNIVERSAL,
  480. };
  481. enum {
  482. FORMAT_VERSION = 1
  483. };
  484. enum FormatBits {
  485. FORMAT_BIT_STREAM = 1 << 22,
  486. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  487. };
  488. private:
  489. Error _load_data(const String &p_path, Vector<Ref<Image>> &r_data, Image::Format &r_format, int &r_width, int &r_height, int &r_depth, bool &r_mipmaps);
  490. String path_to_file;
  491. mutable RID texture;
  492. Image::Format format = Image::FORMAT_MAX;
  493. int w = 0;
  494. int h = 0;
  495. int d = 0;
  496. bool mipmaps = false;
  497. virtual void reload_from_file() override;
  498. protected:
  499. static void _bind_methods();
  500. void _validate_property(PropertyInfo &p_property) const;
  501. public:
  502. Image::Format get_format() const override;
  503. Error load(const String &p_path);
  504. String get_load_path() const;
  505. int get_width() const override;
  506. int get_height() const override;
  507. int get_depth() const override;
  508. virtual bool has_mipmaps() const override;
  509. virtual RID get_rid() const override;
  510. virtual void set_path(const String &p_path, bool p_take_over) override;
  511. virtual Vector<Ref<Image>> get_data() const override;
  512. CompressedTexture3D();
  513. ~CompressedTexture3D();
  514. };
  515. class ResourceFormatLoaderCompressedTexture3D : public ResourceFormatLoader {
  516. public:
  517. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  518. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  519. virtual bool handles_type(const String &p_type) const;
  520. virtual String get_resource_type(const String &p_path) const;
  521. };
  522. class CurveTexture : public Texture2D {
  523. GDCLASS(CurveTexture, Texture2D);
  524. RES_BASE_EXTENSION("curvetex")
  525. public:
  526. enum TextureMode {
  527. TEXTURE_MODE_RGB,
  528. TEXTURE_MODE_RED,
  529. };
  530. private:
  531. mutable RID _texture;
  532. Ref<Curve> _curve;
  533. int _width = 256;
  534. int _current_width = 0;
  535. TextureMode texture_mode = TEXTURE_MODE_RGB;
  536. TextureMode _current_texture_mode = TEXTURE_MODE_RGB;
  537. void _update();
  538. protected:
  539. static void _bind_methods();
  540. public:
  541. void set_width(int p_width);
  542. int get_width() const override;
  543. void set_texture_mode(TextureMode p_mode);
  544. TextureMode get_texture_mode() const;
  545. void ensure_default_setup(float p_min = 0, float p_max = 1);
  546. void set_curve(Ref<Curve> p_curve);
  547. Ref<Curve> get_curve() const;
  548. virtual RID get_rid() const override;
  549. virtual int get_height() const override { return 1; }
  550. virtual bool has_alpha() const override { return false; }
  551. CurveTexture();
  552. ~CurveTexture();
  553. };
  554. VARIANT_ENUM_CAST(CurveTexture::TextureMode)
  555. class CurveXYZTexture : public Texture2D {
  556. GDCLASS(CurveXYZTexture, Texture2D);
  557. RES_BASE_EXTENSION("curvetex")
  558. private:
  559. mutable RID _texture;
  560. Ref<Curve> _curve_x;
  561. Ref<Curve> _curve_y;
  562. Ref<Curve> _curve_z;
  563. int _width = 256;
  564. int _current_width = 0;
  565. void _update();
  566. protected:
  567. static void _bind_methods();
  568. public:
  569. void set_width(int p_width);
  570. int get_width() const override;
  571. void ensure_default_setup(float p_min = 0, float p_max = 1);
  572. void set_curve_x(Ref<Curve> p_curve);
  573. Ref<Curve> get_curve_x() const;
  574. void set_curve_y(Ref<Curve> p_curve);
  575. Ref<Curve> get_curve_y() const;
  576. void set_curve_z(Ref<Curve> p_curve);
  577. Ref<Curve> get_curve_z() const;
  578. virtual RID get_rid() const override;
  579. virtual int get_height() const override { return 1; }
  580. virtual bool has_alpha() const override { return false; }
  581. CurveXYZTexture();
  582. ~CurveXYZTexture();
  583. };
  584. class GradientTexture1D : public Texture2D {
  585. GDCLASS(GradientTexture1D, Texture2D);
  586. public:
  587. struct Point {
  588. float offset = 0.0;
  589. Color color;
  590. bool operator<(const Point &p_ponit) const {
  591. return offset < p_ponit.offset;
  592. }
  593. };
  594. private:
  595. Ref<Gradient> gradient;
  596. bool update_pending = false;
  597. RID texture;
  598. int width = 256;
  599. bool use_hdr = false;
  600. void _queue_update();
  601. void _update();
  602. protected:
  603. static void _bind_methods();
  604. public:
  605. void set_gradient(Ref<Gradient> p_gradient);
  606. Ref<Gradient> get_gradient() const;
  607. void set_width(int p_width);
  608. int get_width() const override;
  609. void set_use_hdr(bool p_enabled);
  610. bool is_using_hdr() const;
  611. virtual RID get_rid() const override { return texture; }
  612. virtual int get_height() const override { return 1; }
  613. virtual bool has_alpha() const override { return true; }
  614. virtual Ref<Image> get_image() const override;
  615. GradientTexture1D();
  616. virtual ~GradientTexture1D();
  617. };
  618. class GradientTexture2D : public Texture2D {
  619. GDCLASS(GradientTexture2D, Texture2D);
  620. public:
  621. enum Fill {
  622. FILL_LINEAR,
  623. FILL_RADIAL,
  624. };
  625. enum Repeat {
  626. REPEAT_NONE,
  627. REPEAT,
  628. REPEAT_MIRROR,
  629. };
  630. private:
  631. Ref<Gradient> gradient;
  632. mutable RID texture;
  633. int width = 64;
  634. int height = 64;
  635. bool use_hdr = false;
  636. Vector2 fill_from;
  637. Vector2 fill_to = Vector2(1, 0);
  638. Fill fill = FILL_LINEAR;
  639. Repeat repeat = REPEAT_NONE;
  640. float _get_gradient_offset_at(int x, int y) const;
  641. bool update_pending = false;
  642. void _queue_update();
  643. void _update();
  644. protected:
  645. static void _bind_methods();
  646. public:
  647. void set_gradient(Ref<Gradient> p_gradient);
  648. Ref<Gradient> get_gradient() const;
  649. void set_width(int p_width);
  650. virtual int get_width() const override;
  651. void set_height(int p_height);
  652. virtual int get_height() const override;
  653. void set_use_hdr(bool p_enabled);
  654. bool is_using_hdr() const;
  655. void set_fill(Fill p_fill);
  656. Fill get_fill() const;
  657. void set_fill_from(Vector2 p_fill_from);
  658. Vector2 get_fill_from() const;
  659. void set_fill_to(Vector2 p_fill_to);
  660. Vector2 get_fill_to() const;
  661. void set_repeat(Repeat p_repeat);
  662. Repeat get_repeat() const;
  663. virtual RID get_rid() const override;
  664. virtual bool has_alpha() const override { return true; }
  665. virtual Ref<Image> get_image() const override;
  666. GradientTexture2D();
  667. virtual ~GradientTexture2D();
  668. };
  669. VARIANT_ENUM_CAST(GradientTexture2D::Fill);
  670. VARIANT_ENUM_CAST(GradientTexture2D::Repeat);
  671. class ProxyTexture : public Texture2D {
  672. private:
  673. mutable RID proxy_ph;
  674. mutable RID proxy;
  675. Ref<Texture2D> base;
  676. protected:
  677. static void _bind_methods();
  678. public:
  679. void set_base(const Ref<Texture2D> &p_texture);
  680. Ref<Texture2D> get_base() const;
  681. virtual int get_width() const override;
  682. virtual int get_height() const override;
  683. virtual RID get_rid() const override;
  684. virtual bool has_alpha() const override;
  685. ProxyTexture();
  686. ~ProxyTexture();
  687. };
  688. class AnimatedTexture : public Texture2D {
  689. GDCLASS(AnimatedTexture, Texture2D);
  690. //use readers writers lock for this, since its far more times read than written to
  691. RWLock rw_lock;
  692. public:
  693. enum {
  694. MAX_FRAMES = 256
  695. };
  696. private:
  697. RID proxy_ph;
  698. RID proxy;
  699. struct Frame {
  700. Ref<Texture2D> texture;
  701. float delay_sec = 0.0;
  702. };
  703. Frame frames[MAX_FRAMES];
  704. int frame_count = 1.0;
  705. int current_frame = 0;
  706. bool pause = false;
  707. bool oneshot = false;
  708. float fps = 4.0;
  709. float time = 0.0;
  710. uint64_t prev_ticks = 0;
  711. void _update_proxy();
  712. protected:
  713. static void _bind_methods();
  714. void _validate_property(PropertyInfo &p_property) const;
  715. public:
  716. void set_frames(int p_frames);
  717. int get_frames() const;
  718. void set_current_frame(int p_frame);
  719. int get_current_frame() const;
  720. void set_pause(bool p_pause);
  721. bool get_pause() const;
  722. void set_oneshot(bool p_oneshot);
  723. bool get_oneshot() const;
  724. void set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture);
  725. Ref<Texture2D> get_frame_texture(int p_frame) const;
  726. void set_frame_delay(int p_frame, float p_delay_sec);
  727. float get_frame_delay(int p_frame) const;
  728. void set_fps(float p_fps);
  729. float get_fps() const;
  730. virtual int get_width() const override;
  731. virtual int get_height() const override;
  732. virtual RID get_rid() const override;
  733. virtual bool has_alpha() const override;
  734. virtual Ref<Image> get_image() const override;
  735. bool is_pixel_opaque(int p_x, int p_y) const override;
  736. AnimatedTexture();
  737. ~AnimatedTexture();
  738. };
  739. class CameraTexture : public Texture2D {
  740. GDCLASS(CameraTexture, Texture2D);
  741. private:
  742. mutable RID _texture;
  743. int camera_feed_id = 0;
  744. CameraServer::FeedImage which_feed = CameraServer::FEED_RGBA_IMAGE;
  745. protected:
  746. static void _bind_methods();
  747. public:
  748. virtual int get_width() const override;
  749. virtual int get_height() const override;
  750. virtual RID get_rid() const override;
  751. virtual bool has_alpha() const override;
  752. virtual Ref<Image> get_image() const override;
  753. void set_camera_feed_id(int p_new_id);
  754. int get_camera_feed_id() const;
  755. void set_which_feed(CameraServer::FeedImage p_which);
  756. CameraServer::FeedImage get_which_feed() const;
  757. void set_camera_active(bool p_active);
  758. bool get_camera_active() const;
  759. CameraTexture();
  760. ~CameraTexture();
  761. };
  762. class PlaceholderTexture2D : public Texture2D {
  763. GDCLASS(PlaceholderTexture2D, Texture2D)
  764. RID rid;
  765. Size2 size = Size2(1, 1);
  766. protected:
  767. static void _bind_methods();
  768. public:
  769. void set_size(Size2 p_size);
  770. virtual int get_width() const override;
  771. virtual int get_height() const override;
  772. virtual RID get_rid() const override;
  773. virtual bool has_alpha() const override;
  774. virtual Ref<Image> get_image() const override;
  775. PlaceholderTexture2D();
  776. ~PlaceholderTexture2D();
  777. };
  778. class PlaceholderTexture3D : public Texture3D {
  779. GDCLASS(PlaceholderTexture3D, Texture3D)
  780. RID rid;
  781. Vector3i size = Vector3i(1, 1, 1);
  782. protected:
  783. static void _bind_methods();
  784. public:
  785. void set_size(const Vector3i &p_size);
  786. Vector3i get_size() const;
  787. virtual Image::Format get_format() const override;
  788. virtual int get_width() const override;
  789. virtual int get_height() const override;
  790. virtual int get_depth() const override;
  791. virtual bool has_mipmaps() const override;
  792. virtual Vector<Ref<Image>> get_data() const override;
  793. PlaceholderTexture3D();
  794. ~PlaceholderTexture3D();
  795. };
  796. class PlaceholderTextureLayered : public TextureLayered {
  797. GDCLASS(PlaceholderTextureLayered, TextureLayered)
  798. RID rid;
  799. Size2i size = Size2i(1, 1);
  800. int layers = 1;
  801. LayeredType layered_type = LAYERED_TYPE_2D_ARRAY;
  802. protected:
  803. static void _bind_methods();
  804. public:
  805. void set_size(const Size2i &p_size);
  806. Size2i get_size() const;
  807. void set_layers(int p_layers);
  808. virtual Image::Format get_format() const override;
  809. virtual LayeredType get_layered_type() const override;
  810. virtual int get_width() const override;
  811. virtual int get_height() const override;
  812. virtual int get_layers() const override;
  813. virtual bool has_mipmaps() const override;
  814. virtual Ref<Image> get_layer_data(int p_layer) const override;
  815. PlaceholderTextureLayered(LayeredType p_type);
  816. ~PlaceholderTextureLayered();
  817. };
  818. class PlaceholderTexture2DArray : public PlaceholderTextureLayered {
  819. GDCLASS(PlaceholderTexture2DArray, PlaceholderTextureLayered)
  820. public:
  821. PlaceholderTexture2DArray() :
  822. PlaceholderTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  823. };
  824. class PlaceholderCubemap : public PlaceholderTextureLayered {
  825. GDCLASS(PlaceholderCubemap, PlaceholderTextureLayered)
  826. public:
  827. PlaceholderCubemap() :
  828. PlaceholderTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  829. };
  830. class PlaceholderCubemapArray : public PlaceholderTextureLayered {
  831. GDCLASS(PlaceholderCubemapArray, PlaceholderTextureLayered)
  832. public:
  833. PlaceholderCubemapArray() :
  834. PlaceholderTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  835. };
  836. #endif // TEXTURE_H