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