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