add custom-models in hand support

This commit is contained in:
MihailRis 2024-11-04 18:02:42 +03:00
parent 5ed37d8604
commit 5a431ed898
6 changed files with 180 additions and 7 deletions

View File

@ -8,7 +8,12 @@ inline constexpr glm::vec3 X(1, 0, 0);
inline constexpr glm::vec3 Y(0, 1, 0);
inline constexpr glm::vec3 Z(0, 0, 1);
void Mesh::addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm) {
void Mesh::addPlane(
const glm::vec3& pos,
const glm::vec3& right,
const glm::vec3& up,
const glm::vec3& norm
) {
vertices.push_back({pos-right-up, {0,0}, norm});
vertices.push_back({pos+right-up, {1,0}, norm});
vertices.push_back({pos+right+up, {1,1}, norm});
@ -18,7 +23,23 @@ void Mesh::addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm
vertices.push_back({pos-right+up, {0,1}, norm});
}
void Mesh::addBox(glm::vec3 pos, glm::vec3 size) {
void Mesh::addPlane(
const glm::vec3& pos,
const glm::vec3& right,
const glm::vec3& up,
const glm::vec3& norm,
const UVRegion& uv
) {
vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm});
vertices.push_back({pos+right-up, {uv.u2, uv.v1}, norm});
vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm});
vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm});
vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm});
vertices.push_back({pos-right+up, {uv.u1, uv.u2}, norm});
}
void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) {
addPlane(pos+Z*size, X*size, Y*size, Z);
addPlane(pos-Z*size, -X*size, Y*size, -Z);
@ -29,6 +50,19 @@ void Mesh::addBox(glm::vec3 pos, glm::vec3 size) {
addPlane(pos-X*size, Z*size, Y*size, -X);
}
void Mesh::addBox(
const glm::vec3& pos, const glm::vec3& size, const UVRegion (&uvs)[6]
) {
addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]);
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]);
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]);
addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]);
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]);
}
void Mesh::scale(const glm::vec3& size) {
for (auto& vertex : vertices) {
vertex.coord *= size;

View File

@ -4,6 +4,8 @@
#include <vector>
#include <glm/glm.hpp>
#include "maths/UVRegion.hpp"
namespace model {
struct Vertex {
glm::vec3 coord;
@ -14,9 +16,27 @@ namespace model {
struct Mesh {
std::string texture;
std::vector<Vertex> vertices;
void addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm);
void addBox(glm::vec3 pos, glm::vec3 size);
bool lighting = true;
void addPlane(
const glm::vec3& pos,
const glm::vec3& right,
const glm::vec3& up,
const glm::vec3& norm
);
void addPlane(
const glm::vec3& pos,
const glm::vec3& right,
const glm::vec3& up,
const glm::vec3& norm,
const UVRegion& region
);
void addBox(const glm::vec3& pos, const glm::vec3& size);
void addBox(
const glm::vec3& pos,
const glm::vec3& size,
const UVRegion (&texfaces)[6]
);
void scale(const glm::vec3& size);
};

View File

@ -80,3 +80,56 @@ glm::vec4 MainBatch::sampleLight(
glm::max(Lightmap::extract(light, 3), minIntensity) / 15.0f
);
}
inline glm::vec4 do_tint(float value) {
return glm::vec4(value, value, value, 1.0f);
}
void MainBatch::cube(
const glm::vec3& coord,
const glm::vec3& size,
const UVRegion(&texfaces)[6],
const glm::vec4& tint,
bool shading
) {
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);
quad(
coord + glm::vec3(0.0f, 0.0f, 0.0f),
X, Y, glm::vec2(size.x, size.y),
(shading ? do_tint(0.8) * tint : tint),
glm::vec3(1.0f), texfaces[5]
);
quad(
coord + glm::vec3(size.x, 0.0f, -size.z),
-X, Y, glm::vec2(size.x, size.y),
(shading ? do_tint(0.8) * tint : tint),
glm::vec3(1.0f), texfaces[4]
);
quad(
coord + glm::vec3(0.0f, size.y, 0.0f),
X, -Z, glm::vec2(size.x, size.z),
(shading ? do_tint(1.0f) * tint : tint),
glm::vec3(1.0f), texfaces[3]
);
quad(
coord + glm::vec3(0.0f, 0.0f, -size.z),
X, Z, glm::vec2(size.x, size.z),
(shading ? do_tint(0.7f) * tint : tint),
glm::vec3(1.0f), texfaces[2]
);
quad(
coord + glm::vec3(0.0f, 0.0f, -size.z),
Z, Y, glm::vec2(size.z, size.y),
(shading ? do_tint(0.9f) * tint : tint),
glm::vec3(1.0f), texfaces[0]
);
quad(
coord + glm::vec3(size.x, 0.0f, 0.0f),
-Z, Y, glm::vec2(size.z, size.y),
(shading ? do_tint(0.9f) * tint : tint),
glm::vec3(1.0f), texfaces[1]
);
}

View File

@ -118,4 +118,12 @@ public:
tint
);
}
void cube(
const glm::vec3& coord,
const glm::vec3& size,
const UVRegion(&texfaces)[6],
const glm::vec4& tint,
bool shading
);
};

View File

@ -1,6 +1,7 @@
#include "ModelsGenerator.hpp"
#include "assets/Assets.hpp"
#include "assets/assets_util.hpp"
#include "items/ItemDef.hpp"
#include "voxels/Block.hpp"
#include "content/Content.hpp"
@ -40,6 +41,13 @@ static model::Model create_flat_model(
return model;
}
static inline UVRegion get_region_for(
const std::string& texture, const Assets& assets
) {
auto texreg = util::get_texture_region(assets, "blocks:" + texture, "");
return texreg.region;
}
model::Model ModelsGenerator::generate(
const ItemDef& def, const Content& content, const Assets& assets
) {
@ -50,6 +58,56 @@ model::Model ModelsGenerator::generate(
return create_flat_model(
"blocks:" + blockDef.textureFaces.at(0), assets
);
} else if (blockDef.model == BlockModel::custom) {
model = model::Model();
for (size_t i = 0; i < blockDef.modelBoxes.size(); i++) {
auto& mesh =
model.addMesh("blocks:" + blockDef.modelTextures[i * 6]);
mesh.lighting = !def.rt.emissive;
const UVRegion (&boxtexfaces)[6] = {
get_region_for(blockDef.modelTextures[i * 6], assets),
get_region_for(blockDef.modelTextures[i * 6 + 1], assets),
get_region_for(blockDef.modelTextures[i * 6 + 2], assets),
get_region_for(blockDef.modelTextures[i * 6 + 3], assets),
get_region_for(blockDef.modelTextures[i * 6 + 4], assets),
get_region_for(blockDef.modelTextures[i * 6 + 5], assets)
};
mesh.addBox(
blockDef.modelBoxes[i].center(),
blockDef.modelBoxes[i].size()*0.5f, boxtexfaces
);
}
const auto& points = blockDef.modelExtraPoints;
glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
glm::vec3 norm {0, 1, 0};
for (size_t i = 0; i < blockDef.modelExtraPoints.size() / 4; i++) {
auto texture =
"blocks:" +
blockDef.modelTextures[blockDef.modelBoxes.size() * 6 + i];
auto& mesh = model.addMesh(texture);
mesh.lighting = !def.rt.emissive;
auto reg = get_region_for(texture, assets);
mesh.vertices.push_back(
{points[i * 4 + 0] - poff, glm::vec2(reg.u1, reg.v1), norm}
);
mesh.vertices.push_back(
{points[i * 4 + 1] - poff, glm::vec2(reg.u2, reg.v1), norm}
);
mesh.vertices.push_back(
{points[i * 4 + 2] - poff, glm::vec2(reg.u2, reg.v2), norm}
);
mesh.vertices.push_back(
{points[i * 4 + 3] - poff, glm::vec2(reg.u1, reg.v1), norm}
);
mesh.vertices.push_back(
{points[i * 4 + 4] - poff, glm::vec2(reg.u2, reg.v2), norm}
);
mesh.vertices.push_back(
{points[i * 4 + 0] - poff, glm::vec2(reg.u1, reg.v2), norm}
);
}
}
for (auto& mesh : model.meshes) {
switch (blockDef.model) {

View File

@ -116,8 +116,8 @@ public:
std::vector<std::string> modelTextures = {};
std::vector<BoxModel> modelBoxes = {};
std::vector<glm::vec3> modelExtraPoints =
{}; // initially made for tetragons
// initially made for tetragons
std::vector<glm::vec3> modelExtraPoints = {};
std::vector<UVRegion> modelUVs = {}; // boxes' tex-UVs also there
/// @brief id of used BlockMaterial, may specify non-existing material