Fading Normal Map

Right, we have a shader that we are using to create a “spreading grass” effect. using a tilling texture for the diffuse, a texture which is animated (grayscale) this controls if the surface shows the plain white colour, or the texture, creating the “growth”.

Everything works fine for it except for the normal maps. much like the texture, I only want the normal map to have an effect as the grass grows over the area. (I figured do a lerp between whatever the normal would be if I wasn’t setting it, with the value from the bump map, controlled by the grayscale value).

However I can’t seem to work out how to get the normal value into my surface shader (that is the default normal value that would be used if I didn’t set it).

I originally tried just using float3 worldNormal in my input. (but then if I try to use this I get compile errors, saying “ERROR: 0:428: ‘TtoW0’ : no such field in structure”
Then i saw a post that said you needed to use INTERNAL_DATA and do the following

o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_MainTex));
float3 worldNormal = WorldNormalVector (IN, o.Normal);

This also doesn’t give me back the “unedited” normal of the surface.

I’m sure its just me missing a step, or misunderstanding what I’ve read, but if anyone could explain it, or provide some sample code I’d really appreciate it.

If you want to do reflections that are affected by normal maps, it needs to be slightly more involved: INTERNAL_DATA needs to be added to the Input structure, and WorldReflectionVector function used to compute per-pixel reflection vector after you’ve written the Normal output. (link to)

for just normal fade in/out you need:

  • read normal vector
  • lerp it with unbumped normal (0,0,1)
  • normalize it

so finally your line should look like:

o.Normal = normalize(lerp(UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex)), float3(0,0,1), [lerp_value]));