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

Transparent pixels rendered to depth buffer #33

Open
Danjb1 opened this issue Oct 31, 2022 · 1 comment
Open

Transparent pixels rendered to depth buffer #33

Danjb1 opened this issue Oct 31, 2022 · 1 comment

Comments

@Danjb1
Copy link

Danjb1 commented Oct 31, 2022

Transparent pixels are being rendered to the depth buffer, which causes them to incorrectly obscure pixels behind them.

After uncommenting and tweaking the debug code to render the depth buffer, this was my result:

image

This is a Minecraft skin where the outer layer is mostly transparent, but here it appears fully opaque.

This was the code I used to render the depth buffer:

    // debug; draw zbuffer

    for (var i = 0, l = zbuffer.length; i < l; i++) {
      // This is a bit of a fine-tuning exercise.
      // These numbers seem to work for my scene but may not work in all cases.
      // My objects are between z=0 and z=10.
      const o = i * 4;
      var v = (1 - (zbuffer[i] / 100000 - 166)) * 255;
      data[o + 0] = v;
      data[o + 1] = v;
      data[o + 2] = v;
      data[o + 3] = 255;
    }

If I find a fix I will post it here.

@Danjb1 Danjb1 changed the title Depth buffer not used Transparent pixels rendered to depth buffer Oct 31, 2022
@Danjb1
Copy link
Author

Danjb1 commented Oct 31, 2022

I managed to fix this by changing the basicMaterialShader function.

Before:

  if (buffer[colorOffset + 3] == 255)	// Only opaue pixls write to the depth buffer
    depthBuf[offset] = depth;

After:

  if (opaci > 0)	// Only opaue pixls write to the depth buffer
    depthBuf[offset] = depth;

Result:

image

I'm not sure this is 100% correct as this could also write translucent pixels to the depth buffer, but it's good enough for my purposes as I don't have any translucent obejcts.

Cuiously, in my testing, opaci was 0 for transparent pixels, 1 for opaque pixels on a layer with transparency, and 255 for fully opaque layers, so I suspect there may be something amiss there..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant