Skip to content

Commit

Permalink
Merge pull request #370 from MihailRis/transparency-fix
Browse files Browse the repository at this point in the history
Fix transparency
  • Loading branch information
MihailRis authored Nov 17, 2024
2 parents 7a3a447 + eaa5150 commit be9bd3e
Show file tree
Hide file tree
Showing 27 changed files with 430 additions and 121 deletions.
6 changes: 6 additions & 0 deletions doc/en/block-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Block model type from list:

Integer specifying number of block draw group (render order). Used for semi-transparent blocks.

### *translucent*

Enables translucency support in block textures (examples: water, ice).
Should only be used when needed, as it impacts performance.
Not required for full transparency (grass, flowers).

### *rotation*

Rotation profile (set of available block rotations and behaviour of placing block rotation) from list:
Expand Down
6 changes: 6 additions & 0 deletions doc/ru/block-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
Целое число определяющее номер группы отрисовки данного блока.
Актуально для полупрозрачных блоков - решает проблемы невидимых сторон блоков за этим блоком.

### Полупрозрачность - *translucent*

Включает поддержку полупрозрачности в текстурах блока (примеры: вода, лёд).
Следует использовать только при надобности, так как влияет на производительность.
Не требуется для полной прозрачности (трава, цветы).

### Вращение - *rotation*

