2023-01-05 12:25:55 +00: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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "texture.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
|
|
|
#include "core/core_string_names.h"
|
|
|
|
#include "core/io/image_loader.h"
|
2022-03-24 17:18:55 +00:00
|
|
|
#include "core/io/marshalls.h"
|
2021-10-12 14:22:30 +00:00
|
|
|
#include "core/math/geometry_2d.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "core/os/os.h"
|
2019-02-12 16:18:13 +00:00
|
|
|
#include "scene/resources/bit_map.h"
|
2022-08-20 00:15:46 +00:00
|
|
|
#include "scene/resources/mesh.h"
|
2017-08-20 14:17:24 +00:00
|
|
|
#include "servers/camera/camera_feed.h"
|
2022-09-28 14:43:09 +00:00
|
|
|
|
2022-03-10 07:17:38 +00:00
|
|
|
int Texture2D::get_width() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Texture2D::get_height() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Size2 Texture2D::get_size() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return Size2(get_width(), get_height());
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
bool Texture2D::is_pixel_opaque(int p_x, int p_y) const {
|
2022-10-18 16:47:44 +00:00
|
|
|
bool ret = true;
|
|
|
|
GDVIRTUAL_CALL(_is_pixel_opaque, p_x, p_y, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
2022-10-31 13:25:04 +00:00
|
|
|
|
2022-03-10 07:17:38 +00:00
|
|
|
bool Texture2D::has_alpha() const {
|
2022-10-18 16:47:44 +00:00
|
|
|
bool ret = true;
|
|
|
|
GDVIRTUAL_CALL(_has_alpha, ret);
|
|
|
|
return ret;
|
2018-08-24 01:10:15 +00:00
|
|
|
}
|
2020-02-12 08:59:06 +00:00
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void Texture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
|
2022-03-10 07:17:38 +00:00
|
|
|
if (GDVIRTUAL_CALL(_draw, p_canvas_item, p_pos, p_modulate, p_transpose)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-24 15:15:43 +00: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 01:10:30 +00:00
|
|
|
}
|
2020-02-12 08:59:06 +00:00
|
|
|
|
2020-10-24 15:15:43 +00: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 07:17:38 +00:00
|
|
|
if (GDVIRTUAL_CALL(_draw_rect, p_canvas_item, p_rect, p_tile, p_modulate, p_transpose)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-24 15:15:43 +00: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 01:10:30 +00:00
|
|
|
}
|
2020-02-12 08:59:06 +00:00
|
|
|
|
2020-10-24 15:15:43 +00: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 07:17:38 +00: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 15:15:43 +00: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 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00: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 13:56:39 +00:00
|
|
|
r_rect = p_rect;
|
|
|
|
r_src_rect = p_src_rect;
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-12-20 21:49:11 +00:00
|
|
|
Ref<Resource> Texture2D::create_placeholder() const {
|
|
|
|
Ref<PlaceholderTexture2D> placeholder;
|
|
|
|
placeholder.instantiate();
|
|
|
|
placeholder->set_size(get_size());
|
|
|
|
return placeholder;
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00: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 15:15:43 +00: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 11:32:17 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_image"), &Texture2D::get_image);
|
2022-12-20 21:49:11 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2D::create_placeholder);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2018-05-18 15:37:18 +00:00
|
|
|
ADD_GROUP("", "");
|
2022-03-10 07:17:38 +00: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 19:04:45 +00:00
|
|
|
GDVIRTUAL_BIND(_draw_rect_region, "to_canvas_item", "rect", "src_rect", "modulate", "transpose", "clip_uv");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Texture2D::Texture2D() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////
|
|
|
|
|
|
|
|
void ImageTexture::reload_from_file() {
|
2017-06-28 20:00:18 +00:00
|
|
|
String path = ResourceLoader::path_remap(get_path());
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!path.is_resource_file()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Ref<Image> img;
|
2021-06-17 22:03:09 +00:00
|
|
|
img.instantiate();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-05-08 15:20:42 +00:00
|
|
|
if (ImageLoader::load_image(path, img) == OK) {
|
2022-05-03 23:49:20 +00:00
|
|
|
set_image(img);
|
2019-05-08 15:20:42 +00:00
|
|
|
} else {
|
|
|
|
Resource::reload_from_file();
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2019-09-25 18:48:48 +00:00
|
|
|
emit_changed();
|
2019-05-08 15:20:42 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 22:50:25 +00:00
|
|
|
bool ImageTexture::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
if (p_name == "image") {
|
2022-05-03 23:49:20 +00:00
|
|
|
set_image(p_value);
|
2022-06-08 22:06:11 +00:00
|
|
|
return true;
|
2022-06-07 22:50:25 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageTexture::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
if (p_name == "image") {
|
|
|
|
r_ret = get_image();
|
2022-06-08 22:06:11 +00:00
|
|
|
return true;
|
2022-06-07 22:50:25 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageTexture::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("image"), PROPERTY_HINT_RESOURCE_TYPE, "Image", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT));
|
|
|
|
}
|
|
|
|
|
2022-05-03 23:49:20 +00:00
|
|
|
Ref<ImageTexture> ImageTexture::create_from_image(const Ref<Image> &p_image) {
|
2023-05-05 18:33:01 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(p_image.is_null(), Ref<ImageTexture>(), "Invalid image: null");
|
|
|
|
ERR_FAIL_COND_V_MSG(p_image->is_empty(), Ref<ImageTexture>(), "Invalid image: image is empty");
|
2022-05-03 23:49:20 +00:00
|
|
|
|
|
|
|
Ref<ImageTexture> image_texture;
|
|
|
|
image_texture.instantiate();
|
|
|
|
image_texture->set_image(p_image);
|
|
|
|
return image_texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageTexture::set_image(const Ref<Image> &p_image) {
|
2021-03-03 23:53:55 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_image.is_null() || p_image->is_empty(), "Invalid image");
|
2017-05-17 10:36:47 +00:00
|
|
|
w = p_image->get_width();
|
|
|
|
h = p_image->get_height();
|
|
|
|
format = p_image->get_format();
|
2019-06-11 18:43:37 +00:00
|
|
|
mipmaps = p_image->has_mipmaps();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_null()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
texture = RenderingServer::get_singleton()->texture_2d_create(p_image);
|
2019-06-11 18:43:37 +00:00
|
|
|
} else {
|
2020-03-27 18:21:27 +00:00
|
|
|
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_image);
|
|
|
|
RenderingServer::get_singleton()->texture_replace(texture, new_texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2019-09-25 18:48:48 +00:00
|
|
|
emit_changed();
|
2019-01-25 16:39:43 +00:00
|
|
|
|
2019-02-21 23:49:42 +00:00
|
|
|
image_stored = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Image::Format ImageTexture::get_format() const {
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2021-07-01 02:17:47 +00:00
|
|
|
void ImageTexture::update(const Ref<Image> &p_image) {
|
2020-09-29 14:27:13 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_image.is_null(), "Invalid image");
|
|
|
|
ERR_FAIL_COND_MSG(texture.is_null(), "Texture is not initialized.");
|
|
|
|
ERR_FAIL_COND_MSG(p_image->get_width() != w || p_image->get_height() != h,
|
|
|
|
"The new image dimensions must match the texture size.");
|
|
|
|
ERR_FAIL_COND_MSG(p_image->get_format() != format,
|
|
|
|
"The new image format must match the texture's image format.");
|
|
|
|
ERR_FAIL_COND_MSG(mipmaps != p_image->has_mipmaps(),
|
|
|
|
"The new image mipmaps configuration must match the texture's image mipmaps configuration");
|
2018-07-25 09:23:34 +00:00
|
|
|
|
2022-03-12 11:19:59 +00:00
|
|
|
RS::get_singleton()->texture_2d_update(texture, p_image);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2019-09-25 18:48:48 +00:00
|
|
|
emit_changed();
|
|
|
|
|
2018-08-24 01:10:15 +00:00
|
|
|
alpha_cache.unref();
|
2019-02-21 23:49:42 +00:00
|
|
|
image_stored = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 11:32:17 +00:00
|
|
|
Ref<Image> ImageTexture::get_image() const {
|
2019-02-21 23:49:42 +00:00
|
|
|
if (image_stored) {
|
2020-03-27 18:21:27 +00:00
|
|
|
return RenderingServer::get_singleton()->texture_2d_get(texture);
|
2019-02-21 23:49:42 +00:00
|
|
|
} else {
|
|
|
|
return Ref<Image>();
|
2019-01-25 16:39:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ImageTexture::get_width() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
return w;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ImageTexture::get_height() const {
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
RID ImageTexture::get_rid() const {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_null()) {
|
|
|
|
//we are in trouble, create something temporary
|
2020-03-27 18:21:27 +00:00
|
|
|
texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageTexture::has_alpha() const {
|
2016-10-03 19:33:42 +00:00
|
|
|
return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void ImageTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((w | h) == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, Size2(w, h)), texture, false, p_modulate, p_transpose);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void ImageTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((w | h) == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void ImageTexture::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 {
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((w | h) == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, p_clip_uv);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 01:10:15 +00:00
|
|
|
bool ImageTexture::is_pixel_opaque(int p_x, int p_y) const {
|
|
|
|
if (!alpha_cache.is_valid()) {
|
2021-03-28 11:32:17 +00:00
|
|
|
Ref<Image> img = get_image();
|
2018-08-24 01:10:15 +00:00
|
|
|
if (img.is_valid()) {
|
|
|
|
if (img->is_compressed()) { //must decompress, if compressed
|
|
|
|
Ref<Image> decom = img->duplicate();
|
|
|
|
decom->decompress();
|
|
|
|
img = decom;
|
|
|
|
}
|
2021-06-17 22:03:09 +00:00
|
|
|
alpha_cache.instantiate();
|
2018-08-24 01:10:15 +00:00
|
|
|
alpha_cache->create_from_image_alpha(img);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha_cache.is_valid()) {
|
|
|
|
int aw = int(alpha_cache->get_size().width);
|
|
|
|
int ah = int(alpha_cache->get_size().height);
|
|
|
|
if (aw == 0 || ah == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = p_x * aw / w;
|
|
|
|
int y = p_y * ah / h;
|
|
|
|
|
|
|
|
x = CLAMP(x, 0, aw);
|
2019-01-15 04:59:45 +00:00
|
|
|
y = CLAMP(y, 0, ah);
|
2018-08-24 01:10:15 +00:00
|
|
|
|
2022-09-01 16:39:17 +00:00
|
|
|
return alpha_cache->get_bit(x, y);
|
2018-08-24 01:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-09 20:43:34 +00:00
|
|
|
void ImageTexture::set_size_override(const Size2i &p_size) {
|
|
|
|
Size2i s = p_size;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (s.x != 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
w = s.x;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (s.y != 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
h = s.y;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->texture_set_size_override(texture, w, h);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 12:50:44 +00:00
|
|
|
void ImageTexture::set_path(const String &p_path, bool p_take_over) {
|
|
|
|
if (texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
2015-10-21 12:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Resource::set_path(p_path, p_take_over);
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ImageTexture::_bind_methods() {
|
2022-05-03 23:49:20 +00:00
|
|
|
ClassDB::bind_static_method("ImageTexture", D_METHOD("create_from_image", "image"), &ImageTexture::create_from_image);
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_format"), &ImageTexture::get_format);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2022-07-17 21:55:46 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_image", "image"), &ImageTexture::set_image);
|
2021-07-01 02:17:47 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("update", "image"), &ImageTexture::update);
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_size_override", "size"), &ImageTexture::set_size_override);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:24:36 +00:00
|
|
|
ImageTexture::ImageTexture() {}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ImageTexture::~ImageTexture() {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->free(texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:18:55 +00:00
|
|
|
/////////////////////
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::_set_data(const Vector<uint8_t> &p_data) {
|
|
|
|
if (p_data.size() == 0) {
|
|
|
|
return; //nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *data = p_data.ptr();
|
|
|
|
uint32_t data_size = p_data.size();
|
|
|
|
ERR_FAIL_COND(data_size < 20);
|
|
|
|
compression_mode = CompressionMode(decode_uint32(data + 0));
|
|
|
|
format = Image::Format(decode_uint32(data + 4));
|
|
|
|
uint32_t mipmap_count = decode_uint32(data + 8);
|
|
|
|
size.width = decode_uint32(data + 12);
|
|
|
|
size.height = decode_uint32(data + 16);
|
|
|
|
mipmaps = mipmap_count > 1;
|
|
|
|
|
|
|
|
data += 20;
|
|
|
|
data_size -= 20;
|
|
|
|
|
|
|
|
Ref<Image> image;
|
|
|
|
|
|
|
|
switch (compression_mode) {
|
|
|
|
case COMPRESSION_MODE_LOSSLESS:
|
|
|
|
case COMPRESSION_MODE_LOSSY: {
|
|
|
|
Vector<uint8_t> image_data;
|
|
|
|
|
|
|
|
ERR_FAIL_COND(data_size < 4);
|
|
|
|
for (uint32_t i = 0; i < mipmap_count; i++) {
|
|
|
|
uint32_t mipsize = decode_uint32(data);
|
|
|
|
data += 4;
|
|
|
|
data_size -= 4;
|
|
|
|
ERR_FAIL_COND(mipsize < data_size);
|
|
|
|
Ref<Image> img = memnew(Image(data, data_size));
|
|
|
|
ERR_FAIL_COND(img->is_empty());
|
|
|
|
if (img->get_format() != format) { // May happen due to webp/png in the tiny mipmaps.
|
|
|
|
img->convert(format);
|
|
|
|
}
|
|
|
|
image_data.append_array(img->get_data());
|
|
|
|
|
|
|
|
data += mipsize;
|
|
|
|
data_size -= mipsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
image = Ref<Image>(memnew(Image(size.width, size.height, mipmap_count > 1, format, image_data)));
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case COMPRESSION_MODE_BASIS_UNIVERSAL: {
|
|
|
|
ERR_FAIL_COND(!Image::basis_universal_unpacker_ptr);
|
|
|
|
image = Image::basis_universal_unpacker_ptr(data, data_size);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case COMPRESSION_MODE_S3TC:
|
|
|
|
case COMPRESSION_MODE_ETC2:
|
|
|
|
case COMPRESSION_MODE_BPTC: {
|
|
|
|
image = Ref<Image>(memnew(Image(size.width, size.height, mipmap_count > 1, format, p_data.slice(20))));
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
ERR_FAIL_COND(image.is_null());
|
|
|
|
|
|
|
|
if (texture.is_null()) {
|
|
|
|
texture = RenderingServer::get_singleton()->texture_2d_create(image);
|
|
|
|
} else {
|
|
|
|
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(image);
|
|
|
|
RenderingServer::get_singleton()->texture_replace(texture, new_texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
image_stored = true;
|
|
|
|
RenderingServer::get_singleton()->texture_set_size_override(texture, size_override.width, size_override.height);
|
|
|
|
alpha_cache.unref();
|
|
|
|
|
|
|
|
if (keep_all_compressed_buffers || keep_compressed_buffer) {
|
|
|
|
compressed_buffer = p_data;
|
|
|
|
} else {
|
|
|
|
compressed_buffer.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PortableCompressedTexture2D::CompressionMode PortableCompressedTexture2D::get_compression_mode() const {
|
|
|
|
return compression_mode;
|
|
|
|
}
|
|
|
|
Vector<uint8_t> PortableCompressedTexture2D::_get_data() const {
|
|
|
|
return compressed_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::create_from_image(const Ref<Image> &p_image, CompressionMode p_compression_mode, bool p_normal_map, float p_lossy_quality) {
|
|
|
|
ERR_FAIL_COND(p_image.is_null() || p_image->is_empty());
|
|
|
|
|
|
|
|
Vector<uint8_t> buffer;
|
|
|
|
|
|
|
|
buffer.resize(20);
|
|
|
|
encode_uint32(p_compression_mode, buffer.ptrw());
|
|
|
|
encode_uint32(p_image->get_format(), buffer.ptrw() + 4);
|
|
|
|
encode_uint32(p_image->get_mipmap_count() + 1, buffer.ptrw() + 8);
|
|
|
|
encode_uint32(p_image->get_width(), buffer.ptrw() + 12);
|
|
|
|
encode_uint32(p_image->get_height(), buffer.ptrw() + 16);
|
|
|
|
|
|
|
|
switch (p_compression_mode) {
|
|
|
|
case COMPRESSION_MODE_LOSSLESS:
|
|
|
|
case COMPRESSION_MODE_LOSSY: {
|
|
|
|
for (int i = 0; i < p_image->get_mipmap_count() + 1; i++) {
|
|
|
|
Vector<uint8_t> data;
|
|
|
|
if (p_compression_mode == COMPRESSION_MODE_LOSSY) {
|
|
|
|
data = Image::webp_lossy_packer(p_image->get_image_from_mipmap(i), p_lossy_quality);
|
|
|
|
} else {
|
|
|
|
data = Image::webp_lossless_packer(p_image->get_image_from_mipmap(i));
|
|
|
|
}
|
|
|
|
int data_len = data.size();
|
|
|
|
buffer.resize(buffer.size() + 4);
|
|
|
|
encode_uint32(data_len, buffer.ptrw() + buffer.size() - 4);
|
|
|
|
buffer.append_array(data);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case COMPRESSION_MODE_BASIS_UNIVERSAL: {
|
|
|
|
Image::UsedChannels uc = p_image->detect_used_channels(p_normal_map ? Image::COMPRESS_SOURCE_NORMAL : Image::COMPRESS_SOURCE_GENERIC);
|
|
|
|
Vector<uint8_t> budata = Image::basis_universal_packer(p_image, uc);
|
|
|
|
buffer.append_array(budata);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case COMPRESSION_MODE_S3TC:
|
|
|
|
case COMPRESSION_MODE_ETC2:
|
|
|
|
case COMPRESSION_MODE_BPTC: {
|
|
|
|
Ref<Image> copy = p_image->duplicate();
|
|
|
|
switch (p_compression_mode) {
|
|
|
|
case COMPRESSION_MODE_S3TC:
|
|
|
|
copy->compress(Image::COMPRESS_S3TC);
|
|
|
|
break;
|
|
|
|
case COMPRESSION_MODE_ETC2:
|
|
|
|
copy->compress(Image::COMPRESS_ETC2);
|
|
|
|
break;
|
|
|
|
case COMPRESSION_MODE_BPTC:
|
|
|
|
copy->compress(Image::COMPRESS_BPTC);
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.append_array(copy->get_data());
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_set_data(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Image::Format PortableCompressedTexture2D::get_format() const {
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> PortableCompressedTexture2D::get_image() const {
|
|
|
|
if (image_stored) {
|
|
|
|
return RenderingServer::get_singleton()->texture_2d_get(texture);
|
|
|
|
} else {
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int PortableCompressedTexture2D::get_width() const {
|
|
|
|
return size.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PortableCompressedTexture2D::get_height() const {
|
|
|
|
return size.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
RID PortableCompressedTexture2D::get_rid() const {
|
|
|
|
if (texture.is_null()) {
|
|
|
|
//we are in trouble, create something temporary
|
|
|
|
texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PortableCompressedTexture2D::has_alpha() const {
|
|
|
|
return (format == Image::FORMAT_LA8 || format == Image::FORMAT_RGBA8);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
|
|
|
|
if (size.width == 0 || size.height == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), texture, false, p_modulate, p_transpose);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
|
|
|
|
if (size.width == 0 || size.height == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::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 {
|
|
|
|
if (size.width == 0 || size.height == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, p_clip_uv);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PortableCompressedTexture2D::is_pixel_opaque(int p_x, int p_y) const {
|
|
|
|
if (!alpha_cache.is_valid()) {
|
|
|
|
Ref<Image> img = get_image();
|
|
|
|
if (img.is_valid()) {
|
|
|
|
if (img->is_compressed()) { //must decompress, if compressed
|
|
|
|
Ref<Image> decom = img->duplicate();
|
|
|
|
decom->decompress();
|
|
|
|
img = decom;
|
|
|
|
}
|
|
|
|
alpha_cache.instantiate();
|
|
|
|
alpha_cache->create_from_image_alpha(img);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha_cache.is_valid()) {
|
|
|
|
int aw = int(alpha_cache->get_size().width);
|
|
|
|
int ah = int(alpha_cache->get_size().height);
|
|
|
|
if (aw == 0 || ah == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = p_x * aw / size.width;
|
|
|
|
int y = p_y * ah / size.height;
|
|
|
|
|
|
|
|
x = CLAMP(x, 0, aw);
|
|
|
|
y = CLAMP(y, 0, ah);
|
|
|
|
|
2022-09-01 16:39:17 +00:00
|
|
|
return alpha_cache->get_bit(x, y);
|
2022-03-24 17:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::set_size_override(const Size2 &p_size) {
|
|
|
|
size_override = p_size;
|
|
|
|
RenderingServer::get_singleton()->texture_set_size_override(texture, size_override.width, size_override.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 PortableCompressedTexture2D::get_size_override() const {
|
|
|
|
return size_override;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::set_path(const String &p_path, bool p_take_over) {
|
|
|
|
if (texture.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Resource::set_path(p_path, p_take_over);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PortableCompressedTexture2D::keep_all_compressed_buffers = false;
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::set_keep_all_compressed_buffers(bool p_keep) {
|
|
|
|
keep_all_compressed_buffers = p_keep;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PortableCompressedTexture2D::is_keeping_all_compressed_buffers() {
|
|
|
|
return keep_all_compressed_buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::set_keep_compressed_buffer(bool p_keep) {
|
|
|
|
keep_compressed_buffer = p_keep;
|
|
|
|
if (!p_keep) {
|
|
|
|
compressed_buffer.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PortableCompressedTexture2D::is_keeping_compressed_buffer() const {
|
|
|
|
return keep_compressed_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortableCompressedTexture2D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("create_from_image", "image", "compression_mode", "normal_map", "lossy_quality"), &PortableCompressedTexture2D::create_from_image, DEFVAL(false), DEFVAL(0.8));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_format"), &PortableCompressedTexture2D::get_format);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_compression_mode"), &PortableCompressedTexture2D::get_compression_mode);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_size_override", "size"), &PortableCompressedTexture2D::set_size_override);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_size_override"), &PortableCompressedTexture2D::get_size_override);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_keep_compressed_buffer", "keep"), &PortableCompressedTexture2D::set_keep_compressed_buffer);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_keeping_compressed_buffer"), &PortableCompressedTexture2D::is_keeping_compressed_buffer);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_data", "data"), &PortableCompressedTexture2D::_set_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_data"), &PortableCompressedTexture2D::_get_data);
|
|
|
|
|
|
|
|
ClassDB::bind_static_method("PortableCompressedTexture2D", D_METHOD("set_keep_all_compressed_buffers", "keep"), &PortableCompressedTexture2D::set_keep_all_compressed_buffers);
|
|
|
|
ClassDB::bind_static_method("PortableCompressedTexture2D", D_METHOD("is_keeping_all_compressed_buffers"), &PortableCompressedTexture2D::is_keeping_all_compressed_buffers);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "_set_data", "_get_data");
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size_override", PROPERTY_HINT_NONE, "suffix:px"), "set_size_override", "get_size_override");
|
2022-03-24 17:18:55 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_compressed_buffer"), "set_keep_compressed_buffer", "is_keeping_compressed_buffer");
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESSION_MODE_LOSSLESS);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESSION_MODE_LOSSY);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESSION_MODE_BASIS_UNIVERSAL);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESSION_MODE_S3TC);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESSION_MODE_ETC2);
|
|
|
|
BIND_ENUM_CONSTANT(COMPRESSION_MODE_BPTC);
|
|
|
|
}
|
|
|
|
|
|
|
|
PortableCompressedTexture2D::PortableCompressedTexture2D() {}
|
|
|
|
|
|
|
|
PortableCompressedTexture2D::~PortableCompressedTexture2D() {
|
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2022-03-24 17:18:55 +00:00
|
|
|
RenderingServer::get_singleton()->free(texture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 12:45:45 +00:00
|
|
|
//////////////////////////////////////////
|
|
|
|
|
2022-03-23 09:08:58 +00:00
|
|
|
Ref<Image> CompressedTexture2D::load_image_from_file(Ref<FileAccess> f, int p_size_limit) {
|
2019-09-27 02:16:44 +00:00
|
|
|
uint32_t data_format = f->get_32();
|
|
|
|
uint32_t w = f->get_16();
|
|
|
|
uint32_t h = f->get_16();
|
|
|
|
uint32_t mipmaps = f->get_32();
|
|
|
|
Image::Format format = Image::Format(f->get_32());
|
2017-02-06 03:38:39 +00:00
|
|
|
|
2022-07-19 00:58:27 +00:00
|
|
|
if (data_format == DATA_FORMAT_PNG || data_format == DATA_FORMAT_WEBP) {
|
2022-10-28 18:17:49 +00:00
|
|
|
//look for a PNG or WebP file inside
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
int sw = w;
|
|
|
|
int sh = h;
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
//mipmaps need to be read independently, they will be later combined
|
2020-03-17 06:33:00 +00:00
|
|
|
Vector<Ref<Image>> mipmap_images;
|
2019-03-26 17:51:13 +00:00
|
|
|
uint64_t total_size = 0;
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mipmaps + 1; i++) {
|
|
|
|
uint32_t size = f->get_32();
|
|
|
|
|
|
|
|
if (p_size_limit > 0 && i < (mipmaps - 1) && (sw > p_size_limit || sh > p_size_limit)) {
|
|
|
|
//can't load this due to size limit
|
|
|
|
sw = MAX(sw >> 1, 1);
|
|
|
|
sh = MAX(sh >> 1, 1);
|
|
|
|
f->seek(f->get_position() + size);
|
|
|
|
continue;
|
2017-02-16 11:55:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> pv;
|
2017-02-01 12:45:45 +00:00
|
|
|
pv.resize(size);
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *wr = pv.ptrw();
|
|
|
|
f->get_buffer(wr, size);
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Ref<Image> img;
|
2022-07-19 00:58:27 +00:00
|
|
|
if (data_format == DATA_FORMAT_PNG && Image::png_unpacker) {
|
2021-04-12 16:56:50 +00:00
|
|
|
img = Image::png_unpacker(pv);
|
2021-07-29 13:05:03 +00:00
|
|
|
} else if (data_format == DATA_FORMAT_WEBP && Image::webp_unpacker) {
|
2021-04-12 16:56:50 +00:00
|
|
|
img = Image::webp_unpacker(pv);
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 12:04:21 +00:00
|
|
|
if (img.is_null() || img->is_empty()) {
|
|
|
|
ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
|
2019-09-27 02:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
//format will actually be the format of the first image,
|
|
|
|
//as it may have changed on compression
|
|
|
|
format = img->get_format();
|
|
|
|
first = false;
|
|
|
|
} else if (img->get_format() != format) {
|
|
|
|
img->convert(format); //all needs to be the same format
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
2017-05-23 11:55:32 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
total_size += img->get_data().size();
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
mipmap_images.push_back(img);
|
2019-09-27 02:16:44 +00:00
|
|
|
|
|
|
|
sw = MAX(sw >> 1, 1);
|
|
|
|
sh = MAX(sh >> 1, 1);
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 08:32:52 +00:00
|
|
|
//print_line("mipmap read total: " + itos(mipmap_images.size()));
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
Ref<Image> image;
|
2021-06-17 22:03:09 +00:00
|
|
|
image.instantiate();
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
if (mipmap_images.size() == 1) {
|
2019-09-27 02:16:44 +00:00
|
|
|
//only one image (which will most likely be the case anyway for this format)
|
2017-02-01 12:45:45 +00:00
|
|
|
image = mipmap_images[0];
|
2019-09-27 02:16:44 +00:00
|
|
|
return image;
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
} else {
|
2019-09-27 02:16:44 +00:00
|
|
|
//rarer use case, but needs to be supported
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> img_data;
|
2017-02-01 12:45:45 +00:00
|
|
|
img_data.resize(total_size);
|
|
|
|
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *wr = img_data.ptrw();
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
int ofs = 0;
|
|
|
|
for (int i = 0; i < mipmap_images.size(); i++) {
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> id = mipmap_images[i]->get_data();
|
2017-02-01 12:45:45 +00:00
|
|
|
int len = id.size();
|
2020-02-17 21:06:54 +00:00
|
|
|
const uint8_t *r = id.ptr();
|
2021-04-27 14:19:21 +00:00
|
|
|
memcpy(&wr[ofs], r, len);
|
2017-02-01 12:45:45 +00:00
|
|
|
ofs += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 18:06:19 +00:00
|
|
|
image->set_data(w, h, true, mipmap_images[0]->get_format(), img_data);
|
2019-09-27 02:16:44 +00:00
|
|
|
return image;
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 00:58:27 +00:00
|
|
|
} else if (data_format == DATA_FORMAT_BASIS_UNIVERSAL) {
|
|
|
|
int sw = w;
|
|
|
|
int sh = h;
|
|
|
|
uint32_t size = f->get_32();
|
|
|
|
if (p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {
|
|
|
|
//can't load this due to size limit
|
|
|
|
sw = MAX(sw >> 1, 1);
|
|
|
|
sh = MAX(sh >> 1, 1);
|
|
|
|
f->seek(f->get_position() + size);
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
Vector<uint8_t> pv;
|
|
|
|
pv.resize(size);
|
|
|
|
{
|
|
|
|
uint8_t *wr = pv.ptrw();
|
|
|
|
f->get_buffer(wr, size);
|
|
|
|
}
|
|
|
|
Ref<Image> img;
|
|
|
|
img = Image::basis_universal_unpacker(pv);
|
|
|
|
if (img.is_null() || img->is_empty()) {
|
|
|
|
ERR_FAIL_COND_V(img.is_null() || img->is_empty(), Ref<Image>());
|
|
|
|
}
|
|
|
|
format = img->get_format();
|
|
|
|
sw = MAX(sw >> 1, 1);
|
|
|
|
sh = MAX(sh >> 1, 1);
|
|
|
|
return img;
|
2019-09-27 02:16:44 +00:00
|
|
|
} else if (data_format == DATA_FORMAT_IMAGE) {
|
|
|
|
int size = Image::get_image_data_size(w, h, format, mipmaps ? true : false);
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
for (uint32_t i = 0; i < mipmaps + 1; i++) {
|
|
|
|
int tw, th;
|
|
|
|
int ofs = Image::get_image_mipmap_offset_and_dimensions(w, h, format, i, tw, th);
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
if (p_size_limit > 0 && i < mipmaps && (p_size_limit > tw || p_size_limit > th)) {
|
|
|
|
if (ofs) {
|
|
|
|
f->seek(f->get_position() + ofs);
|
|
|
|
}
|
|
|
|
continue; //oops, size limit enforced, go to next
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> data;
|
2019-09-27 02:16:44 +00:00
|
|
|
data.resize(size - ofs);
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *wr = data.ptrw();
|
|
|
|
f->get_buffer(wr, data.size());
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 18:06:19 +00:00
|
|
|
Ref<Image> image = Image::create_from_data(tw, th, mipmaps - i ? true : false, format, data);
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
return Ref<Image>();
|
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::set_path(const String &p_path, bool p_take_over) {
|
2019-09-27 02:16:44 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
2019-09-27 02:16:44 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
Resource::set_path(p_path, p_take_over);
|
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::_requested_3d(void *p_ud) {
|
2022-03-10 03:22:36 +00:00
|
|
|
CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
|
|
|
|
Ref<CompressedTexture2D> ctex(ct);
|
2019-09-27 02:16:44 +00:00
|
|
|
ERR_FAIL_COND(!request_3d_callback);
|
2022-03-10 03:22:36 +00:00
|
|
|
request_3d_callback(ctex);
|
2019-09-27 02:16:44 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::_requested_roughness(void *p_ud, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_roughness_channel) {
|
2022-03-10 03:22:36 +00:00
|
|
|
CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
|
|
|
|
Ref<CompressedTexture2D> ctex(ct);
|
2019-09-27 02:16:44 +00:00
|
|
|
ERR_FAIL_COND(!request_roughness_callback);
|
2022-03-10 03:22:36 +00:00
|
|
|
request_roughness_callback(ctex, p_normal_path, p_roughness_channel);
|
2019-09-27 02:16:44 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::_requested_normal(void *p_ud) {
|
2022-03-10 03:22:36 +00:00
|
|
|
CompressedTexture2D *ct = (CompressedTexture2D *)p_ud;
|
|
|
|
Ref<CompressedTexture2D> ctex(ct);
|
2019-09-27 02:16:44 +00:00
|
|
|
ERR_FAIL_COND(!request_normal_callback);
|
2022-03-10 03:22:36 +00:00
|
|
|
request_normal_callback(ctex);
|
2019-09-27 02:16:44 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
CompressedTexture2D::TextureFormatRequestCallback CompressedTexture2D::request_3d_callback = nullptr;
|
|
|
|
CompressedTexture2D::TextureFormatRoughnessRequestCallback CompressedTexture2D::request_roughness_callback = nullptr;
|
|
|
|
CompressedTexture2D::TextureFormatRequestCallback CompressedTexture2D::request_normal_callback = nullptr;
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Image::Format CompressedTexture2D::get_format() const {
|
2019-09-27 02:16:44 +00:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Error CompressedTexture2D::_load_data(const String &p_path, int &r_width, int &r_height, Ref<Image> &image, bool &r_request_3d, bool &r_request_normal, bool &r_request_roughness, int &mipmap_limit, int p_size_limit) {
|
2019-09-27 02:16:44 +00:00
|
|
|
alpha_cache.unref();
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(image.is_null(), ERR_INVALID_PARAMETER);
|
|
|
|
|
2022-03-23 09:08:58 +00:00
|
|
|
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
|
2019-09-27 02:16:44 +00:00
|
|
|
|
|
|
|
uint8_t header[4];
|
|
|
|
f->get_buffer(header, 4);
|
|
|
|
if (header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != '2') {
|
2022-03-10 03:22:36 +00:00
|
|
|
ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is corrupt (Bad header).");
|
2019-09-27 02:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t version = f->get_32();
|
|
|
|
|
|
|
|
if (version > FORMAT_VERSION) {
|
2022-03-10 03:22:36 +00:00
|
|
|
ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
|
2019-09-27 02:16:44 +00:00
|
|
|
}
|
2021-05-25 15:03:12 +00:00
|
|
|
r_width = f->get_32();
|
|
|
|
r_height = f->get_32();
|
2019-09-27 02:16:44 +00:00
|
|
|
uint32_t df = f->get_32(); //data format
|
|
|
|
|
|
|
|
//skip reserved
|
|
|
|
mipmap_limit = int(f->get_32());
|
|
|
|
//reserved
|
|
|
|
f->get_32();
|
|
|
|
f->get_32();
|
|
|
|
f->get_32();
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
|
|
|
r_request_3d = request_3d_callback && df & FORMAT_BIT_DETECT_3D;
|
|
|
|
r_request_roughness = request_roughness_callback && df & FORMAT_BIT_DETECT_ROUGNESS;
|
|
|
|
r_request_normal = request_normal_callback && df & FORMAT_BIT_DETECT_NORMAL;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
r_request_3d = false;
|
|
|
|
r_request_roughness = false;
|
|
|
|
r_request_normal = false;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
if (!(df & FORMAT_BIT_STREAM)) {
|
|
|
|
p_size_limit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
image = load_image_from_file(f, p_size_limit);
|
|
|
|
|
2020-12-15 12:04:21 +00:00
|
|
|
if (image.is_null() || image->is_empty()) {
|
2019-09-27 02:16:44 +00:00
|
|
|
return ERR_CANT_OPEN;
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 02:16:44 +00:00
|
|
|
return OK;
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Error CompressedTexture2D::load(const String &p_path) {
|
2021-05-25 15:03:12 +00:00
|
|
|
int lw, lh;
|
2017-05-17 10:36:47 +00:00
|
|
|
Ref<Image> image;
|
2021-06-17 22:03:09 +00:00
|
|
|
image.instantiate();
|
2019-09-14 03:37:42 +00:00
|
|
|
|
|
|
|
bool request_3d;
|
|
|
|
bool request_normal;
|
|
|
|
bool request_roughness;
|
2019-09-27 02:16:44 +00:00
|
|
|
int mipmap_limit;
|
2019-09-14 03:37:42 +00:00
|
|
|
|
2021-05-25 15:03:12 +00:00
|
|
|
Error err = _load_data(p_path, lw, lh, image, request_3d, request_normal, request_roughness, mipmap_limit);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (err) {
|
2017-02-01 12:45:45 +00:00
|
|
|
return err;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
RS::get_singleton()->texture_replace(texture, new_texture);
|
2019-02-25 01:34:12 +00:00
|
|
|
} else {
|
2020-03-27 18:21:27 +00:00
|
|
|
texture = RS::get_singleton()->texture_2d_create(image);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2021-05-25 15:03:12 +00:00
|
|
|
if (lw || lh) {
|
|
|
|
RS::get_singleton()->texture_set_size_override(texture, lw, lh);
|
2019-02-25 01:34:12 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2021-05-25 15:03:12 +00:00
|
|
|
w = lw;
|
|
|
|
h = lh;
|
2017-02-01 12:45:45 +00:00
|
|
|
path_to_file = p_path;
|
2017-05-17 10:36:47 +00:00
|
|
|
format = image->get_format();
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2021-12-09 09:42:46 +00:00
|
|
|
if (get_path().is_empty()) {
|
2019-09-14 03:37:42 +00:00
|
|
|
//temporarily set path if no path set for resource, helps find errors
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
2019-09-14 03:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
|
|
|
if (request_3d) {
|
|
|
|
//print_line("request detect 3D at " + p_path);
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->texture_set_detect_3d_callback(texture, _requested_3d, this);
|
2019-09-14 03:37:42 +00:00
|
|
|
} else {
|
|
|
|
//print_line("not requesting detect 3D at " + p_path);
|
2020-04-01 23:20:12 +00:00
|
|
|
RS::get_singleton()->texture_set_detect_3d_callback(texture, nullptr, nullptr);
|
2019-09-14 03:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (request_roughness) {
|
|
|
|
//print_line("request detect srgb at " + p_path);
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->texture_set_detect_roughness_callback(texture, _requested_roughness, this);
|
2019-09-14 03:37:42 +00:00
|
|
|
} else {
|
|
|
|
//print_line("not requesting detect srgb at " + p_path);
|
2020-04-01 23:20:12 +00:00
|
|
|
RS::get_singleton()->texture_set_detect_roughness_callback(texture, nullptr, nullptr);
|
2019-09-14 03:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (request_normal) {
|
|
|
|
//print_line("request detect srgb at " + p_path);
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->texture_set_detect_normal_callback(texture, _requested_normal, this);
|
2019-09-14 03:37:42 +00:00
|
|
|
} else {
|
|
|
|
//print_line("not requesting detect normal at " + p_path);
|
2020-04-01 23:20:12 +00:00
|
|
|
RS::get_singleton()->texture_set_detect_normal_callback(texture, nullptr, nullptr);
|
2019-09-14 03:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2019-09-25 18:48:48 +00:00
|
|
|
emit_changed();
|
2017-02-01 12:45:45 +00:00
|
|
|
return OK;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
String CompressedTexture2D::get_load_path() const {
|
2017-02-01 12:45:45 +00:00
|
|
|
return path_to_file;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
int CompressedTexture2D::get_width() const {
|
2017-02-01 12:45:45 +00:00
|
|
|
return w;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
int CompressedTexture2D::get_height() const {
|
2017-02-01 12:45:45 +00:00
|
|
|
return h;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
RID CompressedTexture2D::get_rid() const {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (!texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
texture = RS::get_singleton()->texture_2d_placeholder_create();
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((w | h) == 0) {
|
2017-02-01 12:45:45 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, Size2(w, h)), texture, false, p_modulate, p_transpose);
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((w | h) == 0) {
|
2017-02-01 12:45:45 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, texture, p_tile, p_modulate, p_transpose);
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::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 {
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((w | h) == 0) {
|
2017-02-01 12:45:45 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, texture, p_src_rect, p_modulate, p_transpose, p_clip_uv);
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
bool CompressedTexture2D::has_alpha() const {
|
2017-02-01 12:45:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-15 11:29:46 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Ref<Image> CompressedTexture2D::get_image() const {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
return RS::get_singleton()->texture_2d_get(texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
} else {
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
2017-02-15 11:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
bool CompressedTexture2D::is_pixel_opaque(int p_x, int p_y) const {
|
2018-08-24 01:10:15 +00:00
|
|
|
if (!alpha_cache.is_valid()) {
|
2021-03-28 11:32:17 +00:00
|
|
|
Ref<Image> img = get_image();
|
2018-08-24 01:10:15 +00:00
|
|
|
if (img.is_valid()) {
|
|
|
|
if (img->is_compressed()) { //must decompress, if compressed
|
|
|
|
Ref<Image> decom = img->duplicate();
|
|
|
|
decom->decompress();
|
|
|
|
img = decom;
|
|
|
|
}
|
2019-02-25 01:34:12 +00:00
|
|
|
|
2021-06-17 22:03:09 +00:00
|
|
|
alpha_cache.instantiate();
|
2018-08-24 01:10:15 +00:00
|
|
|
alpha_cache->create_from_image_alpha(img);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha_cache.is_valid()) {
|
|
|
|
int aw = int(alpha_cache->get_size().width);
|
|
|
|
int ah = int(alpha_cache->get_size().height);
|
|
|
|
if (aw == 0 || ah == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = p_x * aw / w;
|
|
|
|
int y = p_y * ah / h;
|
|
|
|
|
|
|
|
x = CLAMP(x, 0, aw);
|
2019-01-15 04:59:45 +00:00
|
|
|
y = CLAMP(y, 0, ah);
|
2018-08-24 01:10:15 +00:00
|
|
|
|
2022-09-01 16:39:17 +00:00
|
|
|
return alpha_cache->get_bit(x, y);
|
2018-08-24 01:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::reload_from_file() {
|
2017-06-28 20:00:18 +00:00
|
|
|
String path = get_path();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!path.is_resource_file()) {
|
2017-06-28 20:00:18 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
path = ResourceLoader::path_remap(path); //remap for translation
|
|
|
|
path = ResourceLoader::import_remap(path); //remap for import
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!path.is_resource_file()) {
|
2017-06-28 20:00:18 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
load(path);
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 20:57:11 +00:00
|
|
|
void CompressedTexture2D::_validate_property(PropertyInfo &p_property) const {
|
2019-03-06 15:50:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture2D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture2D::load);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture2D::get_load_path);
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
CompressedTexture2D::CompressedTexture2D() {}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
CompressedTexture2D::~CompressedTexture2D() {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->free(texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 23:43:50 +00:00
|
|
|
Ref<Resource> ResourceFormatLoaderCompressedTexture2D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
|
2022-03-05 15:43:38 +00:00
|
|
|
Ref<CompressedTexture2D> st;
|
2021-06-17 22:03:09 +00:00
|
|
|
st.instantiate();
|
2017-02-01 12:45:45 +00:00
|
|
|
Error err = st->load(p_path);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (r_error) {
|
2017-02-01 12:45:45 +00:00
|
|
|
*r_error = err;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (err != OK) {
|
2022-05-02 23:43:50 +00:00
|
|
|
return Ref<Resource>();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void ResourceFormatLoaderCompressedTexture2D::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
p_extensions->push_back("ctex");
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
bool ResourceFormatLoaderCompressedTexture2D::handles_type(const String &p_type) const {
|
|
|
|
return p_type == "CompressedTexture2D";
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
String ResourceFormatLoaderCompressedTexture2D::get_resource_type(const String &p_path) const {
|
|
|
|
if (p_path.get_extension().to_lower() == "ctex") {
|
|
|
|
return "CompressedTexture2D";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:40:51 +00:00
|
|
|
////////////////////////////////////
|
|
|
|
|
2022-03-10 07:17:38 +00:00
|
|
|
TypedArray<Image> Texture3D::_get_datai() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
Vector<Ref<Image>> data = get_data();
|
|
|
|
|
|
|
|
TypedArray<Image> ret;
|
|
|
|
ret.resize(data.size());
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
ret[i] = data[i];
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-10 07:17:38 +00:00
|
|
|
Image::Format Texture3D::get_format() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
Image::Format ret = Image::FORMAT_MAX;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_format, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Texture3D::get_width() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Texture3D::get_height() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Texture3D::get_depth() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_depth, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Texture3D::has_mipmaps() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
bool ret = false;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_has_mipmaps, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Ref<Image>> Texture3D::get_data() const {
|
|
|
|
TypedArray<Image> ret;
|
2022-11-30 15:40:50 +00:00
|
|
|
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];
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
2022-11-30 15:40:50 +00:00
|
|
|
return data;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
2020-09-09 15:40:51 +00: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);
|
2022-03-10 07:17:38 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_data"), &Texture3D::_get_datai);
|
2022-12-20 21:49:11 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture3D::create_placeholder);
|
2022-03-10 07:17:38 +00: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);
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
2022-12-20 21:49:11 +00:00
|
|
|
|
|
|
|
Ref<Resource> Texture3D::create_placeholder() const {
|
|
|
|
Ref<PlaceholderTexture3D> placeholder;
|
|
|
|
placeholder.instantiate();
|
|
|
|
placeholder->set_size(Vector3i(get_width(), get_height(), get_depth()));
|
|
|
|
return placeholder;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
//////////////////////////////////////////
|
|
|
|
|
2020-09-09 15:40:51 +00:00
|
|
|
Image::Format ImageTexture3D::get_format() const {
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
int ImageTexture3D::get_width() const {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
int ImageTexture3D::get_height() const {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
int ImageTexture3D::get_depth() const {
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
bool ImageTexture3D::has_mipmaps() const {
|
|
|
|
return mipmaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error ImageTexture3D::_create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const TypedArray<Image> &p_data) {
|
|
|
|
Vector<Ref<Image>> images;
|
|
|
|
images.resize(p_data.size());
|
|
|
|
for (int i = 0; i < images.size(); i++) {
|
|
|
|
images.write[i] = p_data[i];
|
|
|
|
}
|
|
|
|
return create(p_format, p_width, p_height, p_depth, p_mipmaps, images);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageTexture3D::_update(const TypedArray<Image> &p_data) {
|
|
|
|
Vector<Ref<Image>> images;
|
|
|
|
images.resize(p_data.size());
|
|
|
|
for (int i = 0; i < images.size(); i++) {
|
|
|
|
images.write[i] = p_data[i];
|
|
|
|
}
|
|
|
|
return update(images);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error ImageTexture3D::create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) {
|
|
|
|
RID tex = RenderingServer::get_singleton()->texture_3d_create(p_format, p_width, p_height, p_depth, p_mipmaps, p_data);
|
|
|
|
ERR_FAIL_COND_V(tex.is_null(), ERR_CANT_CREATE);
|
|
|
|
|
|
|
|
if (texture.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->texture_replace(texture, tex);
|
2022-10-04 22:04:55 +00:00
|
|
|
} else {
|
|
|
|
texture = tex;
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 22:57:17 +00:00
|
|
|
format = p_format;
|
|
|
|
width = p_width;
|
|
|
|
height = p_height;
|
|
|
|
depth = p_depth;
|
|
|
|
mipmaps = p_mipmaps;
|
|
|
|
|
2020-09-09 15:40:51 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageTexture3D::update(const Vector<Ref<Image>> &p_data) {
|
|
|
|
ERR_FAIL_COND(!texture.is_valid());
|
|
|
|
RenderingServer::get_singleton()->texture_3d_update(texture, p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Ref<Image>> ImageTexture3D::get_data() const {
|
|
|
|
ERR_FAIL_COND_V(!texture.is_valid(), Vector<Ref<Image>>());
|
|
|
|
return RS::get_singleton()->texture_3d_get(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
RID ImageTexture3D::get_rid() const {
|
|
|
|
if (!texture.is_valid()) {
|
|
|
|
texture = RS::get_singleton()->texture_3d_placeholder_create();
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
void ImageTexture3D::set_path(const String &p_path, bool p_take_over) {
|
|
|
|
if (texture.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Resource::set_path(p_path, p_take_over);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageTexture3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("create", "format", "width", "height", "depth", "use_mipmaps", "data"), &ImageTexture3D::_create);
|
|
|
|
ClassDB::bind_method(D_METHOD("update", "data"), &ImageTexture3D::_update);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageTexture3D::ImageTexture3D() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageTexture3D::~ImageTexture3D() {
|
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-09-09 15:40:51 +00:00
|
|
|
RS::get_singleton()->free(texture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture3D::set_path(const String &p_path, bool p_take_over) {
|
2020-09-09 15:40:51 +00:00
|
|
|
if (texture.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Resource::set_path(p_path, p_take_over);
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Image::Format CompressedTexture3D::get_format() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Error CompressedTexture3D::_load_data(const String &p_path, Vector<Ref<Image>> &r_data, Image::Format &r_format, int &r_width, int &r_height, int &r_depth, bool &r_mipmaps) {
|
2022-03-23 09:08:58 +00:00
|
|
|
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
|
2020-09-09 15:40:51 +00:00
|
|
|
|
|
|
|
uint8_t header[4];
|
|
|
|
f->get_buffer(header, 4);
|
|
|
|
ERR_FAIL_COND_V(header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L', ERR_FILE_UNRECOGNIZED);
|
|
|
|
|
2022-03-10 03:22:36 +00:00
|
|
|
//stored as compressed textures (used for lossless and lossy compression)
|
2020-09-09 15:40:51 +00:00
|
|
|
uint32_t version = f->get_32();
|
|
|
|
|
|
|
|
if (version > FORMAT_VERSION) {
|
2022-03-10 03:22:36 +00:00
|
|
|
ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r_depth = f->get_32(); //depth
|
|
|
|
f->get_32(); //ignored (mode)
|
|
|
|
f->get_32(); // ignored (data format)
|
|
|
|
|
|
|
|
f->get_32(); //ignored
|
2022-09-29 09:53:28 +00:00
|
|
|
int mipmap_count = f->get_32();
|
2020-09-09 15:40:51 +00:00
|
|
|
f->get_32(); //ignored
|
|
|
|
f->get_32(); //ignored
|
|
|
|
|
2022-09-29 09:53:28 +00:00
|
|
|
r_mipmaps = mipmap_count != 0;
|
2020-09-09 15:40:51 +00:00
|
|
|
|
|
|
|
r_data.clear();
|
|
|
|
|
2022-09-29 09:53:28 +00:00
|
|
|
for (int i = 0; i < (r_depth + mipmap_count); i++) {
|
2022-03-05 15:43:38 +00:00
|
|
|
Ref<Image> image = CompressedTexture2D::load_image_from_file(f, 0);
|
2020-12-15 12:04:21 +00:00
|
|
|
ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
|
2020-09-09 15:40:51 +00:00
|
|
|
if (i == 0) {
|
|
|
|
r_format = image->get_format();
|
|
|
|
r_width = image->get_width();
|
|
|
|
r_height = image->get_height();
|
|
|
|
}
|
|
|
|
r_data.push_back(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Error CompressedTexture3D::load(const String &p_path) {
|
2020-09-09 15:40:51 +00:00
|
|
|
Vector<Ref<Image>> data;
|
|
|
|
|
|
|
|
int tw, th, td;
|
|
|
|
Image::Format tfmt;
|
|
|
|
bool tmm;
|
|
|
|
|
|
|
|
Error err = _load_data(p_path, data, tfmt, tw, th, td, tmm);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture.is_valid()) {
|
|
|
|
RID new_texture = RS::get_singleton()->texture_3d_create(tfmt, tw, th, td, tmm, data);
|
|
|
|
RS::get_singleton()->texture_replace(texture, new_texture);
|
|
|
|
} else {
|
|
|
|
texture = RS::get_singleton()->texture_3d_create(tfmt, tw, th, td, tmm, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
w = tw;
|
|
|
|
h = th;
|
|
|
|
d = td;
|
|
|
|
mipmaps = tmm;
|
|
|
|
format = tfmt;
|
|
|
|
|
|
|
|
path_to_file = p_path;
|
|
|
|
|
2021-12-09 09:42:46 +00:00
|
|
|
if (get_path().is_empty()) {
|
2020-09-09 15:40:51 +00:00
|
|
|
//temporarily set path if no path set for resource, helps find errors
|
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
|
|
|
}
|
|
|
|
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2020-09-09 15:40:51 +00:00
|
|
|
emit_changed();
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
String CompressedTexture3D::get_load_path() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
return path_to_file;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
int CompressedTexture3D::get_width() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
int CompressedTexture3D::get_height() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
int CompressedTexture3D::get_depth() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
bool CompressedTexture3D::has_mipmaps() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
return mipmaps;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
RID CompressedTexture3D::get_rid() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
if (!texture.is_valid()) {
|
|
|
|
texture = RS::get_singleton()->texture_3d_placeholder_create();
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Vector<Ref<Image>> CompressedTexture3D::get_data() const {
|
2020-09-09 15:40:51 +00:00
|
|
|
if (texture.is_valid()) {
|
|
|
|
return RS::get_singleton()->texture_3d_get(texture);
|
|
|
|
} else {
|
|
|
|
return Vector<Ref<Image>>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture3D::reload_from_file() {
|
2020-09-09 15:40:51 +00:00
|
|
|
String path = get_path();
|
|
|
|
if (!path.is_resource_file()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = ResourceLoader::path_remap(path); //remap for translation
|
|
|
|
path = ResourceLoader::import_remap(path); //remap for import
|
|
|
|
if (!path.is_resource_file()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
load(path);
|
|
|
|
}
|
|
|
|
|
2022-08-12 20:57:11 +00:00
|
|
|
void CompressedTexture3D::_validate_property(PropertyInfo &p_property) const {
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTexture3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTexture3D::load);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTexture3D::get_load_path);
|
2020-09-09 15:40:51 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
CompressedTexture3D::CompressedTexture3D() {}
|
2020-09-09 15:40:51 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
CompressedTexture3D::~CompressedTexture3D() {
|
2020-09-09 15:40:51 +00:00
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-09-09 15:40:51 +00:00
|
|
|
RS::get_singleton()->free(texture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
|
2022-05-02 23:43:50 +00:00
|
|
|
Ref<Resource> ResourceFormatLoaderCompressedTexture3D::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
|
2022-03-05 15:43:38 +00:00
|
|
|
Ref<CompressedTexture3D> st;
|
2021-06-17 22:03:09 +00:00
|
|
|
st.instantiate();
|
2020-09-09 15:40:51 +00:00
|
|
|
Error err = st->load(p_path);
|
|
|
|
if (r_error) {
|
|
|
|
*r_error = err;
|
|
|
|
}
|
|
|
|
if (err != OK) {
|
2022-05-02 23:43:50 +00:00
|
|
|
return Ref<Resource>();
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void ResourceFormatLoaderCompressedTexture3D::get_recognized_extensions(List<String> *p_extensions) const {
|
2022-03-10 03:22:36 +00:00
|
|
|
p_extensions->push_back("ctex3d");
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
bool ResourceFormatLoaderCompressedTexture3D::handles_type(const String &p_type) const {
|
|
|
|
return p_type == "CompressedTexture3D";
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
String ResourceFormatLoaderCompressedTexture3D::get_resource_type(const String &p_path) const {
|
2022-03-10 03:22:36 +00:00
|
|
|
if (p_path.get_extension().to_lower() == "ctex3d") {
|
2022-03-05 15:43:38 +00:00
|
|
|
return "CompressedTexture3D";
|
2020-09-09 15:40:51 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int AtlasTexture::get_width() const {
|
|
|
|
if (region.size.width == 0) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (atlas.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return atlas->get_width();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return region.size.width + margin.size.width;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int AtlasTexture::get_height() const {
|
|
|
|
if (region.size.height == 0) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (atlas.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return atlas->get_height();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return region.size.height + margin.size.height;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
RID AtlasTexture::get_rid() const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (atlas.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return atlas->get_rid();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return RID();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtlasTexture::has_alpha() const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (atlas.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return atlas->has_alpha();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
void AtlasTexture::set_atlas(const Ref<Texture2D> &p_atlas) {
|
2019-01-28 13:56:06 +00:00
|
|
|
ERR_FAIL_COND(p_atlas == this);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (atlas == p_atlas) {
|
2016-06-20 01:16:41 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2022-09-27 16:53:14 +00:00
|
|
|
// Support recursive AtlasTextures.
|
|
|
|
if (Ref<AtlasTexture>(atlas).is_valid()) {
|
|
|
|
atlas->disconnect(CoreStringNames::get_singleton()->changed, callable_mp((Resource *)this, &AtlasTexture::emit_changed));
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
atlas = p_atlas;
|
2022-09-27 16:53:14 +00:00
|
|
|
if (Ref<AtlasTexture>(atlas).is_valid()) {
|
|
|
|
atlas->connect(CoreStringNames::get_singleton()->changed, callable_mp((Resource *)this, &AtlasTexture::emit_changed));
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
emit_changed();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> AtlasTexture::get_atlas() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return atlas;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasTexture::set_region(const Rect2 &p_region) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (region == p_region) {
|
2017-03-29 23:30:24 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
region = p_region;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 AtlasTexture::get_region() const {
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasTexture::set_margin(const Rect2 &p_margin) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (margin == p_margin) {
|
2017-03-29 23:30:24 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
margin = p_margin;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 AtlasTexture::get_margin() const {
|
|
|
|
return margin;
|
|
|
|
}
|
|
|
|
|
2017-11-05 20:45:36 +00:00
|
|
|
void AtlasTexture::set_filter_clip(const bool p_enable) {
|
|
|
|
filter_clip = p_enable;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtlasTexture::has_filter_clip() const {
|
|
|
|
return filter_clip;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void AtlasTexture::_bind_methods() {
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_atlas", "atlas"), &AtlasTexture::set_atlas);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_atlas"), &AtlasTexture::get_atlas);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_region", "region"), &AtlasTexture::set_region);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_region"), &AtlasTexture::get_region);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_margin", "margin"), &AtlasTexture::set_margin);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_margin"), &AtlasTexture::get_margin);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-05 20:45:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_filter_clip", "enable"), &AtlasTexture::set_filter_clip);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_filter_clip"), &AtlasTexture::has_filter_clip);
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "atlas", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_atlas", "get_atlas");
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region", PROPERTY_HINT_NONE, "suffix:px"), "set_region", "get_region");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "margin", PROPERTY_HINT_NONE, "suffix:px"), "set_margin", "get_margin");
|
2017-11-05 20:45:36 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_clip"), "set_filter_clip", "has_filter_clip");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void AtlasTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!atlas.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
Fix many errors found by PVS-Studio
Fix errors 2, 3, 4, 6, 8, 9, 11, 12, 13, 14, and 15.
2018-11-28 02:58:00 +00:00
|
|
|
Rect2 rc = region;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (rc.size.width == 0) {
|
|
|
|
rc.size.width = atlas->get_width();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc.size.height == 0) {
|
|
|
|
rc.size.height = atlas->get_height();
|
|
|
|
}
|
|
|
|
|
2022-01-14 20:27:31 +00:00
|
|
|
atlas->draw_rect_region(p_canvas_item, Rect2(p_pos + margin.position, rc.size), rc, p_modulate, p_transpose, filter_clip);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!atlas.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
Fix many errors found by PVS-Studio
Fix errors 2, 3, 4, 6, 8, 9, 11, 12, 13, 14, and 15.
2018-11-28 02:58:00 +00:00
|
|
|
Rect2 rc = region;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (rc.size.width == 0) {
|
|
|
|
rc.size.width = atlas->get_width();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc.size.height == 0) {
|
|
|
|
rc.size.height = atlas->get_height();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 scale = p_rect.size / (region.size + margin.size);
|
2017-06-03 22:25:13 +00:00
|
|
|
Rect2 dr(p_rect.position + margin.position * scale, rc.size * scale);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-01-14 20:27:31 +00:00
|
|
|
atlas->draw_rect_region(p_canvas_item, dr, rc, p_modulate, p_transpose, filter_clip);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void AtlasTexture::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 {
|
2017-03-24 20:45:31 +00:00
|
|
|
//this might not necessarily work well if using a rect, needs to be fixed properly
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!atlas.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-08-18 15:37:50 +00:00
|
|
|
Rect2 dr;
|
|
|
|
Rect2 src_c;
|
|
|
|
get_rect_region(p_rect, p_src_rect, dr, src_c);
|
2014-03-19 13:02:17 +00:00
|
|
|
|
2022-01-14 20:27:31 +00:00
|
|
|
atlas->draw_rect_region(p_canvas_item, dr, src_c, p_modulate, p_transpose, filter_clip);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!atlas.is_valid()) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
|
|
|
Rect2 src = p_src_rect;
|
2018-08-18 15:37:50 +00:00
|
|
|
if (src.size == Size2()) {
|
2022-12-17 21:07:14 +00:00
|
|
|
src.size = region.size;
|
2018-08-18 15:37:50 +00:00
|
|
|
}
|
|
|
|
Vector2 scale = p_rect.size / src.size;
|
|
|
|
|
2022-12-17 21:07:14 +00:00
|
|
|
src.position += (region.position - margin.position);
|
|
|
|
Rect2 src_clipped = region.intersection(src);
|
|
|
|
if (src_clipped.size == Size2()) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2022-12-17 21:07:14 +00:00
|
|
|
Vector2 ofs = (src_clipped.position - src.position);
|
2014-05-29 13:56:39 +00:00
|
|
|
if (scale.x < 0) {
|
2022-12-17 21:07:14 +00:00
|
|
|
ofs.x += (src_clipped.size.x - src.size.x);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
if (scale.y < 0) {
|
2022-12-17 21:07:14 +00:00
|
|
|
ofs.y += (src_clipped.size.y - src.size.y);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2022-12-17 21:07:14 +00:00
|
|
|
r_rect = Rect2(p_rect.position + ofs * scale, src_clipped.size * scale);
|
|
|
|
r_src_rect = src_clipped;
|
2014-05-29 13:56:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-24 01:10:15 +00:00
|
|
|
bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!atlas.is_valid()) {
|
Fix many errors found by PVS-Studio
Fix errors 2, 3, 4, 6, 8, 9, 11, 12, 13, 14, and 15.
2018-11-28 02:58:00 +00:00
|
|
|
return true;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-08-24 01:10:15 +00:00
|
|
|
|
2018-12-20 13:40:40 +00:00
|
|
|
int x = p_x + region.position.x - margin.position.x;
|
|
|
|
int y = p_y + region.position.y - margin.position.y;
|
|
|
|
|
|
|
|
// margin edge may outside of atlas
|
2020-05-14 14:41:43 +00:00
|
|
|
if (x < 0 || x >= atlas->get_width()) {
|
2018-12-20 13:40:40 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (y < 0 || y >= atlas->get_height()) {
|
2018-12-20 13:40:40 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
Fix many errors found by PVS-Studio
Fix errors 2, 3, 4, 6, 8, 9, 11, 12, 13, 14, and 15.
2018-11-28 02:58:00 +00:00
|
|
|
|
|
|
|
return atlas->is_pixel_opaque(x, y);
|
2018-08-24 01:10:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 14:54:47 +00:00
|
|
|
Ref<Image> AtlasTexture::get_image() const {
|
2021-09-30 13:30:05 +00:00
|
|
|
if (!atlas.is_valid() || !atlas->get_image().is_valid()) {
|
2021-09-28 14:54:47 +00:00
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
|
2022-09-18 00:19:55 +00:00
|
|
|
return atlas->get_image()->get_region(region);
|
2021-09-28 14:54:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:24:36 +00:00
|
|
|
AtlasTexture::AtlasTexture() {}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
/////////////////////////////////////////
|
|
|
|
|
|
|
|
int MeshTexture::get_width() const {
|
|
|
|
return size.width;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
int MeshTexture::get_height() const {
|
|
|
|
return size.height;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
RID MeshTexture::get_rid() const {
|
|
|
|
return RID();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MeshTexture::has_alpha() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshTexture::set_mesh(const Ref<Mesh> &p_mesh) {
|
|
|
|
mesh = p_mesh;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
Ref<Mesh> MeshTexture::get_mesh() const {
|
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshTexture::set_image_size(const Size2 &p_size) {
|
|
|
|
size = p_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 MeshTexture::get_image_size() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
void MeshTexture::set_base_texture(const Ref<Texture2D> &p_texture) {
|
2019-04-19 18:54:33 +00:00
|
|
|
base_texture = p_texture;
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> MeshTexture::get_base_texture() const {
|
2019-04-19 18:54:33 +00:00
|
|
|
return base_texture;
|
|
|
|
}
|
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void MeshTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
|
2019-04-19 18:54:33 +00:00
|
|
|
if (mesh.is_null() || base_texture.is_null()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Transform2D xform;
|
|
|
|
xform.set_origin(p_pos);
|
|
|
|
if (p_transpose) {
|
2022-04-24 21:59:24 +00:00
|
|
|
SWAP(xform.columns[0][1], xform.columns[1][0]);
|
|
|
|
SWAP(xform.columns[0][0], xform.columns[1][1]);
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void MeshTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
|
2019-04-19 18:54:33 +00:00
|
|
|
if (mesh.is_null() || base_texture.is_null()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Transform2D xform;
|
|
|
|
Vector2 origin = p_rect.position;
|
|
|
|
if (p_rect.size.x < 0) {
|
|
|
|
origin.x += size.x;
|
|
|
|
}
|
|
|
|
if (p_rect.size.y < 0) {
|
|
|
|
origin.y += size.y;
|
|
|
|
}
|
|
|
|
xform.set_origin(origin);
|
|
|
|
xform.set_scale(p_rect.size / size);
|
|
|
|
|
|
|
|
if (p_transpose) {
|
2022-04-24 21:59:24 +00:00
|
|
|
SWAP(xform.columns[0][1], xform.columns[1][0]);
|
|
|
|
SWAP(xform.columns[0][0], xform.columns[1][1]);
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-10-24 15:15:43 +00:00
|
|
|
void MeshTexture::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 {
|
2019-04-19 18:54:33 +00:00
|
|
|
if (mesh.is_null() || base_texture.is_null()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Transform2D xform;
|
|
|
|
Vector2 origin = p_rect.position;
|
|
|
|
if (p_rect.size.x < 0) {
|
|
|
|
origin.x += size.x;
|
|
|
|
}
|
|
|
|
if (p_rect.size.y < 0) {
|
|
|
|
origin.y += size.y;
|
|
|
|
}
|
|
|
|
xform.set_origin(origin);
|
|
|
|
xform.set_scale(p_rect.size / size);
|
|
|
|
|
|
|
|
if (p_transpose) {
|
2022-04-24 21:59:24 +00:00
|
|
|
SWAP(xform.columns[0][1], xform.columns[1][0]);
|
|
|
|
SWAP(xform.columns[0][0], xform.columns[1][1]);
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
2020-10-24 15:15:43 +00:00
|
|
|
RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
bool MeshTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
|
|
|
|
r_rect = p_rect;
|
|
|
|
r_src_rect = p_src_rect;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MeshTexture::is_pixel_opaque(int p_x, int p_y) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshTexture::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshTexture::set_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh"), &MeshTexture::get_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_image_size", "size"), &MeshTexture::set_image_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_image_size"), &MeshTexture::get_image_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_base_texture", "texture"), &MeshTexture::set_base_texture);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_base_texture"), &MeshTexture::get_base_texture);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
|
2019-06-11 18:43:37 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "base_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_base_texture", "get_base_texture");
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "image_size", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_image_size", "get_image_size");
|
2019-04-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeshTexture::MeshTexture() {
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
//////////////////////////////////////////
|
|
|
|
|
2017-04-07 02:36:37 +00:00
|
|
|
void CurveTexture::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_width", "width"), &CurveTexture::set_width);
|
|
|
|
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_curve", "curve"), &CurveTexture::set_curve);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_curve"), &CurveTexture::get_curve);
|
2017-04-30 14:27:10 +00:00
|
|
|
|
2021-07-01 18:25:35 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_texture_mode", "texture_mode"), &CurveTexture::set_texture_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_texture_mode"), &CurveTexture::get_texture_mode);
|
|
|
|
|
2017-04-30 14:27:10 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_update"), &CurveTexture::_update);
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,4096,suffix:px"), "set_width", "get_width");
|
2021-07-01 18:25:35 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_mode", PROPERTY_HINT_ENUM, "RGB,Red"), "set_texture_mode", "get_texture_mode");
|
2017-04-30 14:27:10 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
|
2021-07-01 18:25:35 +00:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(TEXTURE_MODE_RGB);
|
|
|
|
BIND_ENUM_CONSTANT(TEXTURE_MODE_RED);
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurveTexture::set_width(int p_width) {
|
|
|
|
ERR_FAIL_COND(p_width < 32 || p_width > 4096);
|
2021-07-01 18:25:35 +00:00
|
|
|
|
|
|
|
if (_width == p_width) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-30 14:27:10 +00:00
|
|
|
_width = p_width;
|
|
|
|
_update();
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
2017-06-26 21:39:35 +00:00
|
|
|
|
2017-04-07 02:36:37 +00:00
|
|
|
int CurveTexture::get_width() const {
|
2017-04-30 14:27:10 +00:00
|
|
|
return _width;
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 21:39:35 +00:00
|
|
|
void CurveTexture::ensure_default_setup(float p_min, float p_max) {
|
2017-04-30 14:27:10 +00:00
|
|
|
if (_curve.is_null()) {
|
|
|
|
Ref<Curve> curve = Ref<Curve>(memnew(Curve));
|
|
|
|
curve->add_point(Vector2(0, 1));
|
|
|
|
curve->add_point(Vector2(1, 1));
|
2017-06-26 21:39:35 +00:00
|
|
|
curve->set_min_value(p_min);
|
|
|
|
curve->set_max_value(p_max);
|
2017-04-30 14:27:10 +00:00
|
|
|
set_curve(curve);
|
2017-06-26 21:39:35 +00:00
|
|
|
// Min and max is 0..1 by default
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
2017-04-30 14:27:10 +00:00
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2017-04-30 14:27:10 +00:00
|
|
|
void CurveTexture::set_curve(Ref<Curve> p_curve) {
|
|
|
|
if (_curve != p_curve) {
|
|
|
|
if (_curve.is_valid()) {
|
2020-02-21 22:26:13 +00:00
|
|
|
_curve->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveTexture::_update));
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
2017-04-30 14:27:10 +00:00
|
|
|
_curve = p_curve;
|
|
|
|
if (_curve.is_valid()) {
|
2020-02-21 22:26:13 +00:00
|
|
|
_curve->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveTexture::_update));
|
2017-04-30 14:27:10 +00:00
|
|
|
}
|
|
|
|
_update();
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 14:27:10 +00:00
|
|
|
void CurveTexture::_update() {
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> data;
|
2021-07-01 18:25:35 +00:00
|
|
|
data.resize(_width * sizeof(float) * (texture_mode == TEXTURE_MODE_RGB ? 3 : 1));
|
2017-04-30 14:27:10 +00:00
|
|
|
|
|
|
|
// The array is locked in that scope
|
2017-04-07 02:36:37 +00:00
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *wd8 = data.ptrw();
|
|
|
|
float *wd = (float *)wd8;
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2017-04-30 14:27:10 +00:00
|
|
|
if (_curve.is_valid()) {
|
|
|
|
Curve &curve = **_curve;
|
|
|
|
for (int i = 0; i < _width; ++i) {
|
|
|
|
float t = i / static_cast<float>(_width);
|
2021-07-01 18:25:35 +00:00
|
|
|
if (texture_mode == TEXTURE_MODE_RGB) {
|
2022-07-24 16:47:57 +00:00
|
|
|
wd[i * 3 + 0] = curve.sample_baked(t);
|
2021-07-01 18:25:35 +00:00
|
|
|
wd[i * 3 + 1] = wd[i * 3 + 0];
|
|
|
|
wd[i * 3 + 2] = wd[i * 3 + 0];
|
|
|
|
} else {
|
2022-07-24 16:47:57 +00:00
|
|
|
wd[i] = curve.sample_baked(t);
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:27:10 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < _width; ++i) {
|
2021-07-01 18:25:35 +00:00
|
|
|
if (texture_mode == TEXTURE_MODE_RGB) {
|
|
|
|
wd[i * 3 + 0] = 0;
|
|
|
|
wd[i * 3 + 1] = 0;
|
|
|
|
wd[i * 3 + 2] = 0;
|
|
|
|
} else {
|
|
|
|
wd[i] = 0;
|
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:25:35 +00:00
|
|
|
Ref<Image> image = memnew(Image(_width, 1, false, texture_mode == TEXTURE_MODE_RGB ? Image::FORMAT_RGBF : Image::FORMAT_RF, data));
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
if (_texture.is_valid()) {
|
2021-07-01 18:25:35 +00:00
|
|
|
if (_current_texture_mode != texture_mode || _current_width != _width) {
|
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
RS::get_singleton()->texture_replace(_texture, new_texture);
|
|
|
|
} else {
|
|
|
|
RS::get_singleton()->texture_2d_update(_texture, image);
|
|
|
|
}
|
2019-06-11 18:43:37 +00:00
|
|
|
} else {
|
2020-03-27 18:21:27 +00:00
|
|
|
_texture = RS::get_singleton()->texture_2d_create(image);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2021-07-01 18:25:35 +00:00
|
|
|
_current_texture_mode = texture_mode;
|
|
|
|
_current_width = _width;
|
2017-04-07 02:36:37 +00:00
|
|
|
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2017-04-30 14:27:10 +00:00
|
|
|
Ref<Curve> CurveTexture::get_curve() const {
|
|
|
|
return _curve;
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 18:25:35 +00:00
|
|
|
void CurveTexture::set_texture_mode(TextureMode p_mode) {
|
2021-11-01 10:57:59 +00:00
|
|
|
ERR_FAIL_COND(p_mode < TEXTURE_MODE_RGB || p_mode > TEXTURE_MODE_RED);
|
2021-07-01 18:25:35 +00:00
|
|
|
if (texture_mode == p_mode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
texture_mode = p_mode;
|
|
|
|
_update();
|
|
|
|
}
|
|
|
|
CurveTexture::TextureMode CurveTexture::get_texture_mode() const {
|
|
|
|
return texture_mode;
|
|
|
|
}
|
|
|
|
|
2017-04-07 02:36:37 +00:00
|
|
|
RID CurveTexture::get_rid() const {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (!_texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
_texture = RS::get_singleton()->texture_2d_placeholder_create();
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2017-04-30 14:27:10 +00:00
|
|
|
return _texture;
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:24:36 +00:00
|
|
|
CurveTexture::CurveTexture() {}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-04-07 02:36:37 +00:00
|
|
|
CurveTexture::~CurveTexture() {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (_texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->free(_texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-04-07 02:36:37 +00:00
|
|
|
//////////////////
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
void CurveXYZTexture::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_width", "width"), &CurveXYZTexture::set_width);
|
2021-07-01 18:25:35 +00:00
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_curve_x", "curve"), &CurveXYZTexture::set_curve_x);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_curve_x"), &CurveXYZTexture::get_curve_x);
|
2021-07-01 18:25:35 +00:00
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_curve_y", "curve"), &CurveXYZTexture::set_curve_y);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_curve_y"), &CurveXYZTexture::get_curve_y);
|
2021-07-01 18:25:35 +00:00
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_curve_z", "curve"), &CurveXYZTexture::set_curve_z);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_curve_z"), &CurveXYZTexture::get_curve_z);
|
2021-07-01 18:25:35 +00:00
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_update"), &CurveXYZTexture::_update);
|
2021-07-01 18:25:35 +00:00
|
|
|
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,4096,suffix:px"), "set_width", "get_width");
|
2021-07-01 18:25:35 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_x", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_x", "get_curve_x");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_y", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_y", "get_curve_y");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_z", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_z", "get_curve_z");
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
void CurveXYZTexture::set_width(int p_width) {
|
2021-07-01 18:25:35 +00:00
|
|
|
ERR_FAIL_COND(p_width < 32 || p_width > 4096);
|
|
|
|
|
|
|
|
if (_width == p_width) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_width = p_width;
|
|
|
|
_update();
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
int CurveXYZTexture::get_width() const {
|
2021-07-01 18:25:35 +00:00
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
void CurveXYZTexture::ensure_default_setup(float p_min, float p_max) {
|
2021-07-01 18:25:35 +00:00
|
|
|
if (_curve_x.is_null()) {
|
|
|
|
Ref<Curve> curve = Ref<Curve>(memnew(Curve));
|
|
|
|
curve->add_point(Vector2(0, 1));
|
|
|
|
curve->add_point(Vector2(1, 1));
|
|
|
|
curve->set_min_value(p_min);
|
|
|
|
curve->set_max_value(p_max);
|
|
|
|
set_curve_x(curve);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_curve_y.is_null()) {
|
|
|
|
Ref<Curve> curve = Ref<Curve>(memnew(Curve));
|
|
|
|
curve->add_point(Vector2(0, 1));
|
|
|
|
curve->add_point(Vector2(1, 1));
|
|
|
|
curve->set_min_value(p_min);
|
|
|
|
curve->set_max_value(p_max);
|
|
|
|
set_curve_y(curve);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_curve_z.is_null()) {
|
|
|
|
Ref<Curve> curve = Ref<Curve>(memnew(Curve));
|
|
|
|
curve->add_point(Vector2(0, 1));
|
|
|
|
curve->add_point(Vector2(1, 1));
|
|
|
|
curve->set_min_value(p_min);
|
|
|
|
curve->set_max_value(p_max);
|
|
|
|
set_curve_z(curve);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
void CurveXYZTexture::set_curve_x(Ref<Curve> p_curve) {
|
2021-07-01 18:25:35 +00:00
|
|
|
if (_curve_x != p_curve) {
|
|
|
|
if (_curve_x.is_valid()) {
|
2021-07-13 22:06:44 +00:00
|
|
|
_curve_x->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update));
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
_curve_x = p_curve;
|
|
|
|
if (_curve_x.is_valid()) {
|
2022-07-28 20:56:41 +00:00
|
|
|
_curve_x->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
void CurveXYZTexture::set_curve_y(Ref<Curve> p_curve) {
|
2021-07-01 18:25:35 +00:00
|
|
|
if (_curve_y != p_curve) {
|
|
|
|
if (_curve_y.is_valid()) {
|
2021-07-13 22:06:44 +00:00
|
|
|
_curve_y->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update));
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
_curve_y = p_curve;
|
|
|
|
if (_curve_y.is_valid()) {
|
2022-07-28 20:56:41 +00:00
|
|
|
_curve_y->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
void CurveXYZTexture::set_curve_z(Ref<Curve> p_curve) {
|
2021-07-01 18:25:35 +00:00
|
|
|
if (_curve_z != p_curve) {
|
|
|
|
if (_curve_z.is_valid()) {
|
2021-07-13 22:06:44 +00:00
|
|
|
_curve_z->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update));
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
_curve_z = p_curve;
|
|
|
|
if (_curve_z.is_valid()) {
|
2022-07-28 20:56:41 +00:00
|
|
|
_curve_z->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
void CurveXYZTexture::_update() {
|
2021-07-01 18:25:35 +00:00
|
|
|
Vector<uint8_t> data;
|
|
|
|
data.resize(_width * sizeof(float) * 3);
|
|
|
|
|
|
|
|
// The array is locked in that scope
|
|
|
|
{
|
|
|
|
uint8_t *wd8 = data.ptrw();
|
|
|
|
float *wd = (float *)wd8;
|
|
|
|
|
|
|
|
if (_curve_x.is_valid()) {
|
|
|
|
Curve &curve_x = **_curve_x;
|
|
|
|
for (int i = 0; i < _width; ++i) {
|
|
|
|
float t = i / static_cast<float>(_width);
|
2022-07-24 16:47:57 +00:00
|
|
|
wd[i * 3 + 0] = curve_x.sample_baked(t);
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < _width; ++i) {
|
|
|
|
wd[i * 3 + 0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_curve_y.is_valid()) {
|
|
|
|
Curve &curve_y = **_curve_y;
|
|
|
|
for (int i = 0; i < _width; ++i) {
|
|
|
|
float t = i / static_cast<float>(_width);
|
2022-07-24 16:47:57 +00:00
|
|
|
wd[i * 3 + 1] = curve_y.sample_baked(t);
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < _width; ++i) {
|
|
|
|
wd[i * 3 + 1] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_curve_z.is_valid()) {
|
|
|
|
Curve &curve_z = **_curve_z;
|
|
|
|
for (int i = 0; i < _width; ++i) {
|
|
|
|
float t = i / static_cast<float>(_width);
|
2022-07-24 16:47:57 +00:00
|
|
|
wd[i * 3 + 2] = curve_z.sample_baked(t);
|
2021-07-01 18:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < _width; ++i) {
|
|
|
|
wd[i * 3 + 2] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> image = memnew(Image(_width, 1, false, Image::FORMAT_RGBF, data));
|
|
|
|
|
|
|
|
if (_texture.is_valid()) {
|
|
|
|
if (_current_width != _width) {
|
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
RS::get_singleton()->texture_replace(_texture, new_texture);
|
|
|
|
} else {
|
|
|
|
RS::get_singleton()->texture_2d_update(_texture, image);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
}
|
|
|
|
_current_width = _width;
|
|
|
|
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
Ref<Curve> CurveXYZTexture::get_curve_x() const {
|
2021-07-01 18:25:35 +00:00
|
|
|
return _curve_x;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
Ref<Curve> CurveXYZTexture::get_curve_y() const {
|
2021-07-01 18:25:35 +00:00
|
|
|
return _curve_y;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
Ref<Curve> CurveXYZTexture::get_curve_z() const {
|
2021-07-01 18:25:35 +00:00
|
|
|
return _curve_z;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
RID CurveXYZTexture::get_rid() const {
|
2021-07-01 18:25:35 +00:00
|
|
|
if (!_texture.is_valid()) {
|
|
|
|
_texture = RS::get_singleton()->texture_2d_placeholder_create();
|
|
|
|
}
|
|
|
|
return _texture;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
CurveXYZTexture::CurveXYZTexture() {}
|
2021-07-01 18:25:35 +00:00
|
|
|
|
2021-07-13 22:06:44 +00:00
|
|
|
CurveXYZTexture::~CurveXYZTexture() {
|
2021-07-01 18:25:35 +00:00
|
|
|
if (_texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2021-07-01 18:25:35 +00:00
|
|
|
RS::get_singleton()->free(_texture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
GradientTexture1D::GradientTexture1D() {
|
2017-04-07 02:36:37 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
GradientTexture1D::~GradientTexture1D() {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->free(texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
void GradientTexture1D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture1D::set_gradient);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture1D::get_gradient);
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture1D::set_width);
|
2021-05-02 01:53:10 +00:00
|
|
|
// The `get_width()` method is already exposed by the parent class Texture2D.
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture1D::set_use_hdr);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture1D::is_using_hdr);
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_update"), &GradientTexture1D::_update);
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2022-07-17 02:04:57 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,16384,suffix:px"), "set_width", "get_width");
|
2021-05-02 01:53:10 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
|
2017-06-24 19:28:22 +00:00
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
void GradientTexture1D::set_gradient(Ref<Gradient> p_gradient) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_gradient == gradient) {
|
2017-06-24 19:28:22 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-06-24 19:28:22 +00:00
|
|
|
if (gradient.is_valid()) {
|
2021-11-07 12:35:45 +00:00
|
|
|
gradient->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GradientTexture1D::_update));
|
2017-06-24 19:28:22 +00:00
|
|
|
}
|
|
|
|
gradient = p_gradient;
|
|
|
|
if (gradient.is_valid()) {
|
2021-11-07 12:35:45 +00:00
|
|
|
gradient->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GradientTexture1D::_update));
|
2017-06-24 19:28:22 +00:00
|
|
|
}
|
|
|
|
_update();
|
|
|
|
emit_changed();
|
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
Ref<Gradient> GradientTexture1D::get_gradient() const {
|
2017-06-24 19:28:22 +00:00
|
|
|
return gradient;
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
void GradientTexture1D::_queue_update() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (update_pending) {
|
2017-04-07 02:36:37 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2018-08-24 22:25:06 +00:00
|
|
|
update_pending = true;
|
2021-07-17 21:22:52 +00:00
|
|
|
call_deferred(SNAME("_update"));
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
void GradientTexture1D::_update() {
|
2018-08-24 22:25:06 +00:00
|
|
|
update_pending = false;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (gradient.is_null()) {
|
2017-06-24 19:28:22 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-06-24 19:28:22 +00:00
|
|
|
|
2021-05-02 01:53:10 +00:00
|
|
|
if (use_hdr) {
|
|
|
|
// High dynamic range.
|
|
|
|
Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBAF));
|
2017-06-24 19:28:22 +00:00
|
|
|
Gradient &g = **gradient;
|
2021-05-02 01:53:10 +00:00
|
|
|
// `create()` isn't available for non-uint8_t data, so fill in the data manually.
|
2017-04-07 02:36:37 +00:00
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
float ofs = float(i) / (width - 1);
|
2021-05-02 01:53:10 +00:00
|
|
|
image->set_pixel(i, 0, g.get_color_at_offset(ofs));
|
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2021-05-02 01:53:10 +00:00
|
|
|
if (texture.is_valid()) {
|
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
RS::get_singleton()->texture_replace(texture, new_texture);
|
|
|
|
} else {
|
|
|
|
texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Low dynamic range. "Overbright" colors will be clamped.
|
|
|
|
Vector<uint8_t> data;
|
|
|
|
data.resize(width * 4);
|
|
|
|
{
|
|
|
|
uint8_t *wd8 = data.ptrw();
|
|
|
|
Gradient &g = **gradient;
|
|
|
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
float ofs = float(i) / (width - 1);
|
|
|
|
Color color = g.get_color_at_offset(ofs);
|
|
|
|
|
|
|
|
wd8[i * 4 + 0] = uint8_t(CLAMP(color.r * 255.0, 0, 255));
|
|
|
|
wd8[i * 4 + 1] = uint8_t(CLAMP(color.g * 255.0, 0, 255));
|
|
|
|
wd8[i * 4 + 2] = uint8_t(CLAMP(color.b * 255.0, 0, 255));
|
|
|
|
wd8[i * 4 + 3] = uint8_t(CLAMP(color.a * 255.0, 0, 255));
|
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 01:53:10 +00:00
|
|
|
Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data));
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2021-05-02 01:53:10 +00:00
|
|
|
if (texture.is_valid()) {
|
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
RS::get_singleton()->texture_replace(texture, new_texture);
|
|
|
|
} else {
|
|
|
|
texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
}
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
void GradientTexture1D::set_width(int p_width) {
|
2022-06-04 23:58:53 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
|
2017-04-07 02:36:37 +00:00
|
|
|
width = p_width;
|
|
|
|
_queue_update();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
int GradientTexture1D::get_width() const {
|
2017-04-07 02:36:37 +00:00
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
void GradientTexture1D::set_use_hdr(bool p_enabled) {
|
2021-05-02 01:53:10 +00:00
|
|
|
if (p_enabled == use_hdr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
use_hdr = p_enabled;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
bool GradientTexture1D::is_using_hdr() const {
|
2021-05-02 01:53:10 +00:00
|
|
|
return use_hdr;
|
|
|
|
}
|
|
|
|
|
2021-11-07 12:35:45 +00:00
|
|
|
Ref<Image> GradientTexture1D::get_image() const {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (!texture.is_valid()) {
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
2020-03-27 18:21:27 +00:00
|
|
|
return RenderingServer::get_singleton()->texture_2d_get(texture);
|
2017-06-09 03:23:50 +00:00
|
|
|
}
|
2017-12-04 19:54:17 +00:00
|
|
|
|
2022-04-19 17:51:49 +00:00
|
|
|
//////////////////
|
|
|
|
|
2021-10-12 14:22:30 +00:00
|
|
|
GradientTexture2D::GradientTexture2D() {
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
GradientTexture2D::~GradientTexture2D() {
|
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2021-10-12 14:22:30 +00:00
|
|
|
RS::get_singleton()->free(texture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::set_gradient(Ref<Gradient> p_gradient) {
|
|
|
|
if (gradient == p_gradient) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (gradient.is_valid()) {
|
|
|
|
gradient->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GradientTexture2D::_queue_update));
|
|
|
|
}
|
|
|
|
gradient = p_gradient;
|
|
|
|
if (gradient.is_valid()) {
|
|
|
|
gradient->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GradientTexture2D::_queue_update));
|
|
|
|
}
|
2022-04-19 17:51:49 +00:00
|
|
|
_update();
|
|
|
|
emit_changed();
|
2021-10-12 14:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Gradient> GradientTexture2D::get_gradient() const {
|
|
|
|
return gradient;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::_queue_update() {
|
|
|
|
if (update_pending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
update_pending = true;
|
2022-02-06 14:53:53 +00:00
|
|
|
call_deferred(SNAME("_update"));
|
2021-10-12 14:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::_update() {
|
|
|
|
update_pending = false;
|
|
|
|
|
|
|
|
if (gradient.is_null()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ref<Image> image;
|
|
|
|
image.instantiate();
|
|
|
|
|
2023-01-02 14:52:05 +00:00
|
|
|
if (gradient->get_point_count() <= 1) { // No need to interpolate.
|
2022-07-22 18:06:19 +00:00
|
|
|
image->initialize_data(width, height, false, (use_hdr) ? Image::FORMAT_RGBAF : Image::FORMAT_RGBA8);
|
2023-01-02 14:52:05 +00:00
|
|
|
image->fill((gradient->get_point_count() == 1) ? gradient->get_color(0) : Color(0, 0, 0, 1));
|
2021-10-12 14:22:30 +00:00
|
|
|
} else {
|
|
|
|
if (use_hdr) {
|
2022-07-22 18:06:19 +00:00
|
|
|
image->initialize_data(width, height, false, Image::FORMAT_RGBAF);
|
2021-10-12 14:22:30 +00:00
|
|
|
Gradient &g = **gradient;
|
|
|
|
// `create()` isn't available for non-uint8_t data, so fill in the data manually.
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
float ofs = _get_gradient_offset_at(x, y);
|
|
|
|
image->set_pixel(x, y, g.get_color_at_offset(ofs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Vector<uint8_t> data;
|
|
|
|
data.resize(width * height * 4);
|
|
|
|
{
|
|
|
|
uint8_t *wd8 = data.ptrw();
|
|
|
|
Gradient &g = **gradient;
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
float ofs = _get_gradient_offset_at(x, y);
|
|
|
|
const Color &c = g.get_color_at_offset(ofs);
|
|
|
|
|
|
|
|
wd8[(x + (y * width)) * 4 + 0] = uint8_t(CLAMP(c.r * 255.0, 0, 255));
|
|
|
|
wd8[(x + (y * width)) * 4 + 1] = uint8_t(CLAMP(c.g * 255.0, 0, 255));
|
|
|
|
wd8[(x + (y * width)) * 4 + 2] = uint8_t(CLAMP(c.b * 255.0, 0, 255));
|
|
|
|
wd8[(x + (y * width)) * 4 + 3] = uint8_t(CLAMP(c.a * 255.0, 0, 255));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 18:06:19 +00:00
|
|
|
image->set_data(width, height, false, Image::FORMAT_RGBA8, data);
|
2021-10-12 14:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture.is_valid()) {
|
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
RS::get_singleton()->texture_replace(texture, new_texture);
|
|
|
|
} else {
|
|
|
|
texture = RS::get_singleton()->texture_2d_create(image);
|
|
|
|
}
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
float GradientTexture2D::_get_gradient_offset_at(int x, int y) const {
|
|
|
|
if (fill_to == fill_from) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
float ofs = 0;
|
|
|
|
Vector2 pos;
|
|
|
|
if (width > 1) {
|
|
|
|
pos.x = static_cast<float>(x) / (width - 1);
|
|
|
|
}
|
|
|
|
if (height > 1) {
|
|
|
|
pos.y = static_cast<float>(y) / (height - 1);
|
|
|
|
}
|
|
|
|
if (fill == Fill::FILL_LINEAR) {
|
|
|
|
Vector2 segment[2];
|
|
|
|
segment[0] = fill_from;
|
|
|
|
segment[1] = fill_to;
|
|
|
|
Vector2 closest = Geometry2D::get_closest_point_to_segment_uncapped(pos, &segment[0]);
|
|
|
|
ofs = (closest - fill_from).length() / (fill_to - fill_from).length();
|
|
|
|
if ((closest - fill_from).dot(fill_to - fill_from) < 0) {
|
|
|
|
ofs *= -1;
|
|
|
|
}
|
|
|
|
} else if (fill == Fill::FILL_RADIAL) {
|
|
|
|
ofs = (pos - fill_from).length() / (fill_to - fill_from).length();
|
|
|
|
}
|
|
|
|
if (repeat == Repeat::REPEAT_NONE) {
|
|
|
|
ofs = CLAMP(ofs, 0.0, 1.0);
|
|
|
|
} else if (repeat == Repeat::REPEAT) {
|
|
|
|
ofs = Math::fmod(ofs, 1.0f);
|
|
|
|
if (ofs < 0) {
|
|
|
|
ofs = 1 + ofs;
|
|
|
|
}
|
|
|
|
} else if (repeat == Repeat::REPEAT_MIRROR) {
|
|
|
|
ofs = Math::abs(ofs);
|
|
|
|
ofs = Math::fmod(ofs, 2.0f);
|
|
|
|
if (ofs > 1.0) {
|
|
|
|
ofs = 2.0 - ofs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::set_width(int p_width) {
|
2022-06-04 23:58:53 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
|
2021-10-12 14:22:30 +00:00
|
|
|
width = p_width;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int GradientTexture2D::get_width() const {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::set_height(int p_height) {
|
2022-06-04 23:58:53 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_height <= 0 || p_height > 16384, "Texture dimensions have to be within 1 to 16384 range.");
|
2021-10-12 14:22:30 +00:00
|
|
|
height = p_height;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
int GradientTexture2D::get_height() const {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::set_use_hdr(bool p_enabled) {
|
|
|
|
if (p_enabled == use_hdr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
use_hdr = p_enabled;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GradientTexture2D::is_using_hdr() const {
|
|
|
|
return use_hdr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::set_fill_from(Vector2 p_fill_from) {
|
|
|
|
fill_from = p_fill_from;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 GradientTexture2D::get_fill_from() const {
|
|
|
|
return fill_from;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::set_fill_to(Vector2 p_fill_to) {
|
|
|
|
fill_to = p_fill_to;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 GradientTexture2D::get_fill_to() const {
|
|
|
|
return fill_to;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::set_fill(Fill p_fill) {
|
|
|
|
fill = p_fill;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
GradientTexture2D::Fill GradientTexture2D::get_fill() const {
|
|
|
|
return fill;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::set_repeat(Repeat p_repeat) {
|
|
|
|
repeat = p_repeat;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
GradientTexture2D::Repeat GradientTexture2D::get_repeat() const {
|
|
|
|
return repeat;
|
|
|
|
}
|
|
|
|
|
|
|
|
RID GradientTexture2D::get_rid() const {
|
|
|
|
if (!texture.is_valid()) {
|
|
|
|
texture = RS::get_singleton()->texture_2d_placeholder_create();
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> GradientTexture2D::get_image() const {
|
|
|
|
if (!texture.is_valid()) {
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
return RenderingServer::get_singleton()->texture_2d_get(texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientTexture2D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture2D::set_gradient);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture2D::get_gradient);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture2D::set_width);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_height", "height"), &GradientTexture2D::set_height);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture2D::set_use_hdr);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture2D::is_using_hdr);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_fill", "fill"), &GradientTexture2D::set_fill);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_fill"), &GradientTexture2D::get_fill);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_fill_from", "fill_from"), &GradientTexture2D::set_fill_from);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_fill_from"), &GradientTexture2D::get_fill_from);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_fill_to", "fill_to"), &GradientTexture2D::set_fill_to);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_fill_to"), &GradientTexture2D::get_fill_to);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &GradientTexture2D::set_repeat);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_repeat"), &GradientTexture2D::get_repeat);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("_update"), &GradientTexture2D::_update);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height");
|
2021-10-12 14:22:30 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
|
|
|
|
|
|
|
|
ADD_GROUP("Fill", "fill_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "fill", PROPERTY_HINT_ENUM, "Linear,Radial"), "set_fill", "get_fill");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_from"), "set_fill_from", "get_fill_from");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_to"), "set_fill_to", "get_fill_to");
|
|
|
|
|
|
|
|
ADD_GROUP("Repeat", "repeat_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "repeat", PROPERTY_HINT_ENUM, "No Repeat,Repeat,Mirror Repeat"), "set_repeat", "get_repeat");
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(FILL_LINEAR);
|
|
|
|
BIND_ENUM_CONSTANT(FILL_RADIAL);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(REPEAT_NONE);
|
|
|
|
BIND_ENUM_CONSTANT(REPEAT);
|
|
|
|
BIND_ENUM_CONSTANT(REPEAT_MIRROR);
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:54:17 +00:00
|
|
|
//////////////////////////////////////
|
|
|
|
|
2018-07-16 14:43:26 +00:00
|
|
|
void AnimatedTexture::_update_proxy() {
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockRead r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
float delta;
|
|
|
|
if (prev_ticks == 0) {
|
|
|
|
delta = 0;
|
|
|
|
prev_ticks = OS::get_singleton()->get_ticks_usec();
|
|
|
|
} else {
|
|
|
|
uint64_t ticks = OS::get_singleton()->get_ticks_usec();
|
|
|
|
delta = float(double(ticks - prev_ticks) / 1000000.0);
|
|
|
|
prev_ticks = ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
time += delta;
|
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
float speed = speed_scale == 0 ? 0 : abs(1.0 / speed_scale);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
int iter_max = frame_count;
|
2020-04-28 09:03:36 +00:00
|
|
|
while (iter_max && !pause) {
|
2022-09-01 10:09:06 +00:00
|
|
|
float frame_limit = frames[current_frame].duration * speed;
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
if (time > frame_limit) {
|
2022-09-01 10:09:06 +00:00
|
|
|
if (speed_scale > 0.0) {
|
|
|
|
current_frame++;
|
|
|
|
} else {
|
|
|
|
current_frame--;
|
|
|
|
}
|
2018-07-16 14:43:26 +00:00
|
|
|
if (current_frame >= frame_count) {
|
2022-09-01 13:19:23 +00:00
|
|
|
if (one_shot) {
|
2020-04-28 09:03:36 +00:00
|
|
|
current_frame = frame_count - 1;
|
|
|
|
} else {
|
|
|
|
current_frame = 0;
|
|
|
|
}
|
2022-09-01 10:09:06 +00:00
|
|
|
} else if (current_frame < 0) {
|
|
|
|
if (one_shot) {
|
|
|
|
current_frame = 0;
|
|
|
|
} else {
|
|
|
|
current_frame = frame_count - 1;
|
|
|
|
}
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
|
|
|
time -= frame_limit;
|
2021-02-10 20:18:45 +00:00
|
|
|
|
2018-07-16 14:43:26 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iter_max--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frames[current_frame].texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->texture_proxy_update(proxy, frames[current_frame].texture->get_rid());
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedTexture::set_frames(int p_frames) {
|
|
|
|
ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES);
|
|
|
|
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockWrite r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
frame_count = p_frames;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-07-16 14:43:26 +00:00
|
|
|
int AnimatedTexture::get_frames() const {
|
|
|
|
return frame_count;
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:03:36 +00:00
|
|
|
void AnimatedTexture::set_current_frame(int p_frame) {
|
|
|
|
ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count);
|
|
|
|
|
|
|
|
RWLockWrite r(rw_lock);
|
|
|
|
|
|
|
|
current_frame = p_frame;
|
2021-05-26 09:49:53 +00:00
|
|
|
time = 0;
|
2020-04-28 09:03:36 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-04-28 09:03:36 +00:00
|
|
|
int AnimatedTexture::get_current_frame() const {
|
|
|
|
return current_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedTexture::set_pause(bool p_pause) {
|
|
|
|
RWLockWrite r(rw_lock);
|
|
|
|
pause = p_pause;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-04-28 09:03:36 +00:00
|
|
|
bool AnimatedTexture::get_pause() const {
|
|
|
|
return pause;
|
|
|
|
}
|
|
|
|
|
2022-09-01 13:19:23 +00:00
|
|
|
void AnimatedTexture::set_one_shot(bool p_one_shot) {
|
2020-04-28 09:03:36 +00:00
|
|
|
RWLockWrite r(rw_lock);
|
2022-09-01 13:19:23 +00:00
|
|
|
one_shot = p_one_shot;
|
2020-04-28 09:03:36 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-09-01 13:19:23 +00:00
|
|
|
bool AnimatedTexture::get_one_shot() const {
|
|
|
|
return one_shot;
|
2020-04-28 09:03:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture) {
|
2019-01-28 13:56:06 +00:00
|
|
|
ERR_FAIL_COND(p_texture == this);
|
2018-07-16 14:43:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
|
|
|
|
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockWrite w(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
frames[p_frame].texture = p_texture;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> AnimatedTexture::get_frame_texture(int p_frame) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture2D>());
|
2018-07-16 14:43:26 +00:00
|
|
|
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockRead r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
return frames[p_frame].texture;
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
void AnimatedTexture::set_frame_duration(int p_frame, float p_duration) {
|
2018-07-16 14:43:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
|
|
|
|
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockRead r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
frames[p_frame].duration = p_duration;
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
float AnimatedTexture::get_frame_duration(int p_frame) const {
|
2018-07-16 14:43:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0);
|
|
|
|
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockRead r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
return frames[p_frame].duration;
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
void AnimatedTexture::set_speed_scale(float p_scale) {
|
|
|
|
ERR_FAIL_COND(p_scale < -1000 || p_scale >= 1000);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
speed_scale = p_scale;
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
float AnimatedTexture::get_speed_scale() const {
|
|
|
|
return speed_scale;
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AnimatedTexture::get_width() const {
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockRead r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
if (!frames[current_frame].texture.is_valid()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frames[current_frame].texture->get_width();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-07-16 14:43:26 +00:00
|
|
|
int AnimatedTexture::get_height() const {
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockRead r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
if (!frames[current_frame].texture.is_valid()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frames[current_frame].texture->get_height();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-07-16 14:43:26 +00:00
|
|
|
RID AnimatedTexture::get_rid() const {
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimatedTexture::has_alpha() const {
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockRead r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
if (!frames[current_frame].texture.is_valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frames[current_frame].texture->has_alpha();
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:32:17 +00:00
|
|
|
Ref<Image> AnimatedTexture::get_image() const {
|
2018-08-23 16:27:17 +00:00
|
|
|
RWLockRead r(rw_lock);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
if (!frames[current_frame].texture.is_valid()) {
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:32:17 +00:00
|
|
|
return frames[current_frame].texture->get_image();
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 01:10:15 +00:00
|
|
|
bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const {
|
|
|
|
RWLockRead r(rw_lock);
|
|
|
|
|
|
|
|
if (frames[current_frame].texture.is_valid()) {
|
|
|
|
return frames[current_frame].texture->is_pixel_opaque(p_x, p_y);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-12 20:57:11 +00:00
|
|
|
void AnimatedTexture::_validate_property(PropertyInfo &p_property) const {
|
|
|
|
String prop = p_property.name;
|
2018-07-16 14:43:26 +00:00
|
|
|
if (prop.begins_with("frame_")) {
|
|
|
|
int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
|
|
|
|
if (frame >= frame_count) {
|
2022-08-12 20:57:11 +00:00
|
|
|
p_property.usage = PROPERTY_USAGE_NONE;
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedTexture::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames);
|
|
|
|
|
2020-04-28 09:03:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_current_frame", "frame"), &AnimatedTexture::set_current_frame);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_current_frame"), &AnimatedTexture::get_current_frame);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_pause", "pause"), &AnimatedTexture::set_pause);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_pause"), &AnimatedTexture::get_pause);
|
|
|
|
|
2022-09-01 13:19:23 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_one_shot", "one_shot"), &AnimatedTexture::set_one_shot);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_one_shot"), &AnimatedTexture::get_one_shot);
|
2020-04-28 09:03:36 +00:00
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &AnimatedTexture::set_speed_scale);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedTexture::get_speed_scale);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_frame_texture", "frame", "texture"), &AnimatedTexture::set_frame_texture);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_frame_texture", "frame"), &AnimatedTexture::get_frame_texture);
|
|
|
|
|
2022-09-01 10:09:06 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_frame_duration", "frame", "duration"), &AnimatedTexture::set_frame_duration);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_frame_duration", "frame"), &AnimatedTexture::get_frame_duration);
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "frames", PROPERTY_HINT_RANGE, "1," + itos(MAX_FRAMES), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_frames", "get_frames");
|
2021-06-17 23:10:18 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_frame", "get_current_frame");
|
2020-04-28 09:03:36 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause"), "set_pause", "get_pause");
|
2022-09-01 13:19:23 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
|
2022-09-01 10:09:06 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-60,60,0.1,or_greater,or_lesser"), "set_speed_scale", "get_speed_scale");
|
2018-07-16 14:43:26 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < MAX_FRAMES; i++) {
|
2019-06-11 18:43:37 +00:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "frame_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_texture", "get_frame_texture", i);
|
2022-09-01 10:09:06 +00:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "frame_" + itos(i) + "/duration", PROPERTY_HINT_RANGE, "0.0,16.0,0.01,or_greater,suffix:s", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_duration", "get_frame_duration", i);
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
2019-01-27 12:11:40 +00:00
|
|
|
|
|
|
|
BIND_CONSTANT(MAX_FRAMES);
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimatedTexture::AnimatedTexture() {
|
2020-03-27 18:21:27 +00:00
|
|
|
//proxy = RS::get_singleton()->texture_create();
|
|
|
|
proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
|
|
|
|
proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);
|
2019-06-24 19:13:06 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);
|
|
|
|
RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimatedTexture::~AnimatedTexture() {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->free(proxy);
|
|
|
|
RS::get_singleton()->free(proxy_ph);
|
2018-07-16 14:43:26 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-08-06 17:56:06 +00:00
|
|
|
///////////////////////////////
|
|
|
|
|
2022-03-10 07:17:38 +00:00
|
|
|
Image::Format TextureLayered::get_format() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
Image::Format ret = Image::FORMAT_MAX;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_format, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureLayered::LayeredType TextureLayered::get_layered_type() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
uint32_t ret = LAYERED_TYPE_2D_ARRAY;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_layered_type, ret);
|
|
|
|
return (LayeredType)ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int TextureLayered::get_width() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_width, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int TextureLayered::get_height() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_height, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int TextureLayered::get_layers() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_layers, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TextureLayered::has_mipmaps() const {
|
2022-11-30 15:40:50 +00:00
|
|
|
bool ret = false;
|
|
|
|
GDVIRTUAL_REQUIRED_CALL(_has_mipmaps, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> TextureLayered::get_layer_data(int p_layer) const {
|
|
|
|
Ref<Image> ret;
|
2022-11-30 15:40:50 +00:00
|
|
|
GDVIRTUAL_REQUIRED_CALL(_get_layer_data, p_layer, ret);
|
|
|
|
return ret;
|
2022-03-10 07:17:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00: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);
|
2020-05-12 08:10:34 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_layer_data", "layer"), &TextureLayered::get_layer_data);
|
2020-05-01 12:34:23 +00:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(LAYERED_TYPE_2D_ARRAY);
|
|
|
|
BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP);
|
|
|
|
BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP_ARRAY);
|
2022-03-10 07:17:38 +00: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");
|
2020-05-01 12:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
Image::Format ImageTextureLayered::get_format() const {
|
2018-08-06 17:56:06 +00:00
|
|
|
return format;
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
int ImageTextureLayered::get_width() const {
|
2018-08-06 17:56:06 +00:00
|
|
|
return width;
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
int ImageTextureLayered::get_height() const {
|
2018-08-06 17:56:06 +00:00
|
|
|
return height;
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
int ImageTextureLayered::get_layers() const {
|
2019-06-11 18:43:37 +00:00
|
|
|
return layers;
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
bool ImageTextureLayered::has_mipmaps() const {
|
|
|
|
return mipmaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageTextureLayered::LayeredType ImageTextureLayered::get_layered_type() const {
|
|
|
|
return layered_type;
|
|
|
|
}
|
|
|
|
|
2022-08-31 17:24:04 +00:00
|
|
|
Error ImageTextureLayered::_create_from_images(const TypedArray<Image> &p_images) {
|
2020-03-17 06:33:00 +00:00
|
|
|
Vector<Ref<Image>> images;
|
2019-06-11 18:43:37 +00:00
|
|
|
for (int i = 0; i < p_images.size(); i++) {
|
|
|
|
Ref<Image> img = p_images[i];
|
|
|
|
ERR_FAIL_COND_V(img.is_null(), ERR_INVALID_PARAMETER);
|
|
|
|
images.push_back(img);
|
|
|
|
}
|
2018-08-06 17:56:06 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
return create_from_images(images);
|
|
|
|
}
|
2018-08-06 17:56:06 +00:00
|
|
|
|
2022-08-31 17:24:04 +00:00
|
|
|
TypedArray<Image> ImageTextureLayered::_get_images() const {
|
|
|
|
TypedArray<Image> images;
|
2019-06-11 18:43:37 +00:00
|
|
|
for (int i = 0; i < layers; i++) {
|
|
|
|
images.push_back(get_layer_data(i));
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2019-06-11 18:43:37 +00:00
|
|
|
return images;
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 16:07:39 +00:00
|
|
|
void ImageTextureLayered::_set_images(const TypedArray<Image> &p_images) {
|
|
|
|
ERR_FAIL_COND(_create_from_images(p_images) != OK);
|
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
Error ImageTextureLayered::create_from_images(Vector<Ref<Image>> p_images) {
|
2019-06-11 18:43:37 +00:00
|
|
|
int new_layers = p_images.size();
|
|
|
|
ERR_FAIL_COND_V(new_layers == 0, ERR_INVALID_PARAMETER);
|
2020-05-01 12:34:23 +00:00
|
|
|
if (layered_type == LAYERED_TYPE_CUBEMAP) {
|
2019-06-11 18:43:37 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(new_layers != 6, ERR_INVALID_PARAMETER,
|
|
|
|
"Cubemaps require exactly 6 layers");
|
2020-05-01 12:34:23 +00:00
|
|
|
} else if (layered_type == LAYERED_TYPE_CUBEMAP_ARRAY) {
|
2019-06-11 18:43:37 +00:00
|
|
|
ERR_FAIL_COND_V_MSG((new_layers % 6) != 0, ERR_INVALID_PARAMETER,
|
|
|
|
"Cubemap array layers must be a multiple of 6");
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-12-15 12:04:21 +00:00
|
|
|
ERR_FAIL_COND_V(p_images[0].is_null() || p_images[0]->is_empty(), ERR_INVALID_PARAMETER);
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Image::Format new_format = p_images[0]->get_format();
|
|
|
|
int new_width = p_images[0]->get_width();
|
|
|
|
int new_height = p_images[0]->get_height();
|
|
|
|
bool new_mipmaps = p_images[0]->has_mipmaps();
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
for (int i = 1; i < p_images.size(); i++) {
|
|
|
|
ERR_FAIL_COND_V_MSG(p_images[i]->get_format() != new_format, ERR_INVALID_PARAMETER,
|
|
|
|
"All images must share the same format");
|
|
|
|
ERR_FAIL_COND_V_MSG(p_images[i]->get_width() != new_width || p_images[i]->get_height() != new_height, ERR_INVALID_PARAMETER,
|
|
|
|
"All images must share the same dimensions");
|
|
|
|
ERR_FAIL_COND_V_MSG(p_images[i]->has_mipmaps() != new_mipmaps, ERR_INVALID_PARAMETER,
|
|
|
|
"All images must share the usage of mipmaps");
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-05-01 12:34:23 +00:00
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_layered_create(p_images, RS::TextureLayeredType(layered_type));
|
2019-06-11 18:43:37 +00:00
|
|
|
ERR_FAIL_COND_V(!new_texture.is_valid(), ERR_CANT_CREATE);
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->texture_replace(texture, new_texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
} else {
|
2020-05-01 12:34:23 +00:00
|
|
|
texture = RS::get_singleton()->texture_2d_layered_create(p_images, RS::TextureLayeredType(layered_type));
|
2019-06-11 18:43:37 +00:00
|
|
|
ERR_FAIL_COND_V(!texture.is_valid(), ERR_CANT_CREATE);
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
format = new_format;
|
|
|
|
width = new_width;
|
|
|
|
height = new_height;
|
|
|
|
layers = new_layers;
|
|
|
|
mipmaps = new_mipmaps;
|
|
|
|
return OK;
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
void ImageTextureLayered::update_layer(const Ref<Image> &p_image, int p_layer) {
|
2022-07-07 04:09:07 +00:00
|
|
|
ERR_FAIL_COND_MSG(texture.is_null(), "Texture is not initialized.");
|
|
|
|
ERR_FAIL_COND_MSG(p_image.is_null(), "Invalid image.");
|
|
|
|
ERR_FAIL_COND_MSG(p_image->get_format() != format, "Image format must match texture's image format.");
|
|
|
|
ERR_FAIL_COND_MSG(p_image->get_width() != width || p_image->get_height() != height, "Image size must match texture's image size.");
|
|
|
|
ERR_FAIL_COND_MSG(p_image->has_mipmaps() != mipmaps, "Image mipmap configuration must match texture's image mipmap configuration.");
|
|
|
|
ERR_FAIL_INDEX_MSG(p_layer, layers, "Layer index is out of bounds.");
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->texture_2d_update(texture, p_image, p_layer);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
Ref<Image> ImageTextureLayered::get_layer_data(int p_layer) const {
|
2019-06-11 18:43:37 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_layer, layers, Ref<Image>());
|
2020-03-27 18:21:27 +00:00
|
|
|
return RS::get_singleton()->texture_2d_layer_get(texture, p_layer);
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
RID ImageTextureLayered::get_rid() const {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_null()) {
|
2020-05-01 12:34:23 +00:00
|
|
|
texture = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2018-08-06 17:56:06 +00:00
|
|
|
return texture;
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
void ImageTextureLayered::set_path(const String &p_path, bool p_take_over) {
|
2018-08-06 17:56:06 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->texture_set_path(texture, p_path);
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2018-08-06 17:56:06 +00:00
|
|
|
Resource::set_path(p_path, p_take_over);
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
void ImageTextureLayered::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("create_from_images", "images"), &ImageTextureLayered::_create_from_images);
|
|
|
|
ClassDB::bind_method(D_METHOD("update_layer", "image", "layer"), &ImageTextureLayered::update_layer);
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_get_images"), &ImageTextureLayered::_get_images);
|
2023-03-09 16:07:39 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_images", "images"), &ImageTextureLayered::_set_images);
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2023-03-09 16:07:39 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_images", PROPERTY_HINT_ARRAY_TYPE, "Image", PROPERTY_USAGE_INTERNAL), "_set_images", "_get_images");
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
ImageTextureLayered::ImageTextureLayered(LayeredType p_layered_type) {
|
2019-06-11 18:43:37 +00:00
|
|
|
layered_type = p_layered_type;
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
ImageTextureLayered::~ImageTextureLayered() {
|
2018-06-26 11:59:26 +00:00
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->free(texture);
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:49:11 +00:00
|
|
|
void Texture2DArray::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2DArray::create_placeholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Resource> Texture2DArray::create_placeholder() const {
|
|
|
|
Ref<PlaceholderTexture2DArray> placeholder;
|
|
|
|
placeholder.instantiate();
|
|
|
|
placeholder->set_size(Size2i(get_width(), get_height()));
|
|
|
|
placeholder->set_layers(get_layers());
|
|
|
|
return placeholder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cubemap::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("create_placeholder"), &Cubemap::create_placeholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Resource> Cubemap::create_placeholder() const {
|
|
|
|
Ref<PlaceholderCubemap> placeholder;
|
|
|
|
placeholder.instantiate();
|
|
|
|
placeholder->set_size(Size2i(get_width(), get_height()));
|
|
|
|
placeholder->set_layers(get_layers());
|
|
|
|
return placeholder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CubemapArray::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("create_placeholder"), &CubemapArray::create_placeholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Resource> CubemapArray::create_placeholder() const {
|
|
|
|
Ref<PlaceholderCubemapArray> placeholder;
|
|
|
|
placeholder.instantiate();
|
|
|
|
placeholder->set_size(Size2i(get_width(), get_height()));
|
|
|
|
placeholder->set_layers(get_layers());
|
|
|
|
return placeholder;
|
|
|
|
}
|
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
///////////////////////////////////////////
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTextureLayered::set_path(const String &p_path, bool p_take_over) {
|
2020-05-01 12:34:23 +00:00
|
|
|
if (texture.is_valid()) {
|
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
Resource::set_path(p_path, p_take_over);
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Image::Format CompressedTextureLayered::get_format() const {
|
2020-05-01 12:34:23 +00:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Error CompressedTextureLayered::_load_data(const String &p_path, Vector<Ref<Image>> &images, int &mipmap_limit, int p_size_limit) {
|
2020-05-01 12:34:23 +00:00
|
|
|
ERR_FAIL_COND_V(images.size() != 0, ERR_INVALID_PARAMETER);
|
|
|
|
|
2022-03-23 09:08:58 +00:00
|
|
|
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Unable to open file: %s.", p_path));
|
2020-05-01 12:34:23 +00:00
|
|
|
|
|
|
|
uint8_t header[4];
|
|
|
|
f->get_buffer(header, 4);
|
|
|
|
if (header[0] != 'G' || header[1] != 'S' || header[2] != 'T' || header[3] != 'L') {
|
2022-03-10 03:22:36 +00:00
|
|
|
ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture layered file is corrupt (Bad header).");
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
uint32_t version = f->get_32();
|
|
|
|
|
|
|
|
if (version > FORMAT_VERSION) {
|
2022-03-10 03:22:36 +00:00
|
|
|
ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Compressed texture file is too new.");
|
2020-05-01 12:34:23 +00:00
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
uint32_t layer_count = f->get_32(); //layer count
|
|
|
|
uint32_t type = f->get_32(); //layer count
|
2022-09-28 14:43:09 +00:00
|
|
|
ERR_FAIL_COND_V((int)type != layered_type, ERR_INVALID_DATA);
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
uint32_t df = f->get_32(); //data format
|
|
|
|
mipmap_limit = int(f->get_32());
|
2020-07-16 16:54:15 +00:00
|
|
|
//reserved
|
2020-05-01 12:34:23 +00:00
|
|
|
f->get_32();
|
|
|
|
f->get_32();
|
|
|
|
f->get_32();
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
if (!(df & FORMAT_BIT_STREAM)) {
|
|
|
|
p_size_limit = 0;
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
images.resize(layer_count);
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
for (uint32_t i = 0; i < layer_count; i++) {
|
2022-03-05 15:43:38 +00:00
|
|
|
Ref<Image> image = CompressedTexture2D::load_image_from_file(f, p_size_limit);
|
2020-12-15 12:04:21 +00:00
|
|
|
ERR_FAIL_COND_V(image.is_null() || image->is_empty(), ERR_CANT_OPEN);
|
2020-05-01 12:34:23 +00:00
|
|
|
images.write[i] = image;
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
return OK;
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Error CompressedTextureLayered::load(const String &p_path) {
|
2020-05-01 12:34:23 +00:00
|
|
|
Vector<Ref<Image>> images;
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
int mipmap_limit;
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
Error err = _load_data(p_path, images, mipmap_limit);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (err) {
|
2020-05-01 12:34:23 +00:00
|
|
|
return err;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
if (texture.is_valid()) {
|
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
|
|
|
|
RS::get_singleton()->texture_replace(texture, new_texture);
|
|
|
|
} else {
|
|
|
|
texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TextureLayeredType(layered_type));
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
w = images[0]->get_width();
|
|
|
|
h = images[0]->get_height();
|
|
|
|
mipmaps = images[0]->has_mipmaps();
|
|
|
|
format = images[0]->get_format();
|
|
|
|
layers = images.size();
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
path_to_file = p_path;
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2021-12-09 09:42:46 +00:00
|
|
|
if (get_path().is_empty()) {
|
2020-05-01 12:34:23 +00:00
|
|
|
//temporarily set path if no path set for resource, helps find errors
|
|
|
|
RenderingServer::get_singleton()->texture_set_path(texture, p_path);
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2020-05-01 12:34:23 +00:00
|
|
|
emit_changed();
|
|
|
|
return OK;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
String CompressedTextureLayered::get_load_path() const {
|
2020-05-01 12:34:23 +00:00
|
|
|
return path_to_file;
|
|
|
|
}
|
2018-06-26 11:59:26 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
int CompressedTextureLayered::get_width() const {
|
2020-05-01 12:34:23 +00:00
|
|
|
return w;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
int CompressedTextureLayered::get_height() const {
|
2020-05-01 12:34:23 +00:00
|
|
|
return h;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
int CompressedTextureLayered::get_layers() const {
|
2020-05-01 12:34:23 +00:00
|
|
|
return layers;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
bool CompressedTextureLayered::has_mipmaps() const {
|
2020-05-01 12:34:23 +00:00
|
|
|
return mipmaps;
|
|
|
|
}
|
2018-08-06 17:56:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
TextureLayered::LayeredType CompressedTextureLayered::get_layered_type() const {
|
2020-05-01 12:34:23 +00:00
|
|
|
return layered_type;
|
|
|
|
}
|
2018-08-06 17:56:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
RID CompressedTextureLayered::get_rid() const {
|
2020-05-01 12:34:23 +00:00
|
|
|
if (!texture.is_valid()) {
|
|
|
|
texture = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
2020-05-01 12:34:23 +00:00
|
|
|
return texture;
|
|
|
|
}
|
2018-08-06 17:56:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
Ref<Image> CompressedTextureLayered::get_layer_data(int p_layer) const {
|
2020-05-01 12:34:23 +00:00
|
|
|
if (texture.is_valid()) {
|
|
|
|
return RS::get_singleton()->texture_2d_layer_get(texture, p_layer);
|
2019-06-11 18:43:37 +00:00
|
|
|
} else {
|
2020-05-01 12:34:23 +00:00
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTextureLayered::reload_from_file() {
|
2020-05-01 12:34:23 +00:00
|
|
|
String path = get_path();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!path.is_resource_file()) {
|
2020-05-01 12:34:23 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-06-11 18:43:37 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
path = ResourceLoader::path_remap(path); //remap for translation
|
|
|
|
path = ResourceLoader::import_remap(path); //remap for import
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!path.is_resource_file()) {
|
2020-05-01 12:34:23 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-05-01 12:34:23 +00:00
|
|
|
|
|
|
|
load(path);
|
|
|
|
}
|
|
|
|
|
2022-08-12 20:57:11 +00:00
|
|
|
void CompressedTextureLayered::_validate_property(PropertyInfo &p_property) const {
|
2020-05-01 12:34:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void CompressedTextureLayered::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("load", "path"), &CompressedTextureLayered::load);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_load_path"), &CompressedTextureLayered::get_load_path);
|
2020-05-01 12:34:23 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "load_path", PROPERTY_HINT_FILE, "*.ctex"), "load", "get_load_path");
|
2020-05-01 12:34:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
CompressedTextureLayered::CompressedTextureLayered(LayeredType p_type) {
|
2020-05-01 12:34:23 +00:00
|
|
|
layered_type = p_type;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
CompressedTextureLayered::~CompressedTextureLayered() {
|
2020-05-01 12:34:23 +00:00
|
|
|
if (texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-05-01 12:34:23 +00:00
|
|
|
RS::get_singleton()->free(texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2020-05-01 12:34:23 +00:00
|
|
|
}
|
2018-08-06 17:56:06 +00:00
|
|
|
|
2020-05-01 12:34:23 +00:00
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
2022-05-02 23:43:50 +00:00
|
|
|
Ref<Resource> ResourceFormatLoaderCompressedTextureLayered::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
|
2022-03-10 03:22:36 +00:00
|
|
|
Ref<CompressedTextureLayered> ct;
|
|
|
|
if (p_path.get_extension().to_lower() == "ctexarray") {
|
|
|
|
Ref<CompressedTexture2DArray> c;
|
|
|
|
c.instantiate();
|
|
|
|
ct = c;
|
|
|
|
} else if (p_path.get_extension().to_lower() == "ccube") {
|
|
|
|
Ref<CompressedCubemap> c;
|
|
|
|
c.instantiate();
|
|
|
|
ct = c;
|
|
|
|
} else if (p_path.get_extension().to_lower() == "ccubearray") {
|
|
|
|
Ref<CompressedCubemapArray> c;
|
|
|
|
c.instantiate();
|
|
|
|
ct = c;
|
2020-05-01 12:34:23 +00:00
|
|
|
} else {
|
|
|
|
if (r_error) {
|
|
|
|
*r_error = ERR_FILE_UNRECOGNIZED;
|
|
|
|
}
|
2022-05-02 23:43:50 +00:00
|
|
|
return Ref<Resource>();
|
2020-05-01 12:34:23 +00:00
|
|
|
}
|
2022-03-10 03:22:36 +00:00
|
|
|
Error err = ct->load(p_path);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (r_error) {
|
2020-05-01 12:34:23 +00:00
|
|
|
*r_error = err;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (err != OK) {
|
2022-05-02 23:43:50 +00:00
|
|
|
return Ref<Resource>();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-05-01 12:34:23 +00:00
|
|
|
|
2022-03-10 03:22:36 +00:00
|
|
|
return ct;
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
void ResourceFormatLoaderCompressedTextureLayered::get_recognized_extensions(List<String> *p_extensions) const {
|
2022-03-10 03:22:36 +00:00
|
|
|
p_extensions->push_back("ctexarray");
|
|
|
|
p_extensions->push_back("ccube");
|
|
|
|
p_extensions->push_back("ccubearray");
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
bool ResourceFormatLoaderCompressedTextureLayered::handles_type(const String &p_type) const {
|
|
|
|
return p_type == "CompressedTexture2DArray" || p_type == "CompressedCubemap" || p_type == "CompressedCubemapArray";
|
2018-08-06 17:56:06 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2022-03-05 15:43:38 +00:00
|
|
|
String ResourceFormatLoaderCompressedTextureLayered::get_resource_type(const String &p_path) const {
|
2022-03-10 03:22:36 +00:00
|
|
|
if (p_path.get_extension().to_lower() == "ctexarray") {
|
2022-03-05 15:43:38 +00:00
|
|
|
return "CompressedTexture2DArray";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2022-03-10 03:22:36 +00:00
|
|
|
if (p_path.get_extension().to_lower() == "ccube") {
|
2022-03-05 15:43:38 +00:00
|
|
|
return "CompressedCubemap";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2022-03-10 03:22:36 +00:00
|
|
|
if (p_path.get_extension().to_lower() == "ccubearray") {
|
2022-03-05 15:43:38 +00:00
|
|
|
return "CompressedCubemapArray";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-08-06 17:56:06 +00:00
|
|
|
return "";
|
2018-06-26 11:59:26 +00:00
|
|
|
}
|
2017-08-20 14:17:24 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
///////////////////////////////
|
|
|
|
|
2017-08-20 14:17:24 +00:00
|
|
|
void CameraTexture::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_camera_feed_id", "feed_id"), &CameraTexture::set_camera_feed_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_camera_feed_id"), &CameraTexture::get_camera_feed_id);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_which_feed", "which_feed"), &CameraTexture::set_which_feed);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_which_feed"), &CameraTexture::get_which_feed);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_camera_active", "active"), &CameraTexture::set_camera_active);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_camera_active"), &CameraTexture::get_camera_active);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "camera_feed_id"), "set_camera_feed_id", "get_camera_feed_id");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "which_feed"), "set_which_feed", "get_which_feed");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "camera_is_active"), "set_camera_active", "get_camera_active");
|
|
|
|
}
|
|
|
|
|
|
|
|
int CameraTexture::get_width() const {
|
|
|
|
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
|
|
|
|
if (feed.is_valid()) {
|
|
|
|
return feed->get_base_width();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CameraTexture::get_height() const {
|
|
|
|
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
|
|
|
|
if (feed.is_valid()) {
|
|
|
|
return feed->get_base_height();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CameraTexture::has_alpha() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RID CameraTexture::get_rid() const {
|
|
|
|
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
|
|
|
|
if (feed.is_valid()) {
|
|
|
|
return feed->get_texture(which_feed);
|
|
|
|
} else {
|
2021-08-13 09:32:01 +00:00
|
|
|
if (_texture.is_null()) {
|
|
|
|
_texture = RenderingServer::get_singleton()->texture_2d_placeholder_create();
|
|
|
|
}
|
|
|
|
return _texture;
|
2017-08-20 14:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:32:17 +00:00
|
|
|
Ref<Image> CameraTexture::get_image() const {
|
2017-08-20 14:17:24 +00:00
|
|
|
// not (yet) supported
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraTexture::set_camera_feed_id(int p_new_id) {
|
|
|
|
camera_feed_id = p_new_id;
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2017-08-20 14:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CameraTexture::get_camera_feed_id() const {
|
|
|
|
return camera_feed_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraTexture::set_which_feed(CameraServer::FeedImage p_which) {
|
|
|
|
which_feed = p_which;
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2017-08-20 14:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CameraServer::FeedImage CameraTexture::get_which_feed() const {
|
|
|
|
return which_feed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraTexture::set_camera_active(bool p_active) {
|
|
|
|
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
|
|
|
|
if (feed.is_valid()) {
|
|
|
|
feed->set_active(p_active);
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2017-08-20 14:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CameraTexture::get_camera_active() const {
|
|
|
|
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
|
|
|
|
if (feed.is_valid()) {
|
|
|
|
return feed->is_active();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:24:36 +00:00
|
|
|
CameraTexture::CameraTexture() {}
|
2017-08-20 14:17:24 +00:00
|
|
|
|
|
|
|
CameraTexture::~CameraTexture() {
|
2021-08-13 09:32:01 +00:00
|
|
|
if (_texture.is_valid()) {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2021-08-13 09:32:01 +00:00
|
|
|
RenderingServer::get_singleton()->free(_texture);
|
|
|
|
}
|
2017-08-20 14:17:24 +00:00
|
|
|
}
|
2022-04-28 09:46:45 +00:00
|
|
|
|
|
|
|
///////////////////////////
|
|
|
|
|
|
|
|
void PlaceholderTexture2D::set_size(Size2 p_size) {
|
|
|
|
size = p_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PlaceholderTexture2D::get_width() const {
|
|
|
|
return size.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PlaceholderTexture2D::get_height() const {
|
|
|
|
return size.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderTexture2D::has_alpha() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> PlaceholderTexture2D::get_image() const {
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
|
|
|
|
RID PlaceholderTexture2D::get_rid() const {
|
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaceholderTexture2D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaceholderTexture2D::set_size);
|
|
|
|
|
2023-01-01 16:12:10 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
|
2022-04-28 09:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PlaceholderTexture2D::PlaceholderTexture2D() {
|
|
|
|
rid = RS::get_singleton()->texture_2d_placeholder_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
PlaceholderTexture2D::~PlaceholderTexture2D() {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2022-04-28 09:46:45 +00:00
|
|
|
RS::get_singleton()->free(rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
|
|
|
|
|
|
void PlaceholderTexture3D::set_size(const Vector3i &p_size) {
|
|
|
|
size = p_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3i PlaceholderTexture3D::get_size() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Image::Format PlaceholderTexture3D::get_format() const {
|
|
|
|
return Image::FORMAT_RGB8;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PlaceholderTexture3D::get_width() const {
|
|
|
|
return size.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PlaceholderTexture3D::get_height() const {
|
|
|
|
return size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PlaceholderTexture3D::get_depth() const {
|
|
|
|
return size.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderTexture3D::has_mipmaps() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Ref<Image>> PlaceholderTexture3D::get_data() const {
|
|
|
|
return Vector<Ref<Image>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaceholderTexture3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaceholderTexture3D::set_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_size"), &PlaceholderTexture3D::get_size);
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
|
2022-04-28 09:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PlaceholderTexture3D::PlaceholderTexture3D() {
|
|
|
|
rid = RS::get_singleton()->texture_3d_placeholder_create();
|
|
|
|
}
|
|
|
|
PlaceholderTexture3D::~PlaceholderTexture3D() {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2022-04-28 09:46:45 +00:00
|
|
|
RS::get_singleton()->free(rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void PlaceholderTextureLayered::set_size(const Size2i &p_size) {
|
|
|
|
size = p_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2i PlaceholderTextureLayered::get_size() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaceholderTextureLayered::set_layers(int p_layers) {
|
|
|
|
layers = p_layers;
|
|
|
|
}
|
|
|
|
|
|
|
|
Image::Format PlaceholderTextureLayered::get_format() const {
|
|
|
|
return Image::FORMAT_RGB8;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureLayered::LayeredType PlaceholderTextureLayered::get_layered_type() const {
|
|
|
|
return layered_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PlaceholderTextureLayered::get_width() const {
|
|
|
|
return size.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PlaceholderTextureLayered::get_height() const {
|
|
|
|
return size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PlaceholderTextureLayered::get_layers() const {
|
|
|
|
return layers;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderTextureLayered::has_mipmaps() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> PlaceholderTextureLayered::get_layer_data(int p_layer) const {
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaceholderTextureLayered::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaceholderTextureLayered::set_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_size"), &PlaceholderTextureLayered::get_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_layers", "layers"), &PlaceholderTextureLayered::set_layers);
|
2022-05-20 05:24:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
|
2022-04-28 09:46:45 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_RANGE, "1,4096"), "set_layers", "get_layers");
|
|
|
|
}
|
|
|
|
|
|
|
|
PlaceholderTextureLayered::PlaceholderTextureLayered(LayeredType p_type) {
|
|
|
|
layered_type = p_type;
|
|
|
|
rid = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
|
|
|
|
}
|
|
|
|
PlaceholderTextureLayered::~PlaceholderTextureLayered() {
|
2022-12-12 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2022-04-28 09:46:45 +00:00
|
|
|
RS::get_singleton()->free(rid);
|
|
|
|
}
|