- Home /
What can I do to solve problems of z-fighting with sprites in a 3d world
So I've been looking for a solution to a problem i'm having creating a character with the 2d sprites in a 3d world. I've looked at a couple of similar questions but they havent given me the full solution to the problem. Btw even with an orthographic camera it gives me the problem and I also tried changing the near and far clips of the camera with no success.
This post is the one that helped me the most but its giving me a new problem:
So my character is composed of a couple of quads with the sprite renderer component to create a player that moves. All of the quads are in a different Z and I can't use the sorting layer that unity 2d provides since these characters are going to be moving back and forward in the z plane. Here is an image with the problem:
Original: The original is when i look at it straight, as you can see it doesn't give me any problems, the problem is when I angle the camera (which i need to for my game)
Original Angled: As you can see once i angle the camera the Z-fighting starts as it just goes and cuts off his hand :(
Custom Shader: I created the custom shader exactly as the post above said and it did fix most of the problems when i angled the camera but i gave me new ones, they are the ones highlighted in the red boxes, they appear to be caused by the alpha from the sprites which seem to cut out the stuff that is close behind it and they start showing up more and more when you angle the camera, for example look at the two images of the head, one shows the neck and once i tilt the camera a bit more it cuts it right off.
I don't really know much about shaders, I've been looking around and I haven't been able to find and answer, what should I do?
Just in case here is the shader that i'm using:
Shader "Sprites/DefaultZ"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="TransparentCutout"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite On
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : COLOR
{
return tex2D(_MainTex, IN.texcoord) * IN.color;
}
ENDCG
}
}
}
Have you checked that all the normals are facing outward from your 3D application?
Yes all of the normals are facing the camera. I just checked just in case.
Answer by jashan · Jan 06, 2015 at 05:47 PM
You should probably use Sorting Layer and Order in Layer. That would be much easier than messing with Shaders and it gives you full control over rendering order.
These properties are documented in Sprite Render, and there's also a tutorial available: Tutorials / Topics / 2D / Sorting Layers
Answer by Rodiaz89 · Jan 06, 2015 at 06:02 PM
Yeah, those usually do the trick, problem is that the game has some 3d to it, so if I assign a sorting layer and another character is behind him if would look weird due to the sorting layer, it would show a piece of the arm of the player who is behind the other one. But thanks for the comment :)
Did you manage to solve this problem in the end? I'm having almost exactly the same issue and not getting anywhere.
Not really, I mean the camera in 2d mode helped a bit but in the end I had to adjust it to the best possible scenario but still get some errors by changing the scale, camera angle and distance between the planes. For the camera the more inclined it is the more error you will get. $$anonymous$$aybe there is a better solution but I gave up and left it still with some issues.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142115im_/https://answers.unity.com/themes/thub/images/avi.jpg)