DX11CustomPixelShader.hlsl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. int effectCount;
  23. }
  24. // lights
  25. struct DiffuseLight
  26. {
  27. float3 direction;
  28. float3 color;
  29. };
  30. struct PointLight
  31. {
  32. float3 position;
  33. float3 color;
  34. float radius;
  35. };
  36. cbuffer TexturEffect : register(b3)
  37. {
  38. bool effectEnabled;
  39. float effectPercentage;
  40. };
  41. StructuredBuffer< DiffuseLight > difuseLights : register(t1);
  42. StructuredBuffer< PointLight > pointLights : register(t2);
  43. Texture2D additionalTexture : register(t3);
  44. //////////////
  45. // TYPEDEFS //
  46. //////////////
  47. struct PixelInputType
  48. {
  49. float4 worldPos : POSITION;
  50. float4 position : SV_POSITION;
  51. float2 tex : TEXCOORD0;
  52. float3 normal : TEXCOORD1;
  53. float4 light : TEXCOORD2;
  54. };
  55. ////////////////////////////////////////////////////////////////////////////////
  56. // Pixel Shader
  57. ////////////////////////////////////////////////////////////////////////////////
  58. float4 TexturePixelShader(PixelInputType input) : SV_TARGET
  59. {
  60. float3 diffuseLight = float3(0, 0, 0);
  61. float3 specularLight = float3(0, 0, 0);
  62. if (input.light.w > 0)
  63. {
  64. diffuseLight = input.light.xyz;
  65. }
  66. else
  67. {
  68. for (int j = 0; j < diffuseLightCount; j++)
  69. {
  70. if (dot(input.normal, -difuseLights[j].direction) < 0)
  71. continue;
  72. diffuseLight += difuseLights[j].color * dot(input.normal, -difuseLights[j].direction);
  73. }
  74. for (int i = 0; i < pointLightCount; i++)
  75. {
  76. float3 lightDir = pointLights[i].position - input.worldPos.xyz;
  77. float factor;
  78. if (length(lightDir) < 1)
  79. factor = 1;
  80. else
  81. factor = pointLights[i].radius / length(lightDir);
  82. float f = dot(input.normal, normalize(lightDir));
  83. if (f > 0)
  84. {
  85. diffuseLight += pointLights[i].color * f * factor;
  86. f = dot(normalize(reflect(normalize(-lightDir), input.normal)), normalize(kPosition.xyz - input.worldPos.xyz));
  87. if (f > 0)
  88. specularLight += pointLights[i].color * f * factor;
  89. }
  90. }
  91. }
  92. //if (!(diffuseLight.x >= 0 && diffuseLight.x <= 1))
  93. // diffuseLight.x = 0;
  94. float4 materialColor = shaderTexture.Sample(SampleType, input.tex);
  95. if (effectEnabled)
  96. {
  97. float dist = sqrt((input.tex.x - 0.5f) * (input.tex.x - 0.5f) + (input.tex.y - 0.5f) * (input.tex.y - 0.5f)) / sqrt(0.5f);
  98. if (dist < effectPercentage)
  99. {
  100. float alphaMultiplier = (effectPercentage - dist) / 0.2f;
  101. if (alphaMultiplier > 1)
  102. alphaMultiplier = 1.f;
  103. float4 effectColor = additionalTexture.Sample(SampleType, input.tex);
  104. materialColor = effectColor * (effectColor.a * alphaMultiplier) + materialColor * (1 - effectColor.a * alphaMultiplier);
  105. }
  106. }
  107. float4 textureColor = saturate((materialColor * ambientFactor) + (float4(diffuseLight.x, diffuseLight.y, diffuseLight.z, 0) * diffusFactor) + (float4(specularLight.x, specularLight.y, specularLight.z, 0) * specularFactor));
  108. textureColor.a = materialColor.a;
  109. if (isnan(diffuseLight.x * diffusFactor))
  110. textureColor = materialColor;
  111. return textureColor;
  112. //return textureColor;
  113. //if (diffusFactor == 0)
  114. // return float4(1, 1, 0, 1);
  115. /*if (isnan(diffuseLight.x) || isnan(diffusFactor) || isinf(diffuseLight.x) || isinf(-diffuseLight.x))
  116. return float4(0, 1, 1, 1);
  117. if (isnan(diffuseLight.x - diffuseLight.x) && isnan(diffuseLight.x * diffusFactor) )
  118. return float4(1, 1, 1, 1);
  119. if ((diffuseLight.x * diffusFactor) != 0 && (diffuseLight.x * diffusFactor) != -0)
  120. return float4(0, 0, 1, 1);
  121. return float4(0, 1, 0, 1);*/
  122. }