Advertisement
tonynogo

Demo 92 - Outline and screenspace texture

Jul 6th, 2017
12,672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Outline and ScreenSpace texture"
  2. {
  3.     Properties
  4.     {
  5.         [Header(Outline)]
  6.         _OutlineVal ("Outline value", Range(0., 2.)) = 1.
  7.         _OutlineCol ("Outline color", color) = (1., 1., 1., 1.)
  8.         [Header(Texture)]
  9.         _MainTex ("Texture", 2D) = "white" {}
  10.         _Zoom ("Zoom", Range(0.5, 20)) = 1
  11.         _SpeedX ("Speed along X", Range(-1, 1)) = 0
  12.         _SpeedY ("Speed along Y", Range(-1, 1)) = 0
  13.     }
  14.     SubShader
  15.     {
  16.         Tags { "Queue"="Geometry" "RenderType"="Opaque" }
  17.        
  18.         Pass
  19.         {
  20.             Cull Front
  21.  
  22.             CGPROGRAM
  23.             #pragma vertex vert
  24.             #pragma fragment frag
  25.             #include "UnityCG.cginc"
  26.  
  27.             struct v2f {
  28.                 float4 pos : SV_POSITION;
  29.             };
  30.  
  31.             float _OutlineVal;
  32.  
  33.             v2f vert(appdata_base v) {
  34.                 v2f o;
  35.  
  36.                 // Convert vertex to clip space
  37.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  38.  
  39.                 // Convert normal to view space (camera space)
  40.                 float3 normal = mul((float3x3) UNITY_MATRIX_IT_MV, v.normal);
  41.  
  42.                 // Compute normal value in clip space
  43.                 normal.x *= UNITY_MATRIX_P[0][0];
  44.                 normal.y *= UNITY_MATRIX_P[1][1];
  45.  
  46.                 // Scale the model depending the previous computed normal and outline value
  47.                 o.pos.xy += _OutlineVal * normal.xy;
  48.                 return o;
  49.             }
  50.  
  51.             fixed4 _OutlineCol;
  52.  
  53.             fixed4 frag(v2f i) : SV_Target {
  54.                 return _OutlineCol;
  55.             }
  56.  
  57.             ENDCG
  58.         }
  59.  
  60.         Pass
  61.         {
  62.             CGPROGRAM
  63.             #pragma vertex vert
  64.             #pragma fragment frag
  65.             #include "UnityCG.cginc"
  66.  
  67.             float4 vert(appdata_base v) : SV_POSITION
  68.             {
  69.                 return mul(UNITY_MATRIX_MVP, v.vertex);
  70.             }
  71.  
  72.             sampler2D _MainTex;
  73.             float _Zoom;
  74.             float _SpeedX;
  75.             float _SpeedY;
  76.  
  77.             fixed4 frag(float4 i : VPOS) : SV_Target
  78.             {
  79.                 // Screen space texture
  80.                 return tex2D(_MainTex, ((i.xy / _ScreenParams.xy) + float2(_Time.y * _SpeedX, _Time.y * _SpeedY)) / _Zoom);
  81.             }
  82.  
  83.             ENDCG
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement