mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
feat: naive texture-faces support for blockwraps implementation
This commit is contained in:
parent
c6c23c3c53
commit
561fb0fb58
4 changed files with 63 additions and 55 deletions
|
|
@ -25,59 +25,58 @@ BlockWrapsRenderer::BlockWrapsRenderer(
|
|||
BlockWrapsRenderer::~BlockWrapsRenderer() = default;
|
||||
|
||||
void BlockWrapsRenderer::draw(const BlockWrapper& wrapper) {
|
||||
auto textureRegion = util::get_texture_region(assets, wrapper.texture, "");
|
||||
|
||||
auto& shader = assets.require<Shader>("entity");
|
||||
shader.use();
|
||||
shader.uniform1i("u_alphaClip", false);
|
||||
|
||||
const UVRegion& cracksRegion = textureRegion.region;
|
||||
UVRegion regions[6] {
|
||||
cracksRegion, cracksRegion, cracksRegion,
|
||||
cracksRegion, cracksRegion, cracksRegion
|
||||
};
|
||||
batch->setTexture(textureRegion.texture);
|
||||
util::TextureRegion texRegions[6] {};
|
||||
UVRegion uvRegions[6] {};
|
||||
for (int i = 0; i < 6; i++) {
|
||||
auto texRegion = util::get_texture_region(assets, wrapper.textureFaces[i], "");
|
||||
texRegions[i] = texRegion;
|
||||
uvRegions[i] = texRegion.region;
|
||||
}
|
||||
batch->setTexture(texRegions[0].texture);
|
||||
|
||||
const voxel* vox = chunks.get(wrapper.position);
|
||||
if (vox == nullptr) {
|
||||
if (vox == nullptr || vox->id == BLOCK_VOID) {
|
||||
return;
|
||||
}
|
||||
if (vox->id != BLOCK_VOID) {
|
||||
const auto& def = level.content.getIndices()->blocks.require(vox->id);
|
||||
switch (def.getModel(vox->state.userbits).type) {
|
||||
case BlockModelType::BLOCK:
|
||||
batch->cube(
|
||||
glm::vec3(wrapper.position) + glm::vec3(0.5f),
|
||||
glm::vec3(1.01f),
|
||||
regions,
|
||||
glm::vec4(1,1,1,0),
|
||||
false
|
||||
);
|
||||
break;
|
||||
case BlockModelType::AABB: {
|
||||
const auto& aabb =
|
||||
(def.rotatable ? def.rt.hitboxes[vox->state.rotation]
|
||||
: def.hitboxes)
|
||||
.at(0);
|
||||
const auto& size = aabb.size();
|
||||
regions[0].scale(size.z, size.y);
|
||||
regions[1].scale(size.z, size.y);
|
||||
regions[2].scale(size.x, size.z);
|
||||
regions[3].scale(size.x, size.z);
|
||||
regions[4].scale(size.x, size.y);
|
||||
regions[5].scale(size.x, size.y);
|
||||
batch->cube(
|
||||
glm::vec3(wrapper.position) + aabb.center(),
|
||||
size * glm::vec3(1.01f),
|
||||
regions,
|
||||
glm::vec4(1,1,1,0),
|
||||
false
|
||||
);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
const auto& def = level.content.getIndices()->blocks.require(vox->id);
|
||||
switch (def.getModel(vox->state.userbits).type) {
|
||||
case BlockModelType::BLOCK:
|
||||
batch->cube(
|
||||
glm::vec3(wrapper.position) + glm::vec3(0.5f),
|
||||
glm::vec3(1.01f),
|
||||
uvRegions,
|
||||
glm::vec4(1, 1, 1, 0),
|
||||
false,
|
||||
wrapper.cullingBits
|
||||
);
|
||||
break;
|
||||
case BlockModelType::AABB: {
|
||||
const auto& aabb =
|
||||
(def.rotatable ? def.rt.hitboxes[vox->state.rotation]
|
||||
: def.hitboxes).at(0);
|
||||
const auto& size = aabb.size();
|
||||
uvRegions[0].scale(size.z, size.y);
|
||||
uvRegions[1].scale(size.z, size.y);
|
||||
uvRegions[2].scale(size.x, size.z);
|
||||
uvRegions[3].scale(size.x, size.z);
|
||||
uvRegions[4].scale(size.x, size.y);
|
||||
uvRegions[5].scale(size.x, size.y);
|
||||
batch->cube(
|
||||
glm::vec3(wrapper.position) + aabb.center(),
|
||||
size * glm::vec3(1.01f),
|
||||
uvRegions,
|
||||
glm::vec4(1, 1, 1, 0),
|
||||
false,
|
||||
wrapper.cullingBits
|
||||
);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -93,9 +92,8 @@ u64id_t BlockWrapsRenderer::add(
|
|||
const glm::ivec3& position, const std::string& texture
|
||||
) {
|
||||
u64id_t id = nextWrapper++;
|
||||
wrappers[id] = std::make_unique<BlockWrapper>(
|
||||
BlockWrapper {position, texture}
|
||||
);
|
||||
wrappers[id] = std::make_unique<BlockWrapper>(BlockWrapper {
|
||||
position, {texture, texture, texture, texture, texture, texture}});
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
|
@ -15,7 +16,8 @@ class DrawContext;
|
|||
|
||||
struct BlockWrapper {
|
||||
glm::ivec3 position;
|
||||
std::string texture;
|
||||
std::array<std::string, 6> textureFaces {};
|
||||
uint8_t cullingBits = 0xFF;
|
||||
};
|
||||
|
||||
class BlockWrapsRenderer {
|
||||
|
|
|
|||
|
|
@ -83,50 +83,57 @@ inline glm::vec4 do_tint(float value) {
|
|||
}
|
||||
|
||||
void MainBatch::cube(
|
||||
const glm::vec3 &coord,
|
||||
const glm::vec3 &size,
|
||||
const UVRegion(&texfaces)[6],
|
||||
const glm::vec4 &tint,
|
||||
bool shading
|
||||
const glm::vec3& coord,
|
||||
const glm::vec3& size,
|
||||
const UVRegion(&texfaces)[6],
|
||||
const glm::vec4& tint,
|
||||
bool shading,
|
||||
uint8_t cullingBits
|
||||
) {
|
||||
const glm::vec3 X(1.0f, 0.0f, 0.0f);
|
||||
const glm::vec3 Y(0.0f, 1.0f, 0.0f);
|
||||
const glm::vec3 Z(0.0f, 0.0f, 1.0f);
|
||||
|
||||
if (cullingBits & 0x20)
|
||||
quad(
|
||||
coord + Z * size.z * 0.5f,
|
||||
X, Y, Z, glm::vec2(size.x, size.y),
|
||||
(shading ? do_tint(0.8) * tint : tint),
|
||||
glm::vec3(1.0f), texfaces[5]
|
||||
);
|
||||
if (cullingBits & 0x10)
|
||||
quad(
|
||||
coord - Z * size.z * 0.5f,
|
||||
-X, Y, -Z, glm::vec2(size.x, size.y),
|
||||
(shading ? do_tint(0.9f) * tint : tint),
|
||||
glm::vec3(1.0f), texfaces[4]
|
||||
);
|
||||
if (cullingBits & 0x8)
|
||||
quad(
|
||||
coord + Y * size.y * 0.5f,
|
||||
-X, Z, Y, glm::vec2(size.x, size.z),
|
||||
(shading ? do_tint(1.0f) * tint : tint),
|
||||
glm::vec3(1.0f), texfaces[3]
|
||||
);
|
||||
if (cullingBits & 0x4)
|
||||
quad(
|
||||
coord - Y * size.y * 0.5f,
|
||||
X, Z, -Y, glm::vec2(size.x, size.z),
|
||||
(shading ? do_tint(0.7f) * tint : tint),
|
||||
glm::vec3(1.0f), texfaces[2]
|
||||
);
|
||||
if (cullingBits & 0x2)
|
||||
quad(
|
||||
coord + X * size.x * 0.5f,
|
||||
-Z, Y, X, glm::vec2(size.z, size.y),
|
||||
(shading ? do_tint(0.8f) * tint : tint),
|
||||
glm::vec3(1.0f), texfaces[1]
|
||||
);
|
||||
if (cullingBits & 0x1)
|
||||
quad(
|
||||
coord - X * size.x * 0.5f,
|
||||
Z, Y, -X, glm::vec2(size.z, size.y),
|
||||
(shading ? do_tint(0.9f) * tint : tint),
|
||||
glm::vec3(1.0f), texfaces[1]
|
||||
glm::vec3(1.0f), texfaces[0]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -152,6 +152,7 @@ public:
|
|||
const glm::vec3& size,
|
||||
const UVRegion(&texfaces)[6],
|
||||
const glm::vec4& tint,
|
||||
bool shading
|
||||
bool shading,
|
||||
uint8_t cullingBits = 0xFF
|
||||
);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue