Advertisement
tonynogo

Demo 82 - Extrude

Jul 6th, 2017
11,775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Geometry/Extrude"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Texture", 2D) = "white" {}
  6.         _Factor ("Factor", Range(0., 2.)) = 0.2
  7.     }
  8.     SubShader
  9.     {
  10.         Tags { "RenderType"="Opaque" }
  11.         Cull Off
  12.  
  13.         Pass
  14.         {
  15.             CGPROGRAM
  16.             #pragma vertex vert
  17.             #pragma fragment frag
  18.             #pragma geometry geom
  19.  
  20.             #include "UnityCG.cginc"
  21.  
  22.             struct v2g
  23.             {
  24.                 float4 vertex : POSITION;
  25.                 float3 normal : NORMAL;
  26.                 float2 uv : TEXCOORD0;
  27.             };
  28.  
  29.             struct g2f
  30.             {
  31.                 float4 pos : SV_POSITION;
  32.                 float2 uv : TEXCOORD0;
  33.                 fixed4 col : COLOR;
  34.             };
  35.  
  36.             sampler2D _MainTex;
  37.             float4 _MainTex_ST;
  38.            
  39.             v2g vert (appdata_base v)
  40.             {
  41.                 v2g o;
  42.                 o.vertex = v.vertex;
  43.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  44.                 o.normal = v.normal;
  45.                 return o;
  46.             }
  47.  
  48.             float _Factor;
  49.  
  50.             [maxvertexcount(24)]
  51.             void geom(triangle v2g IN[3], inout TriangleStream<g2f> tristream)
  52.             {
  53.                 g2f o;
  54.  
  55.                 float3 edgeA = IN[1].vertex - IN[0].vertex;
  56.                 float3 edgeB = IN[2].vertex - IN[0].vertex;
  57.                 float3 normalFace = normalize(cross(edgeA, edgeB));
  58.  
  59.                 for(int i = 0; i < 3; i++)
  60.                 {
  61.                     o.pos = UnityObjectToClipPos(IN[i].vertex);
  62.                     o.uv = IN[i].uv;
  63.                     o.col = fixed4(0., 0., 0., 1.);
  64.                     tristream.Append(o);
  65.  
  66.                     o.pos = UnityObjectToClipPos(IN[i].vertex + float4(normalFace, 0) * _Factor);
  67.                     o.uv = IN[i].uv;
  68.                     o.col = fixed4(1., 1., 1., 1.);
  69.                     tristream.Append(o);
  70.  
  71.                     int inext = (i+1) % 3;
  72.  
  73.                     o.pos = UnityObjectToClipPos(IN[inext].vertex);
  74.                     o.uv = IN[inext].uv;
  75.                     o.col = fixed4(0., 0., 0., 1.);
  76.                     tristream.Append(o);
  77.  
  78.                     tristream.RestartStrip();
  79.  
  80.                     o.pos = UnityObjectToClipPos(IN[i].vertex + float4(normalFace, 0) * _Factor);
  81.                     o.uv = IN[i].uv;
  82.                     o.col = fixed4(1., 1., 1., 1.);
  83.                     tristream.Append(o);
  84.  
  85.                     o.pos = UnityObjectToClipPos(IN[inext].vertex);
  86.                     o.uv = IN[inext].uv;
  87.                     o.col = fixed4(0., 0., 0., 1.);
  88.                     tristream.Append(o);
  89.  
  90.                     o.pos = UnityObjectToClipPos(IN[inext].vertex + float4(normalFace, 0) * _Factor);
  91.                     o.uv = IN[inext].uv;
  92.                     o.col = fixed4(1., 1., 1., 1.);
  93.                     tristream.Append(o);
  94.  
  95.                     tristream.RestartStrip();
  96.                 }
  97.  
  98.                 for(int i = 0; i < 3; i++)
  99.                 {
  100.                     o.pos = UnityObjectToClipPos(IN[i].vertex + float4(normalFace, 0) * _Factor);
  101.                     o.uv = IN[i].uv;
  102.                     o.col = fixed4(1., 1., 1., 1.);
  103.                     tristream.Append(o);
  104.                 }
  105.  
  106.                 tristream.RestartStrip();
  107.  
  108.                 for(int i = 0; i < 3; i++)
  109.                 {
  110.                     o.pos = UnityObjectToClipPos(IN[i].vertex);
  111.                     o.uv = IN[i].uv;
  112.                     o.col = fixed4(0., 0., 0., 1.);
  113.                     tristream.Append(o);
  114.                 }
  115.  
  116.                 tristream.RestartStrip();
  117.             }
  118.            
  119.             fixed4 frag (g2f i) : SV_Target
  120.             {
  121.                 fixed4 col = tex2D(_MainTex, i.uv) * i.col;
  122.                 return col;
  123.             }
  124.             ENDCG
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement