real time vertex shading / color or transparency by vertex height

I have a plane that has many vertices. I have been playing around with a script that deforms the plane by moving the verts up and down (terrain) on run time to deform the plane.

I was wondering if it’s possible to change the assigned material’s color or transparency by each vertice by its height in run time? With the plane’s Y 0 being neutral and transparent or particle/multiply. Then a vertices deviance would add +3 = white -3 = black etc. (imagine snowy peaks and dark troughs.)

Really not good with shaders… any clues welcomed.

I made this shader for practice.

Shader "Custom/HeightShader" {
	Properties {
		_MainTex ("Main Texture", 2D) = "white" {}
		_CenterHeight ("Center Height", Float) = 0.0
		_MaxVariance ("Maximum Variance", Float) = 3.0
		_HighColor ("High Color", Color) = (1.0, 1.0, 1.0, 1.0)
		_LowColor ("Low Color", Color) = (0.0, 0.0, 0.0, 1.0)
	SubShader {
			Tags { "RenderType"="Opaque" }
			Cull Off
			#pragma surface surf Lambert vertex:vert
			#include <UnityCG.cginc>
			float _CenterHeight;
			float _MaxVariance;
			float4 _HighColor;
			float4 _LowColor;
			sampler2D _MainTex;
			struct Input{
				float2 uv_MainTex;
				float4 color : COLOR;
			void vert(inout appdata_full v){
				// Convert to world position
				float4 worldPos = mul(_Object2World, v.vertex);
				float diff = worldPos.y - _CenterHeight;
				float cFactor = saturate(diff/(2*_MaxVariance) + 0.5);
				//lerp by factor
				v.color = lerp(_LowColor, _HighColor, cFactor);
			void surf(Input IN, inout SurfaceOutput o){
				o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb * IN.color;
	FallBack "Diffuse"

Here’s the result…


You can tune a lot of parameters including center height, variance, colors at extremes and also apply a general texture. This shader also interacts with lights.


Not sure how to do this with a shader, but you could probably do this with a simple gradient and some UV operations. (each vertex would get an arbitrary U coordinate, the V coordinate would be the height, normalized, so it is between 0 and 1)