Phong shader

Does anyone know if there is a phong shader made? I can’t find anything.

This is a list of all buildin shaders.

http://unity3d.com/support/documentation/Components/Built-in%20Shader%20Guide.html

And this is the list of Wiki shaders

All built-in shaders with “Specular” in their name implement Phong-Blinn lighting model. Or are you looking for raw Phong model, without Blinn’s approximation for half-angle?

im looking for a raw phong model

Then you should take the source of one of built-in specular shaders; it contains a call to SpecularLight function that does actual lighting calculation. This function is defined in UnityCG.cginc file, and currently looks like this:

// Calculates Blinn-Phong (specular) lighting model
inline half4 SpecularLight( half3 lightDir, half3 viewDir, half3 normal, half4 color, float specK, half atten )
{
	#ifndef USING_DIRECTIONAL_LIGHT
	lightDir = normalize(lightDir);
	#endif
	viewDir = normalize(viewDir);
	half3 h = normalize( lightDir + viewDir );
	
	half diffuse = dot( normal, lightDir );
	
	float nh = saturate( dot( h, normal ) );
	float spec = pow( nh, specK ) * color.a;
	
	half4 c;
	c.rgb = (color.rgb * _ModelLightColor0.rgb * diffuse +
	    _SpecularLightColor0.rgb * spec) * (atten * 2);
	// specular passes by default put highlights to overbright
	c.a = _SpecularLightColor0.a * spec * atten; 
	return c;
}

You can see that it computes half-angle using Blinn’s formula. To get the original Phong’s model, you should replace that part with calculation of the real reflection vector.

…but why do you actually need Phong lighting? It does give slightly “more correct” results (if there can be such thing in graphics at all), but Blinn’s model is faster to calculate, and hence more widely used in realtime graphics.

2 Likes

Thanks for the reply. Just trying to see how it looks testing different styles for our players.

If you want tight control over the way light falls, try the cubic map lighting example.
http://unity3d.com/support/resources/unite-presentations/shading-and-shadows
Artists can easily control the look by changing the ramp texture.

note that it needs some fixing as you’ll see it’s camera dependant.

Hi two questions please.
1)Is there any documentation available on the .cginc files?
2)I could not find the code for specular shader.I downloaded the shaders from unity website and saw a couple of them.They involve calls to the specular shader using UsePass.