- Home /
Shader problem with alpha blend and zwrite iOS
I am trying to get some characters rendering correctly while moving in front of and behind eachother. This is how the character should look normally:
This is how they look with overlap:
Currently, the red character on the right is closer to the camera and the body is correctly rendering on top of the orange body, but the mouth of the orange character shows through which is the problem.
The distance from the camera for the orange one is 200 with its eyes and mouth at 195. The distance from the camera for the red one is 170 with eyes and mouth at 165
I would like all of the the composed orange character to show behind the red character.
I have also tried the shader below with ZWrite On but that causes the entire eyes/mouth not to render at all when the other alien is on top.
Any help would be greatly appreciated!
Here is the shader I am using currently using:
Code:
Shader "CustomGradientShader"
{
Properties
{
_MainTex( "Particle Texture", 2D ) = "white" {}
}
SubShader
{
Tags { "RenderType"="Transparent"}
Tags { "Queue" = "Transparent+1" }
Blend SrcAlpha OneMinusSrcAlpha
AlphaTest Off
ColorMask RGB
Cull Off
Lighting Off
ZWrite Off
Fog {Mode Off}
pass
{
CGPROGRAM
#pragma vertex wfiVertCol
#pragma fragment passThrough
#include "UnityCG.cginc"
sampler2D _MainTex;
struct VertOut
{
float4 position : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
struct VertIn
{
float4 vertex : POSITION;
float4 color : COLOR;
float4 texcoord : TEXCOORD0;
};
float4 _MainTex_ST;
VertOut wfiVertCol(VertIn input, float3 normal : NORMAL)
{
VertOut output;
output.position = mul(UNITY_MATRIX_MVP,input.vertex);
output.color = input.color;
output.uv = TRANSFORM_TEX (input.texcoord, _MainTex);
return output;
}
struct FragOut
{
float4 color : COLOR;
};
FragOut passThrough(VertOut vertexOut)
{
FragOut output;
float4 texColor = tex2D(_MainTex, vertexOut.uv);
float r = texColor[0]+(1-(texColor[0]/1))*vertexOut.color[0];
float g = texColor[1]+(1-(texColor[1]/1))*vertexOut.color[1];
float b = texColor[2]+(1-(texColor[2]/1))*vertexOut.color[2];
output.color = float4(r,g,b,texColor[3]);
return output;
}
ENDCG
}
}
FallBack "Diffuse"
}
Yes they all use this exact shader. I have also tried creating a separate shader for the eyes and mouth which didn't seem to have any effect.
hmm... could you make a package with this example to reproduce it?
Answer by Dragonlance · Aug 13, 2012 at 11:28 PM
I am not sure about your initial problem, but you can make the shader perform a lot better with
if(texColor[0] != 0.0)
{
float r = texColor[0]+(1-(texColor[0]/1))*vertexOut.color[0];
float g = texColor[1]+(1-(texColor[1]/1))*vertexOut.color[1];
float b = texColor[2]+(1-(texColor[2]/1))*vertexOut.color[2];
output.color = float4(r,g,b,texColor[3]);
}
else
{
output.color = float4(0, 0, 0, 0);
}
What does that do? texColor[0]/1
About the eye and mouth problem.
Looks like you use z distance to order the guys for the orthographic camera. To avoid z-fight you surely popped the mouth and eyes out a put. Maybe that distance was to far and they came in front of the guys?
Your answer
Follow this Question
Related Questions
Cg Language incompatibility Desktop VS GLSL Android 0 Answers
Grabpass refraction masking 0 Answers
How to prevent shader optimizations? 1 Answer
CG compiled to GLSL: hyperbolic functions not found. 0 Answers
Sizeof float in cg shader 1 Answer