alphaTest with fixed mipmap level sample
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 116k
- Forks
- 36.6k
- Avg merge
- 19h 25m
- Merged PRs (30d)
- 164
Description
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:

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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the alphaTest handling in the material shader and compare the existing diffuseColor.a sampling with the proposed fixed-level texture sampling. Reproduce the vegetation, hair, or fine-detail cases described in the issue, then verify that the chosen sampling behavior reduces premature detail loss without unacceptable close-up pixelation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100