Light.ps 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. Texture2D shaderTexture;
  2. SamplerState SampleType;
  3. cbuffer LightBuffer
  4. {
  5. float4 ambientColor;
  6. float4 diffuseColor;
  7. float3 lightDirection;
  8. float specularPower;
  9. float4 specularColor;
  10. };
  11. struct PixelInputType
  12. {
  13. float4 position : SV_POSITION;
  14. float2 tex : TEXCOORD0;
  15. float3 normal : NORMAL;
  16. float3 viewDirection : TEXCOORD1;
  17. };
  18. float4 LightPixelShader(PixelInputType input) : SV_TARGET
  19. {
  20. float4 textureColor;
  21. float3 lightDir;
  22. float lightIntensity;
  23. float4 color;
  24. float3 reflection;
  25. float4 specular;
  26. // Sample the pixel color from the texture using the sampler at this texture coordinate
  27. textureColor = shaderTexture.Sample(SampleType, input.tex);
  28. // Set the default output color to the ambient light value
  29. color = ambientColor;
  30. // Init the specular color
  31. specular = float4(0.0f, 0.0f, 0.0f, 0.0f);
  32. // Invert the light direction for calculations
  33. lightDir = -lightDirection;
  34. // Calculate the amount of light on this pixel
  35. lightIntensity = saturate(dot(input.normal, lightDir));
  36. if (lightIntensity > 0.0f)
  37. {
  38. color += diffuseColor * lightIntensity;
  39. // Saturate the ambient and diffuse color
  40. color = saturate(color);
  41. // Calculate the reflection vector
  42. reflection = normalize(2 * lightIntensity * input.normal - lightDir);
  43. // Determine the amount of specular light based on the reflection vector, viewing direction, and specular power
  44. specular = pow (saturate(dot(reflection, input.viewDirection)), specularPower);
  45. }
  46. // Multiply the texture pixel and the final diffuse color to get the final pixel result
  47. color = color * textureColor;
  48. // Add the specular component last to the output color
  49. color = saturate(color + specular);
  50. return color;
  51. }