DX12PixelShader.hlsl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /////////////
  2. // GLOBALS //
  3. /////////////
  4. Texture2D shaderTexture : register( t0 );
  5. SamplerState SampleType : register( s0 );
  6. // Matrizen für die einzelnen Knochen des Modells
  7. struct MatrixBuffer
  8. {
  9. matrix knochenMatrix[ 128 ];
  10. };
  11. // The projection and view matrix
  12. struct KameraBuffer2
  13. {
  14. matrix view;
  15. matrix projection;
  16. };
  17. // The position of the kamera
  18. struct KameraBuffer
  19. {
  20. float4 kPosition;
  21. };
  22. // these values should sum up to 1
  23. struct Material
  24. {
  25. float ambientFactor;
  26. float diffusFactor;
  27. float specularFactor;
  28. };
  29. struct LightCount
  30. {
  31. int diffuseLightCount;
  32. int pointLightCount;
  33. };
  34. ConstantBuffer<KameraBuffer2> Kamera2 : register( b0 );
  35. ConstantBuffer<MatrixBuffer> Skelett : register( b1 );
  36. ConstantBuffer<KameraBuffer> Kamera : register( b2 );
  37. ConstantBuffer<Material> Object : register( b3 );
  38. ConstantBuffer<LightCount> Light : register( b4 );
  39. // lights
  40. struct DiffuseLight
  41. {
  42. float3 direction;
  43. float3 color;
  44. };
  45. struct PointLight
  46. {
  47. float3 position;
  48. float3 color;
  49. float radius;
  50. };
  51. //StructuredBuffer< DiffuseLight > difuseLights : register( t1 );
  52. //StructuredBuffer< PointLight > pointLights : register( t2 );
  53. //////////////
  54. // TYPEDEFS //
  55. //////////////
  56. struct PixelInputType
  57. {
  58. float4 worldPos : POSITION;
  59. float4 position : SV_POSITION;
  60. float2 tex : TEXCOORD;
  61. float3 normal : NORMAL;
  62. };
  63. ////////////////////////////////////////////////////////////////////////////////
  64. // Pixel Shader
  65. ////////////////////////////////////////////////////////////////////////////////
  66. float4 main( PixelInputType input ) : SV_TARGET
  67. {
  68. float3 diffuseLight = float3( 0, 0, 0 );
  69. float3 specularLight = float3( 0, 0, 0 );
  70. /* for( int j = 0; j < Light.diffuseLightCount; j++ )
  71. {
  72. if( dot( input.normal, -difuseLights[ j ].direction ) < 0 )
  73. continue;
  74. diffuseLight += difuseLights[ j ].color * dot( input.normal, -difuseLights[ j ].direction );
  75. }
  76. for( int i = 0; i < Light.pointLightCount; i++ )
  77. {
  78. float3 lightDir = pointLights[ i ].position - input.worldPos.xyz;
  79. float factor = pointLights[ i ].radius / length( lightDir );
  80. float f = dot( input.normal, normalize( lightDir ) );
  81. if( f > 0 )
  82. {
  83. diffuseLight += pointLights[ i ].color * f * factor;
  84. f = dot( normalize( reflect( normalize( -lightDir ), input.normal ) ), normalize( Kamera.kPosition.xyz - input.worldPos.xyz ) );
  85. if( f > 0 )
  86. specularLight += pointLights[ i ].color * f * factor;
  87. }
  88. }*/
  89. float4 materialColor = shaderTexture.Sample( SampleType, input.tex );
  90. 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 );
  91. textureColor.a = materialColor.a;
  92. return textureColor;
  93. }