Does anyone know how to cycle colors from within shader code?

Can anyone help me write a simple shader that would allow be to specify a “from” and “to” color and then have the shader code transition between the two values in a ping pong loop?

I know that we can use the Color.Lerp function to change the material color from within Unity script but I think it may be better, in some cases, if the begin and end color can be set as shader properties to off load that work to the GPU.

I think this would be a plus for not having to write code in Coroutines or Updates to compute the Color values manually each frame – especially when there are multiple different concurrent materials that have different color cycling values.


Shader “Custom/CycleColor” {
Properties {
_Color (“Color1”, Color) = ( 1.0, 0.0, 0.0, 1.0 )
_Color2 (“Color2”, Color) = ( 0.0, 0.0, 1.0, 1.0 )
_Speed (“Speed”, float) = 0.2
SubShader {
Pass {
#pragma vertex vert
#pragma fragment frag

            #include "UnityCG.cginc"
            uniform fixed4 _Color;
            uniform fixed4 _Color2;
            uniform float  _Speed;

            struct vertOut {
                float4 pos:SV_POSITION;
                fixed4 color;

            vertOut vert(appdata_base v) {
                vertOut o;
                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                o.color = lerp(_Color, _Color2, abs(fmod(_Time.a * _Speed, 2.0) - 1.0));
                return o; 

            fixed4 frag(vertOut i) : COLOR0 {
                   return i.color;