Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. float4x4 ProjectionMatrix;
  2. float4x4 WorldMatrix;
  3. float2 Viewport;
  4.  
  5. bool DrawLighting;
  6. float3 lightDirection;
  7. float lightIntensity;
  8.  
  9. static const float HuesPerTexture = 2048;
  10. static const float ToGrayScale = 3;
  11.  
  12. sampler DrawSampler : register(s0);
  13. sampler HueSampler0 : register(s1);
  14. sampler HueSampler1 : register(s2);
  15. sampler MiniMapSampler : register(s3);
  16.  
  17. #define PS_S vs_4_0_level_9_1
  18. #define VS_S ps_4_0_level_9_1
  19.  
  20. struct VS_INPUT
  21. {
  22. float4 Position : POSITION0;
  23. float3 Normal : NORMAL0;
  24. float3 TexCoord : TEXCOORD0;
  25. float3 Hue : TEXCOORD1;
  26. };
  27.  
  28. struct PS_INPUT
  29. {
  30. float4 Position : POSITION0;
  31. float3 TexCoord : TEXCOORD0;
  32. float3 Normal : TEXCOORD1;
  33. float3 Hue : TEXCOORD2;
  34. };
  35.  
  36. PS_INPUT VertexShaderFunction(VS_INPUT IN)
  37. {
  38. PS_INPUT OUT;
  39.  
  40. OUT.Position = mul(mul(IN.Position, WorldMatrix), ProjectionMatrix);
  41.  
  42. // Half pixel offset for correct texel centering.
  43. OUT.Position.x -= 0.5 / Viewport.x;
  44. OUT.Position.y += 0.5 / Viewport.y;
  45.  
  46. OUT.TexCoord = IN.TexCoord;
  47. OUT.Normal = IN.Normal;
  48. OUT.Hue = IN.Hue;
  49.  
  50. return OUT;
  51. }
  52.  
  53. float4 PixelShader_Hue(PS_INPUT IN) : COLOR0
  54. {
  55. // Get the initial pixel and discard it if the alpha == 0
  56. float4 color = tex2D(DrawSampler, IN.TexCoord);
  57. if (color.a == 0)
  58. discard;
  59.  
  60. // flag for no lighting
  61. bool drawLighting = true;
  62. if (IN.Hue.y >= 4)
  63. {
  64. IN.Hue.y -= 4;
  65. drawLighting = false;
  66. }
  67.  
  68. // Hue the color if the hue vector y component is greater than 0.
  69. if (IN.Hue.y > 0)
  70. {
  71. float inHueIndex = ((color.r + color.g + color.b) / 3.0f);
  72. float4 hueColor;
  73. if (IN.Hue.x >= HuesPerTexture)
  74. {
  75. hueColor = tex2D(HueSampler1, float2(inHueIndex, (IN.Hue.x - HuesPerTexture) / HuesPerTexture));
  76. }
  77. else
  78. {
  79. hueColor = tex2D(HueSampler0, float2(inHueIndex, IN.Hue.x / HuesPerTexture));
  80. }
  81. hueColor.a = color.a;
  82.  
  83. if (IN.Hue.y >= 2)
  84. {
  85. // partial hue - map any grayscale pixels to the hue. Colored pixels remain colored.
  86. if ((color.r == color.g) && (color.r == color.b))
  87. color = hueColor;
  88. }
  89. else
  90. {
  91. // normal hue - map the hue to the grayscale.
  92. color = hueColor;
  93. }
  94. }
  95.  
  96. // Hue.z is the transparency value. alpha = (1 - Hue.z)
  97. float alpha = 1 - IN.Hue.z;
  98. color *= alpha;
  99.  
  100. // Darken the color based on the ambient lighting and the normal.
  101. if (DrawLighting && drawLighting)
  102. {
  103. float3 light = normalize(lightDirection);
  104. float3 normal = normalize(IN.Normal);
  105. float3 nDotL = min(saturate(dot(light, normal)), 1.0f);
  106.  
  107. color.rgb = saturate((color.rgb * nDotL * lightIntensity * 0.2f + color.rgb * lightIntensity * 0.8f));
  108. }
  109.  
  110. return color;
  111. }
  112.  
  113. float4 PixelShader_MiniMap(PS_INPUT IN) : COLOR0
  114. {
  115. // Get the initial pixel and discard it if the alpha == 0
  116. float4 color = tex2D(DrawSampler, IN.TexCoord);
  117. if ((color.r == 1) && (color.g == 0) && (color.b == 1))
  118. {
  119. color = tex2D(MiniMapSampler, IN.Normal);
  120. }
  121.  
  122. if (color.a == 0)
  123. discard;
  124.  
  125.  
  126. return color;
  127. }
  128.  
  129. float4 PixelShader_Grayscale(PS_INPUT IN) : COLOR0
  130. {
  131. // Get the initial pixel and discard it if the alpha == 0
  132. float4 color = tex2D(DrawSampler, IN.TexCoord);
  133. if (color.a == 0)
  134. discard;
  135.  
  136. float greyscaleAverage = (0.2989 * color.r + 0.5870 * color.g + 0.1140 * color.b);
  137. color = float4(greyscaleAverage, greyscaleAverage, greyscaleAverage, color.a);
  138.  
  139. // Darken the color based on the ambient lighting and the normal.
  140. if (DrawLighting)
  141. {
  142. float3 light = normalize(lightDirection);
  143. float3 normal = normalize(IN.Normal);
  144. float3 nDotL = min(saturate(dot(light, normal)), 1.0f);
  145.  
  146. color.rgb = saturate((color.rgb * nDotL * lightIntensity * 0.2f + color.rgb * lightIntensity * 0.8f));
  147. }
  148.  
  149. return color;
  150. }
  151.  
  152. float4 PixelShader_ShadowSet(PS_INPUT IN) : COLOR0
  153. {
  154. // Get the initial pixel and discard it if the alpha == 0
  155. float4 color = tex2D(DrawSampler, IN.TexCoord);
  156. if (color.a == 0)
  157. discard;
  158. // if pixel was opaque, return black half-transparent.
  159. // we use the stencil buffer to only write one shadow pixel to each screen pixel.
  160. return float4(0, 0, 0, .5);
  161. }
  162.  
  163.  
  164. technique HueTechnique
  165. {
  166. pass p0
  167. {
  168. VertexShader = compile PS_S VertexShaderFunction();
  169. PixelShader = compile VS_S PixelShader_Hue();
  170. }
  171. }
  172.  
  173. technique MiniMapTechnique
  174. {
  175. pass p0
  176. {
  177. VertexShader = compile PS_S VertexShaderFunction();
  178. PixelShader = compile VS_S PixelShader_MiniMap();
  179. }
  180. }
  181.  
  182. technique GrayscaleTechnique
  183. {
  184. pass p0
  185. {
  186. VertexShader = compile PS_S VertexShaderFunction();
  187. PixelShader = compile VS_S PixelShader_Grayscale();
  188. }
  189. }
  190.  
  191. technique ShadowSetTechnique
  192. {
  193. pass p0
  194. {
  195. VertexShader = compile PS_S VertexShaderFunction();
  196. PixelShader = compile VS_S PixelShader_ShadowSet();
  197. }
  198. }
  199.  
  200. technique StandardEffect
  201. {
  202. pass p0
  203. {
  204. VertexShader = compile PS_S VertexShaderFunction();
  205. PixelShader = compile VS_S PixelShader_Hue();
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement