fix block model change wrapper reaction

This commit is contained in:
MihailRis 2025-12-20 22:33:47 +03:00
parent b0ecfd5add
commit 6d6d38c218
3 changed files with 19 additions and 7 deletions

View file

@ -25,7 +25,7 @@ BlockWrapsRenderer::BlockWrapsRenderer(
BlockWrapsRenderer::~BlockWrapsRenderer() = default; BlockWrapsRenderer::~BlockWrapsRenderer() = default;
void BlockWrapsRenderer::draw(const BlockWrapper& wrapper, const Texture* texture) { void BlockWrapsRenderer::draw(BlockWrapper& wrapper, const Texture* texture) {
if (wrapper.cullingBits == 0x0) { if (wrapper.cullingBits == 0x0) {
return; return;
} }
@ -41,8 +41,15 @@ void BlockWrapsRenderer::draw(const BlockWrapper& wrapper, const Texture* textur
return; return;
} }
const auto& def = level.content.getIndices()->blocks.require(wrapper.vox->id); // one frame can be invalid due to texture change but ok
switch (def.getModel(wrapper.vox->state.userbits).type) { const auto& def =
level.content.getIndices()->blocks.require(wrapper.vox->id);
if (wrapper.modelType != def.getModel(wrapper.vox->state.userbits).type) {
wrapper.dirtySides = 0xFF;
refreshWrapper(wrapper);
}
switch (wrapper.modelType) {
case BlockModelType::BLOCK: case BlockModelType::BLOCK:
batch->cube( batch->cube(
glm::vec3(wrapper.position) + glm::vec3(0.5f), glm::vec3(wrapper.position) + glm::vec3(0.5f),
@ -78,7 +85,8 @@ void BlockWrapsRenderer::refreshWrapper(BlockWrapper& wrapper) {
if ((wrapper.cullingBits & (1 << i)) == 0) { if ((wrapper.cullingBits & (1 << i)) == 0) {
continue; continue;
} }
auto texRegion = util::get_texture_region(assets, wrapper.textureFaces[i], ""); auto texRegion =
util::get_texture_region(assets, wrapper.textureFaces[i], "");
wrapper.texRegions[i] = texRegion; wrapper.texRegions[i] = texRegion;
wrapper.uvRegions[i] = texRegion.region; wrapper.uvRegions[i] = texRegion.region;
@ -91,7 +99,8 @@ void BlockWrapsRenderer::refreshWrapper(BlockWrapper& wrapper) {
return; return;
} }
const auto& def = level.content.getIndices()->blocks.require(vox->id); const auto& def = level.content.getIndices()->blocks.require(vox->id);
switch (def.getModel(vox->state.userbits).type) { wrapper.modelType = def.getModel(vox->state.userbits).type;
switch (wrapper.modelType) {
case BlockModelType::AABB: { case BlockModelType::AABB: {
const auto& aabb = const auto& aabb =
(def.rotatable ? def.rt.hitboxes[vox->state.rotation] (def.rotatable ? def.rt.hitboxes[vox->state.rotation]

View file

@ -7,6 +7,7 @@
#include <map> #include <map>
#include "assets/assets_util.hpp" #include "assets/assets_util.hpp"
#include "voxels/Block.hpp"
#include "MainBatch.hpp" #include "MainBatch.hpp"
#include "typedefs.hpp" #include "typedefs.hpp"
@ -24,6 +25,7 @@ struct BlockWrapper {
util::TextureRegion texRegions[6] {}; util::TextureRegion texRegions[6] {};
UVRegion uvRegions[6] {}; UVRegion uvRegions[6] {};
const voxel* vox = nullptr; const voxel* vox = nullptr;
BlockModelType modelType {};
uint8_t cullingBits = 0xFF; uint8_t cullingBits = 0xFF;
uint8_t dirtySides = 0xFF; uint8_t dirtySides = 0xFF;
}; };
@ -38,7 +40,7 @@ class BlockWrapsRenderer {
std::unordered_map<u64id_t, std::unique_ptr<BlockWrapper>> wrappers; std::unordered_map<u64id_t, std::unique_ptr<BlockWrapper>> wrappers;
u64id_t nextWrapper = 1; u64id_t nextWrapper = 1;
void draw(const BlockWrapper& wrapper, const Texture* texture); void draw(BlockWrapper& wrapper, const Texture* texture);
void refreshWrapper(BlockWrapper& wrapper); void refreshWrapper(BlockWrapper& wrapper);
public: public:

View file

@ -47,7 +47,8 @@ static int l_set_faces(lua::State* L) {
} }
} else { } else {
auto texture = lua::require_string(L, 2 + i);; auto texture = lua::require_string(L, 2 + i);;
if ((wrapper->cullingBits & (1 << i)) == 0x0 || wrapper->textureFaces[i] != texture) { if ((wrapper->cullingBits & (1 << i)) == 0x0
|| wrapper->textureFaces[i] != texture) {
wrapper->cullingBits |= (1 << i); wrapper->cullingBits |= (1 << i);
wrapper->textureFaces[i] = texture; wrapper->textureFaces[i] = texture;
} }