Advertisement
tonynogo

Demo 80 - Triangles to pyramids

Jul 6th, 2017
3,996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Geometry/Pyramids"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Texture", 2D) = "white" {}
  6.         _Factor ("Factor", Range(0., 0.5)) = 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 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.                 float3 centerPos = (IN[0].vertex + IN[1].vertex + IN[2].vertex) / 3;
  60.                 float2 centerTex = (IN[0].uv + IN[1].uv + IN[2].uv) / 3;
  61.                 centerPos += float4(normalFace, 0) * _Factor;
  62.  
  63.                 for(int i = 0; i < 3; i++)
  64.                 {
  65.                     o.pos = UnityObjectToClipPos(IN[i].vertex);
  66.                     o.uv = IN[i].uv;
  67.                     o.col = fixed4(0., 0., 0., 1.);
  68.                     tristream.Append(o);
  69.  
  70.                     int inext = (i+1)%3;
  71.                     o.pos = UnityObjectToClipPos(IN[inext].vertex);
  72.                     o.uv = IN[inext].uv;
  73.                     o.col = fixed4(0., 0., 0., 1.);
  74.                     tristream.Append(o);
  75.  
  76.                     o.pos = UnityObjectToClipPos(float4(centerPos, 1));
  77.                     o.uv = centerTex;
  78.                     o.col = fixed4(1.0, 1.0, 1.0, 1.);
  79.                     tristream.Append(o);
  80.  
  81.                     tristream.RestartStrip();
  82.                 }
  83.  
  84.                 o.pos = UnityObjectToClipPos(IN[0].vertex);
  85.                 o.uv = IN[0].uv;
  86.                 o.col = fixed4(0., 0., 0., 1.);
  87.                 tristream.Append(o);
  88.  
  89.                 o.pos = UnityObjectToClipPos(IN[1].vertex);
  90.                 o.uv = IN[1].uv;
  91.                 o.col = fixed4(0., 0., 0., 1.);
  92.                 tristream.Append(o);
  93.  
  94.                 o.pos = UnityObjectToClipPos(IN[2].vertex);
  95.                 o.uv = IN[2].uv;
  96.                 o.col = fixed4(0., 0., 0., 1.);
  97.                 tristream.Append(o);
  98.  
  99.                 tristream.RestartStrip();
  100.             }
  101.            
  102.             fixed4 frag (g2f i) : SV_Target
  103.             {
  104.                 fixed4 col = tex2D(_MainTex, i.uv) * i.col;
  105.                 return col;
  106.             }
  107.             ENDCG
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement