- Home /
z-fighting problem with shaders
Hi, I have a set of muscles in Unity with a standard fade shader applied and for each muscle I have a texture map with an alpha channel and a normal map. The muscles overlap each other and aren't rendering in the correct depth order and I keep seeing weird artifacts as seen here:https://i.gyazo.com/a4ab8a10d015321aef5921f5cd8c9d8e.gif. Apparently, this is a common zdepth rendering problem with transparency shaders. I have tried to address this by using this shader (http://wiki.unity3d.com/index.php?title=AlphaVertexLitZ) or writing in a zOff as per this discussion (http://answers.unity3d.com/questions/562613/transparency-render-issue.html). However, the AlphaVertexLitZ shader doesn't support normal maps and the zOff in shader isn't working for me. I am looking for a solution where I can incorporate texture map with its alpha channel, normal map, and correct rendering depth. I am aware that all of these may be a long shot but any suggestions are appreciated!
Answer by FortisVenaliter · Jul 13, 2017 at 07:55 PM
There are some code solutions to this... but they tend to be very complicated and often need engine-level support.
Honestly, the best way to solve this problem is not with code, but with art. Unity doesn't sort triangles by depth, but it does sort objects by depth. So if you separate your transparent mesh into separate meshes on separate objects, then it can resolve their depths and draw them in the correct order to maintain the correct visual.
Thanks for you answer but I already have the muscles as separate objects. Unity is sorting them by depth but in certain views some of the deeper objects switch to surface and surface muscles appear to be behind deeper muscles. Do you by any chance know of any work-arounds to this offhand?
Okay, two questions:
You mean that each of the muscles are separate objects, right? It's not just separated between bone and muscle?
Also, it looks like the materials are opaque... for what reason do you need the transparent shader?
yes, each muscle is a separate object. I have the transparent shader because my texture maps have an alpha channel which allows the portions of the muscle to fade out as they approach their attachment sites to bone and other muscle -so it looks like they fade into fibres rather than solid chunkiness. I also have visibility sliders in the UI which allow the user to change the opacity of any object from opaque to completely transparent, so even if I changed the muscle material to opaque inititially I run into the same problem with the visibility sliders.
Oookay, gotcha. What I would do then would be to remove those transparent tails into their own separate meshes. That way you could run the rest as opaque, and only the tails would have to be sorted, which, since they take much less screen/depth space, would be much easier to sort and display properly.
Answer by judy18 · Jul 14, 2017 at 04:54 PM
OK thank you! I also got another suggestion to remove the back of the meshes (so it would just be one-sided surface, no volume)