Advertisement
foureyedcat

Variable pixel size shader

Mar 22nd, 2020
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Unlit/Pixel to tiles sampling test"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Texture", 2D) = "white" {} //turn off mip map
  6.         _TexSize("Texture Pixel Size XY", Vector) = (32,32,0,0)
  7.         _TileSize("TileSize", Range (0.0, 2.0)) = 2.0
  8.        
  9.         _EdgeSoftness("Edge Softness", Range (0.0, 2.0)) = 0.1
  10.        
  11.         _Distance("blur dist ", Float) = 30
  12.        
  13.  
  14.     }
  15.     SubShader
  16.     {
  17.         Tags { "RenderType"="Opaque" }
  18.         LOD 100
  19.  
  20.         Pass
  21.         {
  22.             CGPROGRAM
  23.             #pragma vertex vert
  24.             #pragma fragment frag
  25.             // make fog work
  26.             #pragma multi_compile_fog
  27.  
  28.             #include "UnityCG.cginc"
  29.            
  30.             #define PI 3.14159
  31.  
  32.             struct appdata
  33.             {
  34.                 float4 vertex : POSITION;
  35.                 float2 uv : TEXCOORD0;
  36.             };
  37.  
  38.             struct v2f
  39.             {
  40.                 float2 uv : TEXCOORD0;
  41.                 UNITY_FOG_COORDS(1)
  42.                 float4 vertex : SV_POSITION;
  43.                 float4 worldPosition : TEXCOORD3;
  44.                
  45.             };
  46.  
  47.             sampler2D _MainTex;
  48.             float4 _MainTex_ST;
  49.             float2 _TexSize;
  50.             float _TileSize;
  51.             float _EdgeSoftness;
  52.             float _Distance;
  53.            
  54.            
  55.             sampler2D _CameraDepthTexture;        
  56.             float4 _CameraDepthTexture_ST;
  57.  
  58.             v2f vert (appdata v)
  59.             {
  60.                 v2f o;
  61.                 o.vertex = UnityObjectToClipPos(v.vertex);
  62.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  63.                 UNITY_TRANSFER_FOG(o,o.vertex);
  64.                 o.worldPosition = mul(unity_ObjectToWorld, v.vertex);
  65.                 return o;
  66.             }
  67.            
  68.             // staircase functions by joeytwiddle
  69.             // https://math.stackexchange.com/a/2235096
  70.             //
  71.             float stepStair (float u) {
  72.                 return .5 - .5 * cos(PI * u);
  73.             }
  74.            
  75.             float stair (float x, float b, float c) {
  76.            
  77.                 float width = b + c;
  78.                 float base = floor(x / width);    // base of this step
  79.                 float o = x - width * base;//x % width;  // offset, between 0 and width
  80.  
  81.                 //return base + (o < b ? 0 : stepStair((o - b) / c));
  82.                 return base + step(b, o) * stepStair((o - b) / c);
  83.             }
  84.             //
  85.            
  86.                      
  87.             float ChangeUV(float value, float texSize)
  88.             {
  89.                 float halfTexSize = texSize/2;
  90.                 float offsetTex =  1/(texSize* 2);//+ _var1;
  91.                 float integer;
  92.                 float fraction = modf( value * halfTexSize, integer);
  93.                
  94.                 float stairInputOffset = ( _TileSize) * 1.5 + _EdgeSoftness - 1 ;
  95.                 float stairOutput = stair((fraction)*2 + stairInputOffset , _TileSize, _EdgeSoftness );
  96.                 float normalize = clamp(stairOutput,0.0, 2 ) / 2;
  97.                
  98.                 return  normalize/halfTexSize - offsetTex + integer/halfTexSize ;
  99.                
  100.             }
  101.            
  102.             float FloorPixel(float value, float texSize)
  103.             {
  104.                 float halfTexSize = texSize/2;
  105.                 float offsetTex =  1/(texSize* 2);
  106.                 return floor(value * halfTexSize)/halfTexSize + offsetTex;
  107.             }
  108.  
  109.             fixed4 frag (v2f i) : SV_Target
  110.             {  
  111.            
  112.                 // sample the texture
  113.                 float2 uv = i.uv;
  114.                 uv.x = ChangeUV(uv.x , _TexSize.x) ;
  115.                 uv.y = ChangeUV(uv.y, _TexSize.y) ;
  116.                
  117.                 fixed4 col = tex2D(_MainTex, uv);
  118.                 uv = i.uv;
  119.                 uv.x = FloorPixel(uv.x , _TexSize.x) ;
  120.                 uv.y = FloorPixel(uv.y, _TexSize.y) ;
  121.                
  122.                 fixed4 col2 = tex2D(_MainTex,  uv);
  123.  
  124.                 float dist = distance(i.worldPosition, _WorldSpaceCameraPos)/_Distance;
  125.                 dist = max(0.0, dist);
  126.                 col = lerp(col, col2, saturate( dist) );
  127.                
  128.                 //col.b = distance(i.worldPosition, _WorldSpaceCameraPos)/5;
  129.                 // apply fog
  130.                 UNITY_APPLY_FOG(i.fogCoord, col);
  131.                 return col;
  132.             }
  133.             ENDCG
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement