Sinuousity

WireframeFull.shader

Jul 21st, 2020
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Copyright 2020 PROTOZOA LLC
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  5.  
  6. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7.  
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  9. */
  10.  
  11. CGINCLUDE
  12. #include "UnityCG.cginc"
  13.  
  14. struct v2g
  15. {
  16.     float4 vertex : POSITION;
  17.     float3 normal : NORMAL;
  18.     float2 uv : TEXCOORD0;
  19. };
  20.  
  21. struct g2f
  22. {
  23.     float4 vertex : SV_POSITION;
  24.     float3 normal : NORMAL;
  25.     float2 uv : TEXCOORD0;
  26.     float3 wpos : TEXCOORD1;
  27.     float vis : TEXCOORD2;
  28. };
  29.  
  30.  
  31. sampler2D _MainTex;
  32. float4 _MainTex_ST;
  33.  
  34. half4 _Color;
  35. half _Bloom, _Shade;
  36. float _FrontVisibility, _BackVisibility, _EdgeVisibility, _CrossVisibility;
  37. half _Padding, _Extrude;
  38.  
  39. v2g vert(v2g v){return v;}
  40.  
  41. g2f getg2f(v2g v,float vis = 1.0)//single vertex
  42. {
  43.     g2f o;
  44.     v.vertex.xyz += v.normal.xyz * _Extrude;
  45.     o.vertex = UnityObjectToClipPos(v.vertex);
  46.     o.normal = UnityObjectToWorldNormal(v.normal);
  47.     o.uv = v.uv;
  48.     o.wpos = mul(unity_ObjectToWorld,v.vertex).xyz;
  49.     o.vis = vis;
  50.     return o;
  51. }
  52.  
  53. g2f getg2f(v2g v[3],float vis = 1.0)//triangle center point
  54. {
  55.     g2f o;
  56.     float3 lp = (v[0].vertex + v[1].vertex + v[2].vertex) * 0.3333333;
  57.     float3 ln = normalize(v[0].normal + v[1].normal + v[2].normal);
  58.     lp.xyz += ln.xyz * _Extrude;
  59.     o.vertex = UnityObjectToClipPos(lp);
  60.     o.normal = UnityObjectToWorldNormal(ln);
  61.     o.uv = (v[0].uv + v[1].uv + v[2].uv) * 0.3333333;
  62.     o.wpos = mul(unity_ObjectToWorld,float4(lp,1)).xyz;
  63.     o.vis = vis;
  64.     return o;
  65. }
  66.  
  67. half4 frag (g2f i) : SV_Target
  68. {
  69.     float3 vd = normalize(i.wpos - _WorldSpaceCameraPos);
  70.     float facing = -dot(i.normal.xyz , vd);
  71.     float vis = smoothstep(-0.1,0.5,facing) * _FrontVisibility + smoothstep(0.1,-0.5,facing) * _BackVisibility;
  72.     vis = saturate(vis) * i.vis;
  73.     if(vis < 0.001) discard;
  74.     half4 col = tex2D(_MainTex, i.uv * _MainTex_ST.xy + _MainTex_ST.zw);
  75.     col.a = 1.0;
  76.     col = col * half4(_Color.rgb , _Color.a * vis);
  77.     col.rgb *= 1.0 + 10.0 * _Bloom * saturate(facing);
  78.  
  79.     float light = 0.5 + 0.5 * dot(i.normal.xyz,float3(0,1,0));
  80.     return col * lerp(1.0,light,_Shade);
  81. }
  82. ENDCG
  83.  
  84. Shader "Protozoa/Wireframe/Full"
  85. {
  86.     Properties
  87.     {
  88.         [Header(RGB)]
  89.         _Color ("Color", Color) = (1,1,1,1)
  90.         _Bloom ("Bloom", Range(0.0,1.0)) = 0.0
  91.         _Shade ("Shade", Range(0.0,1.0)) = 0.0
  92.         _MainTex ("Texture", 2D) = "white" {}
  93.  
  94.         [Header(Alpha)]
  95.         _FrontVisibility ("Front Visibility", Range(0.0,1.0)) = 1.0
  96.         _BackVisibility ("Back Visibility", Range(0.0,1.0)) = 0.1
  97.         _EdgeVisibility ("Edge Visibility", Range(0.0,1.0)) = 0.1
  98.         _CrossVisibility ("Cross Visibility", Range(0.0,1.0)) = 0.1
  99.  
  100.         [Header(Form)]
  101.         _Padding ("Padding", Range(0.0,1.0)) = 0.1
  102.         _Extrude ("Extrude", Range(0.0,0.1)) = 0.0
  103.     }
  104.  
  105.     SubShader
  106.     {
  107.         Tags { "RenderType"="Opaque" "Queue"="Geometry+5" }
  108.         Blend SrcAlpha OneMinusSrcAlpha
  109.         ZTest LEqual
  110.         ZWrite Off
  111.         Cull Off
  112.  
  113.         Pass
  114.         {
  115.             Name "LINES"
  116.             CGPROGRAM
  117.             #pragma vertex vert
  118.             #pragma geometry geom
  119.             #pragma fragment frag
  120.  
  121.             [maxvertexcount(32)]
  122.             void geom (triangle v2g v[3] , uint pid : SV_PRIMITIVEID , inout LineStream<g2f> stream)
  123.             {
  124.                 g2f gc = getg2f(v,_CrossVisibility);
  125.                 g2f g0 = getg2f(v[0],_EdgeVisibility);
  126.                 g2f g1 = getg2f(v[1],_EdgeVisibility);
  127.                 g2f g2 = getg2f(v[2],_EdgeVisibility);
  128.  
  129.                 g0.vertex = lerp(g0.vertex,gc.vertex,_Padding);
  130.                 g1.vertex = lerp(g1.vertex,gc.vertex,_Padding);
  131.                 g2.vertex = lerp(g2.vertex,gc.vertex,_Padding);
  132.  
  133.                 stream.Append(g0);
  134.                 stream.Append(g1);
  135.                 stream.Append(g2);
  136.                 stream.Append(g0);
  137.                 stream.RestartStrip();
  138.  
  139.                 g0.vis = _CrossVisibility;
  140.                 g1.vis = _CrossVisibility;
  141.                 g2.vis = _CrossVisibility;
  142.  
  143.                 stream.Append(g0);
  144.                 stream.Append(gc);
  145.                 stream.RestartStrip();
  146.                 stream.Append(g1);
  147.                 stream.Append(gc);
  148.                 stream.RestartStrip();
  149.                 stream.Append(g2);
  150.                 stream.Append(gc);
  151.                 stream.RestartStrip();
  152.             }
  153.             ENDCG
  154.         }
  155.     }
  156. }
Add Comment
Please, Sign In to add comment