Профиль вращения (набор положений, в которые можно установить блок) из списка:
Expand Down
3 changes: 2 additions & 1 deletion res/content/base/blocks/glass.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"material": "base:glass",
"draw-group": 2,
"light-passing": true,
"sky-light-passing": true
"sky-light-passing": true,
"translucent": true
}
7 changes: 7 additions & 0 deletions res/content/base/blocks/ice.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"texture": "ice",
"material": "base:glass",
"draw-group": 4,
"light-passing": true,
"translucent": true
}
3 changes: 2 additions & 1 deletion res/content/base/blocks/water.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"sky-light-passing": false,
"obstacle": false,
"selectable": false,
"replaceable": true
"replaceable": true,
"translucent": true
}
15 changes: 8 additions & 7 deletions res/content/base/content.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"items": [
"bazalt_breaker"
"entities": [
"drop",
"player",
"falling_block"
],
"blocks": [
"dirt",
Expand All @@ -27,11 +29,10 @@
"lightbulb",
"torch",
"wooden_door",
"coal_ore"
"coal_ore",
"ice"
],
"entities": [
"drop",
"player",
"falling_block"
"items": [
"bazalt_breaker"
]
}
Binary file added res/content/base/textures/blocks/ice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions res/shaders/main.glslf
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ uniform samplerCube u_cubemap;
uniform vec3 u_fogColor;
uniform float u_fogFactor;
uniform float u_fogCurve;
uniform bool u_alphaClip;

void main() {
vec3 fogColor = texture(u_cubemap, a_dir).rgb;
vec4 tex_color = texture(u_texture0, a_texCoord);
float depth = (a_distance/256.0);
float alpha = a_color.a * tex_color.a;
// anyway it's any alpha-test alternative required
if (alpha < 0.3f)
if (u_alphaClip && alpha < 0.9f)
discard;
f_color = mix(a_color * tex_color, vec4(fogColor,1.0),
min(1.0, pow(depth*u_fogFactor, u_fogCurve)));
Expand Down
8 changes: 5 additions & 3 deletions src/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ inline constexpr int CHUNK_D = 16;
inline constexpr uint VOXEL_USER_BITS = 8;
inline constexpr uint VOXEL_USER_BITS_OFFSET = sizeof(blockstate_t)*8-VOXEL_USER_BITS;

/// @brief pixel size of an item inventory icon
inline constexpr int ITEM_ICON_SIZE = 48;

/// @brief chunk volume (count of voxels per Chunk)
inline constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D);

Expand All @@ -53,6 +50,11 @@ inline constexpr uint vox_index(uint x, uint y, uint z, uint w=CHUNK_W, uint d=C
return (y * d + z) * w + x;
}

/// @brief pixel size of an item inventory icon
inline constexpr int ITEM_ICON_SIZE = 48;

inline constexpr int TRANSLUCENT_BLOCKS_SORT_INTERVAL = 8;

inline const std::string SHADERS_FOLDER = "shaders";
inline const std::string TEXTURES_FOLDER = "textures";
inline const std::string FONTS_FOLDER = "fonts";
Expand Down
1 change: 1 addition & 0 deletions src/content/ContentLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ void ContentLoader::loadBlock(
root.at("inventory-size").get(def.inventorySize);
root.at("tick-interval").get(def.tickInterval);
root.at("overlay-texture").get(def.overlayTexture);
root.at("translucent").get(def.translucent);

if (root.has("fields")) {
def.dataStruct = std::make_unique<StructLayout>();
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/core/Batch2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
inline constexpr uint B2D_VERTEX_SIZE = 8;

Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){
const vattr attrs[] = {
const VertexAttribute attrs[] = {
{2}, {2}, {4}, {0}
};

Expand Down
2 changes: 1 addition & 1 deletion src/graphics/core/Batch3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ inline constexpr uint B3D_VERTEX_SIZE = 9;

Batch3D::Batch3D(size_t capacity)
: capacity(capacity) {
const vattr attrs[] = {
const VertexAttribute attrs[] = {
{3}, {2}, {4}, {0}
};

Expand Down
2 changes: 1 addition & 1 deletion src/graphics/core/LineBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
inline constexpr uint LB_VERTEX_SIZE = (3+4);

LineBatch::LineBatch(size_t capacity) : capacity(capacity) {
const vattr attrs[] = { {3},{4}, {0} };
const VertexAttribute attrs[] = { {3},{4}, {0} };
buffer = std::make_unique<float[]>(capacity * LB_VERTEX_SIZE * 2);
mesh = std::make_unique<Mesh>(buffer.get(), 0, attrs);
index = 0;
Expand Down
19 changes: 9 additions & 10 deletions src/graphics/core/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
int Mesh::meshesCount = 0;
int Mesh::drawCalls = 0;

inline size_t calc_vertex_size(const vattr* attrs) {
inline size_t calc_vertex_size(const VertexAttribute* attrs) {
size_t vertexSize = 0;
for (int i = 0; attrs[i].size; i++) {
vertexSize += attrs[i].size;
Expand All @@ -19,10 +19,10 @@ Mesh::Mesh(const MeshData& data)
data.indices.size(),
data.attrs.data()) {}

Mesh::Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const vattr* attrs) :
Mesh::Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const VertexAttribute* attrs) :
ibo(0),
vertices(vertices),
indices(indices)
vertices(0),
indices(0)
{
meshesCount++;
vertexSize = 0;
Expand Down Expand Up @@ -58,10 +58,9 @@ void Mesh::reload(const float* vertexBuffer, size_t vertices, const int* indexBu
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
if (vertexBuffer != nullptr && vertices != 0) {
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, vertexBuffer, GL_STATIC_DRAW);
}
else {
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, vertexBuffer, GL_STREAM_DRAW);
} else {
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STREAM_DRAW);
}
if (indexBuffer != nullptr && indices != 0) {
if (ibo == 0) glGenBuffers(1, &ibo);
Expand All @@ -75,7 +74,7 @@ void Mesh::reload(const float* vertexBuffer, size_t vertices, const int* indexBu
this->indices = indices;
}

void Mesh::draw(unsigned int primitive){
void Mesh::draw(unsigned int primitive) const {
drawCalls++;
glBindVertexArray(vao);
if (ibo != 0) {
Expand All @@ -87,6 +86,6 @@ void Mesh::draw(unsigned int primitive){
glBindVertexArray(0);
}

void Mesh::draw() {
void Mesh::draw() const {
draw(GL_TRIANGLES);
}
8 changes: 4 additions & 4 deletions src/graphics/core/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Mesh {
size_t vertexSize;
public:
Mesh(const MeshData& data);
Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const vattr* attrs);
Mesh(const float* vertexBuffer, size_t vertices, const vattr* attrs) :
Mesh(const float* vertexBuffer, size_t vertices, const int* indexBuffer, size_t indices, const VertexAttribute* attrs);
Mesh(const float* vertexBuffer, size_t vertices, const VertexAttribute* attrs) :
Mesh(vertexBuffer, vertices, nullptr, 0, attrs) {};
~Mesh();

Expand All @@ -28,10 +28,10 @@ class Mesh {

/// @brief Draw mesh with specified primitives type
/// @param primitive primitives type
void draw(unsigned int primitive);
void draw(unsigned int primitive) const;

/// @brief Draw mesh as triangles
void draw();
void draw() const;

/// @brief Total numbers of alive mesh objects
static int meshesCount;
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/core/MeshData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#include "util/Buffer.hpp"

/// @brief Vertex attribute info
struct vattr {
struct VertexAttribute {
ubyte size;
};

/// @brief Raw mesh data structure
struct MeshData {
util::Buffer<float> vertices;
util::Buffer<int> indices;
util::Buffer<vattr> attrs;
util::Buffer<VertexAttribute> attrs;

MeshData() = default;

Expand All @@ -24,7 +24,7 @@ struct MeshData {
MeshData(
util::Buffer<float> vertices,
util::Buffer<int> indices,
util::Buffer<vattr> attrs
util::Buffer<VertexAttribute> attrs
) : vertices(std::move(vertices)),
indices(std::move(indices)),
attrs(std::move(attrs)) {}
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/core/PostProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PostProcessing::PostProcessing() {
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f
};
vattr attrs[] {{2}, {0}};
VertexAttribute attrs[] {{2}, {0}};
quadMesh = std::make_unique<Mesh>(vertices, 6, attrs);
}

Expand Down
Loading

0 comments on commit be9bd3e

Please sign in to comment.