add custom-models in hand support
This commit is contained in:
@@ -8,7 +8,12 @@ inline constexpr glm::vec3 X(1, 0, 0);
|
|||||||
inline constexpr glm::vec3 Y(0, 1, 0);
|
inline constexpr glm::vec3 Y(0, 1, 0);
|
||||||
inline constexpr glm::vec3 Z(0, 0, 1);
|
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, {0,0}, norm});
|
||||||
vertices.push_back({pos+right-up, {1,0}, norm});
|
vertices.push_back({pos+right-up, {1,0}, norm});
|
||||||
vertices.push_back({pos+right+up, {1,1}, 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});
|
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);
|
||||||
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);
|
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) {
|
void Mesh::scale(const glm::vec3& size) {
|
||||||
for (auto& vertex : vertices) {
|
for (auto& vertex : vertices) {
|
||||||
vertex.coord *= size;
|
vertex.coord *= size;
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "maths/UVRegion.hpp"
|
||||||
|
|
||||||
namespace model {
|
namespace model {
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
glm::vec3 coord;
|
glm::vec3 coord;
|
||||||
@@ -14,9 +16,27 @@ namespace model {
|
|||||||
struct Mesh {
|
struct Mesh {
|
||||||
std::string texture;
|
std::string texture;
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
|
bool lighting = true;
|
||||||
void addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm);
|
|
||||||
void addBox(glm::vec3 pos, glm::vec3 size);
|
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);
|
void scale(const glm::vec3& size);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -80,3 +80,56 @@ glm::vec4 MainBatch::sampleLight(
|
|||||||
glm::max(Lightmap::extract(light, 3), minIntensity) / 15.0f
|
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]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -118,4 +118,12 @@ public:
|
|||||||
tint
|
tint
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cube(
|
||||||
|
const glm::vec3& coord,
|
||||||
|
const glm::vec3& size,
|
||||||
|
const UVRegion(&texfaces)[6],
|
||||||
|
const glm::vec4& tint,
|
||||||
|
bool shading
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "ModelsGenerator.hpp"
|
#include "ModelsGenerator.hpp"
|
||||||
|
|
||||||
#include "assets/Assets.hpp"
|
#include "assets/Assets.hpp"
|
||||||
|
#include "assets/assets_util.hpp"
|
||||||
#include "items/ItemDef.hpp"
|
#include "items/ItemDef.hpp"
|
||||||
#include "voxels/Block.hpp"
|
#include "voxels/Block.hpp"
|
||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
@@ -40,6 +41,13 @@ static model::Model create_flat_model(
|
|||||||
return 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(
|
model::Model ModelsGenerator::generate(
|
||||||
const ItemDef& def, const Content& content, const Assets& assets
|
const ItemDef& def, const Content& content, const Assets& assets
|
||||||
) {
|
) {
|
||||||
@@ -50,6 +58,56 @@ model::Model ModelsGenerator::generate(
|
|||||||
return create_flat_model(
|
return create_flat_model(
|
||||||
"blocks:" + blockDef.textureFaces.at(0), assets
|
"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) {
|
for (auto& mesh : model.meshes) {
|
||||||
switch (blockDef.model) {
|
switch (blockDef.model) {
|
||||||
|
|||||||
@@ -116,8 +116,8 @@ public:
|
|||||||
|
|
||||||
std::vector<std::string> modelTextures = {};
|
std::vector<std::string> modelTextures = {};
|
||||||
std::vector<BoxModel> modelBoxes = {};
|
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
|
std::vector<UVRegion> modelUVs = {}; // boxes' tex-UVs also there
|
||||||
|
|
||||||
/// @brief id of used BlockMaterial, may specify non-existing material
|
/// @brief id of used BlockMaterial, may specify non-existing material
|
||||||
|
|||||||
Reference in New Issue
Block a user