Guest User

Untitled

a guest
Sep 22nd, 2017
152
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. float4x4 tWV: WORLDVIEW;
  3. float4x4 tWVP: WORLDVIEWPROJECTION;
  4.  
  5. float LineWidth = 3;
  6. float4 WireColor  <bool color=true;String uiname="WireColor";>  = {0, 0, 1, 1};
  7.  
  8.  
  9. DepthStencilState DSSDepthLessEqual
  10. {
  11.   DepthEnable = true;
  12.   DepthWriteMask = 0x00;
  13.   DepthFunc = Less_Equal;
  14. };
  15.  
  16. RasterizerState RSFill
  17. {
  18.     FillMode = SOLID;
  19.     CullMode = None;
  20.     DepthBias = false;
  21.     MultisampleEnable = true;
  22. };
  23.  
  24.  
  25. BlendState BSBlending
  26. {
  27.     BlendEnable[0] = TRUE;
  28.     SrcBlend = SRC_ALPHA;
  29.     DestBlend = INV_SRC_ALPHA ;
  30.     BlendOp = ADD;
  31.     SrcBlendAlpha = SRC_ALPHA;
  32.     DestBlendAlpha = DEST_ALPHA;
  33.     BlendOpAlpha = ADD;
  34.     RenderTargetWriteMask[0] = 0x0F;
  35. };
  36.  
  37. struct VS_INPUT
  38. {
  39.     float3 Pos  : POSITION;
  40.     float3 Nor  : NORMAL;
  41.     float3 Tex  : TEXCOORD0;
  42. };
  43.  
  44. struct GS_INPUT
  45. {
  46.     float4 Pos  : POSITION;
  47.     float4 PosV : TEXCOORD0;
  48. };
  49.  
  50.  
  51. struct PS_INPUT_WIRE
  52. {
  53.     float4 Pos : SV_POSITION;
  54.   //  float4 Col : TEXCOORD0;
  55.     float3 Heights : TEXCOORD1;
  56. };
  57.  
  58.  
  59. //--------------------------------------------------------------------------------------
  60. // Vertex Shader
  61. //--------------------------------------------------------------------------------------
  62. GS_INPUT VS( VS_INPUT input )
  63. {
  64.     GS_INPUT output;
  65.     output.Pos = mul(float4(input.Pos, 1), tWVP);
  66.     output.PosV = mul(float4(input.Pos, 1), tWV);
  67.     return output;
  68. }
  69.  
  70. //--------------------------------------------------------------------------------------
  71. // Geometry Shader
  72. //--------------------------------------------------------------------------------------
  73.  
  74. [maxvertexcount(3)]
  75. void GS( triangle GS_INPUT input[3],
  76.                          inout TriangleStream<PS_INPUT_WIRE> outStream )
  77. {
  78.     PS_INPUT_WIRE output;
  79.  
  80.     // Shade and colour face.
  81.    // output.Col = WireColor;
  82.  
  83.     // Emit the 3 vertices
  84.     // The Height attribute is based on the constant
  85.     output.Pos = input[0].Pos;
  86.     output.Heights = float3( 1, 0, 0 );
  87.     outStream.Append( output );
  88.  
  89.     output.Pos = input[1].Pos;
  90.     output.Heights = float3( 0, 1, 0 );
  91.     outStream.Append( output );
  92.  
  93.     output.Pos = input[2].Pos;
  94.     output.Heights = float3( 0, 0, 1 );
  95.     outStream.Append( output );
  96.  
  97.     outStream.RestartStrip();
  98. }
  99.  
  100. [maxvertexcount(6)]
  101. void GS_Diag( triangle GS_INPUT input[3],
  102.                          inout TriangleStream<PS_INPUT_WIRE> outStream )
  103. {
  104.     PS_INPUT_WIRE output;
  105.  
  106.     output.Pos = input[0].Pos;
  107.     output.Heights = float3( 1, 0, 0 );
  108.     outStream.Append( output );
  109.  
  110.     output.Pos = input[1].Pos;
  111.     output.Heights = float3( 0, 0, 0 ); // diagonals have no height
  112.     outStream.Append( output );
  113.  
  114.     output.Pos = input[2].Pos;
  115.     output.Heights = float3( 0, 0, 1 );
  116.     outStream.Append( output );
  117.  
  118.     outStream.RestartStrip();
  119.  
  120. }
  121.  
  122. //--------------------------------------------------------------------------------------
  123. // Pixel Shader
  124. //--------------------------------------------------------------------------------------
  125.  
  126. float evalMinDistanceToEdges(in PS_INPUT_WIRE input)
  127. {
  128.     float dist;
  129.  
  130.     float3 ddxHeights = ddx( input.Heights );
  131.     float3 ddyHeights = ddy( input.Heights );
  132.     float3 ddHeights2 =  ddxHeights*ddxHeights + ddyHeights*ddyHeights;
  133.    
  134.     float3 pixHeights2 = input.Heights *  input.Heights / ddHeights2 ;
  135.    
  136.     dist = sqrt( min ( min (pixHeights2.x, pixHeights2.y), pixHeights2.z) );
  137.    
  138.     return dist;
  139. }
  140.  
  141. float4 PS( PS_INPUT_WIRE input) : SV_Target
  142. {
  143.     // Compute the shortest distance between the fragment and the edges.
  144.     float dist = evalMinDistanceToEdges(input);
  145.  
  146.     // Cull fragments too far from the edge.
  147.     if (dist > 0.5*LineWidth+1) discard;
  148.  
  149.     // Map the computed distance to the [0,2] range on the border of the line.
  150.     dist = clamp((dist - (0.5*LineWidth - 1)), 0, 2);
  151.  
  152.     // Alpha is computed from the function exp2(-2(x)^2).
  153.     dist *= dist;
  154.     float alpha = exp2(-2*dist);
  155.  
  156.     // Standard wire color
  157.     float4 color = WireColor;
  158.     color.a *= alpha;
  159.    
  160.     return color;
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167. //--------------------------------------------------------------------------------------
  168.  
  169.  
  170.  
  171. technique10 Wireframe
  172. {    
  173.     pass
  174.     {
  175.         SetDepthStencilState( DSSDepthLessEqual, 0 );
  176.         SetRasterizerState( RSFill );
  177.         SetBlendState( BSBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
  178.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  179.         SetGeometryShader( CompileShader( gs_4_0, GS() ) );
  180.         SetPixelShader( CompileShader( ps_4_0, PS() ) );
  181.     }
  182. }
  183.  
  184.  
  185. technique10 WireframeNoDiag
  186. {    
  187.     pass
  188.     {
  189.         SetDepthStencilState( DSSDepthLessEqual, 0 );
  190.         SetRasterizerState( RSFill );
  191.         SetBlendState( BSBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
  192.         SetVertexShader( CompileShader( vs_4_0, VS() ) );
  193.         SetGeometryShader( CompileShader( gs_4_0, GS_Diag() ) );
  194.         SetPixelShader( CompileShader( ps_4_0, PS() ) );
  195.     }
  196. }
RAW Paste Data