image_texture.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* image_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 IMAGE_TEXTURE_H
  31. #define IMAGE_TEXTURE_H
  32. #include "scene/resources/texture.h"
  33. class BitMap;
  34. class ImageTexture : public Texture2D {
  35. GDCLASS(ImageTexture, Texture2D);
  36. RES_BASE_EXTENSION("tex");
  37. mutable RID texture;
  38. Image::Format format = Image::FORMAT_L8;
  39. bool mipmaps = false;
  40. int w = 0;
  41. int h = 0;
  42. Size2 size_override;
  43. mutable Ref<BitMap> alpha_cache;
  44. bool image_stored = false;
  45. protected:
  46. virtual void reload_from_file() override;
  47. bool _set(const StringName &p_name, const Variant &p_value);
  48. bool _get(const StringName &p_name, Variant &r_ret) const;
  49. void _get_property_list(List<PropertyInfo> *p_list) const;
  50. static void _bind_methods();
  51. public:
  52. void set_image(const Ref<Image> &p_image);
  53. static Ref<ImageTexture> create_from_image(const Ref<Image> &p_image);
  54. Image::Format get_format() const;
  55. void update(const Ref<Image> &p_image);
  56. Ref<Image> get_image() const override;
  57. int get_width() const override;
  58. int get_height() const override;
  59. virtual RID get_rid() const override;
  60. bool has_alpha() const override;
  61. 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;
  62. 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;
  63. 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;
  64. bool is_pixel_opaque(int p_x, int p_y) const override;
  65. void set_size_override(const Size2i &p_size);
  66. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  67. ImageTexture();
  68. ~ImageTexture();
  69. };
  70. class ImageTextureLayered : public TextureLayered {
  71. GDCLASS(ImageTextureLayered, TextureLayered);
  72. LayeredType layered_type;
  73. mutable RID texture;
  74. Image::Format format = Image::FORMAT_L8;
  75. int width = 0;
  76. int height = 0;
  77. int layers = 0;
  78. bool mipmaps = false;
  79. Error _create_from_images(const TypedArray<Image> &p_images);
  80. TypedArray<Image> _get_images() const;
  81. void _set_images(const TypedArray<Image> &p_images);
  82. protected:
  83. static void _bind_methods();
  84. public:
  85. virtual Image::Format get_format() const override;
  86. virtual int get_width() const override;
  87. virtual int get_height() const override;
  88. virtual int get_layers() const override;
  89. virtual bool has_mipmaps() const override;
  90. virtual LayeredType get_layered_type() const override;
  91. Error create_from_images(Vector<Ref<Image>> p_images);
  92. void update_layer(const Ref<Image> &p_image, int p_layer);
  93. virtual Ref<Image> get_layer_data(int p_layer) const override;
  94. virtual RID get_rid() const override;
  95. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  96. ImageTextureLayered(LayeredType p_layered_type);
  97. ~ImageTextureLayered();
  98. };
  99. class ImageTexture3D : public Texture3D {
  100. GDCLASS(ImageTexture3D, Texture3D);
  101. mutable RID texture;
  102. Image::Format format = Image::FORMAT_L8;
  103. int width = 1;
  104. int height = 1;
  105. int depth = 1;
  106. bool mipmaps = false;
  107. TypedArray<Image> _get_images() const;
  108. void _set_images(const TypedArray<Image> &p_images);
  109. protected:
  110. static void _bind_methods();
  111. Error _create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const TypedArray<Image> &p_data);
  112. void _update(const TypedArray<Image> &p_data);
  113. public:
  114. virtual Image::Format get_format() const override;
  115. virtual int get_width() const override;
  116. virtual int get_height() const override;
  117. virtual int get_depth() const override;
  118. virtual bool has_mipmaps() const override;
  119. Error create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data);
  120. void update(const Vector<Ref<Image>> &p_data);
  121. virtual Vector<Ref<Image>> get_data() const override;
  122. virtual RID get_rid() const override;
  123. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  124. ImageTexture3D();
  125. ~ImageTexture3D();
  126. };
  127. class Texture2DArray : public ImageTextureLayered {
  128. GDCLASS(Texture2DArray, ImageTextureLayered)
  129. protected:
  130. static void _bind_methods();
  131. public:
  132. Texture2DArray() :
  133. ImageTextureLayered(LAYERED_TYPE_2D_ARRAY) {}
  134. virtual Ref<Resource> create_placeholder() const;
  135. };
  136. class Cubemap : public ImageTextureLayered {
  137. GDCLASS(Cubemap, ImageTextureLayered);
  138. protected:
  139. static void _bind_methods();
  140. public:
  141. Cubemap() :
  142. ImageTextureLayered(LAYERED_TYPE_CUBEMAP) {}
  143. virtual Ref<Resource> create_placeholder() const;
  144. };
  145. class CubemapArray : public ImageTextureLayered {
  146. GDCLASS(CubemapArray, ImageTextureLayered);
  147. protected:
  148. static void _bind_methods();
  149. public:
  150. CubemapArray() :
  151. ImageTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {}
  152. virtual Ref<Resource> create_placeholder() const;
  153. };
  154. #endif // IMAGE_TEXTURE_H