texture.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*************************************************************************/
  2. /* texture.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/rect2.h"
  35. #include "core/os/file_access.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. public:
  54. virtual int get_width() const = 0;
  55. virtual int get_height() const = 0;
  56. virtual Size2 get_size() const;
  57. virtual bool is_pixel_opaque(int p_x, int p_y) const;
  58. virtual bool has_alpha() const = 0;
  59. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const;
  60. 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;
  61. 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;
  62. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const;
  63. virtual Ref<Image> get_data() const { return Ref<Image>(); }
  64. Texture2D();
  65. };
  66. class BitMap;
  67. class ImageTexture : public Texture2D {
  68. GDCLASS(ImageTexture, Texture2D);
  69. RES_BASE_EXTENSION("tex");
  70. mutable RID texture;
  71. Image::Format format = Image::FORMAT_L8;
  72. bool mipmaps = false;
  73. int w = 0;
  74. int h = 0;
  75. Size2 size_override;
  76. mutable Ref<BitMap> alpha_cache;
  77. bool image_stored = false;
  78. protected:
  79. virtual void reload_from_file() override;
  80. bool _set(const StringName &p_name, const Variant &p_value);
  81. bool _get(const StringName &p_name, Variant &r_ret) const;
  82. void _get_property_list(List<PropertyInfo> *p_list) const;
  83. void _reload_hook(const RID &p_hook);
  84. virtual void _resource_path_changed() override;
  85. static void _bind_methods();
  86. public:
  87. void create_from_image(const Ref<Image> &p_image);
  88. Image::Format get_format() const;
  89. void update(const Ref<Image> &p_image, bool p_immediate = false);
  90. Ref<Image> get_data() const override;
  91. int get_width() const override;
  92. int get_height() const override;
  93. virtual RID get_rid() const override;
  94. bool has_alpha() const override;
  95. 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;
  96. 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;
  97. 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;
  98. bool is_pixel_opaque(int p_x, int p_y) const override;
  99. void set_size_override(const Size2 &p_size);
  100. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  101. ImageTexture();
  102. ~ImageTexture();
  103. };
  104. class StreamTexture2D : public Texture2D {
  105. GDCLASS(StreamTexture2D, Texture2D);
  106. public:
  107. enum DataFormat {
  108. DATA_FORMAT_IMAGE,
  109. DATA_FORMAT_LOSSLESS,
  110. DATA_FORMAT_LOSSY,
  111. DATA_FORMAT_BASIS_UNIVERSAL,
  112. };
  113. enum {
  114. FORMAT_VERSION = 1
  115. };
  116. enum FormatBits {
  117. FORMAT_MASK_IMAGE_FORMAT = (1 << 20) - 1,
  118. FORMAT_BIT_LOSSLESS = 1 << 20,
  119. FORMAT_BIT_LOSSY = 1 << 21,
  120. FORMAT_BIT_STREAM = 1 << 22,
  121. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  122. FORMAT_BIT_DETECT_3D = 1 << 24,
  123. //FORMAT_BIT_DETECT_SRGB = 1 << 25,
  124. FORMAT_BIT_DETECT_NORMAL = 1 << 26,
  125. FORMAT_BIT_DETECT_ROUGNESS = 1 << 27,
  126. };
  127. private:
  128. Error _load_data(const String &p_path, int &tw, int &th, int &tw_custom, int &th_custom, Ref<Image> &image, bool &r_request_3d, bool &r_request_normal, bool &r_request_roughness, int &mipmap_limit, int p_size_limit = 0);
  129. String path_to_file;
  130. mutable RID texture;
  131. Image::Format format = Image::FORMAT_MAX;
  132. int w = 0;
  133. int h = 0;
  134. mutable Ref<BitMap> alpha_cache;
  135. virtual void reload_from_file() override;
  136. static void _requested_3d(void *p_ud);
  137. static void _requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
  138. static void _requested_normal(void *p_ud);
  139. protected:
  140. static void _bind_methods();
  141. void _validate_property(PropertyInfo &property) const override;
  142. public:
  143. static Ref<Image> load_image_from_file(FileAccess *p_file, int p_size_limit);
  144. typedef void (*TextureFormatRequestCallback)(const Ref<StreamTexture2D> &);
  145. typedef void (*TextureFormatRoughnessRequestCallback)(const Ref<StreamTexture2D> &, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel);
  146. static TextureFormatRequestCallback request_3d_callback;
  147. static TextureFormatRoughnessRequestCallback request_roughness_callback;
  148. static TextureFormatRequestCallback request_normal_callback;
  149. Image::Format get_format() const;
  150. Error load(const String &p_path);
  151. String get_load_path() const;
  152. int get_width() const override;
  153. int get_height() const override;
  154. virtual RID get_rid() const override;
  155. virtual void set_path(const String &p_path, bool p_take_over) override;
  156. 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;
  157. 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;
  158. 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;
  159. virtual bool has_alpha() const override;
  160. bool is_pixel_opaque(int p_x, int p_y) const override;
  161. virtual Ref<Image> get_data() const override;
  162. StreamTexture2D();
  163. ~StreamTexture2D();
  164. };
  165. class ResourceFormatLoaderStreamTexture2D : public ResourceFormatLoader {
  166. public:
  167. virtual RES 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);
  168. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  169. virtual bool handles_type(const String &p_type) const;
  170. virtual String get_resource_type(const String &p_path) const;
  171. };
  172. class AtlasTexture : public Texture2D {
  173. GDCLASS(AtlasTexture, Texture2D);
  174. RES_BASE_EXTENSION("atlastex");
  175. protected:
  176. Ref<Texture2D> atlas;
  177. Rect2 region;
  178. Rect2 margin;
  179. bool filter_clip = false;
  180. static void _bind_methods();
  181. public:
  182. virtual int get_width() const override;
  183. virtual int get_height() const override;
  184. virtual RID get_rid() const override;
  185. virtual bool has_alpha() const override;
  186. void set_atlas(const Ref<Texture2D> &p_atlas);
  187. Ref<Texture2D> get_atlas() const;
  188. void set_region(const Rect2 &p_region);
  189. Rect2 get_region() const;
  190. void set_margin(const Rect2 &p_margin);
  191. Rect2 get_margin() const;
  192. void set_filter_clip(const bool p_enable);
  193. bool has_filter_clip() const;
  194. 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;
  195. 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;
  196. 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;
  197. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const override;
  198. bool is_pixel_opaque(int p_x, int p_y) const override;
  199. AtlasTexture();
  200. };
  201. class Mesh;
  202. class MeshTexture : public Texture2D {
  203. GDCLASS(MeshTexture, Texture2D);
  204. RES_BASE_EXTENSION("meshtex");
  205. Ref<Texture2D> base_texture;
  206. Ref<Mesh> mesh;
  207. Size2i size;
  208. protected:
  209. static void _bind_methods();
  210. public:
  211. virtual int get_width() const override;
  212. virtual int get_height() const override;
  213. virtual RID get_rid() const override;
  214. virtual bool has_alpha() const override;
  215. void set_mesh(const Ref<Mesh> &p_mesh);
  216. Ref<Mesh> get_mesh() const;
  217. void set_image_size(const Size2 &p_size);
  218. Size2 get_image_size() const;
  219. void set_base_texture(const Ref<Texture2D> &p_texture);
  220. Ref<Texture2D> get_base_texture() const;
  221. 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;
  222. 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;
  223. 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;
  224. virtual bool get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const override;
  225. bool is_pixel_opaque(int p_x, int p_y) const override;
  226. MeshTexture();
  227. };
  228. class LargeTexture : public Texture2D {
  229. GDCLASS(LargeTexture, Texture2D);
  230. RES_BASE_EXTENSION("largetex");
  231. protected:
  232. struct Piece {
  233. Point2 offset;
  234. Ref<Texture2D> texture;
  235. };
  236. Vector<Piece> pieces;
  237. Size2i size;
  238. Array _get_data() const;
  239. void _set_data(const Array &p_array);
  240. static void _bind_methods();
  241. public:
  242. virtual int get_width() const override;
  243. virtual int get_height() const override;
  244. virtual RID get_rid() const override;
  245. virtual bool has_alpha() const override;
  246. int add_piece(const Point2 &p_offset, const Ref<Texture2D> &p_texture);
  247. void set_piece_offset(int p_idx, const Point2 &p_offset);
  248. void set_piece_texture(int p_idx, const Ref<Texture2D> &p_texture);
  249. void set_size(const Size2 &p_size);
  250. void clear();
  251. int get_piece_count() const;
  252. Vector2 get_piece_offset(int p_idx) const;
  253. Ref<Texture2D> get_piece_texture(int p_idx) const;
  254. Ref<Image> to_image() const;
  255. 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;
  256. 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;
  257. 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;
  258. bool is_pixel_opaque(int p_x, int p_y) const override;
  259. LargeTexture();
  260. };
  261. class TextureLayered : public Texture {
  262. GDCLASS(TextureLayered, Texture);
  263. protected:
  264. static void _bind_methods();
  265. public:
  266. enum LayeredType {
  267. LAYERED_TYPE_2D_ARRAY,
  268. LAYERED_TYPE_CUBEMAP,
  269. LAYERED_TYPE_CUBEMAP_ARRAY
  270. };
  271. virtual Image::Format get_format() const = 0;
  272. virtual LayeredType get_layered_type() const = 0;
  273. virtual int get_width() const = 0;
  274. virtual int get_height() const = 0;
  275. virtual int get_layers() const = 0;
  276. virtual bool has_mipmaps() const = 0;
  277. virtual Ref<Image> get_layer_data(int p_layer) const = 0;
  278. };
  279. VARIANT_ENUM_CAST(TextureLayered::LayeredType)
  280. class ImageTextureLayered : public TextureLayered {
  281. GDCLASS(ImageTextureLayered, TextureLayered);
  282. LayeredType layered_type;
  283. mutable RID texture;
  284. Image::Format format = Image::FORMAT_MAX;
  285. int width = 0;
  286. int height = 0;
  287. int layers = 0;
  288. bool mipmaps = false;
  289. Error _create_from_images(const Array &p_images);
  290. Array _get_images() const;
  291. protected:
  292. static void _bind_methods();
  293. public:
  294. virtual Image::Format get_format() const override;
  295. virtual int get_width() const override;
  296. virtual int get_height() const override;
  297. virtual int get_layers() const override;
  298. virtual bool has_mipmaps() const override;
  299. virtual LayeredType get_layered_type() const override;
  300. Error create_from_images(Vector<Ref<Image>> p_images);
  301. void update_layer(const Ref<Image> &p_image, int p_layer);
  302. virtual Ref<Image> get_layer_data(int p_layer) const override;
  303. virtual RID get_rid() const override;
  304. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  305. ImageTextureLayered(LayeredType p_layered_type);
  306. ~ImageTextureLayered();
  307. };
  308. class Texture2DArray : public ImageTextureLayered {
  309. GDCLASS(Texture2DArray, ImageTextureLayered)
  310. public:
  311. Texture2DArray() :
  312. ImageTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  313. };
  314. class Cubemap : public ImageTextureLayered {
  315. GDCLASS(Cubemap, ImageTextureLayered);
  316. public:
  317. Cubemap() :
  318. ImageTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  319. };
  320. class CubemapArray : public ImageTextureLayered {
  321. GDCLASS(CubemapArray, ImageTextureLayered);
  322. public:
  323. CubemapArray() :
  324. ImageTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  325. };
  326. class StreamTextureLayered : public TextureLayered {
  327. GDCLASS(StreamTextureLayered, TextureLayered);
  328. public:
  329. enum DataFormat {
  330. DATA_FORMAT_IMAGE,
  331. DATA_FORMAT_LOSSLESS,
  332. DATA_FORMAT_LOSSY,
  333. DATA_FORMAT_BASIS_UNIVERSAL,
  334. };
  335. enum {
  336. FORMAT_VERSION = 1
  337. };
  338. enum FormatBits {
  339. FORMAT_MASK_IMAGE_FORMAT = (1 << 20) - 1,
  340. FORMAT_BIT_LOSSLESS = 1 << 20,
  341. FORMAT_BIT_LOSSY = 1 << 21,
  342. FORMAT_BIT_STREAM = 1 << 22,
  343. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  344. };
  345. private:
  346. Error _load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit = 0);
  347. String path_to_file;
  348. mutable RID texture;
  349. Image::Format format = Image::FORMAT_MAX;
  350. int w = 0;
  351. int h = 0;
  352. int layers = 0;
  353. bool mipmaps = false;
  354. LayeredType layered_type = LayeredType::LAYERED_TYPE_2D_ARRAY;
  355. virtual void reload_from_file() override;
  356. protected:
  357. static void _bind_methods();
  358. void _validate_property(PropertyInfo &property) const override;
  359. public:
  360. Image::Format get_format() const override;
  361. Error load(const String &p_path);
  362. String get_load_path() const;
  363. virtual LayeredType get_layered_type() const override;
  364. int get_width() const override;
  365. int get_height() const override;
  366. int get_layers() const override;
  367. virtual bool has_mipmaps() const override;
  368. virtual RID get_rid() const override;
  369. virtual void set_path(const String &p_path, bool p_take_over) override;
  370. virtual Ref<Image> get_layer_data(int p_layer) const override;
  371. StreamTextureLayered(LayeredType p_layered_type);
  372. ~StreamTextureLayered();
  373. };
  374. class StreamTexture2DArray : public StreamTextureLayered {
  375. GDCLASS(StreamTexture2DArray, StreamTextureLayered)
  376. public:
  377. StreamTexture2DArray() :
  378. StreamTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  379. };
  380. class StreamCubemap : public StreamTextureLayered {
  381. GDCLASS(StreamCubemap, StreamTextureLayered);
  382. public:
  383. StreamCubemap() :
  384. StreamTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  385. };
  386. class StreamCubemapArray : public StreamTextureLayered {
  387. GDCLASS(StreamCubemapArray, StreamTextureLayered);
  388. public:
  389. StreamCubemapArray() :
  390. StreamTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  391. };
  392. class ResourceFormatLoaderStreamTextureLayered : public ResourceFormatLoader {
  393. public:
  394. virtual RES 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);
  395. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  396. virtual bool handles_type(const String &p_type) const;
  397. virtual String get_resource_type(const String &p_path) const;
  398. };
  399. class Texture3D : public Texture {
  400. GDCLASS(Texture3D, Texture);
  401. protected:
  402. static void _bind_methods();
  403. TypedArray<Image> _get_data() const;
  404. public:
  405. virtual Image::Format get_format() const = 0;
  406. virtual int get_width() const = 0;
  407. virtual int get_height() const = 0;
  408. virtual int get_depth() const = 0;
  409. virtual bool has_mipmaps() const = 0;
  410. virtual Vector<Ref<Image>> get_data() const = 0;
  411. };
  412. class ImageTexture3D : public Texture3D {
  413. GDCLASS(ImageTexture3D, Texture3D);
  414. mutable RID texture;
  415. Image::Format format = Image::FORMAT_MAX;
  416. int width = 1;
  417. int height = 1;
  418. int depth = 1;
  419. bool mipmaps = false;
  420. protected:
  421. static void _bind_methods();
  422. Error _create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const TypedArray<Image> &p_data);
  423. void _update(const TypedArray<Image> &p_data);
  424. public:
  425. virtual Image::Format get_format() const override;
  426. virtual int get_width() const override;
  427. virtual int get_height() const override;
  428. virtual int get_depth() const override;
  429. virtual bool has_mipmaps() const override;
  430. Error create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data);
  431. void update(const Vector<Ref<Image>> &p_data);
  432. virtual Vector<Ref<Image>> get_data() const override;
  433. virtual RID get_rid() const override;
  434. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  435. ImageTexture3D();
  436. ~ImageTexture3D();
  437. };
  438. class StreamTexture3D : public Texture3D {
  439. GDCLASS(StreamTexture3D, Texture3D);
  440. public:
  441. enum DataFormat {
  442. DATA_FORMAT_IMAGE,
  443. DATA_FORMAT_LOSSLESS,
  444. DATA_FORMAT_LOSSY,
  445. DATA_FORMAT_BASIS_UNIVERSAL,
  446. };
  447. enum {
  448. FORMAT_VERSION = 1
  449. };
  450. enum FormatBits {
  451. FORMAT_MASK_IMAGE_FORMAT = (1 << 20) - 1,
  452. FORMAT_BIT_LOSSLESS = 1 << 20,
  453. FORMAT_BIT_LOSSY = 1 << 21,
  454. FORMAT_BIT_STREAM = 1 << 22,
  455. FORMAT_BIT_HAS_MIPMAPS = 1 << 23,
  456. };
  457. private:
  458. 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);
  459. String path_to_file;
  460. mutable RID texture;
  461. Image::Format format = Image::FORMAT_MAX;
  462. int w = 0;
  463. int h = 0;
  464. int d = 0;
  465. bool mipmaps = false;
  466. virtual void reload_from_file() override;
  467. protected:
  468. static void _bind_methods();
  469. void _validate_property(PropertyInfo &property) const override;
  470. public:
  471. Image::Format get_format() const override;
  472. Error load(const String &p_path);
  473. String get_load_path() const;
  474. int get_width() const override;
  475. int get_height() const override;
  476. int get_depth() const override;
  477. virtual bool has_mipmaps() const override;
  478. virtual RID get_rid() const override;
  479. virtual void set_path(const String &p_path, bool p_take_over) override;
  480. virtual Vector<Ref<Image>> get_data() const override;
  481. StreamTexture3D();
  482. ~StreamTexture3D();
  483. };
  484. class ResourceFormatLoaderStreamTexture3D : public ResourceFormatLoader {
  485. public:
  486. virtual RES 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);
  487. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  488. virtual bool handles_type(const String &p_type) const;
  489. virtual String get_resource_type(const String &p_path) const;
  490. };
  491. class CurveTexture : public Texture2D {
  492. GDCLASS(CurveTexture, Texture2D);
  493. RES_BASE_EXTENSION("curvetex")
  494. private:
  495. mutable RID _texture;
  496. Ref<Curve> _curve;
  497. int _width = 2048;
  498. void _update();
  499. protected:
  500. static void _bind_methods();
  501. public:
  502. void set_width(int p_width);
  503. int get_width() const override;
  504. void ensure_default_setup(float p_min = 0, float p_max = 1);
  505. void set_curve(Ref<Curve> p_curve);
  506. Ref<Curve> get_curve() const;
  507. virtual RID get_rid() const override;
  508. virtual int get_height() const override { return 1; }
  509. virtual bool has_alpha() const override { return false; }
  510. CurveTexture();
  511. ~CurveTexture();
  512. };
  513. /*
  514. enum CubeMapSide {
  515. CUBEMAP_LEFT,
  516. CUBEMAP_RIGHT,
  517. CUBEMAP_BOTTOM,
  518. CUBEMAP_TOP,
  519. CUBEMAP_FRONT,
  520. CUBEMAP_BACK,
  521. };
  522. */
  523. //VARIANT_ENUM_CAST( Texture::CubeMapSide );
  524. class GradientTexture : public Texture2D {
  525. GDCLASS(GradientTexture, Texture2D);
  526. public:
  527. struct Point {
  528. float offset = 0.0;
  529. Color color;
  530. bool operator<(const Point &p_ponit) const {
  531. return offset < p_ponit.offset;
  532. }
  533. };
  534. private:
  535. Ref<Gradient> gradient;
  536. bool update_pending = false;
  537. RID texture;
  538. int width = 2048;
  539. void _queue_update();
  540. void _update();
  541. protected:
  542. static void _bind_methods();
  543. public:
  544. void set_gradient(Ref<Gradient> p_gradient);
  545. Ref<Gradient> get_gradient() const;
  546. void set_width(int p_width);
  547. int get_width() const override;
  548. virtual RID get_rid() const override { return texture; }
  549. virtual int get_height() const override { return 1; }
  550. virtual bool has_alpha() const override { return true; }
  551. virtual Ref<Image> get_data() const override;
  552. GradientTexture();
  553. virtual ~GradientTexture();
  554. };
  555. class ProxyTexture : public Texture2D {
  556. GDCLASS(ProxyTexture, Texture2D);
  557. private:
  558. mutable RID proxy_ph;
  559. mutable RID proxy;
  560. Ref<Texture2D> base;
  561. protected:
  562. static void _bind_methods();
  563. public:
  564. void set_base(const Ref<Texture2D> &p_texture);
  565. Ref<Texture2D> get_base() const;
  566. virtual int get_width() const override;
  567. virtual int get_height() const override;
  568. virtual RID get_rid() const override;
  569. virtual bool has_alpha() const override;
  570. ProxyTexture();
  571. ~ProxyTexture();
  572. };
  573. class AnimatedTexture : public Texture2D {
  574. GDCLASS(AnimatedTexture, Texture2D);
  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_ph;
  583. RID proxy;
  584. struct Frame {
  585. Ref<Texture2D> texture;
  586. float delay_sec = 0.0;
  587. };
  588. Frame frames[MAX_FRAMES];
  589. int frame_count = 1.0;
  590. int current_frame = 0;
  591. bool pause = false;
  592. bool oneshot = false;
  593. float fps = 4.0;
  594. float time = 0.0;
  595. uint64_t prev_ticks = 0;
  596. void _update_proxy();
  597. protected:
  598. static void _bind_methods();
  599. void _validate_property(PropertyInfo &property) const override;
  600. public:
  601. void set_frames(int p_frames);
  602. int get_frames() const;
  603. void set_current_frame(int p_frame);
  604. int get_current_frame() const;
  605. void set_pause(bool p_pause);
  606. bool get_pause() const;
  607. void set_oneshot(bool p_oneshot);
  608. bool get_oneshot() const;
  609. void set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture);
  610. Ref<Texture2D> get_frame_texture(int p_frame) const;
  611. void set_frame_delay(int p_frame, float p_delay_sec);
  612. float get_frame_delay(int p_frame) const;
  613. void set_fps(float p_fps);
  614. float get_fps() const;
  615. virtual int get_width() const override;
  616. virtual int get_height() const override;
  617. virtual RID get_rid() const override;
  618. virtual bool has_alpha() const override;
  619. virtual Ref<Image> get_data() const override;
  620. bool is_pixel_opaque(int p_x, int p_y) const override;
  621. AnimatedTexture();
  622. ~AnimatedTexture();
  623. };
  624. class CameraTexture : public Texture2D {
  625. GDCLASS(CameraTexture, Texture2D);
  626. private:
  627. int camera_feed_id = 0;
  628. CameraServer::FeedImage which_feed = CameraServer::FEED_RGBA_IMAGE;
  629. protected:
  630. static void _bind_methods();
  631. public:
  632. virtual int get_width() const override;
  633. virtual int get_height() const override;
  634. virtual RID get_rid() const override;
  635. virtual bool has_alpha() const override;
  636. virtual void set_flags(uint32_t p_flags);
  637. virtual uint32_t get_flags() const;
  638. virtual Ref<Image> get_data() const override;
  639. void set_camera_feed_id(int p_new_id);
  640. int get_camera_feed_id() const;
  641. void set_which_feed(CameraServer::FeedImage p_which);
  642. CameraServer::FeedImage get_which_feed() const;
  643. void set_camera_active(bool p_active);
  644. bool get_camera_active() const;
  645. CameraTexture();
  646. ~CameraTexture();
  647. };
  648. #endif