- Home /
Alpha Transparency Shader Issue
So having some issues with my simple shader I've put together, it's really basic, and I just would like alpha transparency working correctly.
Here's my code:
Shader "Test/AlphaFINAL"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Pass
{
//Render Back Faces
Cull Front
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
float4 frag(v2f_img i) : COLOR {
return tex2D(_MainTex, i.uv);
}
ENDCG
}
Pass
{
//Render Front Faces
Cull Back
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
float4 frag(v2f_img i) : COLOR
{
return tex2D(_MainTex, i.uv);
}
ENDCG
}
}
}
and here's the result:
Now after a lot of reading and the such, I get this is down to the draw order, in fact that's how I got to this result in the first place. I understand that I have now drawn the back first and as it comes forward I am blending the colours of the foreground and background together.
The issue here is it doesn't entirely seem to be using the alpha the bits of the model that are supposed to be "solid" for example the face and is just adding the back over the front.
This is where I'm stuck. I want it to respect alpha transparency with blending, but then not just show everything with an alpha of > 0 through itself.
I tried turning zwrite on for the second pass, this fixes that to a degree, but then you end up with these errors.
So. Any help or enlightenment on how to fix this would be amazing as I'm kinda stuck on my learning about shaders because of it. scurries off to try and find the answer himself..
Sorry for asking tbh, I really love to work things out on my own, but... this one just stumps me and it's probably a dead simple fix.
Thank you in advance.
Answer by SunnyChow · Feb 06, 2014 at 02:24 AM
I suggest you separate it into 2 shaders: one for solid part, one for transparent.
Unity would try to render transparent objects from back to front, but it doesn't sort every triangles individually. Therefore it's possible that a triangle in back renders over a triangle in front (transparent shader don't do z-write so it can't z-test itself).