# What is Ramp Shading or Lighting?

What is ramp shading or lighting and how does it work? Is it different than toon shading or is it the same concept? How is specularity calculated differently for ramp shading versus blinn-phong or lambert?

-

In traditional Blinn/Phong shading, you calculate the diffuse term for a pixel by measuring the cosine of the angle between the normal at the surface and the direction of the light. So, in this case, a simple shader would look something like this:

``````float DiffuseCoeff(in float3 pos, in float3 normal, in float3 lightPos) {
float3 lightDir = lightPos - pos;
lightDir.normalize();

return max(0.0, Dot(lightDir, normal));
}
``````

In ramp shading, the idea is to modulate the diffuse coefficient by a 1D lookup texture. Depending on what you put in this texture you can generate different effects. Hence, our shader function to compute the diffuse coefficient becomes:

``````texture1D rampTex;
float DiffuseCoeff(in float3 pos, in float3 normal, in float3 lightPos) {
float3 lightDir = lightPos - pos;
lightDir.normalize();

// Map value from [-1, 1] to [0, 1]
float rampCoord = Dot(lightDir, normal) * 0.5 + 0.5;
return tex1D(rampTex, rampCoord);
}
``````

For example, if we wanted to implement a cell shader, what we could do is generate a ramp texture that emulates a step function:

You can use the same idea to generate different effects for the specular lighting as well. "Ramp" shading really only refers to the idea of modulating a shading value by a texture.

EDIT: Team Fortress 2 uses a ramp shader in their rendering. You can check out the details in this paper:

http://www.valvesoftware.com/publications/2007/NPAR07_IllustrativeRenderingInTeamFortress2.pdf

Look under Section 5, at the subsection entitled "Diffuse Warping Function"

-
Great answer! The explanation of the difference for lighting between diffuse and ramp lighting is exactlywhat I was looking for. I also appreciate the comment about what the concept of ramp shading is in general. As an extra aside, could you possibly post an example image from a game that uses this concept? – StampyTurtle Mar 15 '13 at 11:45
@Stampyturtle I've updated my answer with an example. – Mokosha Mar 15 '13 at 15:35