feat: process external custom block models for variable textures
This commit is contained in:
parent
43c76c92ff
commit
9a0f6b23b0
@ -39,6 +39,22 @@ void Mesh::addPlane(
|
||||
vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm});
|
||||
}
|
||||
|
||||
void Mesh::addRect(
|
||||
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.v2}, 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);
|
||||
@ -51,16 +67,23 @@ void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) {
|
||||
}
|
||||
|
||||
void Mesh::addBox(
|
||||
const glm::vec3& pos, const glm::vec3& size, const UVRegion (&uvs)[6]
|
||||
const glm::vec3& pos,
|
||||
const glm::vec3& size,
|
||||
const UVRegion (&uvs)[6],
|
||||
const bool enabledSides[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]);
|
||||
if (enabledSides[0]) // north
|
||||
addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]);
|
||||
if (enabledSides[1]) // south
|
||||
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
|
||||
if (enabledSides[2]) // top
|
||||
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]);
|
||||
if (enabledSides[3]) // bottom
|
||||
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]);
|
||||
if (enabledSides[4]) // west
|
||||
addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]);
|
||||
if (enabledSides[5]) // east
|
||||
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]);
|
||||
}
|
||||
|
||||
void Mesh::scale(const glm::vec3& size) {
|
||||
|
||||
@ -31,11 +31,19 @@ namespace model {
|
||||
const glm::vec3& norm,
|
||||
const UVRegion& region
|
||||
);
|
||||
void addRect(
|
||||
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]
|
||||
const UVRegion (&texfaces)[6],
|
||||
const bool enabledSides[6]
|
||||
);
|
||||
void scale(const glm::vec3& size);
|
||||
};
|
||||
@ -47,6 +55,11 @@ namespace model {
|
||||
/// @param texture texture name
|
||||
/// @return writeable Mesh
|
||||
Mesh& addMesh(const std::string& texture) {
|
||||
for (auto& mesh : meshes) {
|
||||
if (mesh.texture == texture) {
|
||||
return mesh;
|
||||
}
|
||||
}
|
||||
meshes.push_back({texture, {}});
|
||||
return meshes[meshes.size()-1];
|
||||
}
|
||||
|
||||
@ -50,16 +50,28 @@ static inline UVRegion get_region_for(
|
||||
|
||||
void ModelsGenerator::prepare(Content& content, Assets& assets) {
|
||||
for (auto& [name, def] : content.blocks.getDefs()) {
|
||||
if (def->model.type == BlockModelType::CUSTOM && def->model.name.empty()) {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(
|
||||
loadCustomBlockModel(
|
||||
def->model.customRaw, assets, !def->shadeless
|
||||
)
|
||||
),
|
||||
name + ".model"
|
||||
);
|
||||
def->model.name = def->name + ".model";
|
||||
if (def->model.type == BlockModelType::CUSTOM) {
|
||||
if (def->model.name.empty()) {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(
|
||||
loadCustomBlockModel(
|
||||
def->model.customRaw, assets, !def->shadeless
|
||||
)
|
||||
),
|
||||
name + ".model"
|
||||
);
|
||||
def->model.name = def->name + ".model";
|
||||
} else {
|
||||
auto model = assets.get<model::Model>(def->model.name);
|
||||
if (model) {
|
||||
for (auto& mesh : model->meshes) {
|
||||
if (mesh.texture.length() && mesh.texture[0] == '$') {
|
||||
int index = std::stoll(mesh.texture.substr(1));
|
||||
mesh.texture = "blocks:" + def->textureFaces[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto& [name, def] : content.items.getDefs()) {
|
||||
@ -91,8 +103,12 @@ model::Model ModelsGenerator::fromCustom(
|
||||
get_region_for(modelTextures[i * 6 + 1], assets),
|
||||
get_region_for(modelTextures[i * 6 + 0], assets)
|
||||
};
|
||||
bool enabled[6] {1,1,1,1,1,1};
|
||||
mesh.addBox(
|
||||
modelBoxes[i].center(), modelBoxes[i].size() * 0.5f, boxtexfaces
|
||||
modelBoxes[i].center(),
|
||||
modelBoxes[i].size() * 0.5f,
|
||||
boxtexfaces,
|
||||
enabled
|
||||
);
|
||||
}
|
||||
for (size_t i = 0; i < points.size() / 4; i++) {
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
|
||||
struct UVRegion {
|
||||
float u1;
|
||||
@ -51,4 +52,11 @@ struct UVRegion {
|
||||
u2 = cx + w * 0.5f * x;
|
||||
v2 = cy + h * 0.5f * y;
|
||||
}
|
||||
|
||||
void set(const glm::vec4& vec) {
|
||||
u1 = vec.x;
|
||||
v1 = vec.y;
|
||||
u2 = vec.z;
|
||||
v2 = vec.w;
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user