| 1 | /**************************************************************************/ |
| 2 | /* texture.cpp */ |
| 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 | #include "texture.h" |
| 32 | |
| 33 | #include "scene/resources/placeholder_textures.h" |
| 34 | |
| 35 | int Texture2D::get_width() const { |
| 36 | int ret = 0; |
| 37 | GDVIRTUAL_REQUIRED_CALL(_get_width, ret); |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | int Texture2D::get_height() const { |
| 42 | int ret = 0; |
| 43 | GDVIRTUAL_REQUIRED_CALL(_get_height, ret); |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | Size2 Texture2D::get_size() const { |
| 48 | return Size2(get_width(), get_height()); |
| 49 | } |
| 50 | |
| 51 | bool Texture2D::is_pixel_opaque(int p_x, int p_y) const { |
| 52 | bool ret = true; |
| 53 | GDVIRTUAL_CALL(_is_pixel_opaque, p_x, p_y, ret); |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | bool Texture2D::has_alpha() const { |
| 58 | bool ret = true; |
| 59 | GDVIRTUAL_CALL(_has_alpha, ret); |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | void Texture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const { |
| 64 | if (GDVIRTUAL_CALL(_draw, p_canvas_item, p_pos, p_modulate, p_transpose)) { |
| 65 | return; |
| 66 | } |
| 67 | RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, get_size()), get_rid(), false, p_modulate, p_transpose); |
| 68 | } |
| 69 | |
| 70 | void Texture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const { |
| 71 | if (GDVIRTUAL_CALL(_draw_rect, p_canvas_item, p_rect, p_tile, p_modulate, p_transpose)) { |
| 72 | return; |
| 73 | } |
| 74 | RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_rid(), p_tile, p_modulate, p_transpose); |
| 75 | } |
| 76 | |
| 77 | void Texture2D::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const { |
| 78 | if (GDVIRTUAL_CALL(_draw_rect_region, p_canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_clip_uv)) { |
| 79 | return; |
| 80 | } |
| 81 | RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, get_rid(), p_src_rect, p_modulate, p_transpose, p_clip_uv); |
| 82 | } |
| 83 | |
| 84 | bool Texture2D::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const { |
| 85 | r_rect = p_rect; |
| 86 | r_src_rect = p_src_rect; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | Ref<Resource> Texture2D::create_placeholder() const { |
| 91 | Ref<PlaceholderTexture2D> placeholder; |
| 92 | placeholder.instantiate(); |
| 93 | placeholder->set_size(get_size()); |
| 94 | return placeholder; |
| 95 | } |
| 96 | |
| 97 | void Texture2D::_bind_methods() { |
| 98 | ClassDB::bind_method(D_METHOD("get_width" ), &Texture2D::get_width); |
| 99 | ClassDB::bind_method(D_METHOD("get_height" ), &Texture2D::get_height); |
| 100 | ClassDB::bind_method(D_METHOD("get_size" ), &Texture2D::get_size); |
| 101 | ClassDB::bind_method(D_METHOD("has_alpha" ), &Texture2D::has_alpha); |
| 102 | ClassDB::bind_method(D_METHOD("draw" , "canvas_item" , "position" , "modulate" , "transpose" ), &Texture2D::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(false)); |
| 103 | ClassDB::bind_method(D_METHOD("draw_rect" , "canvas_item" , "rect" , "tile" , "modulate" , "transpose" ), &Texture2D::draw_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false)); |
| 104 | ClassDB::bind_method(D_METHOD("draw_rect_region" , "canvas_item" , "rect" , "src_rect" , "modulate" , "transpose" , "clip_uv" ), &Texture2D::draw_rect_region, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(true)); |
| 105 | ClassDB::bind_method(D_METHOD("get_image" ), &Texture2D::get_image); |
| 106 | ClassDB::bind_method(D_METHOD("create_placeholder" ), &Texture2D::create_placeholder); |
| 107 | |
| 108 | ADD_GROUP("" , "" ); |
| 109 | |
| 110 | GDVIRTUAL_BIND(_get_width); |
| 111 | GDVIRTUAL_BIND(_get_height); |
| 112 | GDVIRTUAL_BIND(_is_pixel_opaque, "x" , "y" ); |
| 113 | GDVIRTUAL_BIND(_has_alpha); |
| 114 | |
| 115 | GDVIRTUAL_BIND(_draw, "to_canvas_item" , "pos" , "modulate" , "transpose" ) |
| 116 | GDVIRTUAL_BIND(_draw_rect, "to_canvas_item" , "rect" , "tile" , "modulate" , "transpose" ) |
| 117 | GDVIRTUAL_BIND(_draw_rect_region, "to_canvas_item" , "rect" , "src_rect" , "modulate" , "transpose" , "clip_uv" ); |
| 118 | } |
| 119 | |
| 120 | Texture2D::Texture2D() { |
| 121 | } |
| 122 | |
| 123 | TypedArray<Image> Texture3D::_get_datai() const { |
| 124 | Vector<Ref<Image>> data = get_data(); |
| 125 | |
| 126 | TypedArray<Image> ret; |
| 127 | ret.resize(data.size()); |
| 128 | for (int i = 0; i < data.size(); i++) { |
| 129 | ret[i] = data[i]; |
| 130 | } |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | Image::Format Texture3D::get_format() const { |
| 135 | Image::Format ret = Image::FORMAT_MAX; |
| 136 | GDVIRTUAL_REQUIRED_CALL(_get_format, ret); |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | int Texture3D::get_width() const { |
| 141 | int ret = 0; |
| 142 | GDVIRTUAL_REQUIRED_CALL(_get_width, ret); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | int Texture3D::get_height() const { |
| 147 | int ret = 0; |
| 148 | GDVIRTUAL_REQUIRED_CALL(_get_height, ret); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | int Texture3D::get_depth() const { |
| 153 | int ret = 0; |
| 154 | GDVIRTUAL_REQUIRED_CALL(_get_depth, ret); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | bool Texture3D::has_mipmaps() const { |
| 159 | bool ret = false; |
| 160 | GDVIRTUAL_REQUIRED_CALL(_has_mipmaps, ret); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | Vector<Ref<Image>> Texture3D::get_data() const { |
| 165 | TypedArray<Image> ret; |
| 166 | GDVIRTUAL_REQUIRED_CALL(_get_data, ret); |
| 167 | Vector<Ref<Image>> data; |
| 168 | data.resize(ret.size()); |
| 169 | for (int i = 0; i < data.size(); i++) { |
| 170 | data.write[i] = ret[i]; |
| 171 | } |
| 172 | return data; |
| 173 | } |
| 174 | |
| 175 | void Texture3D::_bind_methods() { |
| 176 | ClassDB::bind_method(D_METHOD("get_format" ), &Texture3D::get_format); |
| 177 | ClassDB::bind_method(D_METHOD("get_width" ), &Texture3D::get_width); |
| 178 | ClassDB::bind_method(D_METHOD("get_height" ), &Texture3D::get_height); |
| 179 | ClassDB::bind_method(D_METHOD("get_depth" ), &Texture3D::get_depth); |
| 180 | ClassDB::bind_method(D_METHOD("has_mipmaps" ), &Texture3D::has_mipmaps); |
| 181 | ClassDB::bind_method(D_METHOD("get_data" ), &Texture3D::_get_datai); |
| 182 | ClassDB::bind_method(D_METHOD("create_placeholder" ), &Texture3D::create_placeholder); |
| 183 | |
| 184 | GDVIRTUAL_BIND(_get_format); |
| 185 | GDVIRTUAL_BIND(_get_width); |
| 186 | GDVIRTUAL_BIND(_get_height); |
| 187 | GDVIRTUAL_BIND(_get_depth); |
| 188 | GDVIRTUAL_BIND(_has_mipmaps); |
| 189 | GDVIRTUAL_BIND(_get_data); |
| 190 | } |
| 191 | |
| 192 | Ref<Resource> Texture3D::create_placeholder() const { |
| 193 | Ref<PlaceholderTexture3D> placeholder; |
| 194 | placeholder.instantiate(); |
| 195 | placeholder->set_size(Vector3i(get_width(), get_height(), get_depth())); |
| 196 | return placeholder; |
| 197 | } |
| 198 | |
| 199 | Image::Format TextureLayered::get_format() const { |
| 200 | Image::Format ret = Image::FORMAT_MAX; |
| 201 | GDVIRTUAL_REQUIRED_CALL(_get_format, ret); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | TextureLayered::LayeredType TextureLayered::get_layered_type() const { |
| 206 | uint32_t ret = LAYERED_TYPE_2D_ARRAY; |
| 207 | GDVIRTUAL_REQUIRED_CALL(_get_layered_type, ret); |
| 208 | return (LayeredType)ret; |
| 209 | } |
| 210 | |
| 211 | int TextureLayered::get_width() const { |
| 212 | int ret = 0; |
| 213 | GDVIRTUAL_REQUIRED_CALL(_get_width, ret); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | int TextureLayered::get_height() const { |
| 218 | int ret = 0; |
| 219 | GDVIRTUAL_REQUIRED_CALL(_get_height, ret); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | int TextureLayered::get_layers() const { |
| 224 | int ret = 0; |
| 225 | GDVIRTUAL_REQUIRED_CALL(_get_layers, ret); |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | bool TextureLayered::has_mipmaps() const { |
| 230 | bool ret = false; |
| 231 | GDVIRTUAL_REQUIRED_CALL(_has_mipmaps, ret); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | Ref<Image> TextureLayered::get_layer_data(int p_layer) const { |
| 236 | Ref<Image> ret; |
| 237 | GDVIRTUAL_REQUIRED_CALL(_get_layer_data, p_layer, ret); |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | void TextureLayered::_bind_methods() { |
| 242 | ClassDB::bind_method(D_METHOD("get_format" ), &TextureLayered::get_format); |
| 243 | ClassDB::bind_method(D_METHOD("get_layered_type" ), &TextureLayered::get_layered_type); |
| 244 | ClassDB::bind_method(D_METHOD("get_width" ), &TextureLayered::get_width); |
| 245 | ClassDB::bind_method(D_METHOD("get_height" ), &TextureLayered::get_height); |
| 246 | ClassDB::bind_method(D_METHOD("get_layers" ), &TextureLayered::get_layers); |
| 247 | ClassDB::bind_method(D_METHOD("has_mipmaps" ), &TextureLayered::has_mipmaps); |
| 248 | ClassDB::bind_method(D_METHOD("get_layer_data" , "layer" ), &TextureLayered::get_layer_data); |
| 249 | |
| 250 | BIND_ENUM_CONSTANT(LAYERED_TYPE_2D_ARRAY); |
| 251 | BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP); |
| 252 | BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP_ARRAY); |
| 253 | |
| 254 | GDVIRTUAL_BIND(_get_format); |
| 255 | GDVIRTUAL_BIND(_get_layered_type); |
| 256 | GDVIRTUAL_BIND(_get_width); |
| 257 | GDVIRTUAL_BIND(_get_height); |
| 258 | GDVIRTUAL_BIND(_get_layers); |
| 259 | GDVIRTUAL_BIND(_has_mipmaps); |
| 260 | GDVIRTUAL_BIND(_get_layer_data, "layer_index" ); |
| 261 | } |
| 262 | |