Advertisement
Jellybit

Sprites/Pixel Snap/Alpha Blended shader

Mar 8th, 2014
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. Shader "Sprites/Pixel Snap/Alpha Blended"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Sprite Texture", 2D) = "white" {}
  6.         _Color ("Main Color", Color) = (1,1,1,1)
  7.     }
  8.  
  9.     SubShader
  10.     {
  11.         Tags
  12.         {
  13.             "Queue"="Transparent"
  14.             "IgnoreProjector"="True"
  15.             "RenderType"="Transparent"
  16.         }
  17.  
  18.         Cull Off
  19.         Lighting Off
  20.         ZWrite Off
  21.         ZTest Always
  22.         Fog { Mode Off }
  23.         Blend SrcAlpha OneMinusSrcAlpha
  24.  
  25.         Pass
  26.         {
  27.         CGPROGRAM
  28.             #pragma vertex vert
  29.             #pragma fragment frag
  30.             #pragma fragmentoption ARB_precision_hint_fastest
  31.             #include "UnityCG.cginc"
  32.  
  33.             sampler2D _MainTex;
  34.             float4 _MainTex_ST;
  35.             fixed4 _Color;
  36.  
  37.             struct appdata_t
  38.             {
  39.                 float4 vertex   : POSITION;
  40.                 float2 texcoord : TEXCOORD0;
  41.             };
  42.  
  43.             struct v2f
  44.             {
  45.                 float4 vertex        : POSITION;
  46.                 float2 texcoord      : TEXCOORD0;
  47.             };
  48.  
  49.             v2f vert(appdata_t IN)
  50.             {
  51.                 v2f OUT;
  52.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
  53.                 OUT.texcoord = TRANSFORM_TEX(IN.texcoord, _MainTex);
  54.  
  55.                 // Snapping params
  56.                 float hpcX = _ScreenParams.x * 0.5;
  57.                 float hpcY = _ScreenParams.y * 0.5;
  58.             #ifdef UNITY_HALF_TEXEL_OFFSET
  59.                 float hpcOX = -0.5;
  60.                 float hpcOY = 0.5;
  61.             #else
  62.                 float hpcOX = 0;
  63.                 float hpcOY = 0;
  64.             #endif 
  65.                 // Snap
  66.                 float pos = floor((OUT.vertex.x / OUT.vertex.w) * hpcX + 0.5f) + hpcOX;
  67.                 OUT.vertex.x = pos / hpcX * OUT.vertex.w;
  68.  
  69.                 pos = floor((OUT.vertex.y / OUT.vertex.w) * hpcY + 0.5f) + hpcOY;
  70.                 OUT.vertex.y = pos / hpcY * OUT.vertex.w;
  71.  
  72.                 return OUT;
  73.             }
  74.  
  75.             fixed4 frag(v2f IN) : COLOR
  76.             {
  77.                 return tex2D( _MainTex, IN.texcoord) * _Color;
  78.             }
  79.         ENDCG
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement