Advertisement
tonynogo

Demo 81 - Quads to Pyramids

Jul 6th, 2017
5,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Geometry/Pyramids2"
  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(12)]
  51.             void geom(triangle v2g IN[3], inout TriangleStream<g2f> tristream)
  52.             {
  53.                 g2f o;
  54.  
  55.                 float3 normalFace = normalize(cross(IN[1].vertex - IN[0].vertex, IN[2].vertex - IN[0].vertex));
  56.  
  57.                 float edge1 = distance(IN[1].vertex, IN[0].vertex);
  58.                 float edge2 = distance(IN[2].vertex, IN[0].vertex);
  59.                 float edge3 = distance(IN[2].vertex, IN[1].vertex);
  60.  
  61.                 float3 centerPos = (IN[0].vertex + IN[1].vertex) / 2;
  62.                 float2 centerTex = (IN[0].uv + IN[1].uv) / 2;
  63.  
  64.                 if((step(edge1, edge2) * step(edge3, edge2)) == 1.0)
  65.                 {
  66.                     centerPos = (IN[2].vertex + IN[0].vertex) / 2;
  67.                     centerTex = (IN[2].uv + IN[0].uv) / 2;
  68.                 }
  69.                 else if((step(edge2, edge3) * step(edge1, edge3)) == 1.0)
  70.                 {
  71.                     centerPos = (IN[1].vertex + IN[2].vertex) / 2;
  72.                     centerTex = (IN[1].uv + IN[2].uv) / 2;
  73.                 }
  74.                
  75.                 centerPos += float4(normalFace, 0) * _Factor;
  76.  
  77.                 for(int i = 0; i < 3; i++)
  78.                 {
  79.                     o.pos = UnityObjectToClipPos(IN[i].vertex);
  80.                     o.uv = IN[i].uv;
  81.                     o.col = fixed4(0., 0., 0., 1.);
  82.                     tristream.Append(o);
  83.  
  84.                     int inext = (i+1)%3;
  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(float4(centerPos, 1));
  91.                     o.uv = centerTex;
  92.                     o.col = fixed4(1.0, 1.0, 1.0, 1.);
  93.                     tristream.Append(o);
  94.  
  95.                     tristream.RestartStrip();
  96.                 }
  97.  
  98.                 o.pos = UnityObjectToClipPos(IN[0].vertex);
  99.                 o.uv = IN[0].uv;
  100.                 o.col = fixed4(0., 0., 0., 1.);
  101.                 tristream.Append(o);
  102.  
  103.                 o.pos = UnityObjectToClipPos(IN[1].vertex);
  104.                 o.uv = IN[1].uv;
  105.                 o.col = fixed4(0., 0., 0., 1.);
  106.                 tristream.Append(o);
  107.  
  108.                 o.pos = UnityObjectToClipPos(IN[2].vertex);
  109.                 o.uv = IN[2].uv;
  110.                 o.col = fixed4(0., 0., 0., 1.);
  111.                 tristream.Append(o);
  112.  
  113.                 tristream.RestartStrip();
  114.             }
  115.            
  116.             fixed4 frag (g2f i) : SV_Target
  117.             {
  118.                 fixed4 col = tex2D(_MainTex, i.uv) * i.col;
  119.                 return col;
  120.             }
  121.             ENDCG
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement