- Home /
How can I add Normal mapping to a Terrain custom shader?
I'm not a shader coder but I got a shader I liked here, the shader gets the object world position an sets a color gradient according to the vertical positions. I'm using it on a terrain and I can paint the textures as usual but I'd like to be able to use normal mapping too, so I tried to code it usisng the manual reference for surface shaders but didn't succeed on that part, also tried taking a look in the built-in shaders but as I'm not a shader coder, I couldn't figure out what I really had to do.
Here's the shader:
Shader "Custom/HeightDependentTint"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_Normal ("Normal (A)", 2D) = "bump" {}
_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;
sampler2D _Normal;
fixed4 _ColorMin;
fixed4 _ColorMax;
float _HeightMin;
float _HeightMax;
struct Input
{
float2 uv_MainTex;
float2 uv_NormalMap;
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;
o.Normal = UnpackNormal (tex2D (_Normal, IN.uv_NormalMap));
}
ENDCG
}
Fallback "Diffuse"
}
Here's a picture of what I got: Here I have a nice brighter color on the top and gets darker in the bottom
terrain-gradient.jpg
(66.9 kB)
Comment