2
\$\begingroup\$

So I've managed to get myself a greedy meshing algorithm implemented for a little voxel game I'm developing to teach myself LWJGL/OpenGL, and it's going great. The thing is, I don't plan on using textures (to truly fit into the voxel theme), and I was wondering how I could go about outlining each individual voxel (there can be multiple per quad). Here's the code I've come up with so far in my fragment shader:

float epsilon = 0.025f;
float xx = fract(mvVertexPos.x);
float yy = fract(mvVertexPos.y);
float zz = fract(mvVertexPos.z);
bool nearX = (xx >= -epsilon && xx <= epsilon);
bool nearY = (yy >= -epsilon && yy <= epsilon);
bool nearZ = (zz >= -epsilon && zz <= epsilon);
if ((nearX && !nearY && !nearZ) || (!nearX && nearY && !nearZ) || (!nearX && !nearY && nearZ)) {
    fragColor -= vec4(0.3, 0.3, 0.3, 0.0);
}

In this sample, mvVertexPos is the position of the vertex from the vertex shader transformed by the model matrix:

vec4 mvPos = modelMatrix * vec4(vert, 1.0);
mvVertexPos = mvPos.xyz;

This method, however, leads to a mesh that looks like this: Glitchy voxel faces

When it looks like this without that part of the shader: Normal voxel faces

And I am trying to make it looks similar to less (not black, but darkened around the edges, just pretend I outlined all the voxels, that's laziness on my part): Voxel faces outlined

I have also ensured that it's not me just duplicated meshes and overlapping them in the world, so that weirdness is a result of my lack of shader experience.

The chunks of terrain will always have the edges of the cubic voxels fall on whole numbers (negative and positive). Essentially, this would be a shader that darkens the frag colors near whole numbers in the world.

Thanks in advance for your help :)

Update: It would appear that the problems stem from the fract calls in these lines:

float xx = fract(vertWorldPos.x);
float yy = fract(vertWorldPos.y);
float zz = fract(vertWorldPos.z);

Drawing the colors like this: fragColor = vec4(xx, yy, zz, 1.0); yields this unexpected behavior: Weird coloring on voxels

Note: I also changed the fract(mvVertexPos.-) calls to fract(vertWorldPos.-) because mvVertexPos has been transformed by the matrix, while vertWorldPos has not.

\$\endgroup\$
2
\$\begingroup\$

Ok, I finally got it to work using this annoying-looking method:

float epsilona = 0.025;
float epsilonb = 0.0001;

float xx = fract(mvVertexPos.x);
float yy = fract(mvVertexPos.y);
float zz = fract(mvVertexPos.z);
bool nearX = (xx >= -epsilona && xx <= epsilona);
bool nearY = (yy >= -epsilona && yy <= epsilona);
bool nearZ = (zz >= -epsilona && zz <= epsilona);
bool overX = (xx >= -epsilonb && xx <= epsilonb);
bool overY = (yy >= -epsilonb && yy <= epsilonb);
bool overZ = (zz >= -epsilonb && zz <= epsilonb);
if ((nearX && !overX) || (nearY && !overY) || (nearZ && !overZ)) {
    fragColor = vec4(color * vec3(0.3, 0.3, 0.3), 1.0);
}

Here's what it looks like (there's some weird glitches when far away, but I can fix those by looking at the transformed z-values and not draw then they're far enough away: Finished outlines

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.