Texture based on vertex height

Hi there,

I have been toying with shaders for a couple of weeks now and feel like i’m making no progress, every step forward gives 2 steps back.

I can put 2 textures in my shader without any issues, i can blend them together by doing:

half4 c = tex2D(_MainTex, IN.uv_MainTex);
half4 d = tex2D(_SecTex, IN.uv_SecTex);
o.Albedo = c.rgb * d.rgb;

But now i’d like to select the texture based on the vertex height. Any way to achieve this?

You just need to calculate a Lerp factor to blend the two textures together. You should define a blending area. For example everything below the height of 20 should take the first texture, above the second one. In addition to the 20 you define a blendAreaSize of 2.5 for example.

The Lerp value would be something like

L = clamp((pos.y- blend height - blendAreaSize*0.5) / blendAreaSize)

I’m writing on my tablet, so I can’t tell got the exact syntax, but this should do the trick.

L will be a value that is 0 for y values below 18.75 and 1 for everything above 21.25.

In between it would linearly blend from 0 to 1. Now you just need to use Lerp with your two color values