question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

alphaTest with fixed mipmap level sample

See original GitHub issue

When using alphaTest the alpha value from map is used where the corresponding mipmap level has been used, but this causes objects to “dissolve” quickly, leaves of trees for example will disappear quickly and they end up as blank trunks.

By sampling the map for alphaTest with a fixed level this problem disappears/gets reduced enough to prevent this to happen too early. Here are 2 tests, the first using the sample from map and the second using fixed mipmap level.

Using diffuseColor.a from map: alphaTest2

Sampling again with fixed mipmap level: alphaTest1

You see in the first there are barely trunks left while they remain filled in the second, it generally improved finer details that tend to disappear quickly. It also could be implemented optionally as it adds another texture fetch, if MSAA is available the issue is reduced a lot as well. However, otherwise there is dissolving vegetation, hair or other which can get really ugly.

I’ve been using textureLod here, but bias with texture2D seems to be the same visually. I’ve temporarily implemented it like this:

#ifdef ALPHATEST

	#if defined( USE_MAP ) && __VERSION__ == 300
		float alphaTest = textureLod( map, vUv, 0.0 ).a;
		if ( alphaTest < ALPHATEST ) discard;
	#else
		if ( diffuseColor.a < ALPHATEST ) discard;
	#endif

#endif

Edit: i changed it to texture and the bias being a parameter, as it works very well with less detailed textures but can turn harshly pixelated close up with very fine detailed textures like hair, this is something that needs to be fine tuned depending on the texture details / mesh size, but for most cases it works well with some general value.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gkjohnsoncommented, Oct 17, 2020

This blog post provides a pretty good survey of the problem in the second half and provide a couple options for addressing it. It seems that Unity will take this into account when generating mip maps so the apparent volume of the alpha tested texture remains consistent as mipmaps change.

https://medium.com/@bgolus/anti-aliased-alpha-test-the-esoteric-alpha-to-coverage-8b177335ae4f

Read more comments on GitHub >

github_iconTop Results From Across the Web

Alpha Distribution for Alpha Testing
Figure 6: Alpha testing mipmap levels of different textures on a plane using different methods. Insets showing the same textures rendered from a...
Read more >
Improving the quality of the alpha test (cutout) materials
First way, we can remap alpha channel values (by using the equation shown below) so that the lowest value is not 0% but...
Read more >
Computing Alpha Mipmaps
Manually adjusting contrast and sharpening the alpha channel of each mipmap in Photoshop. · Scaling the alpha in the shader based on distance...
Read more >
Anti-aliased Alpha Test: The Esoteric Alpha To Coverage
Tools like super sampling, temporal anti-aliasing (TAA), and multi… ... how-to-access-automatic-mipmap-level-in-glsl-fragment-shader-texture.
Read more >
Chapter 28. Mipmap-Level Measurement
So only those pixels that have sampled the highest mipmap level are counted ... However, we instead choose to keep the alpha test...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found