Trail Renderer overlap changes trail colour
In this simple game I have a ball with a trail renderer. The problem is that when the ball changes direction and the trail overlaps with itself, the colour of the trail changes. I'm trying to keep the trail one uniform colour because as you can see, the change looks quite ugly. Is there a setting I'm missing in the trail renderer to disable this behaviour or will I have to write a script to stop it?
Thanks for any help!
(In this image the ball has just bounced off the left-hand side of the screen)

Answer by Katzelschnurr · Aug 10, 2016 at 11:48 AM
You could try using this shader: (just ignore the commented zExtrusion part)
Shader "TrailWithZWrite" {
Properties{
_Color("Main Color", Color) = (1,1,1,0.5)
_AlphaTex("Base (RGB) Trans (A)", 2D) = "white" {}
//_ExtrusionAmount("Z Extrusion Amount", Float) = 0
}
SubShader{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
LOD 100
ZWrite On
ZTest Less
Blend SrcAlpha One
//Offset 0, -5000
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
fixed4 _Color;
sampler2D _AlphaTex;
//float _ExtrusionAmount;
float4 _AlphaTex_ST;
struct v2f {
float4 pos : SV_POSITION;
half4 color : COLOR0;
float2 uv : TEXCOORD0;
};
v2f vert(appdata_full v)
{
v2f o;
//float3 camDirObjSpace = normalize(ObjSpaceViewDir(v.vertex));
//v.vertex.xyz += _ExtrusionAmount * camDirObjSpace;
o.color = v.color;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _AlphaTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 texcol = tex2D(_AlphaTex, i.uv) * i.color;
return texcol * _Color;
}
ENDCG
}
}
FallBack "Unlit/Transparent Cutout"
}
Every time I start a project I end up trying to solve this problem, and every time I give up. There's so little discussion about it, and the improved trail renderers on the Asset Store don't help either. This shader is the closest I've come to a solution, but it seems to me that it trades one problem for another, at least if you're using a semitransparent texture in your trail material. The alpha now overrides earlier pixels whenever the trail crosses itself or another similar trail (see image). Can anyone think of a shader that is free from both of these issues? I imagine it would help a lot of people like me to get more use out of the trail and line renderers...
Yes you could use a shader that test over a stencil buffer for that.
Thank you! Thank you! Thank you! I didn't think of that.. I was blindly looking for a Trail Renderer system on asset store that could avoid overlap when constructing the geometry. I was so wrong! Thank you!
Your answer