Persistent data values in shaders

Hey everyone! Here’s one for all you graphics wizards out there.

Currently diving into the world of shaders thanks to these amazing tutorials and I’m trying to figure out if there’s any way to store the changes made in a vertex function for use in future passes through the shader. For example:

Shader "Example/PersistentProperties" {
	Properties {
		_MainTex ("Texture", 2D) = "white" {}
		_FarColor ("UnlitColor", Color) = (0,0,0,1)
		_NearColor("LitColor", Color) = (1,1,1,1)
	}
	SubShader {
		Tags { "RenderType" = "Opaque" }
		CGPROGRAM
		#pragma surface surf Lambert vertex:vert

		float4 _ObjectPosition;
		sampler2D _MainTex;
		float4 _FarColor;
		float4 _NearColor;

		struct Input {
			float2 uv_MainTex;
			float4 vertColor;
		};

		void vert(inout appdata_full v, out Input o) {
			UNITY_INITIALIZE_OUTPUT(Input, o);
			float distanceVal = 1.0 + ((1.0 / 20.0)*(40.0 - distance(mul(unity_ObjectToWorld, v.vertex).rgb, _ObjectPosition.rgb)));
			
			// This is the value I don't want to reset!
			v.color.a += distanceVal;

			o.vertColor.a = v.color.a;
		}

		void surf (Input IN, inout SurfaceOutput o) {
			o.Albedo = lerp(_FarColor.rgb, _NearColor.rgb, IN.vertColor.a);
		}

		ENDCG
	} 
	Fallback "Diffuse"
}

Here I’ve made a surface shader that illuminates all the vertices around a target object, so the closer the object, the brighter its surroundings. But what I want really want, is for vertices to get brighter the closer this object gets but NOT darker as it moves away. To do that, the input vertex alpha would have to add on to the value from the previous iteration through the shader instead of starting from scratch every time.

So is there any way I can cache the existing vertex colors and manipulate them in the following pass? Or is there some method of permanently overwriting the appdata info from within the shader?

I’d previously accomplished this effect by modifying the mesh colors in a separate C# script, but I’m curious if it’s possible to calculate all this straight from the shader instead.

Thanks!

EDIT:
Here’s an example of what I currently have (pictured on the left) vs. what I’d like to accomplish (on the right):

Example

EDIT 2:
Added an alternate vertex & fragment version with a grab pass in the comment below.

Shaders do not persist values. However, there are plenty of examples of dynamic painting textures on meshes (which is what it looks like you’re trying to do), normally assigning vertex colours to the mesh which are then passed to the shader.

Try this, for example: Reddit - Dive into anything

This sample uses Rendertexture to keep the variables.