- Home /
Models using transparent shader are overlapping themselves
Here's two examples :
Opaque : https://i.imgur.com/iaRX57X.png
Transparent : https://i.imgur.com/4fUtHVE.png
Seen from up :
Opaque : https://i.imgur.com/Sw2itEw.png
Transparent : https://i.imgur.com/8O1Hv2y.png
I need to have a shader compatible with Sorting Layers since my game is a mix of 2D and 3D, that's why I'm using the Standard transparent one
Any way to fix this issue ?
Answer by Bunny83 · Jul 31, 2019 at 06:25 PM
That's a common issue and no easy / cheap solution. First of all you should enable zWrite in your shader which transparent shaders most the time do not do. However depending on your model and if you want to see self occluded parts or not. The triangle of a mesh are usually rendered in order as they are defined in the mesh. This order is never changed.
Any kind of sorting happens on a per object basis. If you want to prevent any self overlapping for transparent shaders you would need a two pass approach. First just drawing into the depth buffer, second pass just drawing into the color buffer. That way only the closest visible surface of an object will be drawn.
Just like explained in the example here for the semi transparent shader.
ZWrite breaks the sorting layers.. :( I don't really understand what you wrote after that
Answer by Lucinar · Jan 11, 2021 at 02:14 PM
In case someone search for this question like I just did, add following code into your shader:
Blend SrcAlpha OneMinusSrcAlpha
Stencil {
Ref 10
ReadMask 10
Comp NotEqual
Pass Replace
}
That worked. I've used the buit-in Legacy/Particles/Alpha blended shader downloaded from Unity download archive and simply added your lines and now I only see the nearest surface of the transparent object.
Answer by contact_unity364 · Mar 03, 2021 at 01:37 PM
I found adding this to the "Transparent/Diffuse ZWrite" shader worked for me, not in a separate pass. Not an expert with shaders but was delighted my transparent scorch marks on the ground did not overlap each other cutting the others out, now they overlap nicely and fade into eachother.
ZWrite On
ColorMask 0
This is the full shader:
Shader "Transparent/Diffuse ZWrite" {
Properties{
_Color("Main Color", Color) = (1,1,1,1)
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader{
Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
LOD 200
ZWrite On
ColorMask 0
// paste in forward rendering passes from Transparent/Diffuse
UsePass "Transparent/Diffuse/FORWARD"
}
Fallback "Transparent/VertexLit"
}
Your answer
Follow this Question
Related Questions
How to make make an object look like an Opaque but still have Alpha fading effect? 1 Answer
Can I draw Opaque geometry on top of Sprites, cheaply? 0 Answers
Standard shader's Fade rendering mode won't allow full opacity with a textured material 0 Answers
Rendering transparent objects back to back (HDRP Lit shader) 0 Answers
Make a complex object semi transparent without changing it 2 Answers