Hello folks,
Due to performance reasons on older iPads, certain changes in our scenes to improve performance included switching the rendering path over to vertex lit. Overall this seems to have provided the boost we needed, however it meant that one of our shaders no longer works.
We have a shader that uses a texture with a gradient, and floating point cuttoff value, the idea being that the gradient combined with the cuttoff value create a mask for the underlying texture. This was done in a surface shader before. but I’m trying now to convert it over to a fragment and vertex shader to work with the vertex lit rendering path…I’m not succeeding.
Here is the original code from the surface shader.
Shader "Custom/TimerSurface"
{
Properties
{
_Colour ("Color", Color) = (0.0, 0.0, 0.0)
_GradientMask ("Gradient", 2D) = "black" {}
_MainTex ("Main Texutre", 2D) = "gray" {}
_Cutoff ("Cutoff Value", Range(0.0,1.0)) = 0.0
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha
sampler2D _MainTex;
sampler2D _GradientMask;
fixed4 _Colour;
fixed _Cutoff;
struct Input
{
half2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed alpha = tex2D(_MainTex, IN.uv_MainTex).a;
fixed grad = tex2D(_GradientMask, IN.uv_MainTex).r;
o.Albedo = _Colour;
if(grad >= _Cutoff && alpha > 0.95)
{
o.Alpha = alpha;
}
else
{
o.Alpha = 0;
}
}
ENDCG
}
FallBack "VertexLit"
}
And here was my attempt to convert it to a vert+frag shader.
Shader "Custom/Timer Vert+Frag"
{
Properties
{
_Colour ("Color", Color) = (0.0, 0.0, 0.0)
_GradientMask ("Gradient", 2D) = "black" {}
_MainTex ("Main Texture", 2D) = "gray" {}
_Cutoff ("Cutoff Value", Range(0.0,1.0)) = 0.0
}
SubShader
{
Tags {"Queue"="AlphaTest"}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _GradientMask;
fixed4 _Colour;
fixed _Cutoff;
float4 _MainTex_ST;
float4 _GradientMask_ST;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
return o;
}
half4 frag (v2f i) : COLOR
{
fixed alpha = tex2D(_MainTex, i.uv).a;
fixed grad = tex2D(_GradientMask, i.uv).r;
if(grad < _Cutoff || alpha < 0.95)
{
alpha = 0;
}
return half4(_Colour.r, _Colour.g, _Colour.b, alpha);
}
ENDCG
}
}
FallBack "VertexLit"
}
I am by no means a shader programmer, so I wouldn’t be surprised if there are a number of things I’m doing here that could be done better (if so I’d really appreciate you point them out + suggest how to change them).
Attached are the Gradient and main textures that I’m using. the material is then just applied to a simple Plane mesh.