Merge pull request #830 from MihailRis/update-3d-text

feat: binding 3d text to entity
This commit is contained in:
MihailRis 2026-04-15 23:57:45 +03:00 • committed by GitHub
commit 82e8cc991f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 71 additions and 5 deletions

View file

@ -79,6 +79,14 @@ gfx.text3d.set_rotation(id: int, rotation: mat4)
Sets the text rotation (Sets the rotated vectors X,Y).
```lua
gfx.text3d.get_entity(id: int) -> int
gfx.text3d.set_entity(id: int, entity: int)
```
Getter and setter for the entity to which the 3D text position is bound.
The position set via `gfx.text3d.set_pos` is then relative to the entity's position.
```lua
gfx.text3d.update_settings(id: int, preset: table)
```

View file

@ -79,6 +79,14 @@ gfx.text3d.set_rotation(id: int, rotation: mat4)
Устанавливает вращение текста (Устанавливает повернутые вектора X,Y).
```lua
gfx.text3d.get_entity(id: int) -> int
gfx.text3d.set_entity(id: int, entity: int)
```
Геттер и сеттер сущности, к позиции которой привязан 3D текст.
Позиция, устанавливаемая через `gfx.text3d.set_pos`, в таком случае, относительна позиции сущности.
```lua
gfx.text3d.update_settings(id: int, preset: table)
```

View file

@ -10,6 +10,14 @@ void TextNote::setText(std::wstring_view text) {
this->text = text;
}
void TextNote::setEntity(entityid_t eid) {
this->entity = eid;
}
entityid_t TextNote::getEntity() const {
return entity;
}
const std::wstring& TextNote::getText() const {
return text;
}

View file

@ -1,5 +1,6 @@
#pragma once
#include "constants.hpp"
#include "presets/NotePreset.hpp"
/// @brief 3D text instance
@ -7,6 +8,7 @@ class TextNote {
std::wstring text;
NotePreset preset;
glm::vec3 position;
entityid_t entity = ENTITY_NONE;
glm::vec3 xAxis {1, 0, 0};
glm::vec3 yAxis {0, 1, 0};
@ -14,6 +16,8 @@ public:
TextNote(std::wstring text, NotePreset preset, glm::vec3 position);
void setText(std::wstring_view text);
void setEntity(entityid_t eid);
entityid_t getEntity() const;
const std::wstring& getText() const;

View file

@ -10,12 +10,18 @@
#include "graphics/core/Shader.hpp"
#include "graphics/core/DrawContext.hpp"
#include "presets/NotePreset.hpp"
#include "world/Level.hpp"
#include "objects/Entities.hpp"
#include "objects/Entity.hpp"
#include "constants.hpp"
TextsRenderer::TextsRenderer(
Batch3D& batch, const Assets& assets, const Frustum& frustum
const Level& level,
Batch3D& batch,
const Assets& assets,
const Frustum& frustum
)
: batch(batch), assets(assets), frustum(frustum) {
: level(level), batch(batch), assets(assets), frustum(frustum) {
}
void TextsRenderer::renderNote(
@ -31,6 +37,13 @@ void TextsRenderer::renderNote(
const auto& preset = note.getPreset();
auto pos = note.getPosition();
entityid_t eid = note.getEntity();
if (eid != ENTITY_NONE) {
if (auto entity = level.entities->get(eid)) {
pos += entity->getTransform().displayPos;
}
}
if (util::distance2(pos, camera.position) >
util::sqr(preset.renderDistance / camera.zoom)) {
return;

View file

@ -12,8 +12,10 @@ class Batch3D;
class Frustum;
class TextNote;
struct EngineSettings;
class Level;
class TextsRenderer {
const Level& level;
Batch3D& batch;
const Assets& assets;
const Frustum& frustum;
@ -31,7 +33,12 @@ class TextsRenderer {
bool projected
);
public:
TextsRenderer(Batch3D& batch, const Assets& assets, const Frustum& frustum);
TextsRenderer(
const Level& level,
Batch3D& batch,
const Assets& assets,
const Frustum& frustum
);
void render(
const DrawContext& context,

View file

@ -91,9 +91,11 @@ WorldRenderer::WorldRenderer(
assets, level, *player.chunks, &engine.getSettings().graphics
)),
particles(std::make_unique<ParticlesRenderer>(
assets, level, *player.chunks, engine.getSettings().graphics
assets, level, *player.chunks, engine.getSettings().graphics
)),
texts(std::make_unique<TextsRenderer>(
level, *batch3d, assets, *frustumCulling
)),
texts(std::make_unique<TextsRenderer>(*batch3d, assets, *frustumCulling)),
blockWraps(std::make_unique<BlockWrapsRenderer>(
assets, level.content, *player.chunks
)) {

View file

@ -95,6 +95,20 @@ static int l_set_rotation(lua::State* L) {
return 0;
}
static int l_get_entity(lua::State* L) {
if (auto note = renderer->texts->get(lua::tointeger(L, 1))) {
return lua::pushinteger(L, note->getEntity());
}
return 0;
}
static int l_set_entity(lua::State* L) {
if (auto note = renderer->texts->get(lua::tointeger(L, 1))) {
note->setEntity(lua::touinteger(L, 2));
}
return 0;
}
const luaL_Reg text3dlib[] = {
{"show", wrap_hud<l_show>},
{"hide", wrap_hud<l_hide>},
@ -108,5 +122,7 @@ const luaL_Reg text3dlib[] = {
{"set_axis_y", wrap_hud<l_set_axis_y>},
{"set_rotation", wrap_hud<l_set_rotation>},
{"update_settings", wrap_hud<l_update_settings>},
{"get_entity", wrap_hud<l_get_entity>},
{"set_entity", wrap_hud<l_set_entity>},
{nullptr, nullptr}
};