2014-02-10 02:10:30 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* texture.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "texture.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
#include "scene/resources/placeholder_textures.h"
|
2022-09-28 16:43:09 +02:00
|
|
|
|
2022-03-10 08:17:38 +01:00
|
|
|
int Texture2D::get_width() const {
|
2022-11-30 16:40:50 +01:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
|
|
|
|
return ret;
|
2022-03-10 08:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int Texture2D::get_height() const {
|
2022-11-30 16:40:50 +01:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
|
|
|
|
return ret;
|
2022-03-10 08:17:38 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
Size2 Texture2D::get_size() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return Size2(get_width(), get_height());
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
bool Texture2D::is_pixel_opaque(int p_x, int p_y) const {
|
2022-10-18 18:47:44 +02:00
|
|
|
bool ret = true;
|
|
|
|
GDVIRTUAL_CALL(_is_pixel_opaque, p_x, p_y, ret);
|
|
|
|
return ret;
|
2022-03-10 08:17:38 +01:00
|
|
|
}
|
2022-10-31 14:25:04 +01:00
|
|
|
|
2022-03-10 08:17:38 +01:00
|
|
|
bool Texture2D::has_alpha() const {
|
2022-10-18 18:47:44 +02:00
|
|
|
bool ret = true;
|
|
|
|
GDVIRTUAL_CALL(_has_alpha, ret);
|
|
|
|
return ret;
|
2018-08-24 03:10:15 +02:00
|
|
|
}
|
2020-02-12 09:59:06 +01:00
|
|
|
|
2020-10-24 17:15:43 +02:00
|
|
|
void Texture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
|
2022-03-10 08:17:38 +01:00
|
|
|
if (GDVIRTUAL_CALL(_draw, p_canvas_item, p_pos, p_modulate, p_transpose)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-24 17:15:43 +02:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, get_size()), get_rid(), false, p_modulate, p_transpose);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-02-12 09:59:06 +01:00
|
|
|
|
2020-10-24 17:15:43 +02:00
|
|
|
void Texture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
|
2022-03-10 08:17:38 +01:00
|
|
|
if (GDVIRTUAL_CALL(_draw_rect, p_canvas_item, p_rect, p_tile, p_modulate, p_transpose)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-24 17:15:43 +02:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_rid(), p_tile, p_modulate, p_transpose);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-02-12 09:59:06 +01:00
|
|
|
|
2020-10-24 17:15:43 +02:00
|
|
|
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 {
|
2022-03-10 08:17:38 +01:00
|
|
|
if (GDVIRTUAL_CALL(_draw_rect_region, p_canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_clip_uv)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-24 17:15:43 +02:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
bool Texture2D::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
|
2014-05-29 15:56:39 +02:00
|
|
|
r_rect = p_rect;
|
|
|
|
r_src_rect = p_src_rect;
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-12-20 22:49:11 +01:00
|
|
|
Ref<Resource> Texture2D::create_placeholder() const {
|
|
|
|
Ref<PlaceholderTexture2D> placeholder;
|
|
|
|
placeholder.instantiate();
|
|
|
|
placeholder->set_size(get_size());
|
|
|
|
return placeholder;
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
void Texture2D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_width"), &Texture2D::get_width);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_height"), &Texture2D::get_height);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_size"), &Texture2D::get_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_alpha"), &Texture2D::has_alpha);
|
2020-10-24 17:15:43 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("draw", "canvas_item", "position", "modulate", "transpose"), &Texture2D::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("draw_rect", "canvas_item", "rect", "tile", "modulate", "transpose"), &Texture2D::draw_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false));
|
|
|
|
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));
|
2021-03-28 13:32:17 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_image"), &Texture2D::get_image);
|
2022-12-20 22:49:11 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2D::create_placeholder);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2018-05-18 17:37:18 +02:00
|
|
|
ADD_GROUP("", "");
|
2022-03-10 08:17:38 +01:00
|
|
|
|
|
|
|
GDVIRTUAL_BIND(_get_width);
|
|
|
|
GDVIRTUAL_BIND(_get_height);
|
|
|
|
GDVIRTUAL_BIND(_is_pixel_opaque, "x", "y");
|
|
|
|
GDVIRTUAL_BIND(_has_alpha);
|
|
|
|
|
|
|
|
GDVIRTUAL_BIND(_draw, "to_canvas_item", "pos", "modulate", "transpose")
|
|
|
|
GDVIRTUAL_BIND(_draw_rect, "to_canvas_item", "rect", "tile", "modulate", "transpose")
|
2022-10-20 21:04:45 +02:00
|
|
|
GDVIRTUAL_BIND(_draw_rect_region, "to_canvas_item", "rect", "src_rect", "modulate", "transpose", "clip_uv");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
Texture2D::Texture2D() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
TypedArray<Image> Texture3D::_get_datai() const {
|
|
|
|
Vector<Ref<Image>> data = get_data();
|
2017-08-20 16:17:24 +02:00
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
TypedArray<Image> ret;
|
|
|
|
ret.resize(data.size());
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
ret[i] = data[i];
|
2017-08-20 16:17:24 +02:00
|
|
|
}
|
2023-07-11 22:29:09 +02:00
|
|
|
return ret;
|
2017-08-20 16:17:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
Image::Format Texture3D::get_format() const {
|
|
|
|
Image::Format ret = Image::FORMAT_MAX;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_format, ret);
|
|
|
|
return ret;
|
2017-08-20 16:17:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
int Texture3D::get_width() const {
|
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
|
|
|
|
return ret;
|
2017-08-20 16:17:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
int Texture3D::get_height() const {
|
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
|
|
|
|
return ret;
|
2017-08-20 16:17:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
int Texture3D::get_depth() const {
|
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_depth, ret);
|
|
|
|
return ret;
|
2017-08-20 16:17:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
bool Texture3D::has_mipmaps() const {
|
|
|
|
bool ret = false;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_has_mipmaps, ret);
|
|
|
|
return ret;
|
2017-08-20 16:17:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
Vector<Ref<Image>> Texture3D::get_data() const {
|
|
|
|
TypedArray<Image> ret;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_data, ret);
|
|
|
|
Vector<Ref<Image>> data;
|
|
|
|
data.resize(ret.size());
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
data.write[i] = ret[i];
|
2021-08-13 11:32:01 +02:00
|
|
|
}
|
2023-07-11 22:29:09 +02:00
|
|
|
return data;
|
2017-08-20 16:17:24 +02:00
|
|
|
}
|
2022-04-28 11:46:45 +02:00
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
void Texture3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_format"), &Texture3D::get_format);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_width"), &Texture3D::get_width);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_height"), &Texture3D::get_height);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_depth"), &Texture3D::get_depth);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_mipmaps"), &Texture3D::has_mipmaps);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_data"), &Texture3D::_get_datai);
|
|
|
|
ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture3D::create_placeholder);
|
2022-04-28 11:46:45 +02:00
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
GDVIRTUAL_BIND(_get_format);
|
|
|
|
GDVIRTUAL_BIND(_get_width);
|
|
|
|
GDVIRTUAL_BIND(_get_height);
|
|
|
|
GDVIRTUAL_BIND(_get_depth);
|
|
|
|
GDVIRTUAL_BIND(_has_mipmaps);
|
|
|
|
GDVIRTUAL_BIND(_get_data);
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
Ref<Resource> Texture3D::create_placeholder() const {
|
|
|
|
Ref<PlaceholderTexture3D> placeholder;
|
|
|
|
placeholder.instantiate();
|
|
|
|
placeholder->set_size(Vector3i(get_width(), get_height(), get_depth()));
|
|
|
|
return placeholder;
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
Image::Format TextureLayered::get_format() const {
|
|
|
|
Image::Format ret = Image::FORMAT_MAX;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_format, ret);
|
|
|
|
return ret;
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
TextureLayered::LayeredType TextureLayered::get_layered_type() const {
|
|
|
|
uint32_t ret = LAYERED_TYPE_2D_ARRAY;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_layered_type, ret);
|
|
|
|
return (LayeredType)ret;
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
int TextureLayered::get_width() const {
|
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
|
|
|
|
return ret;
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
int TextureLayered::get_height() const {
|
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
|
|
|
|
return ret;
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
int TextureLayered::get_layers() const {
|
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_layers, ret);
|
|
|
|
return ret;
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
bool TextureLayered::has_mipmaps() const {
|
|
|
|
bool ret = false;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_has_mipmaps, ret);
|
|
|
|
return ret;
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
Ref<Image> TextureLayered::get_layer_data(int p_layer) const {
|
|
|
|
Ref<Image> ret;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_layer_data, p_layer, ret);
|
|
|
|
return ret;
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
void TextureLayered::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_format"), &TextureLayered::get_format);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_layered_type"), &TextureLayered::get_layered_type);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_width"), &TextureLayered::get_width);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_height"), &TextureLayered::get_height);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_layers"), &TextureLayered::get_layers);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_mipmaps"), &TextureLayered::has_mipmaps);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_layer_data", "layer"), &TextureLayered::get_layer_data);
|
2022-04-28 11:46:45 +02:00
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
BIND_ENUM_CONSTANT(LAYERED_TYPE_2D_ARRAY);
|
|
|
|
BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP);
|
|
|
|
BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP_ARRAY);
|
2022-04-28 11:46:45 +02:00
|
|
|
|
2023-07-11 22:29:09 +02:00
|
|
|
GDVIRTUAL_BIND(_get_format);
|
|
|
|
GDVIRTUAL_BIND(_get_layered_type);
|
|
|
|
GDVIRTUAL_BIND(_get_width);
|
|
|
|
GDVIRTUAL_BIND(_get_height);
|
|
|
|
GDVIRTUAL_BIND(_get_layers);
|
|
|
|
GDVIRTUAL_BIND(_has_mipmaps);
|
|
|
|
GDVIRTUAL_BIND(_get_layer_data, "layer_index");
|
2022-04-28 11:46:45 +02:00
|
|
|
}
|