image_texture.h 6.9 KB

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