Handle Alpha Cutout issues in shader?

Using Cutout shaders you can have issues if the textures are not fully prepared - background color comes in at the edges and if the alpha coverage is not well handled on mip maps (polys disappear too quickly).

Normally you try and fix this on import but if you are loading a PNG at runtime or something, is there a way to fix these issues either in-shader or using shaders to generate mip maps?

Can you share some of the textures that are problematic, and what you expect to see?

To do something like how Unity’s “Preserve Alpha Coverage” option is relatively expensive (though not impossible) to do manually on runtime import. However there’s a relatively cheap approximation that can get you a lot of the benefits, if not entirely as accurate.

Scale the alpha.

This can be done in the shader used to render the object, or it can be done on import to the image so the texture can be used without needing a special shader on the object. But the formula is fairly straightforward.

alpha *= 1.0 + mipLevel * scale

The scale is arbitrary, but a value between 0.25 and 0.33 should work well enough for most assets. If you’re applying runtime compression to the texture after importing it, you’ll need to modify the alpha values on the CPU side. But if you’re using the texture uncompressed you could do this using a shader to render each mip to a render texture, and then use CopyTexture to copy it back to the original texture asset.

1 Like

Thanks as usual!!