Advertisement
tonynogo

Demo 94 - Wireframe without diagonal

Jul 6th, 2017
14,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Geometry/Wireframe"
  2. {
  3.     Properties
  4.     {
  5.         [PowerSlider(3.0)]
  6.         _WireframeVal ("Wireframe width", Range(0., 0.5)) = 0.05
  7.         _FrontColor ("Front color", color) = (1., 1., 1., 1.)
  8.         _BackColor ("Back color", color) = (1., 1., 1., 1.)
  9.         [Toggle] _RemoveDiag("Remove diagonals?", Float) = 0.
  10.     }
  11.     SubShader
  12.     {
  13.         Tags { "Queue"="Geometry" "RenderType"="Opaque" }
  14.        
  15.         Pass
  16.         {
  17.             Cull Front
  18.             CGPROGRAM
  19.             #pragma vertex vert
  20.             #pragma fragment frag
  21.             #pragma geometry geom
  22.  
  23.             // Change "shader_feature" with "pragma_compile" if you want set this keyword from c# code
  24.             #pragma shader_feature __ _REMOVEDIAG_ON
  25.  
  26.             #include "UnityCG.cginc"
  27.  
  28.             struct v2g {
  29.                 float4 worldPos : SV_POSITION;
  30.             };
  31.  
  32.             struct g2f {
  33.                 float4 pos : SV_POSITION;
  34.                 float3 bary : TEXCOORD0;
  35.             };
  36.  
  37.             v2g vert(appdata_base v) {
  38.                 v2g o;
  39.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  40.                 return o;
  41.             }
  42.  
  43.             [maxvertexcount(3)]
  44.             void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream) {
  45.                 float3 param = float3(0., 0., 0.);
  46.  
  47.                 #if _REMOVEDIAG_ON
  48.                 float EdgeA = length(IN[0].worldPos - IN[1].worldPos);
  49.                 float EdgeB = length(IN[1].worldPos - IN[2].worldPos);
  50.                 float EdgeC = length(IN[2].worldPos - IN[0].worldPos);
  51.                
  52.                 if(EdgeA > EdgeB && EdgeA > EdgeC)
  53.                     param.y = 1.;
  54.                 else if (EdgeB > EdgeC && EdgeB > EdgeA)
  55.                     param.x = 1.;
  56.                 else
  57.                     param.z = 1.;
  58.                 #endif
  59.  
  60.                 g2f o;
  61.                 o.pos = mul(UNITY_MATRIX_VP, IN[0].worldPos);
  62.                 o.bary = float3(1., 0., 0.) + param;
  63.                 triStream.Append(o);
  64.                 o.pos = mul(UNITY_MATRIX_VP, IN[1].worldPos);
  65.                 o.bary = float3(0., 0., 1.) + param;
  66.                 triStream.Append(o);
  67.                 o.pos = mul(UNITY_MATRIX_VP, IN[2].worldPos);
  68.                 o.bary = float3(0., 1., 0.) + param;
  69.                 triStream.Append(o);
  70.             }
  71.  
  72.             float _WireframeVal;
  73.             fixed4 _BackColor;
  74.  
  75.             fixed4 frag(g2f i) : SV_Target {
  76.             if(!any(bool3(i.bary.x < _WireframeVal, i.bary.y < _WireframeVal, i.bary.z < _WireframeVal)))
  77.                  discard;
  78.  
  79.                 return _BackColor;
  80.             }
  81.  
  82.             ENDCG
  83.         }
  84.  
  85.         Pass
  86.         {
  87.             Cull Back
  88.             CGPROGRAM
  89.             #pragma vertex vert
  90.             #pragma fragment frag
  91.             #pragma geometry geom
  92.  
  93.             // Change "shader_feature" with "pragma_compile" if you want set this keyword from c# code
  94.             #pragma shader_feature __ _REMOVEDIAG_ON
  95.  
  96.             #include "UnityCG.cginc"
  97.  
  98.             struct v2g {
  99.                 float4 worldPos : SV_POSITION;
  100.             };
  101.  
  102.             struct g2f {
  103.                 float4 pos : SV_POSITION;
  104.                 float3 bary : TEXCOORD0;
  105.             };
  106.  
  107.             v2g vert(appdata_base v) {
  108.                 v2g o;
  109.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  110.                 return o;
  111.             }
  112.  
  113.             [maxvertexcount(3)]
  114.             void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream) {
  115.                 float3 param = float3(0., 0., 0.);
  116.  
  117.                 #if _REMOVEDIAG_ON
  118.                 float EdgeA = length(IN[0].worldPos - IN[1].worldPos);
  119.                 float EdgeB = length(IN[1].worldPos - IN[2].worldPos);
  120.                 float EdgeC = length(IN[2].worldPos - IN[0].worldPos);
  121.                
  122.                 if(EdgeA > EdgeB && EdgeA > EdgeC)
  123.                     param.y = 1.;
  124.                 else if (EdgeB > EdgeC && EdgeB > EdgeA)
  125.                     param.x = 1.;
  126.                 else
  127.                     param.z = 1.;
  128.                 #endif
  129.  
  130.                 g2f o;
  131.                 o.pos = mul(UNITY_MATRIX_VP, IN[0].worldPos);
  132.                 o.bary = float3(1., 0., 0.) + param;
  133.                 triStream.Append(o);
  134.                 o.pos = mul(UNITY_MATRIX_VP, IN[1].worldPos);
  135.                 o.bary = float3(0., 0., 1.) + param;
  136.                 triStream.Append(o);
  137.                 o.pos = mul(UNITY_MATRIX_VP, IN[2].worldPos);
  138.                 o.bary = float3(0., 1., 0.) + param;
  139.                 triStream.Append(o);
  140.             }
  141.  
  142.             float _WireframeVal;
  143.             fixed4 _FrontColor;
  144.  
  145.             fixed4 frag(g2f i) : SV_Target {
  146.             if(!any(bool3(i.bary.x <= _WireframeVal, i.bary.y <= _WireframeVal, i.bary.z <= _WireframeVal)))
  147.                  discard;
  148.  
  149.                 return _FrontColor;
  150.             }
  151.  
  152.             ENDCG
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement