- Home /
Shader based on vertex height
I'm really new to Unity, and I've been trying to make a shader that tints a terrain.
Said mesh with different colors depending on the vertex height locally inside the model itself , not on the global coordinates.
Any help please?
Answer by Cherno · Jan 21, 2015 at 06:18 PM
Try this one, at least it says something about worldpos.y in it...
Shader "Custom/HeightDependentTint"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_HeightMin ("Height Min", Float) = -1
_HeightMax ("Height Max", Float) = 1
_ColorMin ("Tint Color At Min", Color) = (0,0,0,1)
_ColorMax ("Tint Color At Max", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _ColorMin;
fixed4 _ColorMax;
float _HeightMin;
float _HeightMax;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
void surf (Input IN, inout SurfaceOutput o)
{
half4 c = tex2D (_MainTex, IN.uv_MainTex);
float h = (_HeightMax-IN.worldPos.y) / (_HeightMax-_HeightMin);
fixed4 tintColor = lerp(_ColorMax.rgba, _ColorMin.rgba, h);
o.Albedo = c.rgb * tintColor.rgb;
o.Alpha = c.a * tintColor.a;
}
ENDCG
}
Fallback "Diffuse"
}
Thanks for your answer! But, how can I make it work with local coordinates insted?
Oh I'm sorry, I misread that. I thought you wanted a world-height-based shader :( This might help you, as long as your object's axis is set up right:
Thank you very much for this sample, I found it very useful for the shader I am trying to create: http://answers.unity3d.com/questions/1223464/how-do-i-adjust-shader-tint-based-on-normalface-or.html
Answer by wahn-studios · Feb 21, 2016 at 10:03 AM
You can use this asset to so link text works with vertex colors and textures
Your answer
![](https://koobas.hobune.stream/wayback/20220613181102im_/https://answers.unity.com/themes/thub/images/avi.jpg)