alphaTest with fixed mipmap level sample
See original GitHub issueWhen 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:
Sampling again with fixed mipmap level:
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:
- Created 3 years ago
- Comments:18 (3 by maintainers)
Top GitHub Comments
I have not read the article but…
Wouldn’t splitting the color/alpha into
map
andalphaMap
work?https://github.com/mrdoob/three.js/blob/9ef27d1af7809fa4d9943f8d4c4644e365ab6d2d/src/renderers/shaders/ShaderChunk/alphamap_fragment.glsl.js#L4
https://github.com/mrdoob/three.js/blob/9ef27d1af7809fa4d9943f8d4c4644e365ab6d2d/src/renderers/shaders/ShaderChunk/alphatest_fragment.glsl.js#L4
(And disabling mipmapping in
alphaMap
)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