Advertisement
tonynogo

Demo 93 - Wireframe

Jul 6th, 2017
11,957
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.         _MainTex ("Texture", 2D) = "white" {}
  6.         [PowerSlider(3.0)]
  7.         _WireframeVal ("Wireframe width", Range(0., 0.34)) = 0.05
  8.         _FrontColor ("Front color", color) = (1., 1., 1., 1.)
  9.         _BackColor ("Back color", color) = (1., 1., 1., 1.)
  10.     }
  11.     SubShader
  12.     {
  13.         Tags { "RenderType"="Opaque" }
  14.        
  15.  
  16.         Pass
  17.         {
  18.             Cull Front
  19.             CGPROGRAM
  20.             #pragma vertex vert
  21.             #pragma fragment frag
  22.             #pragma geometry geom
  23.             #include "UnityCG.cginc"
  24.  
  25.             struct v2g {
  26.                 float4 pos : SV_POSITION;
  27.             };
  28.  
  29.             struct g2f {
  30.                 float4 pos : SV_POSITION;
  31.                 float3 bary : TEXCOORD0;
  32.             };
  33.  
  34.             v2g vert(appdata_base v) {
  35.                 v2g o;
  36.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  37.                 return o;
  38.             }
  39.  
  40.             [maxvertexcount(3)]
  41.             void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream) {
  42.                 g2f o;
  43.                 o.pos = IN[0].pos;
  44.                 o.bary = float3(1., 0., 0.);
  45.                 triStream.Append(o);
  46.                 o.pos = IN[1].pos;
  47.                 o.bary = float3(0., 0., 1.);
  48.                 triStream.Append(o);
  49.                 o.pos = IN[2].pos;
  50.                 o.bary = float3(0., 1., 0.);
  51.                 triStream.Append(o);
  52.             }
  53.  
  54.             float _WireframeVal;
  55.             fixed4 _BackColor;
  56.  
  57.             fixed4 frag(g2f i) : SV_Target {
  58.             if(!any(bool3(i.bary.x < _WireframeVal, i.bary.y < _WireframeVal, i.bary.z < _WireframeVal)))
  59.                  discard;
  60.  
  61.                 return _BackColor;
  62.             }
  63.  
  64.             ENDCG
  65.         }
  66.  
  67.         Pass
  68.         {
  69.             Cull Back
  70.             CGPROGRAM
  71.             #pragma vertex vert
  72.             #pragma fragment frag
  73.             #pragma geometry geom
  74.             #include "UnityCG.cginc"
  75.  
  76.             struct v2g {
  77.                 float4 pos : SV_POSITION;
  78.             };
  79.  
  80.             struct g2f {
  81.                 float4 pos : SV_POSITION;
  82.                 float3 bary : TEXCOORD0;
  83.             };
  84.  
  85.             v2g vert(appdata_base v) {
  86.                 v2g o;
  87.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  88.                 return o;
  89.             }
  90.  
  91.             [maxvertexcount(3)]
  92.             void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream) {
  93.                 g2f o;
  94.                 o.pos = IN[0].pos;
  95.                 o.bary = float3(1., 0., 0.);
  96.                 triStream.Append(o);
  97.                 o.pos = IN[1].pos;
  98.                 o.bary = float3(0., 0., 1.);
  99.                 triStream.Append(o);
  100.                 o.pos = IN[2].pos;
  101.                 o.bary = float3(0., 1., 0.);
  102.                 triStream.Append(o);
  103.             }
  104.  
  105.             float _WireframeVal;
  106.             fixed4 _FrontColor;
  107.  
  108.             fixed4 frag(g2f i) : SV_Target {
  109.             if(!any(bool3(i.bary.x < _WireframeVal, i.bary.y < _WireframeVal, i.bary.z < _WireframeVal)))
  110.                  discard;
  111.  
  112.                 return _FrontColor;
  113.             }
  114.  
  115.             ENDCG
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement