DX11CustomPixelShader.hlsl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 light1 : TEXCOORD2;
  53. float4 light2 : TEXCOORD3;
  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. float4 materialColor = shaderTexture.Sample(SampleType, input.tex);
  63. if (effectEnabled)
  64. {
  65. bool effectAlpha = effectPercentage > 1.f;
  66. float percentage = effectPercentage;
  67. if (effectAlpha) percentage -= 1.f;
  68. if (effectEnabled && !effectAlpha) clip(materialColor.a - 0.25);
  69. 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);
  70. if (dist < percentage)
  71. {
  72. float alphaMultiplier = (percentage - dist) / 0.2f;
  73. if (alphaMultiplier > 1)
  74. alphaMultiplier = 1.f;
  75. float4 effectColor = additionalTexture.Sample(SampleType, input.tex);
  76. float effectA = effectColor.a;
  77. materialColor = effectColor * (effectColor.a * alphaMultiplier) + materialColor * (1 - effectColor.a * alphaMultiplier);
  78. if (effectAlpha)
  79. {
  80. materialColor.a = effectA * alphaMultiplier;
  81. if (materialColor.a > 1.0) materialColor.a = 1.0;
  82. }
  83. }
  84. }
  85. else
  86. {
  87. clip(materialColor.a - 0.25);
  88. }
  89. if (input.light1.w > 0)
  90. {
  91. diffuseLight = float3(materialColor.x * input.light1.x,
  92. materialColor.y * input.light1.y,
  93. materialColor.z * input.light1.z);
  94. float4 diffuseLight2 = float4(materialColor.x * input.light2.x,
  95. materialColor.y * input.light2.y,
  96. materialColor.z * input.light2.z, 0);
  97. if (diffuseLightCount > 0)
  98. {
  99. float factor = dot(input.normal, -difuseLights[0].direction);
  100. if (factor < 0)
  101. factor = 0;
  102. factor = factor * 0.5 + 0.5;
  103. diffuseLight = diffuseLight * factor;
  104. }
  105. else
  106. {
  107. diffuseLight = diffuseLight * 0.5;
  108. }
  109. if (diffuseLight2.x > diffuseLight.x)
  110. {
  111. diffuseLight.x = diffuseLight2.x;
  112. }
  113. if (diffuseLight2.y > diffuseLight.y)
  114. {
  115. diffuseLight.y = diffuseLight2.y;
  116. }
  117. if (diffuseLight2.z > diffuseLight.z)
  118. {
  119. diffuseLight.z = diffuseLight2.z;
  120. }
  121. }
  122. else
  123. {
  124. for (int j = 0; j < diffuseLightCount; j++)
  125. {
  126. if (dot(input.normal, -difuseLights[j].direction) < 0)
  127. continue;
  128. diffuseLight += difuseLights[j].color * dot(input.normal, -difuseLights[j].direction);
  129. }
  130. diffuseLight = float3(1.0, 1.0, 1.0);
  131. /*for (int i = 0; i < pointLightCount; i++)
  132. {
  133. float3 lightDir = pointLights[i].position - input.worldPos.xyz;
  134. float factor;
  135. if (length(lightDir) < 1)
  136. factor = 1;
  137. else
  138. factor = pointLights[i].radius / length(lightDir);
  139. float f = dot(input.normal, normalize(lightDir));
  140. if (f > 0)
  141. {
  142. diffuseLight += pointLights[i].color * f * factor;
  143. f = dot(normalize(reflect(normalize(-lightDir), input.normal)), normalize(kPosition.xyz - input.worldPos.xyz));
  144. if (f > 0)
  145. specularLight += pointLights[i].color * f * factor;
  146. }
  147. }*/
  148. }
  149. //if (!(diffuseLight.x >= 0 && diffuseLight.x <= 1))
  150. // diffuseLight.x = 0;
  151. float4 textureColor = saturate((materialColor * ambientFactor) + (float4(diffuseLight.x, diffuseLight.y, diffuseLight.z, 0) * diffusFactor) + (float4(specularLight.x, specularLight.y, specularLight.z, 0) * specularFactor));
  152. textureColor.a = materialColor.a;
  153. if (isnan(diffuseLight.x * diffusFactor))
  154. textureColor = materialColor;
  155. return textureColor;
  156. //return textureColor;
  157. //if (diffusFactor == 0)
  158. // return float4(1, 1, 0, 1);
  159. /*if (isnan(diffuseLight.x) || isnan(diffusFactor) || isinf(diffuseLight.x) || isinf(-diffuseLight.x))
  160. return float4(0, 1, 1, 1);
  161. if (isnan(diffuseLight.x - diffuseLight.x) && isnan(diffuseLight.x * diffusFactor) )
  162. return float4(1, 1, 1, 1);
  163. if ((diffuseLight.x * diffusFactor) != 0 && (diffuseLight.x * diffusFactor) != -0)
  164. return float4(0, 0, 1, 1);
  165. return float4(0, 1, 0, 1);*/
  166. }