DX11CustomPixelShader.hlsl 4.9 KB

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