Advertisement
Guest User

Tiler

a guest
Sep 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "Over2D/Tiler"
  3. {
  4.     Properties
  5.     {
  6.         _MainTex("Sprite Texture", 2D) = "white" {}
  7.         _Tiling("Tiling", Vector) = (10,15,0,0)
  8.         _Offset("Offset", Vector) = (0,0,0,0)
  9.         _Color("Tint", Color) = (1,1,1,1)
  10.         [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
  11.     }
  12.  
  13.         SubShader
  14.     {
  15.         Tags
  16.     {
  17.         "Queue" = "Transparent"
  18.         "IgnoreProjector" = "True"
  19.         "RenderType" = "Transparent"
  20.         "PreviewType" = "Plane"
  21.         "CanUseSpriteAtlas" = "True"
  22.     }
  23.  
  24.         Cull Off
  25.         Lighting Off
  26.         ZWrite Off
  27.         Blend One OneMinusSrcAlpha
  28.  
  29.         Fog{ Mode Off }
  30.  
  31.         Pass
  32.         {
  33.             CGPROGRAM
  34.     #pragma vertex vert
  35.     #pragma fragment frag
  36.     #pragma multi_compile _ PIXELSNAP_ON
  37.     #include "UnityCG.cginc"
  38.  
  39.             struct appdata
  40.             {
  41.                 float4 vertex   : POSITION;
  42.                 float2 uv : TEXCOORD0;
  43.             };
  44.  
  45.             struct v2f
  46.             {
  47.                 float4 vertex   : SV_POSITION;
  48.                 half2 uv  : TEXCOORD0;
  49.             };
  50.  
  51.             fixed4 _Color;
  52.             float2 _Tiling;
  53.             float2 _Offset;
  54.             sampler2D _MainTex;
  55.  
  56.             v2f vert(appdata IN)
  57.             {
  58.                 v2f OUT;
  59.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
  60.                 //тайлим текстуру
  61.                 float xScale = length(float3(unity_ObjectToWorld[0][0], unity_ObjectToWorld[1][0], unity_ObjectToWorld[2][0])) / _Tiling.x;
  62.                 float yScale = length(float3(unity_ObjectToWorld[0][1], unity_ObjectToWorld[1][1], unity_ObjectToWorld[2][1])) / _Tiling.y;
  63.                 OUT.uv = IN.uv * half2(xScale, yScale);
  64.  
  65.         #ifdef PIXELSNAP_ON
  66.                 OUT.vertex = UnityPixelSnap(OUT.vertex);
  67.         #endif
  68.  
  69.                 return OUT;
  70.             }
  71.  
  72.             fixed4 SampleSpriteTexture(float2 uv)
  73.             {
  74.                 //сдвигаем Offset текстуры на заданную величину
  75.                 uv += _Offset;
  76.  
  77.                 fixed4 color = tex2D(_MainTex, uv);
  78.  
  79.                 return color;
  80.             }
  81.  
  82.             fixed4 frag(v2f IN) : SV_Target
  83.             {
  84.                 fixed4 c = SampleSpriteTexture(IN.uv);
  85.                 c.rgb *= _Color;
  86.                 c.rgb *= c.a;
  87.                 return c;
  88.             }
  89.             ENDCG
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement