History: CelShading.cg
Source of version: 1
- «
- »
Copy to clipboard
{CODE(wrap="1", colors="c++")}//
// The maximum number of lights per-object. Decrease this value if you need some extra performance.
//
// If you change this, then you must enter 'CelShading.program' and change the '$numLights'
// variable to match this definition.
#define NUM_LIGHTS 8
float2 calculateSurfaceLighting(
float3 pos,
float3 norm,
float4 lightPos,
float4 lightAtten,
sampler1D diffuseRamp,
float3 eyePos,
float shininess,
sampler1D specRamp )
{
norm = normalize(norm);
//
// Calculate light vector and distance
float3 lightVec = 0;
if (lightPos.w == 1)
lightVec = lightPos.xyz - pos;
else
lightVec = lightPos.xyz;
float lightDist = length(lightVec);
lightVec = normalize(lightVec);
//
// Calculate the direction in-between the light direction and the camera's direction.
float3 eyeVec = normalize(eyePos - pos);
float3 halfDir = normalize(lightVec + eyeVec);
//
// Calculate luminosity based on light attenuation
float luminosity = 0;
if( lightAtten.x > lightDist)
luminosity = 1.0 /
( lightAtten.y + lightAtten.z*lightDist +
lightAtten.w*(lightDist*lightDist) );
//
// Get diffuse component based on the dot product of the normal and the light direction,
// multiplied by the luminosity component.
float diffComponent = max(dot(norm, lightVec), 0)*luminosity;
//
// Get specular component based on the dot product of the normal and the half-camera-light
// direction, multiplied by the luminosity component.
float specComponent = pow(max(dot(norm, halfDir), 0), shininess)*luminosity;
float diffuse = tex1D(diffuseRamp, diffComponent).r;
float specular = tex1D(specRamp, specComponent).r;
return float2(diffuse, specular);
}
//
// Vertex program
float4 main_vp(
float4 iPos : POSITION,
float3 iNorm : NORMAL,
float2 iUV : TEXCOORD0,
out float3 oPos : TEXCOORD0,
out float3 oNorm : TEXCOORD1,
out float2 oUV : TEXCOORD3,
uniform float4x4 worldViewProj ) : POSITION
{
oPos = iPos.xyz;
oNorm = iNorm;
oUV = iUV;
return mul(worldViewProj, iPos);
}
//
// Color fragment program
float4 main_fp(
float3 iPos : TEXCOORD0,
float3 iNorm : TEXCOORD1,
uniform float3 eyePosition,
uniform float4 ambientColor,
uniform float4 diffuseColor,
uniform float4 specularColor,
uniform float4 emissiveColor,
uniform float shininess,
uniform float3 ambientLight,
// Light params
uniform float4 lightDiffuse[NUM_LIGHTS],
uniform float4 lightSpecular[NUM_LIGHTS],
uniform float4 lightPosition[NUM_LIGHTS],
uniform float4 lightAttenuation[NUM_LIGHTS],
// Texture params
uniform sampler1D diffuseRamp : register(s0),
uniform sampler1D specRamp : register(s1) ) : COLOR
{
float4 surfaceColor = float4(ambientLight,1)*ambientColor;
for (char i = 0; i < NUM_LIGHTS; i++)
{
if ((length(lightDiffuse[i]) > 0 && length(diffuseColor) > 0)
|| (length(lightSpecular[i]) > 0 && length(specularColor) > 0))
{
float2 surfaceLight = calculateSurfaceLighting(
iPos, iNorm,
lightPosition[i], lightAttenuation[i], diffuseRamp,
eyePosition, shininess, specRamp);
surfaceColor += surfaceLight.x*diffuseColor*lightDiffuse[i]
+ surfaceLight.y*lightSpecular[i]*specularColor;
}
}
return saturate(emissiveColor + surfaceColor);
}
//
// Decal fragment program
float4 mainDecal_fp(
float3 iPos : TEXCOORD0,
float3 iNorm : TEXCOORD1,
float3 iUV : TEXCOORD3,
uniform float3 eyePosition,
uniform float4 ambientColor,
uniform float4 diffuseColor,
uniform float4 specularColor,
uniform float4 emissiveColor,
uniform float shininess,
uniform float3 ambientLight,
// Light params
uniform float4 lightDiffuse[NUM_LIGHTS],
uniform float4 lightSpecular[NUM_LIGHTS],
uniform float4 lightPosition[NUM_LIGHTS],
uniform float4 lightAttenuation[NUM_LIGHTS],
// Texture params
uniform sampler1D diffuseRamp : register(s0),
uniform sampler1D specRamp : register(s1),
uniform sampler2D decalTex : register(s2) ) : COLOR
{
float4 surfaceColor = float4(ambientLight,1)*ambientColor;
float4 specularSurface = 0;
float4 texColor = tex2D(decalTex, iUV);
for (char i = 0; i < NUM_LIGHTS; i++)
{
if ((length(lightDiffuse[i]) > 0 && length(diffuseColor) > 0 && length(texColor) > 0)
|| (length(lightSpecular[i]) > 0 && length(specularColor) > 0))
{
float2 surfaceLight = calculateSurfaceLighting(
iPos, iNorm,
lightPosition[i], lightAttenuation[i], diffuseRamp,
eyePosition, shininess, specRamp);
surfaceColor += surfaceLight.x*diffuseColor*lightDiffuse[i];
specularSurface += surfaceLight.y*lightSpecular[i]*specularColor;
}
}
return texColor*saturate(emissiveColor + surfaceColor) + specularSurface;
}
//
// Decal + Specular Mapping fragment program
float4 mainDecalSpec_fp(
float3 iPos : TEXCOORD0,
float3 iNorm : TEXCOORD1,
float3 iUV : TEXCOORD3,
uniform float3 eyePosition,
uniform float4 ambientColor,
uniform float4 diffuseColor,
uniform float4 specularColor,
uniform float4 emissiveColor,
uniform float shininess,
uniform float3 ambientLight,
// Light params
uniform float4 lightDiffuse[NUM_LIGHTS],
uniform float4 lightSpecular[NUM_LIGHTS],
uniform float4 lightPosition[NUM_LIGHTS],
uniform float4 lightAttenuation[NUM_LIGHTS],
// Texture params
uniform sampler1D diffuseRamp : register(s0),
uniform sampler1D specRamp : register(s1),
uniform sampler2D decalTex : register(s2),
uniform sampler2D specMap : register(s3)) : COLOR
{
float4 surfaceColor = float4(ambientLight,1)*ambientColor;
float4 specularSurface = 0;
float4 texColor = tex2D(decalTex, iUV);
float4 specMapColor = tex2D(specMap, iUV);
for (char i = 0; i < NUM_LIGHTS; i++)
{
if ((length(lightDiffuse[i]) > 0 && length(diffuseColor) > 0 && length(texColor) > 0)
|| (length(lightSpecular[i]) > 0 && length(specularColor) > 0 && length(specMapColor) > 0))
{
float2 surfaceLight = calculateSurfaceLighting(
iPos, iNorm,
lightPosition[i], lightAttenuation[i], diffuseRamp,
eyePosition, shininess, specRamp);
surfaceColor += surfaceLight.x*diffuseColor*lightDiffuse[i];
specularSurface += surfaceLight.y*lightSpecular[i]*specularColor;
}
}
return texColor*saturate(emissiveColor + surfaceColor) + specMapColor*saturate(specularSurface);
}{CODE}