feat: new block custom models implementation (WIP)
This commit is contained in:
@@ -64,41 +64,41 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
|
||||
{
|
||||
glm::vec3 pmul = glm::vec3(size * 0.63f);
|
||||
glm::vec3 hitbox = glm::vec3();
|
||||
for (const auto& box : def.modelBoxes) {
|
||||
hitbox = glm::max(hitbox, box.size());
|
||||
}
|
||||
// for (const auto& box : def.modelBoxes) {
|
||||
// hitbox = glm::max(hitbox, box.size());
|
||||
// }
|
||||
offset.y += (1.0f - hitbox).y * 0.5f;
|
||||
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
|
||||
for (size_t i = 0; i < def.modelBoxes.size(); i++) {
|
||||
const UVRegion (&boxtexfaces)[6] = {
|
||||
def.modelUVs[i * 6],
|
||||
def.modelUVs[i * 6 + 1],
|
||||
def.modelUVs[i * 6 + 2],
|
||||
def.modelUVs[i * 6 + 3],
|
||||
def.modelUVs[i * 6 + 4],
|
||||
def.modelUVs[i * 6 + 5]
|
||||
};
|
||||
batch->cube(
|
||||
def.modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
|
||||
def.modelBoxes[i].size() * pmul,
|
||||
boxtexfaces, glm::vec4(1.0f), !def.rt.emissive
|
||||
);
|
||||
}
|
||||
// for (size_t i = 0; i < def.modelBoxes.size(); i++) {
|
||||
// const UVRegion (&boxtexfaces)[6] = {
|
||||
// def.modelUVs[i * 6],
|
||||
// def.modelUVs[i * 6 + 1],
|
||||
// def.modelUVs[i * 6 + 2],
|
||||
// def.modelUVs[i * 6 + 3],
|
||||
// def.modelUVs[i * 6 + 4],
|
||||
// def.modelUVs[i * 6 + 5]
|
||||
// };
|
||||
// batch->cube(
|
||||
// def.modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
|
||||
// def.modelBoxes[i].size() * pmul,
|
||||
// boxtexfaces, glm::vec4(1.0f), !def.rt.emissive
|
||||
// );
|
||||
// }
|
||||
|
||||
auto& points = def.modelExtraPoints;
|
||||
glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
|
||||
// auto& points = def.modelExtraPoints;
|
||||
// glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
|
||||
|
||||
for (size_t i = 0; i < def.modelExtraPoints.size() / 4; i++) {
|
||||
const UVRegion& reg = def.modelUVs[def.modelBoxes.size() * 6 + i];
|
||||
// for (size_t i = 0; i < def.modelExtraPoints.size() / 4; i++) {
|
||||
// const UVRegion& reg = def.modelUVs[def.modelBoxes.size() * 6 + i];
|
||||
|
||||
batch->vertex((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
|
||||
batch->vertex((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0));
|
||||
batch->vertex((points[i * 4 + 2] - poff) * pmul, glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
|
||||
batch->vertex((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
|
||||
batch->vertex((points[i * 4 + 2] - poff) * pmul, glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
|
||||
batch->vertex((points[i * 4 + 3] - poff) * pmul, glm::vec2(reg.u1, reg.v2), glm::vec4(1.0));
|
||||
}
|
||||
batch->flush();
|
||||
// batch->vertex((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
|
||||
// batch->vertex((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0));
|
||||
// batch->vertex((points[i * 4 + 2] - poff) * pmul, glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
|
||||
// batch->vertex((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
|
||||
// batch->vertex((points[i * 4 + 2] - poff) * pmul, glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
|
||||
// batch->vertex((points[i * 4 + 3] - poff) * pmul, glm::vec2(reg.u1, reg.v2), glm::vec4(1.0));
|
||||
// }
|
||||
// batch->flush();
|
||||
}
|
||||
break;
|
||||
case BlockModel::xsprite: {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "BlocksRenderer.hpp"
|
||||
|
||||
#include "graphics/core/Mesh.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "content/Content.hpp"
|
||||
@@ -303,6 +304,7 @@ void BlocksRenderer::blockAABB(
|
||||
}
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
void BlocksRenderer::blockCustomModel(
|
||||
const glm::ivec3& icoord, const Block* block, ubyte rotation, bool lights, bool ao
|
||||
) {
|
||||
@@ -319,29 +321,31 @@ void BlocksRenderer::blockCustomModel(
|
||||
Z = orient.axisZ;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < block->modelBoxes.size(); i++) {
|
||||
AABB box = block->modelBoxes[i];
|
||||
auto size = box.size();
|
||||
if (block->rotatable) {
|
||||
orient.transform(box);
|
||||
const auto& model = cache->getModel(block->rt.id);
|
||||
for (const auto& mesh : model.meshes) {
|
||||
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * mesh.vertices.size() > capacity) {
|
||||
overflow = true;
|
||||
return;
|
||||
}
|
||||
int i = 0;
|
||||
for (const auto& vertex : mesh.vertices) {
|
||||
if ((i++) % 6 < 3) {
|
||||
// continue;
|
||||
}
|
||||
float d = glm::dot(glm::normalize(vertex.normal), SUN_VECTOR);
|
||||
d = 0.8f + d * 0.2f;
|
||||
const auto& n = vertex.normal;
|
||||
vertexAO(
|
||||
coord + vertex.coord - 0.5f,
|
||||
vertex.uv.x,
|
||||
vertex.uv.y,
|
||||
glm::vec4(1, 1, 1, 0),
|
||||
glm::vec3(n.x, -n.z, n.y),
|
||||
glm::vec3(0, 1, 0),
|
||||
n
|
||||
);
|
||||
indexBuffer[indexSize++] = indexOffset++;
|
||||
}
|
||||
glm::vec3 center_coord = coord - glm::vec3(0.5f) + box.center();
|
||||
faceAO(center_coord, X * size.x, Y * size.y, Z * size.z, block->modelUVs[i * 6 + 5], lights); // north
|
||||
faceAO(center_coord, -X * size.x, Y * size.y, -Z * size.z, block->modelUVs[i * 6 + 4], lights); // south
|
||||
faceAO(center_coord, X * size.x, -Z * size.z, Y * size.y, block->modelUVs[i * 6 + 3], lights); // top
|
||||
faceAO(center_coord, -X * size.x, -Z * size.z, -Y * size.y, block->modelUVs[i * 6 + 2], lights); // bottom
|
||||
faceAO(center_coord, -Z * size.z, Y * size.y, X * size.x, block->modelUVs[i * 6 + 1], lights); // west
|
||||
faceAO(center_coord, Z * size.z, Y * size.y, -X * size.x, block->modelUVs[i * 6 + 0], lights); // east
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < block->modelExtraPoints.size()/4; i++) {
|
||||
tetragonicFace(coord,
|
||||
block->modelExtraPoints[i * 4 + 0],
|
||||
block->modelExtraPoints[i * 4 + 1],
|
||||
block->modelExtraPoints[i * 4 + 2],
|
||||
block->modelExtraPoints[i * 4 + 3],
|
||||
X, Y, Z,
|
||||
block->modelUVs[block->modelBoxes.size()*6 + i], lights);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,13 +59,14 @@ model::Model ModelsGenerator::fromCustom(
|
||||
for (size_t i = 0; i < modelBoxes.size(); i++) {
|
||||
auto& mesh = model.addMesh("blocks:" + modelTextures[i * 6]);
|
||||
mesh.lighting = lighting;
|
||||
const UVRegion(&boxtexfaces)[6] = {
|
||||
const UVRegion boxtexfaces[6] = {
|
||||
get_region_for(modelTextures[i * 6], assets),
|
||||
get_region_for(modelTextures[i * 6 + 1], assets),
|
||||
get_region_for(modelTextures[i * 6 + 2], assets),
|
||||
get_region_for(modelTextures[i * 6 + 3], assets),
|
||||
get_region_for(modelTextures[i * 6 + 4], assets),
|
||||
get_region_for(modelTextures[i * 6 + 5], assets)};
|
||||
get_region_for(modelTextures[i * 6 + 5], assets)
|
||||
};
|
||||
mesh.addBox(
|
||||
modelBoxes[i].center(), modelBoxes[i].size() * 0.5f, boxtexfaces
|
||||
);
|
||||
@@ -112,13 +113,7 @@ model::Model ModelsGenerator::generate(
|
||||
"blocks:" + blockDef.textureFaces.at(0), assets
|
||||
);
|
||||
} else if (blockDef.model == BlockModel::custom) {
|
||||
model = fromCustom(
|
||||
assets,
|
||||
blockDef.modelBoxes,
|
||||
blockDef.modelTextures,
|
||||
blockDef.modelExtraPoints,
|
||||
!blockDef.shadeless
|
||||
);
|
||||
model = assets.require<model::Model>(blockDef.name+".model");
|
||||
for (auto& mesh : model.meshes) {
|
||||
mesh.scale(glm::vec3(0.3f));
|
||||
}
|
||||
@@ -146,3 +141,67 @@ model::Model ModelsGenerator::generate(
|
||||
return model::Model();
|
||||
}
|
||||
}
|
||||
|
||||
model::Model ModelsGenerator::loadCustomBlockModel(
|
||||
const dv::value& primitives, const Assets& assets, bool lighting
|
||||
) {
|
||||
std::vector<AABB> modelBoxes;
|
||||
std::vector<std::string> modelTextures;
|
||||
std::vector<glm::vec3> modelExtraPoints;
|
||||
|
||||
if (primitives.has("aabbs")) {
|
||||
const auto& modelboxes = primitives["aabbs"];
|
||||
for (uint i = 0; i < modelboxes.size(); i++) {
|
||||
// Parse aabb
|
||||
const auto& boxarr = modelboxes[i];
|
||||
AABB modelbox;
|
||||
modelbox.a = glm::vec3(
|
||||
boxarr[0].asNumber(), boxarr[1].asNumber(), boxarr[2].asNumber()
|
||||
);
|
||||
modelbox.b = glm::vec3(
|
||||
boxarr[3].asNumber(), boxarr[4].asNumber(), boxarr[5].asNumber()
|
||||
);
|
||||
modelbox.b += modelbox.a;
|
||||
modelBoxes.push_back(modelbox);
|
||||
|
||||
if (boxarr.size() == 7) {
|
||||
for (uint j = 6; j < 12; j++) {
|
||||
modelTextures.emplace_back(boxarr[6].asString());
|
||||
}
|
||||
} else if (boxarr.size() == 12) {
|
||||
for (uint j = 6; j < 12; j++) {
|
||||
modelTextures.emplace_back(boxarr[j].asString());
|
||||
}
|
||||
} else {
|
||||
for (uint j = 6; j < 12; j++) {
|
||||
modelTextures.emplace_back("notfound");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (primitives.has("tetragons")) {
|
||||
const auto& modeltetragons = primitives["tetragons"];
|
||||
for (uint i = 0; i < modeltetragons.size(); i++) {
|
||||
// Parse tetragon to points
|
||||
const auto& tgonobj = modeltetragons[i];
|
||||
glm::vec3 p1(
|
||||
tgonobj[0].asNumber(), tgonobj[1].asNumber(), tgonobj[2].asNumber()
|
||||
);
|
||||
glm::vec3 xw(
|
||||
tgonobj[3].asNumber(), tgonobj[4].asNumber(), tgonobj[5].asNumber()
|
||||
);
|
||||
glm::vec3 yh(
|
||||
tgonobj[6].asNumber(), tgonobj[7].asNumber(), tgonobj[8].asNumber()
|
||||
);
|
||||
modelExtraPoints.push_back(p1);
|
||||
modelExtraPoints.push_back(p1 + xw);
|
||||
modelExtraPoints.push_back(p1 + xw + yh);
|
||||
modelExtraPoints.push_back(p1 + yh);
|
||||
|
||||
modelTextures.emplace_back(tgonobj[9].asString());
|
||||
}
|
||||
}
|
||||
return fromCustom(
|
||||
assets, modelBoxes, modelTextures, modelExtraPoints, lighting
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
#include "graphics/commons/Model.hpp"
|
||||
#include "maths/aabb.hpp"
|
||||
#include "data/dv.hpp"
|
||||
|
||||
struct ItemDef;
|
||||
class Assets;
|
||||
class Content;
|
||||
struct Block;
|
||||
|
||||
class ModelsGenerator {
|
||||
public:
|
||||
@@ -20,4 +22,8 @@ public:
|
||||
const std::vector<glm::vec3>& points,
|
||||
bool lighting
|
||||
);
|
||||
|
||||
static model::Model loadCustomBlockModel(
|
||||
const dv::value& primitives, const Assets& assets, bool lighting
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user