- Home /
Is it possible to adjust normal map strength at runtime?
I have an "old age" normal map for my characters which at full strength creates strong wrinkles on the character's skin. I would like to be able to control the strength of this normal map via some sort of slider, so that players can choose "how old" and hence how "wrinkly" their character is. I haven't been able to find a shader nor texture setting which allows for this to be done at runtime. Can this be done?
Answer by velipekka · Jan 07, 2013 at 02:25 PM
You need an custom shader for that one.
You can download the build-in shaders from here: http://unity3d.com/unity/download/archive
It's pretty easy to make your own variations from those.
Here is a simple example that has a slider between two normal maps:
Shader "Bumped Diffuse Lerp" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
// Slider to control fading between normalmaps
_BumpMapSlider ("BumpMap Slider", Range (0, 1)) = 0
// Second normal map
_BumpMap2 ("Normalmap 2", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
// Remember to add your properties in here also. Check ShaderLab References from manual for more info
float _BumpMapSlider;
sampler2D _BumpMap2;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
// Read values from _BumpMap and _BumpMap2
fixed3 normal1 = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
fixed3 normal2 = UnpackNormal(tex2D(_BumpMap2, IN.uv_BumpMap));
// Interpolater between them and set the result to the o.Normal
o.Normal = lerp(normal1, normal2, _BumpMapSlider);
}
ENDCG
}
FallBack "Diffuse"
}
You can change _BumpMapSlider value at runtime from script, or even with the animation editor. http://docs.unity3d.com/Documentation/ScriptReference/Material.SetFloat.html
With this shader it would be the following: renderer.material.SetFloat( "_BumpMapSlider", wrinklyness);
I hope this does the trick ;)
That looks like a great idea, I'll test it when I get the chance. Just curious though, I'm guessing the additional normal map would add an additional drawcall wouldn't it? Could there be a way to use a slider with only one normal map and adjust it's strength, similar to when doing this in the advanced texture settings?