- Home /
Shader Passes
Hey i write a little 2D outline shader, but my problem is that i use multiple Passes, so dynamic batching do not work and thats bad for performance.
This is one Pass, i just draw the vertexes like 4 times, x+spread, x-spread,y+spread, y-spread. Than in the last Pass i paint the sprite in his normal color in the middle and i have a nice outline. Is ther a way to combine my passes ? If yes how can i do that?
Pass 1-4 loks like this (this is the case: vertex.x + spread):
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;
float _OutLineSpread;
v2f vert(appdata_t IN)
{
v2f OUT;
float4 vertex = float4(IN.vertex.x , IN.vertex.y , IN.vertex.z, 1);
vertex += float4(vertex.x + _OutLineSpread, vertex.y, vertex.z, 1);
OUT.vertex = mul(UNITY_MATRIX_MVP, vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D(_MainTex, IN.texcoord);
_Color *= c.a;
return _Color;
}
ENDCG
}
And the last Pass loks like this:
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;
float _OutLineSpreadX;
float _OutLineSpreadY;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord ;
OUT.color = IN.color;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D(_MainTex, IN.texcoord);
c.rgb *= c.a;
return c;
}
ENDCG
}
}
Answer by tanoshimi · Jul 10, 2016 at 09:05 AM
No, it's not possible to do in a single pass, unless you were to dynamically bake the outline into the sprite texture on the CPU, but that's almost certain to be slower.
Hm idk because I render up to thousands of this objects with this shade and end up with more than 4000 batches ... is there a way to bake the outline at the begin of the game in the texture and use it after that ?