DX11CustomPixelShader.hlsl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. //materialColor.a = 1.0;
  89. }
  90. if (input.light1.w > 0)
  91. {
  92. diffuseLight = float3(materialColor.x * input.light1.x,
  93. materialColor.y * input.light1.y,
  94. materialColor.z * input.light1.z);
  95. float4 diffuseLight2 = float4(materialColor.x * input.light2.x,
  96. materialColor.y * input.light2.y,
  97. materialColor.z * input.light2.z, 0);
  98. if (diffuseLightCount > 0)
  99. {
  100. float factor = dot(input.normal, -difuseLights[0].direction);
  101. if (factor < 0)
  102. factor = 0;
  103. factor = factor * 0.5 + 0.5;
  104. diffuseLight = diffuseLight * factor;
  105. }
  106. else
  107. {
  108. diffuseLight = diffuseLight * 0.5;
  109. }
  110. if (diffuseLight2.x > diffuseLight.x)
  111. {
  112. diffuseLight.x = diffuseLight2.x;
  113. }
  114. if (diffuseLight2.y > diffuseLight.y)
  115. {
  116. diffuseLight.y = diffuseLight2.y;
  117. }
  118. if (diffuseLight2.z > diffuseLight.z)
  119. {
  120. diffuseLight.z = diffuseLight2.z;
  121. }
  122. }
  123. else
  124. {
  125. for (int j = 0; j < diffuseLightCount; j++)
  126. {
  127. if (dot(input.normal, -difuseLights[j].direction) < 0)
  128. continue;
  129. diffuseLight += difuseLights[j].color * dot(input.normal, -difuseLights[j].direction);
  130. }
  131. diffuseLight = float3(1.0, 1.0, 1.0);
  132. /*for (int i = 0; i < pointLightCount; i++)
  133. {
  134. float3 lightDir = pointLights[i].position - input.worldPos.xyz;
  135. float factor;
  136. if (length(lightDir) < 1)
  137. factor = 1;
  138. else
  139. factor = pointLights[i].radius / length(lightDir);
  140. float f = dot(input.normal, normalize(lightDir));
  141. if (f > 0)
  142. {
  143. diffuseLight += pointLights[i].color * f * factor;
  144. f = dot(normalize(reflect(normalize(-lightDir), input.normal)), normalize(kPosition.xyz - input.worldPos.xyz));
  145. if (f > 0)
  146. specularLight += pointLights[i].color * f * factor;
  147. }
  148. }*/
  149. }
  150. //if (!(diffuseLight.x >= 0 && diffuseLight.x <= 1))
  151. // diffuseLight.x = 0;
  152. float4 textureColor = saturate((materialColor * ambientFactor) + (float4(diffuseLight.x, diffuseLight.y, diffuseLight.z, 0) * diffusFactor) + (float4(specularLight.x, specularLight.y, specularLight.z, 0) * specularFactor));
  153. textureColor.a = materialColor.a;
  154. if (isnan(diffuseLight.x * diffusFactor)) clip(-1);
  155. // textureColor = materialColor;
  156. if (effectEnabled && effectPercentage == 0)
  157. {
  158. clip(textureColor.a - 0.5);
  159. //textureColor.a = 1.0;
  160. }
  161. return textureColor;
  162. //return textureColor;
  163. //if (diffusFactor == 0)
  164. // return float4(1, 1, 0, 1);
  165. /*if (isnan(diffuseLight.x) || isnan(diffusFactor) || isinf(diffuseLight.x) || isinf(-diffuseLight.x))
  166. return float4(0, 1, 1, 1);
  167. if (isnan(diffuseLight.x - diffuseLight.x) && isnan(diffuseLight.x * diffusFactor) )
  168. return float4(1, 1, 1, 1);
  169. if ((diffuseLight.x * diffusFactor) != 0 && (diffuseLight.x * diffusFactor) != -0)
  170. return float4(0, 0, 1, 1);
  171. return float4(0, 1, 0, 1);*/
  172. }