- Home /
Making a shader that respects Z Depth and Sprite Order?
So I've been messing around with sprites in a 3D environment, and one of the bigger problems I've been running into is making sprites function properly with semi-transparent materials using a modified sprite shader. With Z Write on, the character sprites shows properly when in front or behind a semi-transparent object, but I have multiple overlapping sprites that are ordered by their "Order in Layer" property which gets completely ignored when I turn on Z Write, meaning I get a bunch of sprites fighting over the same location. Without the z-depth, the sprites show correctly but the semi-transparent materials only appear either in front or behind the character and doesn't change based on the characters z position. It seems like there is a way to both use z position and sprite order since "Z Test Always" renders the sprites in the correct order (but shows the sprites in front of the meshes then), or at least possible using z offset in the shader. I would prefer to avoid changing the z position of every part of the character since I want to be able to rotate it 360 without seeing those position differences, so if anyone knows how to do this inside a shader, it would be extremely helpful. Here's the shader I currently use (using Z Write):
Shader "Legacy Shaders/Transparent/Cutout/Custom Bumped Diffuse" {
Properties {
_Color ("Main Color", Color) = (1,1,1,0)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout" "PreviewType" = "Plane" "CanUseSpriteAtlas" = "True"}
LOD 300
ZWrite On
Cull Off
Lighting On
CGPROGRAM
#pragma surface surf Lambert addshadow alphatest:_Cutoff
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
float3 n = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Normal = dot(IN.viewDir, float3(0, 0, 1)) > 0 ? n : -n;
}
ENDCG
}
FallBack "Legacy Shaders/Transparent/Cutout/Diffuse"
}
Your answer

Follow this Question
Related Questions
(Shader)How to find the cordinate of a given color inside a ramp texture? 0 Answers
Make Sprite-Diffuse shader work on mesh renderer? 0 Answers
Sprite not visible from behind 0 Answers
Sprite with MaterialPropertyBlock not updating until Animated 1 Answer
Low quality on Sprite and Line Renderer - not sharp enough 1 Answer