DeferredPointLight.fx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "Macros.fxh"
  2. float4x4 World;
  3. float4x4 View;
  4. float4x4 Projection;
  5. //color of the light
  6. float3 Color;
  7. //position of the camera, for specular light
  8. float3 cameraPosition;
  9. //this is used to compute the world-position
  10. float4x4 InvertViewProjection;
  11. //this is the position of the light
  12. float3 lightPosition;
  13. //how far does this light reach
  14. float lightRadius;
  15. //control the brightness of the light
  16. float lightIntensity = 1.0f;
  17. // diffuse color, and specularIntensity in the alpha channel
  18. texture colorMap;
  19. // normals, and specularPower in the alpha channel
  20. texture normalMap;
  21. //depth
  22. texture depthMap;
  23. sampler colorSampler = sampler_state
  24. {
  25. Texture = (colorMap);
  26. AddressU = CLAMP;
  27. AddressV = CLAMP;
  28. MagFilter = LINEAR;
  29. MinFilter = LINEAR;
  30. Mipfilter = LINEAR;
  31. };
  32. sampler depthSampler = sampler_state
  33. {
  34. Texture = (depthMap);
  35. AddressU = CLAMP;
  36. AddressV = CLAMP;
  37. MagFilter = POINT;
  38. MinFilter = POINT;
  39. Mipfilter = POINT;
  40. };
  41. sampler normalSampler = sampler_state
  42. {
  43. Texture = (normalMap);
  44. AddressU = CLAMP;
  45. AddressV = CLAMP;
  46. MagFilter = POINT;
  47. MinFilter = POINT;
  48. Mipfilter = POINT;
  49. };
  50. struct VertexShaderInput
  51. {
  52. float3 Position : POSITION0;
  53. };
  54. struct VertexShaderOutput
  55. {
  56. float4 Position : POSITION0;
  57. float4 ScreenPosition : TEXCOORD0;
  58. };
  59. VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
  60. {
  61. VertexShaderOutput output;
  62. //processing geometry coordinates
  63. float4 worldPosition = mul(float4(input.Position,1), World);
  64. float4 viewPosition = mul(worldPosition, View);
  65. output.Position = mul(viewPosition, Projection);
  66. output.ScreenPosition = output.Position;
  67. return output;
  68. }
  69. float2 halfPixel;
  70. float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
  71. {
  72. //obtain screen position
  73. input.ScreenPosition.xy /= input.ScreenPosition.w;
  74. //obtain textureCoordinates corresponding to the current pixel
  75. //the screen coordinates are in [-1,1]*[1,-1]
  76. //the texture coordinates need to be in [0,1]*[0,1]
  77. float2 texCoord = 0.5f * (float2(input.ScreenPosition.x,-input.ScreenPosition.y) + 1);
  78. //allign texels to pixels
  79. texCoord -=halfPixel;
  80. //get normal data from the normalMap
  81. float4 normalData = tex2D(normalSampler,texCoord);
  82. //tranform normal back into [-1,1] range
  83. float3 normal = 2.0f * normalData.xyz - 1.0f;
  84. //get specular power
  85. float specularPower = normalData.a * 255;
  86. //get specular intensity from the colorMap
  87. float specularIntensity = tex2D(colorSampler, texCoord).a;
  88. //read depth
  89. float depthVal = tex2D(depthSampler,texCoord).r;
  90. //compute screen-space position
  91. float4 position;
  92. position.xy = input.ScreenPosition.xy;
  93. position.z = depthVal;
  94. position.w = 1.0f;
  95. //transform to world space
  96. position = mul(position, InvertViewProjection);
  97. position /= position.w;
  98. //surface-to-light vector
  99. float3 lightVector = lightPosition - position;
  100. //compute attenuation based on distance - linear attenuation
  101. float attenuation = saturate(1.0f - length(lightVector)/lightRadius);
  102. //normalize light vector
  103. lightVector = normalize(lightVector);
  104. //compute diffuse light
  105. float NdL = max(0,dot(normal, lightVector)+0.2);
  106. float3 diffuseLight = NdL * Color.rgb;
  107. //float3 diffuseLight = Color.rgb;
  108. //if(NdL==0) return float4(0,0,0,0);
  109. //reflection vector
  110. float3 reflectionVector = normalize(reflect(-lightVector, normal));
  111. //camera-to-surface vector
  112. float3 directionToCamera = normalize(cameraPosition - position);
  113. //compute specular light
  114. float specularLight = specularIntensity * pow( saturate(dot(reflectionVector, directionToCamera)), specularPower);
  115. //take into account attenuation and lightIntensity.
  116. return attenuation * lightIntensity * float4(diffuseLight.rgb, specularLight);
  117. }
  118. TECHNIQUE( Standard, VertexShaderFunction, PixelShaderFunction );