How does this shader calculation result in different colors?

I’m reading “Unity Shaders and Effects Cookbook” by Kenny Lammers, and I’m already puzzled on one of the very first examples which uses Properties. Given that _EmissiveColor, _AmbientColor and _MySliderValue are properties set in the Unity Editor, I don’t understand how this calculation:

	void surf (Input IN, inout SurfaceOutput o) {
		fixed4 c = pow((_EmissiveColor + _AmbientColor), _MySliderValue);
		o.Albedo = c.rgb;
		o.Alpha = c.a;
	}

… gives an object whose coloration varies depending on lighting/shading. Since the inputs to that pow(...) calculation are all constant values, how can the resulting visible color change from pixel to pixel? Wouldn’t the pixel or its position have to one of the inputs?

I haven’t read the book, so I’m surprised this isn’t explained somewhere, but you’re creating a surface shader. This is a kind of shader template/helper/wizard, specific to Unity, which automates a lot of the tedious, complicated stuff about lighting, shadows etc. that you’d normally have to write by hand.

A bit earlier on in your code, you probably have a line that looks a bit like this:

#pragma surface surf Lambert

The “Lambert” specified here tells Unity which lighting model you want to use, and when it compiles your surface shader into “standard” vert/frag shaders it generates the light/shadow code automatically for you based on that model. So this will vary the fragment colour based on the direction, intensity, and colour of incoming lightsources, lightmaps etc. (The Lambert model comes with Unity and is defined in Lighting.cginc, but you can define any custom lighting model you want). More examples at Unity - Manual: Surface Shader lighting examples

Although, in some senses, surface shaders are easier to use for beginning writing shaders, the fact that they abstract away some of what’s going on behind the scenes can make it harder to understand what’s really going on. I found this an excellent starter guide to learning “standard” (i.e. vert/frag) shaders in Unity: Cg Programming/Unity - Wikibooks, open books for an open world