| 1 |
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 |
| 2 |
{ |
| 3 |
float4 output = 0; |
| 4 |
//get normal data from the normalMap |
| 5 |
float4 normalData = tex2D(normalSampler,input.TexCoord); |
| 6 |
//tranform normal back into [-1,1] range |
| 7 |
float3 normal = 2.0f * normalData.xyz - 1.0f; |
| 8 |
|
| 9 |
//get specular power, and get it into [0,255] range] |
| 10 |
float specularPower = normalData.a * 255; |
| 11 |
//get specular intensity from the colorMap |
| 12 |
float specularIntensity = tex2D(colorSampler, input.TexCoord).a; |
| 13 |
//read depth |
| 14 |
float depthVal = tex2D(depthSampler,input.TexCoord).r; |
| 15 |
|
| 16 |
//compute screen-space position |
| 17 |
float4 position; |
| 18 |
position.x = input.TexCoord.x * 2.0f - 1.0f; |
| 19 |
position.y = -(input.TexCoord.y * 2.0f - 1.0f); |
| 20 |
position.z = depthVal; |
| 21 |
position.w = 1.0f; |
| 22 |
|
| 23 |
//transform to world space |
| 24 |
position = mul(position, InvertViewProjection); |
| 25 |
position /= position.w; |
| 26 |
//////////////////////////// |
| 27 |
//surface-to-light vector |
| 28 |
float3 lightVector = -normalize(lightDirection); |
| 29 |
|
| 30 |
//compute diffuse light |
| 31 |
float NdL = max(0,dot(normal,lightVector)); |
| 32 |
float3 diffuseLight = Color.rgb * NdL; |
| 33 |
|
| 34 |
//reflexion vector |
| 35 |
float3 reflectionVector = normalize(reflect(lightVector, |
| 36 |
normal)); |
| 37 |
|
| 38 |
//camera-to-surface vector |
| 39 |
float3 directionToCamera = normalize(cameraPosition - |
| 40 |
position); |
| 41 |
//compute specular light |
| 42 |
float specularLight = specularIntensity * pow( saturate(dot(reflectionVector, |
| 43 |
directionToCamera)), specularPower); |
| 44 |
|
| 45 |
//----------------SpotLight----------------- |
| 46 |
lightVector = lightPosition - position; |
| 47 |
float attenuation = saturate(1.0f - length(lightVector)/lightRadius); |
| 48 |
|
| 49 |
//normalize light vector |
| 50 |
lightVector = normalize(lightVector); |
| 51 |
|
| 52 |
|
| 53 |
//SpotDotLight = cosine of the angle between spotdirection and lightvector |
| 54 |
|
| 55 |
lightDirection = normalize(lightDirection); |
| 56 |
float SdL = dot(lightDirection,-lightVector); |
| 57 |
|
| 58 |
output = (0,0,0,specularLight); |
| 59 |
if (SdL > spotLightAngleCosine) |
| 60 |
{ |
| 61 |
float spotIntensity = pow(SdL,spotDecayExponent); |
| 62 |
attenuation *= spotIntensity; |
| 63 |
output.rgb = diffuseLight * attenuation * lightIntensity; |
| 64 |
//multiply the attenuation by spotIntensity before applying it to the light |
| 65 |
} |
| 66 |
//----------------------------------------- |
| 67 |
return output; |
| 68 |
} |
| 69 |
|