Terrain shader normals

I’m attempting to modify the lightmapped terrain shader to add a specular component (using the splat textures’ alpha channel as a glossiness value, to boot).

However, any time I attempt to use the vertex normal, I get the following error message:

Shader wants normals, but the mesh doesn't have them.

I feel like I must be missing something here, because both the pixel-lit and vertex-lit terrain shaders do use the normal as part of their calculations. Is there something special I need to set to make Unity calculate those normals for a lightmapped terrain?

Here’s the relevant pieces of shader code, in case they matter:

		Pass {
			Tags { "LightMode" = "Pixel" }
			CGPROGRAM
			#pragma vertex SpecularSplatVertex
			#pragma fragment SpecularSplatFragment
			#pragma fragmentoption ARB_fog_exp2
			#pragma fragmentoption ARB_precision_hint_fastest
			#define TEXTURECOUNT 4

			#include "splatting.cginc"			
			ENDCG
		}
struct v2f_specular {
	V2F_POS_FOG;
	float4 uv[3] : TEXCOORD0;
	float4 color : COLOR;
	float3 normal;
	float3 viewDir;
	float3 lightDir;
};

float4 SpecularSplatFragment (v2f_specular i) : COLOR {
	half4 splat;
	SAMPLE_SPLAT(i, splat);
	
	float4 spec = SpecularLight(normalize(i.lightDir), normalize(i.viewDir), i.normal, splat, splat.a, 1);
	return spec;
}

v2f_specular SpecularSplatVertex (appdata_base v) {
	v2f_specular o;
	PositionFog( v.vertex, o.pos, o.fog);
	CALC_SPLAT_UV(v.texcoord.xy);
	o.normal = v.normal;
	o.viewDir = ObjSpaceViewDir(v.vertex);
	o.lightDir = ObjSpaceLightDir(v.vertex);
	o.color = _ModelLightColor0;
	
	return o;
}

I don’t know but my guess right away is that the terrain engine doesn’t bother to output normals when terrain is lightmapped and there is nothing you can really do about it.

Maybe you should try to change the shaders used for the pixel lit terrain instead?

I don’t really know though, good luck.

Forest is correct; when using lightmapped shaders we just skip normals.

However, I want to stress that modifying terrain shaders is unsupported. They can break anytime in the future.

Thanks, guys - that’s about what I feared.

Don’t worry, Aras, I know this is a very “at-our-own-risk” thing to do.

I know this is a dead topic but I also want to apply specular/normal map to the terrain. So I was wondering if you made any progress on this? Did you actually manage to apply specular on the terrain or was it simply not possible?

I hope to hear from you although it’s a while back;)

-Zylex

No, I’m pretty sure it’s not possible (at least, for reasonable values of “possible”) with the lightmapped terrain shader. Using a custom shader that does so with dynamic lights may be doable, if you’re willing to assume the risks Aras mentioned above.