feat: canvas from texture atlas element

This commit is contained in:
MihailRis
2025-11-05 00:20:58 +03:00
parent 497160a5b9
commit d9f2d54bf0
10 changed files with 126 additions and 28 deletions
@@ -7,7 +7,9 @@
#include "engine/Engine.hpp"
#include "graphics/commons/Model.hpp"
#include "graphics/core/Texture.hpp"
#include "graphics/core/Atlas.hpp"
#include "util/Buffer.hpp"
#include "../lua_custom_types.hpp"
using namespace scripting;
@@ -64,8 +66,37 @@ static int l_parse_model(lua::State* L) {
return 0;
}
static int l_to_canvas(lua::State* L) {
auto alias = lua::require_lstring(L, 1);
size_t sep = alias.rfind(':');
if (sep == std::string::npos) {
return 0;
}
auto atlasName = alias.substr(0, sep);
auto& assets = *engine->getAssets();
if (auto atlas = assets.get<Atlas>(std::string(atlasName))) {
auto textureName = std::string(alias.substr(sep + 1));
auto image = atlas->shareImageData();
auto texture = atlas->shareTexture();
if (auto region = atlas->getIf(textureName)) {
UVRegion uvRegion = *region;
int atlasWidth = static_cast<int>(image->getWidth());
int atlasHeight = static_cast<int>(image->getHeight());
int x = static_cast<int>(uvRegion.u1 * atlasWidth);
int y = static_cast<int>(uvRegion.v1 * atlasHeight);
int w = static_cast<int>(uvRegion.getWidth() * atlasWidth);
int h = static_cast<int>(uvRegion.getHeight() * atlasHeight);
return lua::newuserdata<lua::LuaCanvas>(
L, std::move(texture), image->cropped(x, y, w, h), uvRegion
);
}
}
return 0;
}
const luaL_Reg assetslib[] = {
{"load_texture", lua::wrap<l_load_texture>},
{"parse_model", lua::wrap<l_parse_model>},
{"to_canvas", lua::wrap<l_to_canvas>},
{nullptr, nullptr}
};
+15 -10
View File
@@ -6,6 +6,7 @@
#include <random>
#include "lua_commons.hpp"
#include "maths/UVRegion.hpp"
struct fnl_state;
class Heightmap;
@@ -81,8 +82,9 @@ namespace lua {
class LuaCanvas : public Userdata {
public:
explicit LuaCanvas(
std::shared_ptr<Texture> inTexture,
std::shared_ptr<ImageData> inData
std::shared_ptr<Texture> texture,
std::shared_ptr<ImageData> data,
UVRegion region = UVRegion(0, 0, 1, 1)
);
~LuaCanvas() override = default;
@@ -90,29 +92,32 @@ namespace lua {
return TYPENAME;
}
[[nodiscard]] auto& texture() const {
return *mTexture;
[[nodiscard]] auto& getTexture() const {
return *texture;
}
[[nodiscard]] auto& data() const {
return *mData;
[[nodiscard]] auto& getData() const {
return *data;
}
[[nodiscard]] bool hasTexture() const {
return mTexture != nullptr;
return texture != nullptr;
}
auto shareTexture() const {
return mTexture;
return texture;
}
void update();
void createTexture();
static int createMetatable(lua::State*);
inline static std::string TYPENAME = "Canvas";
private:
std::shared_ptr<Texture> mTexture; // nullable
std::shared_ptr<ImageData> mData;
std::shared_ptr<Texture> texture; // nullable
std::shared_ptr<ImageData> data;
UVRegion region;
};
static_assert(!std::is_abstract<LuaCanvas>());
@@ -10,14 +10,42 @@
using namespace lua;
LuaCanvas::LuaCanvas(
std::shared_ptr<Texture> inTexture, std::shared_ptr<ImageData> inData
std::shared_ptr<Texture> texture,
std::shared_ptr<ImageData> data,
UVRegion region
)
: mTexture(std::move(inTexture)), mData(std::move(inData)) {
: texture(std::move(texture)),
data(std::move(data)),
region(std::move(region)) {
}
void LuaCanvas::update() {
if (!hasTexture()) {
return;
}
if (region.isFull()) {
texture->reload(*data);
} else {
uint texWidth = texture->getWidth();
uint texHeight = texture->getHeight();
uint imgWidth = data->getWidth();
uint imgHeight = data->getHeight();
uint x = static_cast<uint>(region.u1 * texWidth);
uint y = static_cast<uint>(region.v1 * texHeight);
uint w = static_cast<uint>((region.u2 - region.u1) * texWidth);
uint h = static_cast<uint>((region.v2 - region.v1) * texHeight);
w = std::min<uint>(w, imgWidth);
h = std::min<uint>(h, imgHeight);
texture->reloadPartial(*data, x, y, w, h);
}
}
void LuaCanvas::createTexture() {
mTexture = Texture::from(mData.get());
mTexture->setMipMapping(false, true);
texture = Texture::from(data.get());
texture->setMipMapping(false, true);
}
union RGBA {
@@ -41,7 +69,7 @@ static RGBA* get_at(const ImageData& data, uint x, uint y) {
static RGBA* get_at(State* L, uint x, uint y) {
if (auto canvas = touserdata<LuaCanvas>(L, 1)) {
return get_at(canvas->data(), x, y);
return get_at(canvas->getData(), x, y);
}
return nullptr;
}
@@ -97,7 +125,7 @@ static LuaCanvas& require_canvas(State* L, int idx) {
static int l_clear(State* L) {
auto& canvas = require_canvas(L, 1);
auto& image = canvas.data();
auto& image = canvas.getData();
ubyte* data = image.getData();
RGBA rgba {};
if (gettop(L) == 1) {
@@ -122,7 +150,7 @@ static int l_line(State* L) {
RGBA rgba = get_rgba(L, 6);
if (auto canvas = touserdata<LuaCanvas>(L, 1)) {
auto& image = canvas->data();
auto& image = canvas->getData();
image.drawLine(
x1, y1, x2, y2, glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a}
);
@@ -135,13 +163,13 @@ static int l_blit(State* L) {
auto& src = require_canvas(L, 2);
int dst_x = tointeger(L, 3);
int dst_y = tointeger(L, 4);
dst.data().blit(src.data(), dst_x, dst_y);
dst.getData().blit(src.getData(), dst_x, dst_y);
return 0;
}
static int l_set_data(State* L) {
auto& canvas = require_canvas(L, 1);
auto& image = canvas.data();
auto& image = canvas.getData();
auto data = image.getData();
if (lua::isstring(L, 2)) {
@@ -166,9 +194,7 @@ static int l_set_data(State* L) {
static int l_update(State* L) {
if (auto canvas = touserdata<LuaCanvas>(L, 1)) {
if (canvas->hasTexture()) {
canvas->texture().reload(canvas->data());
}
canvas->update();
}
return 0;
}
@@ -202,7 +228,7 @@ static int l_meta_index(State* L) {
if (texture == nullptr) {
return 0;
}
auto& data = texture->data();
auto& data = texture->getData();
if (isnumber(L, 2)) {
if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) {
return pushinteger(L, pixel->rgba);
@@ -231,7 +257,7 @@ static int l_meta_newindex(State* L) {
if (texture == nullptr) {
return 0;
}
auto& data = texture->data();
auto& data = texture->getData();
if (isnumber(L, 2) && isnumber(L, 3)) {
if (auto pixel = get_at(data, static_cast<uint>(tointeger(L, 2)))) {
pixel->rgba = static_cast<uint>(tointeger(L, 3));