Skip to content

Commit 1158510

Browse files
committed
Fix for transparency
1 parent aba6954 commit 1158510

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

include/NavKit/render/Mesh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class Mesh {
3737
glm::vec3 aabbMin;
3838
glm::vec3 aabbMax;
3939

40+
bool isTransparent = false;
41+
4042
Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& indices,
4143
const std::vector<Texture>& textures);
4244

src/render/Mesh.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ Mesh::Mesh(const std::vector<Vertex>& vertices, const std::vector<unsigned int>&
2424
for (const auto& v : vertices) {
2525
aabbMin = glm::min(aabbMin, v.position);
2626
aabbMax = glm::max(aabbMax, v.position);
27+
if (v.color.a < 0.99f) {
28+
isTransparent = true;
29+
}
30+
}
31+
}
32+
33+
for (const auto& t : textures) {
34+
if (t.uploadFormat == GL_RGBA) {
35+
isTransparent = true;
36+
break;
2737
}
2838
}
2939
}

src/render/Model.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,38 @@ void Model::draw(const Shader& shader, const glm::mat4& viewProj) const {
307307
}
308308

309309
std::ranges::sort(indices, [&](unsigned int a, unsigned int b) {
310-
return distances[a] < distances[b];
310+
return distances[a] > distances[b];
311311
});
312312
return indices;
313313
});
314314
}
315315

316+
// Pass 1: Opaque meshes
316317
for (const unsigned int i : drawOrder) {
317318
const Mesh& mesh = meshes[i];
319+
if (mesh.isTransparent) continue;
320+
321+
const glm::vec3 center = (mesh.aabbMin + mesh.aabbMax) * 0.5f;
322+
const glm::vec3 extents = (mesh.aabbMax - mesh.aabbMin) * 0.5f;
323+
bool inside = true;
324+
for (const auto& plane : planes) {
325+
const float r = extents.x * std::abs(plane.x) + extents.y * std::abs(plane.y) + extents.z *
326+
std::abs(plane.z);
327+
if (const float d = glm::dot(glm::vec3(plane), center) + plane.w; d < -r) {
328+
inside = false;
329+
break;
330+
}
331+
}
332+
if (inside) {
333+
mesh.draw(shader);
334+
}
335+
}
336+
337+
// Pass 2: Transparent meshes (sorted back-to-front by distances[a] > distances[b])
338+
for (const unsigned int i : drawOrder) {
339+
const Mesh& mesh = meshes[i];
340+
if (!mesh.isTransparent) continue;
341+
318342
const glm::vec3 center = (mesh.aabbMin + mesh.aabbMax) * 0.5f;
319343
const glm::vec3 extents = (mesh.aabbMax - mesh.aabbMin) * 0.5f;
320344
bool inside = true;

src/resource/fragment.glsl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ void main()
5151
if (flatColor.rgb != vec3(1.0, 1.0, 1.0) || flatColor.a != 1.0) {
5252
result *= flatColor.rgb;
5353
}
54+
55+
float alpha = texColor.a * flatColor.a;
56+
if (alpha < 0.1) {
57+
discard;
58+
}
5459

55-
FragColor = vec4(result, texColor.a * flatColor.a);
60+
FragColor = vec4(result, alpha);
5661
}

0 commit comments

Comments
 (0)