mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 09:59:44 +00:00
optimize animator
This commit is contained in:
parent
17bdbb1a38
commit
fd17ce32ab
11 changed files with 294 additions and 2 deletions
|
|
@ -96,6 +96,7 @@ namespace rigging {
|
|||
std::unordered_map<std::string, std::string> textures;
|
||||
std::vector<ModelReference> modelOverrides;
|
||||
bool visible;
|
||||
bool culled = false;
|
||||
glm::vec4 tint {1.0f, 1.0f, 1.0f, 1.0f};
|
||||
std::vector<glm::vec4> boneTints;
|
||||
|
||||
|
|
|
|||
|
|
@ -270,6 +270,7 @@ void LevelScreen::draw(float delta) {
|
|||
const DrawContext ctx(nullptr, engine.getWindow(), batch.get());
|
||||
|
||||
if (!hud->isPause()) {
|
||||
renderer->updateEntitiesCulling(camera);
|
||||
scripting::on_entities_render(engine.getTime().getDelta());
|
||||
scripting::on_frontend_render();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -329,6 +329,18 @@ void WorldRenderer::renderInWorldLines(
|
|||
}
|
||||
}
|
||||
|
||||
void WorldRenderer::updateEntitiesCulling(const Camera& camera) {
|
||||
bool culling = engine.getSettings().graphics.frustumCulling.get();
|
||||
if (culling) {
|
||||
frustumCulling->update(camera.getProjView());
|
||||
}
|
||||
level.entities->updateCulling(
|
||||
culling ? frustumCulling.get() : nullptr,
|
||||
player.currentCamera.get() == player.fpCamera.get() ? player.getEntity()
|
||||
: 0
|
||||
);
|
||||
}
|
||||
|
||||
void WorldRenderer::update(const Camera& camera, float delta) {
|
||||
timer += delta;
|
||||
chunksRenderer->update();
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ public:
|
|||
~WorldRenderer();
|
||||
|
||||
void update(const Camera& camera, float delta);
|
||||
void updateEntitiesCulling(const Camera& camera);
|
||||
|
||||
void renderFrame(
|
||||
const DrawContext& context,
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@
|
|||
#include "graphics/render/WorldRenderer.hpp"
|
||||
#include "graphics/render/NamedSkeletons.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
namespace scripting {
|
||||
extern WorldRenderer* renderer;
|
||||
}
|
||||
|
|
@ -196,6 +199,165 @@ static int l_exists(lua::State* L) {
|
|||
return lua::pushboolean(L, get_skeleton(L));
|
||||
}
|
||||
|
||||
namespace {
|
||||
struct NativeTrackBone {
|
||||
std::string name;
|
||||
bool translate = false;
|
||||
int translation[3] {};
|
||||
std::vector<std::pair<int, int>> rotations;
|
||||
bool scale = false;
|
||||
int scaling[3] {};
|
||||
};
|
||||
|
||||
struct NativeTrack {
|
||||
std::string name;
|
||||
std::vector<NativeTrackBone> bones;
|
||||
int valuesCount = 0;
|
||||
std::weak_ptr<const rigging::SkeletonConfig> config;
|
||||
std::vector<int> indices;
|
||||
};
|
||||
|
||||
std::vector<NativeTrack> native_tracks;
|
||||
}
|
||||
|
||||
static void read_indices(lua::State* L, int idx, const char* field, int* dst) {
|
||||
lua_getfield(L, idx, field);
|
||||
if (lua_istable(L, -1)) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
lua_rawgeti(L, -1, i + 1);
|
||||
dst[i] = static_cast<int>(lua_tointeger(L, -1));
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
static int l_register_track(lua::State* L) {
|
||||
NativeTrack track;
|
||||
track.name = lua::require_string(L, 1);
|
||||
if (!lua_istable(L, 2)) {
|
||||
throw std::runtime_error("layout table expected");
|
||||
}
|
||||
int maxValue = 0;
|
||||
size_t count = lua_objlen(L, 2);
|
||||
for (size_t i = 1; i <= count; i++) {
|
||||
lua_rawgeti(L, 2, static_cast<int>(i));
|
||||
int bone = lua_gettop(L);
|
||||
NativeTrackBone entry;
|
||||
lua_getfield(L, bone, "name");
|
||||
entry.name = lua::require_string(L, -1);
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_getfield(L, bone, "t");
|
||||
entry.translate = lua_istable(L, -1);
|
||||
lua_pop(L, 1);
|
||||
read_indices(L, bone, "t", entry.translation);
|
||||
|
||||
lua_getfield(L, bone, "s");
|
||||
entry.scale = lua_istable(L, -1);
|
||||
lua_pop(L, 1);
|
||||
read_indices(L, bone, "s", entry.scaling);
|
||||
|
||||
lua_getfield(L, bone, "r");
|
||||
if (lua_istable(L, -1)) {
|
||||
size_t n = lua_objlen(L, -1);
|
||||
for (size_t j = 1; j + 1 <= n; j += 2) {
|
||||
lua_rawgeti(L, -1, static_cast<int>(j));
|
||||
int axis = static_cast<int>(lua_tointeger(L, -1));
|
||||
lua_rawgeti(L, -2, static_cast<int>(j + 1));
|
||||
int value = static_cast<int>(lua_tointeger(L, -1));
|
||||
lua_pop(L, 2);
|
||||
if (axis < 0 || axis > 2) {
|
||||
throw std::runtime_error("invalid rotation axis");
|
||||
}
|
||||
entry.rotations.emplace_back(axis, value);
|
||||
}
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
for (int v : entry.translation) maxValue = std::max(maxValue, v);
|
||||
for (int v : entry.scaling) maxValue = std::max(maxValue, v);
|
||||
for (auto [_, v] : entry.rotations) maxValue = std::max(maxValue, v);
|
||||
track.bones.push_back(std::move(entry));
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
track.valuesCount = maxValue;
|
||||
for (size_t i = 0; i < native_tracks.size(); i++) {
|
||||
if (native_tracks[i].name == track.name) {
|
||||
native_tracks[i] = std::move(track);
|
||||
return lua::pushinteger(L, static_cast<lua::Integer>(i));
|
||||
}
|
||||
}
|
||||
native_tracks.push_back(std::move(track));
|
||||
return lua::pushinteger(L, native_tracks.size() - 1);
|
||||
}
|
||||
|
||||
static int l_apply_track(lua::State* L) {
|
||||
auto skeleton = get_skeleton(L);
|
||||
if (skeleton == nullptr || skeleton->config == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
auto id = lua_tointeger(L, 2);
|
||||
if (id < 0 || static_cast<size_t>(id) >= native_tracks.size()) {
|
||||
throw std::runtime_error("invalid track id");
|
||||
}
|
||||
auto& track = native_tracks[id];
|
||||
const auto& config = skeleton->config;
|
||||
if (track.config.owner_before(config) || config.owner_before(track.config) ||
|
||||
track.config.expired() || track.indices.size() != track.bones.size()) {
|
||||
track.config = config;
|
||||
track.indices.clear();
|
||||
for (const auto& bone : track.bones) {
|
||||
auto found = config->find(bone.name);
|
||||
track.indices.push_back(found ? static_cast<int>(found->getIndex()) : -1);
|
||||
}
|
||||
}
|
||||
constexpr int MAX_VALUES = 256;
|
||||
float values[MAX_VALUES + 1] {};
|
||||
int top = lua_gettop(L);
|
||||
int count = std::min({track.valuesCount, top - 2, MAX_VALUES});
|
||||
for (int i = 1; i <= count; i++) {
|
||||
values[i] = static_cast<float>(lua_tonumber(L, i + 2));
|
||||
}
|
||||
static const glm::vec3 AXES[] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
|
||||
auto value = [&values](int index, float fallback) {
|
||||
return index > 0 && index <= MAX_VALUES ? values[index] : fallback;
|
||||
};
|
||||
auto& matrices = skeleton->pose.matrices;
|
||||
for (size_t i = 0; i < track.bones.size(); i++) {
|
||||
int index = track.indices[i];
|
||||
if (index < 0 || static_cast<size_t>(index) >= matrices.size()) {
|
||||
continue;
|
||||
}
|
||||
const auto& bone = track.bones[i];
|
||||
auto& matrix = matrices[index];
|
||||
if (bone.translate) {
|
||||
matrix = glm::translate(matrix, glm::vec3(
|
||||
value(bone.translation[0], 0.0f),
|
||||
value(bone.translation[1], 0.0f),
|
||||
value(bone.translation[2], 0.0f)
|
||||
));
|
||||
}
|
||||
for (const auto& [axis, v] : bone.rotations) {
|
||||
matrix = glm::rotate(matrix, glm::radians(value(v, 0.0f)), AXES[axis]);
|
||||
}
|
||||
if (bone.scale) {
|
||||
matrix = glm::scale(matrix, glm::vec3(
|
||||
value(bone.scaling[0], 1.0f),
|
||||
value(bone.scaling[1], 1.0f),
|
||||
value(bone.scaling[2], 1.0f)
|
||||
));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_is_culled(lua::State* L) {
|
||||
if (auto skeleton = get_skeleton(L)) {
|
||||
return lua::pushboolean(L, skeleton->culled);
|
||||
}
|
||||
return lua::pushboolean(L, false);
|
||||
}
|
||||
|
||||
const luaL_Reg skeletonlib[] = {
|
||||
{"get_model", lua::wrap<l_get_model>},
|
||||
{"set_model", lua::wrap<l_set_model>},
|
||||
|
|
@ -212,5 +374,8 @@ const luaL_Reg skeletonlib[] = {
|
|||
{"is_interpolated", lua::wrap<l_is_interpolated>},
|
||||
{"set_interpolated", lua::wrap<l_set_interpolated>},
|
||||
{"exists", lua::wrap<l_exists>},
|
||||
{"is_culled", lua::wrap<l_is_culled>},
|
||||
{"register_track", lua::wrap<l_register_track>},
|
||||
{"apply_track", lua::wrap<l_apply_track>},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -509,6 +509,19 @@ void Entities::render(
|
|||
}
|
||||
}
|
||||
|
||||
void Entities::updateCulling(const Frustum* frustum, entityid_t fpsEntity) {
|
||||
auto view = registry->view<EntityId, Transform, rigging::Skeleton, Rigidbody>();
|
||||
for (auto [entity, eid, transform, skeleton, rigidbody] : view.each()) {
|
||||
if (frustum == nullptr || eid.uid == fpsEntity) {
|
||||
skeleton.culled = false;
|
||||
continue;
|
||||
}
|
||||
const auto& pos = transform.pos;
|
||||
const auto& halfSize = rigidbody.hitbox.getHalfSize();
|
||||
skeleton.culled = !frustum->isBoxVisible(pos - halfSize, pos + halfSize);
|
||||
}
|
||||
}
|
||||
|
||||
bool Entities::hasBlockingInside(AABB aabb) {
|
||||
constexpr float eps = 0.05f;
|
||||
auto view = registry->view<EntityId, Rigidbody>();
|
||||
|
|
|
|||
|
|
@ -67,6 +67,8 @@ public:
|
|||
entityid_t fpsEntity
|
||||
);
|
||||
|
||||
void updateCulling(const Frustum* frustum, entityid_t fpsEntity);
|
||||
|
||||
entityid_t spawn(
|
||||
const EntityDef& def,
|
||||
glm::vec3 position,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue