add block 'ambient-occlusion' property

This commit is contained in:
MihailRis 2024-07-27 18:37:18 +03:00
parent f485ba5e54
commit 71e20ff805
10 changed files with 149 additions and 53 deletions

View File

@ -71,6 +71,10 @@ Vertical sky light ray ignores block if **true**. (used for water)
Turns off block model shading Turns off block model shading
### *ambient-occlusion* (Vertex-based Ambient-Occlusion)
Determines the presence of the vertex AO effect. Turned-on by default.
## Physics ## Physics
### *obstacle* ### *obstacle*

View File

@ -74,6 +74,10 @@
Выключает освещение на модели блока. Выключает освещение на модели блока.
### Вершинный Ambient-Occlusion - *ambient-occlusion*
Определяет наличие эффекта вершинного AO. Включен по-умолчанию.
## Физика ## Физика
### Препятствие - *obstacle* ### Препятствие - *obstacle*

View File

@ -13,5 +13,6 @@
"size": [1, 2, 1], "size": [1, 2, 1],
"rotation": "pane", "rotation": "pane",
"model": "aabb", "model": "aabb",
"hitbox": [0.0, 0.0, 0.8, 1.0, 2.0, 0.2] "hitbox": [0.0, 0.0, 0.8, 1.0, 2.0, 0.2],
"ambient-occlusion": false
} }

View File

@ -217,6 +217,7 @@ void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::pat
root->flag("light-passing", def.lightPassing); root->flag("light-passing", def.lightPassing);
root->flag("sky-light-passing", def.skyLightPassing); root->flag("sky-light-passing", def.skyLightPassing);
root->flag("shadeless", def.shadeless); root->flag("shadeless", def.shadeless);
root->flag("ambient-occlusion", def.ambientOcclusion);
root->flag("breakable", def.breakable); root->flag("breakable", def.breakable);
root->flag("selectable", def.selectable); root->flag("selectable", def.selectable);
root->flag("grounded", def.grounded); root->flag("grounded", def.grounded);

View File

@ -104,7 +104,7 @@ void BlocksRenderer::face(
index(0, 1, 3, 1, 2, 3); index(0, 1, 3, 1, 2, 3);
} }
void BlocksRenderer::vertex( void BlocksRenderer::vertexAO(
const vec3& coord, const vec3& coord,
float u, float v, float u, float v,
const vec4& tint, const vec4& tint,
@ -117,7 +117,7 @@ void BlocksRenderer::vertex(
vertex(coord, u, v, light * tint); vertex(coord, u, v, light * tint);
} }
void BlocksRenderer::face( void BlocksRenderer::faceAO(
const vec3& coord, const vec3& coord,
const vec3& X, const vec3& X,
const vec3& Y, const vec3& Y,
@ -140,12 +140,12 @@ void BlocksRenderer::face(
vec3 axisZ = glm::normalize(Z); vec3 axisZ = glm::normalize(Z);
vec4 tint(d); vec4 tint(d);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, axisX, axisY, axisZ); vertexAO(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, axisX, axisY, axisZ);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, axisX, axisY, axisZ); vertexAO(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, axisX, axisY, axisZ);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, axisX, axisY, axisZ); vertexAO(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, axisX, axisY, axisZ);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, axisX, axisY, axisZ); vertexAO(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, axisX, axisY, axisZ);
} else { } else {
vec4 tint(1.0f); glm::vec4 tint(1.0f);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint); vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint); vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint); vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint);
@ -154,6 +154,33 @@ void BlocksRenderer::face(
index(0, 1, 2, 0, 2, 3); index(0, 1, 2, 0, 2, 3);
} }
void BlocksRenderer::face(
const vec3& coord,
const vec3& X,
const vec3& Y,
const vec3& Z,
const UVRegion& region,
vec4 tint,
bool lights
) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) {
overflow = true;
return;
}
float s = 0.5f;
if (lights) {
float d = glm::dot(glm::normalize(Z), SUN_VECTOR);
d = 0.8f + d * 0.2f;
tint *= d;
}
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint);
index(0, 1, 2, 0, 2, 3);
}
void BlocksRenderer::tetragonicFace( void BlocksRenderer::tetragonicFace(
const vec3& coord, const vec3& coord,
const vec3& p1, const vec3& p2, const vec3& p3, const vec3& p4, const vec3& p1, const vec3& p2, const vec3& p3, const vec3& p4,
@ -231,7 +258,8 @@ void BlocksRenderer::blockAABB(
const UVRegion(&texfaces)[6], const UVRegion(&texfaces)[6],
const Block* block, const Block* block,
ubyte rotation, ubyte rotation,
bool lights bool lights,
bool ao
) { ) {
if (block->hitboxes.empty()) { if (block->hitboxes.empty()) {
return; return;
@ -256,18 +284,30 @@ void BlocksRenderer::blockAABB(
} }
coord = vec3(icoord) - vec3(0.5f) + hitbox.center(); coord = vec3(icoord) - vec3(0.5f) + hitbox.center();
face(coord, X*size.x, Y*size.y, Z*size.z, texfaces[5], lights); // north if (ao) {
face(coord, -X*size.x, Y*size.y, -Z*size.z, texfaces[4], lights); // south faceAO(coord, X*size.x, Y*size.y, Z*size.z, texfaces[5], lights); // north
faceAO(coord, -X*size.x, Y*size.y, -Z*size.z, texfaces[4], lights); // south
face(coord, X*size.x, -Z*size.z, Y*size.y, texfaces[3], lights); // top faceAO(coord, X*size.x, -Z*size.z, Y*size.y, texfaces[3], lights); // top
face(coord, -X*size.x, -Z*size.z, -Y*size.y, texfaces[2], lights); // bottom faceAO(coord, -X*size.x, -Z*size.z, -Y*size.y, texfaces[2], lights); // bottom
face(coord, -Z*size.z, Y*size.y, X*size.x, texfaces[1], lights); // west faceAO(coord, -Z*size.z, Y*size.y, X*size.x, texfaces[1], lights); // west
face(coord, Z*size.z, Y*size.y, -X*size.x, texfaces[0], lights); // east faceAO(coord, Z*size.z, Y*size.y, -X*size.x, texfaces[0], lights); // east
} else {
auto tint = pickLight(icoord);
face(coord, X*size.x, Y*size.y, Z*size.z, texfaces[5], tint, lights); // north
face(coord, -X*size.x, Y*size.y, -Z*size.z, texfaces[4], tint, lights); // south
face(coord, X*size.x, -Z*size.z, Y*size.y, texfaces[3], tint, lights); // top
face(coord, -X*size.x, -Z*size.z, -Y*size.y, texfaces[2], tint, lights); // bottom
face(coord, -Z*size.z, Y*size.y, X*size.x, texfaces[1], tint, lights); // west
face(coord, Z*size.z, Y*size.y, -X*size.x, texfaces[0], tint, lights); // east
}
} }
void BlocksRenderer::blockCustomModel( void BlocksRenderer::blockCustomModel(
const ivec3& icoord, const Block* block, ubyte rotation, bool lights const ivec3& icoord, const Block* block, ubyte rotation, bool lights, bool ao
) { ) {
vec3 X(1, 0, 0); vec3 X(1, 0, 0);
vec3 Y(0, 1, 0); vec3 Y(0, 1, 0);
@ -289,12 +329,12 @@ void BlocksRenderer::blockCustomModel(
orient.transform(box); orient.transform(box);
} }
vec3 center_coord = coord - vec3(0.5f) + box.center(); vec3 center_coord = coord - vec3(0.5f) + box.center();
face(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 + 5], lights); // north
face(center_coord, -X * size.x, Y * size.y, -Z * size.z, block->modelUVs[i * 6 + 4], lights); // south faceAO(center_coord, -X * size.x, Y * size.y, -Z * size.z, block->modelUVs[i * 6 + 4], lights); // south
face(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 + 3], lights); // top
face(center_coord, -X * size.x, -Z * size.z, -Y * size.y, block->modelUVs[i * 6 + 2], lights); // bottom faceAO(center_coord, -X * size.x, -Z * size.z, -Y * size.y, block->modelUVs[i * 6 + 2], lights); // bottom
face(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 + 1], lights); // west
face(center_coord, Z * size.z, Y * size.y, -X * size.x, block->modelUVs[i * 6 + 0], lights); // east 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++) { for (size_t i = 0; i < block->modelExtraPoints.size()/4; i++) {
@ -314,7 +354,8 @@ void BlocksRenderer::blockCube(
const UVRegion(&texfaces)[6], const UVRegion(&texfaces)[6],
const Block* block, const Block* block,
blockstate states, blockstate states,
bool lights bool lights,
bool ao
) { ) {
ubyte group = block->drawGroup; ubyte group = block->drawGroup;
@ -330,23 +371,45 @@ void BlocksRenderer::blockCube(
Z = orient.axisZ; Z = orient.axisZ;
} }
if (isOpen(x+Z.x, y+Z.y, z+Z.z, group)) { if (ao) {
face(coord, X, Y, Z, texfaces[5], lights); if (isOpen(x+Z.x, y+Z.y, z+Z.z, group)) {
} faceAO(coord, X, Y, Z, texfaces[5], lights);
if (isOpen(x-Z.x, y-Z.y, z-Z.z, group)) { }
face(coord, -X, Y, -Z, texfaces[4], lights); if (isOpen(x-Z.x, y-Z.y, z-Z.z, group)) {
} faceAO(coord, -X, Y, -Z, texfaces[4], lights);
if (isOpen(x+Y.x, y+Y.y, z+Y.z, group)) { }
face(coord, X, -Z, Y, texfaces[3], lights); if (isOpen(x+Y.x, y+Y.y, z+Y.z, group)) {
} faceAO(coord, X, -Z, Y, texfaces[3], lights);
if (isOpen(x-Y.x, y-Y.y, z-Y.z, group)) { }
face(coord, X, Z, -Y, texfaces[2], lights); if (isOpen(x-Y.x, y-Y.y, z-Y.z, group)) {
} faceAO(coord, X, Z, -Y, texfaces[2], lights);
if (isOpen(x+X.x, y+X.y, z+X.z, group)) { }
face(coord, -Z, Y, X, texfaces[1], lights); if (isOpen(x+X.x, y+X.y, z+X.z, group)) {
} faceAO(coord, -Z, Y, X, texfaces[1], lights);
if (isOpen(x-X.x, y-X.y, z-X.z, group)) { }
face(coord, Z, Y, -X, texfaces[0], lights); if (isOpen(x-X.x, y-X.y, z-X.z, group)) {
faceAO(coord, Z, Y, -X, texfaces[0], lights);
}
} else {
auto tint = pickLight({x, y, z});
if (isOpen(x+Z.x, y+Z.y, z+Z.z, group)) {
face(coord, X, Y, Z, texfaces[5], tint, lights);
}
if (isOpen(x-Z.x, y-Z.y, z-Z.z, group)) {
face(coord, -X, Y, -Z, texfaces[4], tint, lights);
}
if (isOpen(x+Y.x, y+Y.y, z+Y.z, group)) {
face(coord, X, -Z, Y, texfaces[3], tint, lights);
}
if (isOpen(x-Y.x, y-Y.y, z-Y.z, group)) {
face(coord, X, Z, -Y, texfaces[2], tint, lights);
}
if (isOpen(x+X.x, y+X.y, z+X.z, group)) {
face(coord, -Z, Y, X, texfaces[1], tint, lights);
}
if (isOpen(x-X.x, y-X.y, z-X.z, group)) {
face(coord, Z, Y, -X, texfaces[0], tint, lights);
}
} }
} }
@ -440,7 +503,8 @@ void BlocksRenderer::render(const voxel* voxels) {
int z = (i / CHUNK_D) % CHUNK_W; int z = (i / CHUNK_D) % CHUNK_W;
switch (def.model) { switch (def.model) {
case BlockModel::block: case BlockModel::block:
blockCube(x, y, z, texfaces, &def, vox.state, !def.shadeless); blockCube(x, y, z, texfaces, &def, vox.state, !def.shadeless,
def.ambientOcclusion);
break; break;
case BlockModel::xsprite: { case BlockModel::xsprite: {
blockXSprite(x, y, z, vec3(1.0f), blockXSprite(x, y, z, vec3(1.0f),
@ -448,11 +512,13 @@ void BlocksRenderer::render(const voxel* voxels) {
break; break;
} }
case BlockModel::aabb: { case BlockModel::aabb: {
blockAABB(ivec3(x,y,z), texfaces, &def, vox.state.rotation, !def.shadeless); blockAABB(ivec3(x,y,z), texfaces, &def, vox.state.rotation,
!def.shadeless, def.ambientOcclusion);
break; break;
} }
case BlockModel::custom: { case BlockModel::custom: {
blockCustomModel(ivec3(x, y, z), &def, vox.state.rotation, !def.shadeless); blockCustomModel(ivec3(x, y, z), &def, vox.state.rotation,
!def.shadeless, def.ambientOcclusion);
break; break;
} }
default: default:

View File

@ -40,7 +40,7 @@ class BlocksRenderer {
void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light); void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light);
void index(int a, int b, int c, int d, int e, int f); void index(int a, int b, int c, int d, int e, int f);
void vertex( void vertexAO(
const glm::vec3& coord, float u, float v, const glm::vec3& coord, float u, float v,
const glm::vec4& brightness, const glm::vec4& brightness,
const glm::vec3& axisX, const glm::vec3& axisX,
@ -58,6 +58,15 @@ class BlocksRenderer {
const glm::vec4& tint const glm::vec4& tint
); );
void face( void face(
const glm::vec3& coord,
const glm::vec3& X,
const glm::vec3& Y,
const glm::vec3& Z,
const UVRegion& region,
glm::vec4 tint,
bool lights
);
void faceAO(
const glm::vec3& coord, const glm::vec3& coord,
const glm::vec3& axisX, const glm::vec3& axisX,
const glm::vec3& axisY, const glm::vec3& axisY,
@ -80,14 +89,16 @@ class BlocksRenderer {
const UVRegion(&faces)[6], const UVRegion(&faces)[6],
const Block* block, const Block* block,
blockstate states, blockstate states,
bool lights bool lights,
bool ao
); );
void blockAABB( void blockAABB(
const glm::ivec3& coord, const glm::ivec3& coord,
const UVRegion(&faces)[6], const UVRegion(&faces)[6],
const Block* block, const Block* block,
ubyte rotation, ubyte rotation,
bool lights bool lights,
bool ambientOcclusion
); );
void blockXSprite( void blockXSprite(
int x, int y, int z, int x, int y, int z,
@ -100,7 +111,8 @@ class BlocksRenderer {
const glm::ivec3& icoord, const glm::ivec3& icoord,
const Block* block, const Block* block,
ubyte rotation, ubyte rotation,
bool lights bool lights,
bool ao
); );
bool isOpenForLight(int x, int y, int z) const; bool isOpenForLight(int x, int y, int z) const;

View File

@ -50,6 +50,7 @@ ChunksRenderer::ChunksRenderer(
renderer = std::make_unique<BlocksRenderer>( renderer = std::make_unique<BlocksRenderer>(
RENDERER_CAPACITY, level->content, cache, settings RENDERER_CAPACITY, level->content, cache, settings
); );
logger.info() << "created " << threadPool.getWorkersCount() << " workers";
} }
ChunksRenderer::~ChunksRenderer() { ChunksRenderer::~ChunksRenderer() {

View File

@ -200,7 +200,7 @@ public:
} }
} }
void enqueueJob(std::shared_ptr<T> job) { void enqueueJob(const std::shared_ptr<T>& job) {
{ {
std::lock_guard<std::mutex> lock(jobsMutex); std::lock_guard<std::mutex> lock(jobsMutex);
jobs.push(job); jobs.push(job);
@ -244,6 +244,10 @@ public:
update(); update();
} }
} }
uint getWorkersCount() const {
return threads.size();
}
}; };
} // namespace util } // namespace util

View File

@ -20,11 +20,11 @@ namespace timeutil {
* ... * ...
* } * }
*/ */
class ScopeLogTimer : public Timer{ class ScopeLogTimer : public Timer {
long long scopeid_; long long scopeid_;
public: public:
ScopeLogTimer(long long id); ScopeLogTimer(long long id);
~ScopeLogTimer(); ~ScopeLogTimer();
}; };
inline constexpr float time_value(float hour, float minute, float second) { inline constexpr float time_value(float hour, float minute, float second) {

View File

@ -137,6 +137,9 @@ public:
/// @brief Does block model have shading /// @brief Does block model have shading
bool shadeless = false; bool shadeless = false;
/// @brief Does block model have vertex-based AO effect
bool ambientOcclusion = true;
/// @brief Is the block a physical obstacle /// @brief Is the block a physical obstacle
bool obstacle = true; bool obstacle = true;