DX11PixelShader.hlsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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;
  64. if (length(lightDir) < 1)
  65. factor = 1;
  66. else
  67. factor = pointLights[i].radius / length(lightDir);
  68. float f = dot( input.normal, normalize( lightDir ) );
  69. if( f > 0 )
  70. {
  71. diffuseLight += pointLights[ i ].color * f * factor;
  72. f = dot( normalize( reflect( normalize( -lightDir ), input.normal ) ), normalize( kPosition.xyz - input.worldPos.xyz ) );
  73. if( f > 0 )
  74. specularLight += pointLights[ i ].color * f * factor;
  75. }
  76. }
  77. //if (!(diffuseLight.x >= 0 && diffuseLight.x <= 1))
  78. // diffuseLight.x = 0;
  79. float4 materialColor = shaderTexture.Sample( SampleType, input.tex );
  80. float4 textureColor = saturate( (materialColor * ambientFactor) + (float4( diffuseLight.x, diffuseLight.y, diffuseLight.z, 0 ) * diffusFactor) + (float4( specularLight.x, specularLight.y, specularLight.z, 0 ) * specularFactor) );
  81. textureColor.a = materialColor.a;
  82. if(isnan(diffuseLight.x * diffusFactor))
  83. textureColor = materialColor;
  84. return textureColor;
  85. //return textureColor;
  86. //if (diffusFactor == 0)
  87. // return float4(1, 1, 0, 1);
  88. /*if (isnan(diffuseLight.x) || isnan(diffusFactor) || isinf(diffuseLight.x) || isinf(-diffuseLight.x))
  89. return float4(0, 1, 1, 1);
  90. if (isnan(diffuseLight.x - diffuseLight.x) && isnan(diffuseLight.x * diffusFactor) )
  91. return float4(1, 1, 1, 1);
  92. if ((diffuseLight.x * diffusFactor) != 0 && (diffuseLight.x * diffusFactor) != -0)
  93. return float4(0, 0, 1, 1);
  94. return float4(0, 1, 0, 1);*/
  95. }