- Home /
Vertex Lit Shaders
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.
[1]: /storage/temp/3423-circle_timer.png
It's a step forward, but it does raise other issues (perhaps I should have mentioned this in the original post, I didn't realise it would cause an issue)
EDIT: This additional depth buffer issue has been fixed, For those curious I just replaced the Alpha = 0; line with a discard command.
also mike if you want to post that comment as an answer I can mark it as correct.
Answer by push_Mike · Sep 12, 2012 at 01:20 PM
Everything you wrote is correct, Just add the Blend Command in the Top of Tags
Blend SrcAlpha OneMinusSrcAlpha
It Will Work
Thanks
Your answer
Follow this Question
Related Questions
Using Color.Lerp with Lightweight Render Pipeline 1 Answer
Layer Dependent Reflections 1 Answer
how to do a simple multi-pass rendering? 0 Answers
Forward Rendering for Terrain, overriding the shader? 0 Answers
How to remove URP from the project 0 Answers