From eb5715eba9f05f497b58689cd0d5537329b12af7 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 14 Nov 2024 10:32:50 +0300 Subject: [PATCH] add gfx.text3d.spawn(...) --- res/content/base/scripts/hud.lua | 6 +++++ src/graphics/render/TextsRenderer.cpp | 21 ++++++---------- src/graphics/render/TextsRenderer.hpp | 10 ++++++++ src/graphics/render/WorldRenderer.hpp | 2 +- src/logic/scripting/lua/libs/api_lua.hpp | 3 ++- src/logic/scripting/lua/libs/libtext3d.cpp | 29 ++++++++++++++++++++++ src/logic/scripting/scripting_hud.cpp | 1 + 7 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 src/logic/scripting/lua/libs/libtext3d.cpp diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 694ff847..92939b9c 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -24,4 +24,10 @@ function on_hud_open() local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)) drop.rigidbody:set_vel(velocity) end) + + gfx.text3d.spawn({0.5, 99.5, 0.0015}, "Segmentation fault", { + scale = 0.005, + color = {0, 0, 0, 1}, + displayMode = "static_billboard" + }) end diff --git a/src/graphics/render/TextsRenderer.cpp b/src/graphics/render/TextsRenderer.cpp index 349c4a39..f6929100 100644 --- a/src/graphics/render/TextsRenderer.cpp +++ b/src/graphics/render/TextsRenderer.cpp @@ -78,25 +78,20 @@ void TextsRenderer::renderTexts( bool hudVisible, bool frontLayer ) { - std::vector notes; - NotePreset preset; - preset.displayMode = NoteDisplayMode::STATIC_BILLBOARD; - preset.color = glm::vec4(0, 0, 0, 1); - preset.scale = 0.005f; - - notes.emplace_back( - L"Segmentation fault", - std::move(preset), - glm::vec3(0.5f, 99.5f, 0.0015f) - ); auto& shader = assets.require("ui3d"); shader.use(); shader.uniformMatrix("u_projview", camera.getProjView()); shader.uniformMatrix("u_apply", glm::mat4(1.0f)); batch.begin(); - for (const auto& note : notes) { - renderText(note, context, camera, settings, hudVisible); + for (const auto& [id, note] : notes) { + renderText(*note, context, camera, settings, hudVisible); } batch.flush(); } + +u64id_t TextsRenderer::add(std::unique_ptr note) { + u64id_t uid = nextNote++; + notes[uid] = std::move(note); + return uid; +} diff --git a/src/graphics/render/TextsRenderer.hpp b/src/graphics/render/TextsRenderer.hpp index 57dfa24b..d9f64cb4 100644 --- a/src/graphics/render/TextsRenderer.hpp +++ b/src/graphics/render/TextsRenderer.hpp @@ -1,5 +1,10 @@ #pragma once +#include +#include + +#include "typedefs.hpp" + class DrawContext; class Camera; class Assets; @@ -13,6 +18,9 @@ class TextsRenderer { const Assets& assets; const Frustum& frustum; + std::unordered_map> notes; + u64id_t nextNote = 1; + void renderText( const TextNote& note, const DrawContext& context, @@ -30,4 +38,6 @@ public: bool hudVisible, bool frontLayer ); + + u64id_t add(std::unique_ptr note); }; diff --git a/src/graphics/render/WorldRenderer.hpp b/src/graphics/render/WorldRenderer.hpp index 7ae508a2..b94694bd 100644 --- a/src/graphics/render/WorldRenderer.hpp +++ b/src/graphics/render/WorldRenderer.hpp @@ -39,7 +39,6 @@ class WorldRenderer { std::unique_ptr lineBatch; std::unique_ptr batch3d; std::unique_ptr chunks; - std::unique_ptr texts; std::unique_ptr guides; std::unique_ptr skybox; std::unique_ptr modelBatch; @@ -67,6 +66,7 @@ class WorldRenderer { float fogFactor ); public: + std::unique_ptr texts; std::unique_ptr particles; static bool showChunkBorders; diff --git a/src/logic/scripting/lua/libs/api_lua.hpp b/src/logic/scripting/lua/libs/api_lua.hpp index a6edc434..bd810833 100644 --- a/src/logic/scripting/lua/libs/api_lua.hpp +++ b/src/logic/scripting/lua/libs/api_lua.hpp @@ -33,7 +33,8 @@ extern const luaL_Reg mat4lib[]; extern const luaL_Reg packlib[]; extern const luaL_Reg particleslib[]; extern const luaL_Reg playerlib[]; -extern const luaL_Reg quatlib[]; // quat.cpp +extern const luaL_Reg quatlib[]; +extern const luaL_Reg text3dlib[]; extern const luaL_Reg timelib[]; extern const luaL_Reg tomllib[]; extern const luaL_Reg utf8lib[]; diff --git a/src/logic/scripting/lua/libs/libtext3d.cpp b/src/logic/scripting/lua/libs/libtext3d.cpp new file mode 100644 index 00000000..932bec13 --- /dev/null +++ b/src/logic/scripting/lua/libs/libtext3d.cpp @@ -0,0 +1,29 @@ +#include "api_lua.hpp" + +#include "logic/scripting/scripting_hud.hpp" +#include "graphics/render/WorldRenderer.hpp" +#include "graphics/render/TextsRenderer.hpp" +#include "graphics/render/TextNote.hpp" +#include "engine.hpp" + +using namespace scripting; + +static int l_spawn(lua::State* L) { + auto position = lua::tovec3(L, 1); + auto text = lua::require_wstring(L, 2); + auto preset = lua::tovalue(L, 3); + auto extension = lua::tovalue(L, 4); + + NotePreset notePreset {}; + notePreset.deserialize(preset); + if (extension != nullptr) { + notePreset.deserialize(extension); + } + auto note = std::make_unique(text, notePreset, position); + return lua::pushinteger(L, renderer->texts->add(std::move(note))); +} + +const luaL_Reg text3dlib[] = { + {"spawn", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index 16ec1a68..78e7bb12 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -25,6 +25,7 @@ void scripting::on_frontend_init(Hud* hud, WorldRenderer* renderer) { lua::openlib(L, "hud", hudlib); lua::openlib(L, "gfx", "particles", particleslib); + lua::openlib(L, "gfx", "text3d", text3dlib); if (lua::getglobal(L, "__vc_create_hud_rules")) { lua::call_nothrow(L, 0, 0);