Advertisement
Guest User

Untitled

a guest
Nov 25th, 2010
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 KB | None | 0 0
  1. #include "gValue.hfx"
  2. #include "progIN_OUT.hfx"
  3. #include "copy.hfx"
  4.  
  5. ////////////////////////////////////
  6. //calculate distant
  7. ////////////////////////////////////
  8. VS_DIST_OUTPUT VNShader(float3 vInPos : POSITION, float2 vInUV : TEXCOORD0)
  9. {
  10.   VS_DIST_OUTPUT OUT;
  11.  
  12.   OUT.pos = float4(vInPos.xyz, 1.0);
  13.  
  14.   OUT.uvzz = float4(vInUV, 1.0, 1.0);
  15.  
  16.   OUT.uv1 = vInUV + float2(0.0, fPixel);
  17.   OUT.uv2 = vInUV + float2(0.0, -fPixel);
  18.   OUT.uv3 = vInUV + float2(-fPixel, 0.0);
  19.   OUT.uv4 = vInUV + float2(fPixel, 0.0);
  20.  
  21.   return OUT;
  22. }
  23.  
  24. float4 PSCalcDist(VS_DIST_OUTPUT IN) : COLOR
  25. {
  26.  
  27.   float4 s23, s21, s12, s32;
  28.   float dist23Sq, dist21Sq, dist12Sq, dist32Sq;
  29.   float4 s00 = tex2D(float_map, IN.uvzz.xy);
  30.   clip(s00.w - 0.99);
  31.  
  32.   s23 = tex2D(float_map, IN.uv1);
  33.   s21 = tex2D(float_map, IN.uv2);
  34.   s12 = tex2D(float_map, IN.uv3);
  35.   s32 = tex2D(float_map, IN.uv4);
  36.  
  37.   // расстояние от текущего пикселя до ближайшей известной ему границы
  38.   float dist00Sq = length(IN.uvzz.xy - s00.xy);
  39.   // расстояние от текущего пикселя до границы известной соседнему пикселю
  40.   dist23Sq = length(IN.uvzz.xy - s23.xy);
  41.   dist21Sq = length(IN.uvzz.xy - s21.xy);
  42.   dist12Sq = length(IN.uvzz.xy - s12.xy);
  43.   dist32Sq = length(IN.uvzz.xy - s32.xy);
  44.  
  45.   float2 uv;
  46.   float4 res;
  47.   uv = dist00Sq < 1.0 ? s00.xy : vFake;
  48.   res = float4(uv, dist00Sq, s00.w);
  49.  
  50.   uv = dist23Sq < 1.0 ? s23.xy : vFake;
  51.   res = res.z > dist23Sq ? float4(uv, dist23Sq, 1.0) : res;
  52.  
  53.   uv = dist21Sq < 1.0 ? s21.xy : vFake;
  54.   res = res.z > dist21Sq ? float4(uv, dist21Sq, 1.0) : res;
  55.  
  56.   uv = dist12Sq < 1.0 ? s12.xy : vFake;
  57.   res = res.z > dist12Sq ? float4(uv, dist12Sq, 1.0) : res;
  58.  
  59.   uv = dist32Sq < 1.0 ? s32.xy : vFake;
  60.   res = res.z > dist32Sq ? float4(uv, dist32Sq, 1.0) : res;
  61.  
  62.   return res;
  63. }
  64.  
  65. float4 PSCalcMedial(VS_DIST_OUTPUT IN) : COLOR
  66. {
  67.   float4 s00 = tex2D(float_map, IN.uvzz.xy);
  68.   clip(s00.zw - float2(fPixel * 1.1, 0.99));
  69.  
  70.   float4 px = tex2D(float_map, IN.uv1);
  71.   float4 nx = tex2D(float_map, IN.uv2);
  72.   float4 py = tex2D(float_map, IN.uv3);
  73.   float4 ny = tex2D(float_map, IN.uv4);
  74.  
  75.   float s00Dotpx = dot(s00.xy, px.xy);
  76.   float s00Dotpy = dot(s00.xy, py.xy);
  77.   float s00Dotnx = dot(s00.xy, nx.xy);
  78.   float s00Dotny = dot(s00.xy, ny.xy);
  79.  
  80.   float minCosAngle = min(s00Dotpx, min(s00Dotpy, min(s00Dotnx, s00Dotny)));
  81.   minCosAngle = 1.0 - minCosAngle;
  82.  
  83.   float scaledDist = s00.z * 200.0;
  84.  
  85.   clip(minCosAngle * scaledDist - 1.0);
  86.  
  87.   return float4(1.0, 1.0, s00.z, 1.0);
  88. }
  89.  
  90. float4 PSCalcMedialPre(VS_OUTPUT IN) : COLOR
  91. {
  92.   float4 s00 = tex2D(float_map, IN.uvzz.xy);
  93.   clip(s00.z - fPixel);
  94.  
  95.   float2 dist2 = IN.uvzz.xy - s00.xy;
  96.   float lenDist2 = length(dist2);
  97.   dist2 = normalize(dist2);
  98.  
  99.   return float4(dist2, lenDist2, 1.0);
  100. }
  101.  
  102. technique T0
  103. {
  104.   pass P0
  105.   {        
  106.     AlphaTestEnable = FALSE;
  107.     AlphaFunc = ALWAYS;
  108.     AlphaRef = 1;
  109.     AlphaBlendEnable = FALSE;
  110.     CullMode = NONE;
  111.     ZEnable = FALSE;
  112.     ZFunc = ALWAYS;
  113.     ZWriteEnable = FALSE;
  114.     vertexshader = compile vs_1_1 VNShader();
  115.     pixelshader = compile ps_2_0 PSCalcDist();
  116.   }
  117.   pass P1
  118.   {        
  119.     AlphaTestEnable = TRUE;
  120.     AlphaFunc = GREATER;
  121.     AlphaRef = 1;
  122.     AlphaBlendEnable = FALSE;
  123.     CullMode = NONE;
  124.     ZEnable = FALSE;
  125.     ZFunc = ALWAYS;
  126.     ZWriteEnable = FALSE;
  127.     vertexshader = compile vs_1_1 VSimpleShader();
  128.     pixelshader = compile ps_2_0 PSCopyScr(false);
  129.   }
  130.   pass P2
  131.   {        
  132.     AlphaTestEnable = TRUE;
  133.     AlphaFunc = GREATER;
  134.     AlphaRef = 1;
  135.     AlphaBlendEnable = FALSE;
  136.     CullMode = NONE;
  137.     ZEnable = FALSE;
  138.     ZFunc = ALWAYS;
  139.     ZWriteEnable = FALSE;
  140.     vertexshader = compile vs_1_1 VSimpleShader();
  141.     pixelshader = compile ps_2_0 PSCopyScr(true);
  142.   }
  143.   pass P3
  144.   {        
  145.     AlphaTestEnable = FALSE;
  146.     AlphaFunc = ALWAYS;
  147.     AlphaRef = 1;
  148.     AlphaBlendEnable = FALSE;
  149.     CullMode = NONE;
  150.     ZEnable = FALSE;
  151.     ZFunc = ALWAYS;
  152.     ZWriteEnable = FALSE;
  153.     vertexshader = compile vs_1_1 VSimpleShader();
  154.     pixelshader = compile ps_2_0 PSCountCopy();
  155.   }
  156.   pass P4
  157.   {        
  158.     AlphaTestEnable = TRUE;
  159.     AlphaFunc = GREATER;
  160.     AlphaRef = 1;
  161.     AlphaBlendEnable = FALSE;
  162.     CullMode = NONE;
  163.     ZEnable = FALSE;
  164.     ZFunc = ALWAYS;
  165.     ZWriteEnable = FALSE;
  166.     vertexshader = compile vs_1_1 VNShader();
  167.     pixelshader = compile ps_2_0 PSCalcMedial();
  168.   }
  169.   pass P5
  170.   {        
  171.     AlphaTestEnable = TRUE;
  172.     AlphaFunc = GREATER;
  173.     AlphaRef = 1;
  174.     AlphaBlendEnable = FALSE;
  175.     CullMode = NONE;
  176.     ZEnable = FALSE;
  177.     ZFunc = ALWAYS;
  178.     ZWriteEnable = FALSE;
  179.     vertexshader = compile vs_1_1 VSimpleShader();
  180.     pixelshader = compile ps_2_0 PSCalcMedialPre();
  181.   }
  182. }
  183.  
  184. //////////////////////////////////////////////////////
  185. // file "copy.hfx"
  186. //////////////////////////////////////////////////////
  187.  
  188. VS_OUTPUT VSimpleShader(float3 vInPos : POSITION, float2 vInUV : TEXCOORD0)
  189. {
  190.   VS_OUTPUT OUT;
  191.   OUT.pos = float4(vInPos.xyz, 1.0);
  192.   OUT.uvzz = float4(vInUV, 1.0, 1.0);
  193.   return OUT;
  194. }
  195.  
  196. //////////////////////////////////////////////
  197. //copy pass, if border point detected place own coord in xy, else &position
  198. ///////////////////////////////////////////////
  199. float4 PSCountCopy(VS_OUTPUT IN) : COLOR
  200. {
  201.   float fHeghtTreshold = 1.0 / fAbsHeght;
  202.  
  203.   float4 s00 = tex2D(float_map, IN.uvzz.xy);
  204.   clip(s00.w - 0.1);
  205.  
  206.   float4 down =  tex2D(float_map, IN.uvzz.xy + float2(0.0, fPixel));
  207.   float4 up =    tex2D(float_map, IN.uvzz.xy + float2(0.0, -fPixel));
  208.   float4 left =  tex2D(float_map, IN.uvzz.xy + float2(-fPixel, 0.0));
  209.   float4 right =  tex2D(float_map, IN.uvzz.xy + float2(fPixel, 0.0));
  210.  
  211.   int count = 0;
  212.   count = length(down.xyz - s00.xyz)  >= fHeghtTreshold ? count : count + 1;
  213.   count = length(up.xyz - s00.xyz)  >= fHeghtTreshold ? count : count + 1;
  214.   count = length(left.xyz - s00.xyz)  >= fHeghtTreshold ? count : count + 1;
  215.   count = length(right.xyz - s00.xyz)  >= fHeghtTreshold ? count : count + 1;
  216.  
  217.   float4 res = (count < 4) ? float4(IN.uvzz.xy, -1000.0, 0.0) : float4(vFake, 0.0, 1.0);
  218.   return res;
  219. }
  220.  
  221. ////////////////////////////////////////////////
  222. // copy RT to screen, or other RT
  223. ////////////////////////////////////////////////
  224. float4 PSCopyScr(VS_OUTPUT IN, uniform bool bDistantOnly) : COLOR
  225. {
  226.   float4 t0 = tex2D(float_map, IN.uvzz.xy);
  227.  
  228.   float dist = t0.z * fRange;
  229.  
  230.   float4 res = bDistantOnly ? float4(dist, dist, dist, t0.a) : t0;
  231.   return res;  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement