Merge branch 'model3dview' into update-gfx-pipeline

This commit is contained in:
MihailRis 2025-05-30 21:37:25 +03:00
commit 32bb30ff83
29 changed files with 473 additions and 130 deletions

View File

@ -0,0 +1,4 @@
language = "XML"
extensions = ["xml"]
multiline-comment-start = "<!--"
multiline-comment-end = "-->"

View File

@ -1,12 +1,6 @@
<splitbox id="editorRoot" orientation="horizontal" split-pos="0.3">
<splitbox split-pos="0.75">
<panel interval="2" color="0" padding="2">
<textbox pos="2" padding="4,0,4,0" sub-consumer="filter_files" hint="@Filter"></textbox>
<panel id="filesList" color="#00000030" interval="6" padding="4"
size-func="-1,-45" pos="2,38">
<!-- content is generated in script -->
</panel>
</panel>
<iframe src="core:files_panel"></iframe>
<panel id="problemsLog"
color="#00000030"
padding="5,15,5,15">
@ -53,6 +47,7 @@
text-wrap='false'
scroll-step='50'
></textbox>
<modelviewer size="480" model="stairs" center="0.5,0.5,0.5" cam-rotation="45,-45,0" gravity="top-right"/>
</container>
<splitbox orientation="horizontal" split-pos="0.4">
<panel id="traceback" padding="4" color="#000000A0">

View File

@ -1,6 +1,5 @@
local writeables = {}
local registry
local filenames
local current_file = {
filename = "",
@ -49,6 +48,10 @@ events.on("core:error", function (msg, traceback)
table.insert(errors_all, full)
end)
events.on("core:open_in_editor", function(filename, linenum)
open_file_in_editor(filename, linenum)
end)
local function find_mutable(filename)
local packid = file.prefix(filename)
if packid == "core" then
@ -80,16 +83,6 @@ local function refresh_file_title()
..(edited and ' *' or '')
end
function filter_files(text)
local filtered = {}
for _, filename in ipairs(filenames) do
if filename:find(text) then
table.insert(filtered, filename)
end
end
build_files_list(filtered, text)
end
function on_control_combination(keycode)
if keycode == input.keycode("s") then
save_current_file()
@ -229,43 +222,11 @@ function open_file_in_editor(filename, line, mutable)
document.saveIcon.enabled = current_file.modified
end
function build_files_list(filenames, selected)
local files_list = document.filesList
files_list.scroll = 0
files_list:clear()
for _, actual_filename in ipairs(filenames) do
local filename = actual_filename
if selected then
filename = filename:gsub(selected, "**"..selected.."**")
end
local parent = file.parent(filename)
local info = registry.get_info(actual_filename)
local icon = "file"
if info then
icon = info.type == "component" and "entity" or info.type
end
files_list:add(gui.template("script_file", {
path = parent .. (parent[#parent] == ':' and '' or '/'),
name = file.name(filename),
icon = icon,
unit = info and info.unit or '',
filename = actual_filename
}))
end
end
function on_open(mode)
registry = require "core:internal/scripts_registry"
local files_list = document.filesList
document.editorContainer:setInterval(200, refresh_file_title)
clear_traceback()
clear_output()
filenames = registry.filenames
table.sort(filenames)
build_files_list(filenames)
end

View File

@ -0,0 +1,7 @@
<panel interval="2" color="0" padding="2">
<textbox pos="2" padding="4,0,4,0" sub-consumer="filter_files" hint="@Filter"></textbox>
<panel id="filesList" color="#00000030" interval="6" padding="4"
size-func="-1,-45" pos="2,38">
<!-- content is generated in script -->
</panel>
</panel>

View File

@ -0,0 +1,54 @@
local registry
local filenames
function filter_files(text)
local pattern_safe = text:pattern_safe();
local filtered = {}
for _, filename in ipairs(filenames) do
if filename:find(pattern_safe) then
table.insert(filtered, filename)
end
end
build_files_list(filtered, pattern_safe)
end
function open_file_in_editor(filename, linenum)
events.emit("core:open_in_editor", filename, linenum)
end
function build_files_list(filenames, highlighted_part)
local files_list = document.filesList
files_list.scroll = 0
files_list:clear()
for _, actual_filename in ipairs(filenames) do
local filename = actual_filename
if highlighted_part then
filename = filename:gsub(highlighted_part, "**"..highlighted_part.."**")
end
local parent = file.parent(filename)
local info = registry.get_info(actual_filename)
local icon = "file"
if info then
icon = info.type == "component" and "entity" or info.type
end
files_list:add(gui.template("script_file", {
path = parent .. (parent[#parent] == ':' and '' or '/'),
name = file.name(filename),
icon = icon,
unit = info and info.unit or '',
filename = actual_filename,
open_func = "open_file_in_editor",
}))
end
end
function on_open(mode)
registry = require "core:internal/scripts_registry"
local files_list = document.filesList
filenames = registry.filenames
table.sort(filenames)
build_files_list(filenames)
end

View File

@ -3,7 +3,7 @@
<label hover-color='#30A0FF'
pos="20,2"
interactive="true"
onclick='open_file_in_editor("%{filename}")'
onclick='%{open_func}("%{filename}")'
markup='md'
tooltip='%{unit}'
sizefunc="-1,-1">

View File

@ -1,6 +1,6 @@
<model>
<box from="0,0,0" to="1,0.5,1" delete="top,south"/>
<box from="0,0.5,0" to="1,1,0.5" delete="bottom,south"/>
<rect from="0,0.5,0.5" right="1,0,0" up="0,0,0.5"/>
<rect from="0,0,0" right="1,0,0" up="0,1,0"/>
<rect from="0,0.5,0.5" right="1,0,0" up="0,0,0.5" texture="$2"/>
<rect from="0,0,0" right="1,0,0" up="0,1,0" texture="$1"/>
</model>

View File

@ -1,6 +1,6 @@
local export = {
filenames = {},
classification = {}
registry = {}
}
local function collect_components(dirname, dest)
@ -9,7 +9,7 @@ local function collect_components(dirname, dest)
for i, filename in ipairs(files) do
if file.ext(filename) == "lua" then
table.insert(dest, filename)
export.classification[filename] = {
export.registry[filename] = {
type="component",
unit=file.prefix(filename)..":"..file.stem(filename)
}
@ -33,13 +33,13 @@ local function collect_scripts(dirname, dest, ismodule)
end
end
local function load_scripts_list()
local packs = pack.get_installed()
local function load_scripts_list(packs)
local registry = export.registry
for _, packid in ipairs(packs) do
collect_scripts(packid..":modules", export.filenames, true)
end
for _, filename in ipairs(export.filenames) do
export.classification[filename] = {
registry[filename] = {
type="module",
unit=file.join(file.parent(file.prefix(filename)..":"..
filename:sub(filename:find("/")+1)),
@ -51,27 +51,39 @@ local function load_scripts_list()
end
end
function export.build_classification()
local classification = {}
local function load_models_list(packs)
local registry = export.registry
for _, filename in ipairs(file.list("models")) do
local ext = file.ext(filename)
if ext == "xml" then
registry[filename] = {type="model", unit=file.stem(filename)}
table.insert(export.filenames, filename)
end
end
end
function export.build_registry()
local registry = {}
for id, props in pairs(block.properties) do
classification[props["script-file"]] = {type="block", unit=block.name(id)}
registry[props["script-file"]] = {type="block", unit=block.name(id)}
end
for id, props in pairs(item.properties) do
classification[props["script-file"]] = {type="item", unit=item.name(id)}
registry[props["script-file"]] = {type="item", unit=item.name(id)}
end
local packs = pack.get_installed()
for _, packid in ipairs(packs) do
classification[packid..":scripts/world.lua"] = {type="world", unit=packid}
classification[packid..":scripts/hud.lua"] = {type="hud", unit=packid}
registry[packid..":scripts/world.lua"] = {type="world", unit=packid}
registry[packid..":scripts/hud.lua"] = {type="hud", unit=packid}
end
export.classification = classification
export.registry = registry
export.filenames = {}
load_scripts_list()
load_scripts_list(packs)
load_models_list(packs)
end
function export.get_info(filename)
return export.classification[filename]
return export.registry[filename]
end
return export

View File

@ -63,4 +63,4 @@ cache_names(block)
cache_names(item)
local scripts_registry = require "core:internal/scripts_registry"
scripts_registry.build_classification()
scripts_registry.build_registry()

View File

@ -523,9 +523,6 @@ function time.post_runnable(runnable)
table.insert(__post_runnables, runnable)
end
assets = {}
assets.load_texture = core.__load_texture
-- --------- Deprecated functions ------ --
local function wrap_deprecated(func, name, alternatives)
return function (...)

View File

@ -9,7 +9,11 @@ util::TextureRegion util::get_texture_region(
) {
size_t sep = name.find(':');
if (sep == std::string::npos) {
return {assets.get<Texture>(name), UVRegion(0,0,1,1)};
auto texture = assets.get<Texture>(name);
if (texture == nullptr && !fallback.empty()) {
return util::get_texture_region(assets, fallback, "");
}
return {texture, UVRegion(0,0,1,1)};
} else {
auto atlas = assets.get<Atlas>(name.substr(0, sep));
if (atlas) {

View File

@ -14,8 +14,8 @@ static const std::unordered_map<std::string, int> side_indices {
{"south", 1},
{"top", 2},
{"bottom", 3},
{"east", 4},
{"west", 5},
{"west", 4},
{"east", 5},
};
static void perform_rect(const xmlelement& root, model::Model& model) {

View File

@ -77,13 +77,13 @@ void Mesh::addBox(
if (enabledSides[1]) // south
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
if (enabledSides[2]) // top
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]);
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2] * glm::vec2(-1));
if (enabledSides[3]) // bottom
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]);
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3] * glm::vec2(-1, 1));
if (enabledSides[4]) // west
addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]);
if (enabledSides[5]) // east
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]);
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5] * glm::vec2(-1, 1));
}
void Mesh::scale(const glm::vec3& size) {

View File

@ -6,6 +6,11 @@
#include "typedefs.hpp"
#include "maths/UVRegion.hpp"
namespace {
const glm::vec3 SUN_VECTOR(0.528265f, 0.833149f, -0.163704f);
const float DIRECTIONAL_LIGHT_FACTOR = 0.3f;
}
Batch3D::Batch3D(size_t capacity)
: capacity(capacity) {
buffer = std::make_unique<Batch3DVertex[]>(capacity);
@ -29,20 +34,28 @@ void Batch3D::begin(){
}
void Batch3D::vertex(
float x, float y, float z, float u, float v,
float r, float g, float b, float a
float x,
float y,
float z,
float u,
float v,
float r,
float g,
float b,
float a
) {
buffer[index].position = {x, y, z};
buffer[index].uv = {u, v};
buffer[index].uv = {
u * region.getWidth() + region.u1, v * region.getHeight() + region.v1};
buffer[index].color = {r, g, b, a};
index++;
}
void Batch3D::vertex(
glm::vec3 coord, float u, float v,
float r, float g, float b, float a
glm::vec3 coord, float u, float v, float r, float g, float b, float a
) {
buffer[index].position = coord;
buffer[index].uv = {u, v};
buffer[index].uv = {
u * region.getWidth() + region.u1, v * region.getHeight() + region.v1};
buffer[index].color = {r, g, b, a};
index++;
}
@ -52,7 +65,9 @@ void Batch3D::vertex(
float r, float g, float b, float a
) {
buffer[index].position = point;
buffer[index].uv = uvpoint;
buffer[index].uv = {
uvpoint.x * region.getWidth() + region.u1,
uvpoint.y * region.getHeight() + region.v1};
buffer[index].color = {r, g, b, a};
index++;
}
@ -84,14 +99,18 @@ void Batch3D::face(
}
void Batch3D::texture(const Texture* new_texture){
if (currentTexture == new_texture)
if (currentTexture == new_texture) {
return;
}
flush();
currentTexture = new_texture;
if (new_texture == nullptr)
if (new_texture == nullptr) {
blank->bind();
else
region = blank->getUVRegion();
} else {
new_texture->bind();
region = currentTexture->getUVRegion();
}
}
void Batch3D::sprite(
@ -238,6 +257,16 @@ void Batch3D::blockCube(
cube((1.0f - size) * -0.5f, size, texfaces, tint, shading);
}
void Batch3D::setRegion(UVRegion region) {
this->region = region;
}
void Batch3D::vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec3& norm) {
float d = glm::dot(glm::normalize(norm), SUN_VECTOR);
d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR;
vertex(pos, uv, glm::vec4(d, d, d, 1.0f));
}
void Batch3D::vertex(
const glm::vec3& coord, const glm::vec2& uv, const glm::vec4& tint
) {

View File

@ -3,6 +3,7 @@
#include "typedefs.hpp"
#include "commons.hpp"
#include "MeshData.hpp"
#include "maths/UVRegion.hpp"
#include <memory>
#include <cstdlib>
@ -32,8 +33,8 @@ class Batch3D : public Flushable {
std::unique_ptr<Texture> blank;
size_t index;
glm::vec4 tint {1.0f};
const Texture* currentTexture;
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
void vertex(
float x, float y, float z,
@ -102,6 +103,8 @@ public:
const glm::vec4& tint,
bool shading = true
);
void setRegion(UVRegion region);
void vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec3& norm);
void vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec4& tint);
void point(const glm::vec3& pos, const glm::vec4& tint);
void flush() override;

View File

@ -34,7 +34,7 @@ public:
Batch2D* getBatch2D() const;
const glm::uvec2& getViewport() const;
DrawContext sub(Flushable* flushable=nullptr) const;
[[nodiscard]] DrawContext sub(Flushable* flushable=nullptr) const;
void setViewport(const glm::uvec2& viewport);
void setFramebuffer(Bindable* fbo);

View File

@ -16,6 +16,7 @@
namespace fs = std::filesystem;
GLSLExtension* Shader::preprocessor = new GLSLExtension();
Shader* Shader::used = nullptr;
Shader::Shader(uint id) : id(id){
}
@ -25,6 +26,7 @@ Shader::~Shader(){
}
void Shader::use(){
used = this;
glUseProgram(id);
}
@ -131,3 +133,7 @@ std::unique_ptr<Shader> Shader::create(
}
return std::make_unique<Shader>(id);
}
Shader& Shader::getUsed() {
return *used;
}

View File

@ -10,6 +10,7 @@
class GLSLExtension;
class Shader {
static Shader* used;
uint id;
std::unordered_map<std::string, uint> uniformLocations;
@ -43,4 +44,6 @@ public:
const std::string& vertexSource,
const std::string& fragmentSource
);
static Shader& getUsed();
};

View File

@ -98,7 +98,7 @@ model::Model ModelsGenerator::fromCustom(
for (size_t i = 0; i < modelBoxes.size(); i++) {
auto& mesh = model.addMesh("blocks:");
mesh.lighting = lighting;
const UVRegion boxtexfaces[6] = {
UVRegion boxtexfaces[6] = {
get_region_for(modelTextures[i * 6 + 5], assets),
get_region_for(modelTextures[i * 6 + 4], assets),
get_region_for(modelTextures[i * 6 + 3], assets),
@ -106,6 +106,9 @@ model::Model ModelsGenerator::fromCustom(
get_region_for(modelTextures[i * 6 + 1], assets),
get_region_for(modelTextures[i * 6 + 0], assets)
};
boxtexfaces[2].scale(glm::vec2(-1));
boxtexfaces[5].scale(glm::vec2(-1, 1));
bool enabled[6] {1,1,1,1,1,1};
mesh.addBox(
modelBoxes[i].center(),

View File

@ -36,6 +36,7 @@ void InlineFrame::setDocument(const std::shared_ptr<UiDocument>& document) {
}
void InlineFrame::act(float delta) {
Container::act(delta);
if (document || src.empty()) {
return;
}

View File

@ -0,0 +1,151 @@
#include <glm/ext.hpp>
#include "ModelViewer.hpp"
#include "assets/Assets.hpp"
#include "assets/assets_util.hpp"
#include "graphics/commons/Model.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/Batch3D.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/core/Framebuffer.hpp"
#include "graphics/core/DrawContext.hpp"
#include "window/Window.hpp"
#include "../GUI.hpp"
// TODO: remove
#include <GL/glew.h>
using namespace gui;
ModelViewer::ModelViewer(
GUI& gui, const glm::vec2& size, const std::string& modelName
)
: Container(gui, size),
modelName(modelName),
camera(),
batch(std::make_unique<Batch3D>(1024)),
fbo(std::make_unique<Framebuffer>(size.x, size.y)) {
camera.perspective = true;
camera.position = glm::vec3(2, 2, 2);
}
ModelViewer::~ModelViewer() = default;
void ModelViewer::setModel(const std::string& modelName) {
this->modelName = modelName;
}
const std::string& ModelViewer::getModel() const {
return modelName;
}
Camera& ModelViewer::getCamera() {
return camera;
}
const Camera& ModelViewer::getCamera() const {
return camera;
}
void ModelViewer::act(float delta) {
Container::act(delta);
auto& input = gui.getInput();
if (!grabbing && hover && input.jclicked(Mousecode::BUTTON_3)) {
grabbing = true;
}
if (grabbing && input.clicked(Mousecode::BUTTON_3)) {
auto cursor = input.getCursor();
if (input.pressed(Keycode::LEFT_SHIFT)) {
center -= camera.right * (cursor.delta.x / size.x) * distance;
center += camera.up * (cursor.delta.y / size.y) * distance;
} else {
rotation.x -= cursor.delta.x / size.x * glm::two_pi<float>();
rotation.y -= cursor.delta.y / size.y * glm::two_pi<float>();
rotation.y = glm::max(
-glm::half_pi<float>(), glm::min(glm::half_pi<float>(), rotation.y)
);
}
} else if (grabbing) {
grabbing = false;
}
if (hover) {
distance *= 1.0f - 0.2f * input.getScroll();
}
camera.rotation = glm::mat4(1.0f);
camera.rotate(rotation.y, rotation.x, rotation.z);
camera.position = center - camera.front * distance;
}
void ModelViewer::draw(const DrawContext& pctx, const Assets& assets) {
camera.setAspectRatio(size.x / size.y);
camera.updateVectors();
auto model = assets.get<model::Model>(modelName);
if (model == nullptr) {
return;
}
auto& prevShader = Shader::getUsed();
fbo->resize(size.x, size.y);
{
glDisable(GL_SCISSOR_TEST);
auto ctx = pctx.sub();
ctx.setFramebuffer(fbo.get());
ctx.setViewport({size.x, size.y});
ctx.setDepthTest(true);
ctx.setCullFace(true);
display::clear();
auto& ui3dShader = assets.require<Shader>("ui3d");
ui3dShader.use();
ui3dShader.uniformMatrix("u_apply", glm::mat4(1.0f));
ui3dShader.uniformMatrix("u_projview", camera.getProjView());
batch->begin();
for (const auto& mesh : model->meshes) {
util::TextureRegion region;
if (!mesh.texture.empty() && mesh.texture[0] == '$') {
// todo: refactor
static std::array<std::string, 6> faces {
"blocks:dbg_north",
"blocks:dbg_south",
"blocks:dbg_top",
"blocks:dbg_bottom",
"blocks:dbg_west",
"blocks:dbg_east",
};
region = util::get_texture_region(
assets,
faces.at(mesh.texture.at(1) - '0'),
"blocks:notfound"
);
} else {
region = util::get_texture_region(assets, mesh.texture, "blocks:notfound");
}
batch->texture(region.texture);
batch->setRegion(region.region);
for (const auto& vertex : mesh.vertices) {
batch->vertex(vertex.coord, vertex.uv, vertex.normal);
}
}
batch->flush();
glEnable(GL_SCISSOR_TEST);
}
auto pos = calcPos();
prevShader.use();
auto& batch2d = *pctx.getBatch2D();
batch2d.texture(fbo->getTexture());
batch2d.rect(pos.x, pos.y, size.x, size.y, 0.0f, 0.0f, 0.0f, UVRegion {}, false, true, glm::vec4{1.0f});
Container::draw(pctx, assets);
}
void ModelViewer::setCenter(const glm::vec3& center) {
this->center = center;
}
void ModelViewer::setRotation(const glm::vec3& euler) {
this->rotation = euler;
}

View File

@ -0,0 +1,40 @@
#pragma once
#include <memory>
#include <string>
#include "Container.hpp"
#include "window/Camera.hpp"
class Batch3D;
class Framebuffer;
namespace gui {
class ModelViewer : public Container {
private:
std::string modelName;
Camera camera;
std::unique_ptr<Batch3D> batch;
std::unique_ptr<Framebuffer> fbo;
glm::vec3 rotation {};
glm::vec3 center {};
float distance = 4.0f;
bool grabbing = false;
public:
ModelViewer(GUI& gui, const glm::vec2& size, const std::string& modelName);
~ModelViewer();
void setModel(const std::string& modelName);
const std::string& getModel() const;
Camera& getCamera();
const Camera& getCamera() const;
void act(float delta) override;
void draw(const DrawContext& pctx, const Assets& assets) override;
void setRotation(const glm::vec3& euler);
void setCenter(const glm::vec3& center);
};
}

View File

@ -19,6 +19,7 @@
#include "elements/Panel.hpp"
#include "elements/TextBox.hpp"
#include "elements/TrackBar.hpp"
#include "elements/ModelViewer.hpp"
#include "engine/Engine.hpp"
#include "frontend/locale.hpp"
#include "frontend/menu.hpp"
@ -368,6 +369,23 @@ static std::shared_ptr<UINode> read_split_box(
return splitBox;
}
static std::shared_ptr<UINode> read_model_viewer(
UiXmlReader& reader, const xml::xmlelement& element
) {
auto model = element.attr("model", "").getText();
auto viewer = std::make_shared<ModelViewer>(
reader.getGUI(), glm::vec2(), model
);
read_container_impl(reader, element, *viewer);
if (element.has("center")) {
viewer->setCenter(element.attr("center").asVec3());
}
if (element.has("cam-rotation")) {
viewer->setRotation(glm::radians(element.attr("cam-rotation").asVec3()));
}
return viewer;
}
static std::shared_ptr<UINode> read_panel(
UiXmlReader& reader, const xml::xmlelement& element
) {
@ -785,6 +803,7 @@ UiXmlReader::UiXmlReader(gui::GUI& gui, scriptenv&& env) : gui(gui), env(std::mo
add("trackbar", read_track_bar);
add("container", read_container);
add("bindbox", read_input_bind_box);
add("modelviewer", read_model_viewer);
add("inventory", read_inventory);
}

View File

@ -15,6 +15,7 @@
// Libraries
extern const luaL_Reg applib[];
extern const luaL_Reg assetslib[];
extern const luaL_Reg audiolib[];
extern const luaL_Reg base64lib[];
extern const luaL_Reg bjsonlib[];

View File

@ -0,0 +1,66 @@
#include "api_lua.hpp"
#include "assets/Assets.hpp"
#include "coders/png.hpp"
#include "coders/vcm.hpp"
#include "debug/Logger.hpp"
#include "engine/Engine.hpp"
#include "graphics/commons/Model.hpp"
#include "graphics/core/Texture.hpp"
#include "util/Buffer.hpp"
using namespace scripting;
static void load_texture(
const ubyte* bytes, size_t size, const std::string& destname
) {
try {
engine->getAssets()->store(png::load_texture(bytes, size), destname);
} catch (const std::runtime_error& err) {
debug::Logger logger("lua.assetslib");
logger.error() << err.what();
}
}
static int l_load_texture(lua::State* L) {
if (lua::istable(L, 1)) {
lua::pushvalue(L, 1);
size_t size = lua::objlen(L, 1);
util::Buffer<ubyte> buffer(size);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1);
buffer[i] = lua::tointeger(L, -1);
lua::pop(L);
}
lua::pop(L);
load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2));
} else {
auto string = lua::bytearray_as_string(L, 1);
load_texture(
reinterpret_cast<const ubyte*>(string.data()),
string.size(),
lua::require_string(L, 2)
);
lua::pop(L);
}
return 0;
}
static int l_parse_model(lua::State* L) {
auto format = lua::require_lstring(L, 1);
auto string = lua::require_lstring(L, 2);
auto name = lua::require_string(L, 3);
if (format == "xml") {
engine->getAssets()->store(vcm::parse(name, string), name);
} else {
throw std::runtime_error("unknown format " + util::quote(std::string(format)));
}
return 0;
}
const luaL_Reg assetslib[] = {
{"load_texture", lua::wrap<l_load_texture>},
{"parse_model", lua::wrap<l_parse_model>},
{NULL, NULL}
};

View File

@ -2,12 +2,10 @@
#include <vector>
#include "api_lua.hpp"
#include "coders/png.hpp"
#include "constants.hpp"
#include "assets/Assets.hpp"
#include "content/Content.hpp"
#include "content/ContentControl.hpp"
#include "debug/Logger.hpp"
#include "engine/Engine.hpp"
#include "io/engine_paths.hpp"
#include "io/io.hpp"
@ -225,41 +223,6 @@ static int l_get_setting_info(lua::State* L) {
throw std::runtime_error("unsupported setting type");
}
static void load_texture(
const ubyte* bytes, size_t size, const std::string& destname
) {
try {
engine->getAssets()->store(png::load_texture(bytes, size), destname);
} catch (const std::runtime_error& err) {
debug::Logger logger("lua.corelib");
logger.error() << err.what();
}
}
static int l_load_texture(lua::State* L) {
if (lua::istable(L, 1)) {
lua::pushvalue(L, 1);
size_t size = lua::objlen(L, 1);
util::Buffer<ubyte> buffer(size);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1);
buffer[i] = lua::tointeger(L, -1);
lua::pop(L);
}
lua::pop(L);
load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2));
} else {
auto string = lua::bytearray_as_string(L, 1);
load_texture(
reinterpret_cast<const ubyte*>(string.data()),
string.size(),
lua::require_string(L, 2)
);
lua::pop(L);
}
return 0;
}
static int l_open_folder(lua::State* L) {
platform::open_folder(io::resolve(lua::require_string(L, 1)));
return 0;
@ -322,6 +285,5 @@ const luaL_Reg corelib[] = {
{"open_folder", lua::wrap<l_open_folder>},
{"quit", lua::wrap<l_quit>},
{"capture_output", lua::wrap<l_capture_output>},
{"__load_texture", lua::wrap<l_load_texture>},
{NULL, NULL}
};

View File

@ -490,6 +490,12 @@ static int p_get_scroll(UINode* node, lua::State* L) {
}
static int l_gui_getattr(lua::State* L) {
if (!lua::isstring(L, 1)) {
throw std::runtime_error("document name is not a string");
}
if (!lua::isstring(L, 2)) {
throw std::runtime_error("element id is not a string");
}
auto docname = lua::require_string(L, 1);
auto element = lua::require_string(L, 2);
if (lua::isnumber(L, 3)) {
@ -507,6 +513,9 @@ static int l_gui_getattr(lua::State* L) {
const auto& id = request_node_id(DocumentNode {docnode.document, node});
return push_document_node(L, id);
}
if (!lua::isstring(L, 3)) {
throw std::runtime_error("attribute name is not a string");
}
auto attr = lua::require_string(L, 3);
static const std::unordered_map<
@ -755,6 +764,15 @@ static int p_set_scroll(UINode* node, lua::State* L, int idx) {
}
static int l_gui_setattr(lua::State* L) {
if (!lua::isstring(L, 1)) {
throw std::runtime_error("document name is not a string");
}
if (!lua::isstring(L, 2)) {
throw std::runtime_error("element id is not a string");
}
if (!lua::isstring(L, 3)) {
throw std::runtime_error("attribute name is not a string");
}
auto docname = lua::require_string(L, 1);
auto element = lua::require_string(L, 2);
auto attr = lua::require_string(L, 3);

View File

@ -64,6 +64,7 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "__vc_app", applib);
}
if (stateType == StateType::BASE || stateType == StateType::SCRIPT) {
openlib(L, "assets", assetslib);
openlib(L, "audio", audiolib);
openlib(L, "console", consolelib);
openlib(L, "core", corelib);

View File

@ -43,8 +43,8 @@ struct UVRegion {
}
void scale(float x, float y) {
float w = getWidth();
float h = getHeight();
float w = u2 - u1;
float h = v2 - v1;
float cx = (u1 + u2) * 0.5f;
float cy = (v1 + v2) * 0.5f;
u1 = cx - w * 0.5f * x;
@ -63,4 +63,10 @@ struct UVRegion {
u2 = vec.z;
v2 = vec.w;
}
UVRegion operator*(const glm::vec2& scale) const {
auto copy = UVRegion(*this);
copy.scale(scale);
return copy;
}
};