josemorval

Rounded square shader

Feb 25th, 2016
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "morvaly/RoundedSquare"
  2. {
  3.     Properties {
  4.         _scaleX ("scaleX", Float ) = 0
  5.         _scaleY ("scaleY", Float ) = 0
  6.         _Color ("Color", Color) = (0.5,0.5,0.5,1)
  7.         _rounded ("rounded", Range(0,5) ) = 0
  8.  
  9.     }
  10.     SubShader
  11.     {
  12.         Tags {
  13.             "IgnoreProjector"="True"
  14.             "Queue"="Transparent"
  15.             "RenderType"="Transparent"
  16.         }
  17.  
  18.         Pass
  19.         {
  20.  
  21.             Blend SrcAlpha OneMinusSrcAlpha
  22.             ZWrite Off
  23.  
  24.             CGPROGRAM
  25.             #pragma vertex vert
  26.             #pragma fragment frag
  27.             #include "UnityCG.cginc"
  28.  
  29.             uniform float _rounded;
  30.             uniform float _scaleX;
  31.             uniform float _scaleY;
  32.             uniform float4 _Color;
  33.  
  34.             struct appdata
  35.             {
  36.                 float4 vertex : POSITION;
  37.                 float2 uv : TEXCOORD0;
  38.             };
  39.  
  40.             struct v2f
  41.             {
  42.                 float2 uv : TEXCOORD0;
  43.                 float4 vertex : SV_POSITION;
  44.             };
  45.  
  46.             float DoRoundSquare( float2 UV , float val , float scaleX , float scaleY ){
  47.                 UV = UV - float2(0.5,0.5);
  48.                 UV *= 2.0;
  49.                 UV.x *= scaleX;
  50.                 UV.y *= scaleY;
  51.                
  52.                 float dist = val;
  53.                
  54.                 float2 f1 = float2(-scaleX+dist,scaleY-dist);
  55.                 float2 f2 = float2(-scaleX+dist,-scaleY+dist);
  56.                 float2 f3 = float2(scaleX-dist,-scaleY+dist);
  57.                 float2 f4 = float2(scaleX-dist,scaleY-dist);
  58.                
  59.                 if(distance(UV,f1)<dist){
  60.                     return 1.0;
  61.                 }
  62.                
  63.                 if(distance(UV,f2)<dist){
  64.                     return 1.0;
  65.                 }
  66.                
  67.                 if(distance(UV,f3)<dist){
  68.                     return 1.0;
  69.                 }
  70.                
  71.                 if(distance(UV,f4)<dist){
  72.                     return 1.0;
  73.                 }
  74.                
  75.                 if(UV.x>(-scaleX+dist) && UV.x<(scaleX-dist)){
  76.                     return 1.0;
  77.                 }
  78.                
  79.                 if(UV.y>(-scaleY+dist) && UV.y<(scaleY-dist)){
  80.                     return 1.0;
  81.                 }
  82.                
  83.                 return 0.0;
  84.             }
  85.  
  86.             v2f vert (appdata v)
  87.             {
  88.                 v2f o;
  89.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  90.                 o.uv = v.uv;
  91.                 return o;
  92.             }
  93.            
  94.             fixed4 frag (v2f i) : SV_Target
  95.             {
  96.                 float val = DoRoundSquare( i.uv , clamp(_rounded,0.0,(min(_scaleX,_scaleY)/1.5)) , _scaleX , _scaleY );
  97.                 return fixed4(_Color.rgb,val);
  98.             }
  99.             ENDCG
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment