Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Unlit/Pixel to tiles sampling test"
- {
- Properties
- {
- _MainTex ("Texture", 2D) = "white" {} //turn off mip map
- _TexSize("Texture Pixel Size XY", Vector) = (32,32,0,0)
- _TileSize("TileSize", Range (0.0, 2.0)) = 2.0
- _EdgeSoftness("Edge Softness", Range (0.0, 2.0)) = 0.1
- _Distance("blur dist ", Float) = 30
- }
- SubShader
- {
- Tags { "RenderType"="Opaque" }
- LOD 100
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- // make fog work
- #pragma multi_compile_fog
- #include "UnityCG.cginc"
- #define PI 3.14159
- struct appdata
- {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- };
- struct v2f
- {
- float2 uv : TEXCOORD0;
- UNITY_FOG_COORDS(1)
- float4 vertex : SV_POSITION;
- float4 worldPosition : TEXCOORD3;
- };
- sampler2D _MainTex;
- float4 _MainTex_ST;
- float2 _TexSize;
- float _TileSize;
- float _EdgeSoftness;
- float _Distance;
- sampler2D _CameraDepthTexture;
- float4 _CameraDepthTexture_ST;
- v2f vert (appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- UNITY_TRANSFER_FOG(o,o.vertex);
- o.worldPosition = mul(unity_ObjectToWorld, v.vertex);
- return o;
- }
- // staircase functions by joeytwiddle
- // https://math.stackexchange.com/a/2235096
- //
- float stepStair (float u) {
- return .5 - .5 * cos(PI * u);
- }
- float stair (float x, float b, float c) {
- float width = b + c;
- float base = floor(x / width); // base of this step
- float o = x - width * base;//x % width; // offset, between 0 and width
- //return base + (o < b ? 0 : stepStair((o - b) / c));
- return base + step(b, o) * stepStair((o - b) / c);
- }
- //
- float ChangeUV(float value, float texSize)
- {
- float halfTexSize = texSize/2;
- float offsetTex = 1/(texSize* 2);//+ _var1;
- float integer;
- float fraction = modf( value * halfTexSize, integer);
- float stairInputOffset = ( _TileSize) * 1.5 + _EdgeSoftness - 1 ;
- float stairOutput = stair((fraction)*2 + stairInputOffset , _TileSize, _EdgeSoftness );
- float normalize = clamp(stairOutput,0.0, 2 ) / 2;
- return normalize/halfTexSize - offsetTex + integer/halfTexSize ;
- }
- float FloorPixel(float value, float texSize)
- {
- float halfTexSize = texSize/2;
- float offsetTex = 1/(texSize* 2);
- return floor(value * halfTexSize)/halfTexSize + offsetTex;
- }
- fixed4 frag (v2f i) : SV_Target
- {
- // sample the texture
- float2 uv = i.uv;
- uv.x = ChangeUV(uv.x , _TexSize.x) ;
- uv.y = ChangeUV(uv.y, _TexSize.y) ;
- fixed4 col = tex2D(_MainTex, uv);
- uv = i.uv;
- uv.x = FloorPixel(uv.x , _TexSize.x) ;
- uv.y = FloorPixel(uv.y, _TexSize.y) ;
- fixed4 col2 = tex2D(_MainTex, uv);
- float dist = distance(i.worldPosition, _WorldSpaceCameraPos)/_Distance;
- dist = max(0.0, dist);
- col = lerp(col, col2, saturate( dist) );
- //col.b = distance(i.worldPosition, _WorldSpaceCameraPos)/5;
- // apply fog
- UNITY_APPLY_FOG(i.fogCoord, col);
- return col;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement