DX12PixelShader.hlsl 3.2 KB

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