Advertisement
Guest User

Untitled

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