Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translucent pixels in textures don't render correctly. #126

Open
BadComoc opened this issue Feb 8, 2020 · 4 comments
Open

Translucent pixels in textures don't render correctly. #126

BadComoc opened this issue Feb 8, 2020 · 4 comments
Labels
Bug Reporting or fixing something that is not working as intended Rendering Bugs/Features relating to rendering

Comments

@BadComoc
Copy link
Contributor

BadComoc commented Feb 8, 2020

The original texture.
Original Texture
The texture as rendered in game.
Rendered In Game

@BadComoc BadComoc added the Bug Reporting or fixing something that is not working as intended label Feb 8, 2020
@Hopson97
Copy link
Owner

Hopson97 commented Feb 8, 2020

This would be caused by the fragment shaders having a hard if

if (colour.a == 0) 
{
    discard;
}

A fix could be to instead have something like if (colour.a < 0.2)

Or maybe some kind of alpha blending instead

@Hopson97 Hopson97 added the Rendering Bugs/Features relating to rendering label Feb 10, 2020
@BadComoc
Copy link
Contributor Author

BadComoc commented Feb 16, 2020

This would be caused by the fragment shaders having a hard if

if (colour.a == 0) 
{
    discard;
}

A fix could be to instead have something like if (colour.a < 0.2)

Or maybe some kind of alpha blending instead

Changing it to 0.2 doesn't really add support for translucent pixels. it only changes the threshold on how many invisible pixels should appear. It's weird 'cause Fluid Blocks can be transparent but not Floral blocks.

@GrossoDev
Copy link
Contributor

To add alpha blending to Floral voxels you only have to change 2 files:

The transparent_fragment shader

...
void main() {
-   outColour = passBasicLight * texture(textureArray, passTexCoord);

-   if (outColour.a == 0) {
-       discard;
-   }

+   // The alpha channel is half of its actual value, for some reason.
+   outColour = passBasicLight * texture(textureArray, passTexCoord) * vec4(1.0, 1.0, 1.0, 2.0);
}

chunk_renderer.cpp

...
  // Flora voxels
  m_floraShader.program.bind();
  gl::loadUniform(m_floraShader.projectionViewLocation, projectionViewMatrix);
  gl::loadUniform(m_floraShader.timeLocation, time);
  glDisable(GL_CULL_FACE);
+ glEnable(GL_BLEND); 
  ::renderChunks(floraDrawables, frustum, m_floraShader.chunkPositionLocation, result);
+ glDisable(GL_BLEND); 
  glEnable(GL_CULL_FACE);

However, alpha blending (at least this way) requires that you have already rendered everything behind the pixel you're rendering. This was the problem with the leaves not rendering underwater (#143), and in fact it's still a bug:

In this image, you can see the other voxels of water behind:
NowYouSeeIt

In this image, from the other side, you can't:
NowYouDont

In the first image, the farthest water was drawn first and the closest was drawn after it.
In the second image, where we see it from the other side, the farthest water is now the closest (and vice-versa), but the rendering order hasn't changed, so the closest water will now render first without the farthest water behind it

As it is right now, the transparent flora might end up looking something like this (for clarity, I've changed the tall-grass texture):
Example

If this is enough it can be implemented right now, but the only way I can think of to solve this is to sort the faces from farthest to closest before rendering.

(Oh, and the thing about the alpha channel being 1/2 of the original value might have been the problem here)

@obiwac
Copy link

obiwac commented Mar 23, 2020

The original texture really shouldn't have half-transparent pixels in the first place, as that will always cause problems.
Also, I don't understand why the leaves don't show up behind, as the pixels that make up the texture are either fully opaque or fully transparent. I assume it's because all the half-transparent objects are being rendered at the same time as the leaves, but that shouldn't be the case.
Last thing, if you really wanted to add alpha sorting, it would probably be pretty easy, as this is a voxel game and there are presumably no intersecting polygons.

@Hopson97 Hopson97 added this to To do in Open Builder Mar 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reporting or fixing something that is not working as intended Rendering Bugs/Features relating to rendering
Projects
Open Builder
  
To do
Development

No branches or pull requests

4 participants