DX11PixelShader.hlsl 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /////////////
  2. // GLOBALS //
  3. /////////////
  4. Texture2D shaderTexture : register( t0 );
  5. SamplerState SampleType;
  6. // The position of the kamera
  7. cbuffer Kamera : register( b0 )
  8. {
  9. float4 kPosition;
  10. }
  11. // these values should sum up to 1
  12. cbuffer Material : register( b1 )
  13. {
  14. float ambientFactor;
  15. float diffusFactor;
  16. float specularFactor;
  17. };
  18. cbuffer LightCount : register( b2 )
  19. {
  20. int diffuseLightCount;
  21. int pointLightCount;
  22. }
  23. // lights
  24. struct DiffuseLight
  25. {
  26. float3 direction;
  27. float3 color;
  28. };
  29. struct PointLight
  30. {
  31. float3 position;
  32. float3 color;
  33. float radius;
  34. };
  35. StructuredBuffer< DiffuseLight > difuseLights : register( t1 );
  36. StructuredBuffer< PointLight > pointLights : register( t2 );
  37. //////////////
  38. // TYPEDEFS //
  39. //////////////
  40. struct PixelInputType
  41. {
  42. float4 worldPos : POSITION;
  43. float4 position : SV_POSITION;
  44. float2 tex : TEXCOORD0;
  45. float3 normal : TEXCOORD1;
  46. };
  47. ////////////////////////////////////////////////////////////////////////////////
  48. // Pixel Shader
  49. ////////////////////////////////////////////////////////////////////////////////
  50. float4 TexturePixelShader( PixelInputType input ) : SV_TARGET
  51. {
  52. float3 diffuseLight = float3( 0, 0, 0 );
  53. float3 specularLight = float3( 0, 0, 0 );
  54. for( int j = 0; j < diffuseLightCount; j++ )
  55. {
  56. if( dot( input.normal, -difuseLights[ j ].direction ) < 0 )
  57. continue;
  58. diffuseLight += difuseLights[ j ].color * dot( input.normal, -difuseLights[ j ].direction );
  59. }
  60. for( int i = 0; i < pointLightCount; i++ )
  61. {
  62. float3 lightDir = pointLights[ i ].position - input.worldPos.xyz;
  63. float factor = pointLights[ i ].radius / length( lightDir );
  64. float f = dot( input.normal, normalize( lightDir ) );
  65. if( f > 0 )
  66. {
  67. diffuseLight += pointLights[ i ].color * f * factor;
  68. f = dot( normalize( reflect( normalize( -lightDir ), input.normal ) ), normalize( kPosition.xyz - input.worldPos.xyz ) );
  69. if( f > 0 )
  70. specularLight += pointLights[ i ].color * f * factor;
  71. }
  72. }
  73. float4 materialColor = shaderTexture.Sample( SampleType, input.tex );
  74. float4 textureColor = saturate( materialColor * ambientFactor + float4( diffuseLight.x, diffuseLight.y, diffuseLight.z, 0 ) * diffusFactor + float4( specularLight.x, specularLight.y, specularLight.z, 0 ) * specularFactor );
  75. textureColor.a = materialColor.a;
  76. return textureColor;
  77. }