[Shader2D]马赛克
1:效果图
2:源码
Shader "Custom/uiblood" {
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_CloudTex("噪声",2D) ="white"{}
_GrayTex("灰色背景",2D) ="gray"{}
_WhiteTex("白色背景",2D) ="white"{}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
#ifdef UNITY_HALF_TEXEL_OFFSET
OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
OUT.color = IN.color * _Color;
return OUT;
}
sampler2D _MainTex;
sampler2D _CloudTex;
sampler2D _GrayTex;
sampler2D _WhiteTex;
fixed4 frag(v2f IN) : SV_Target
{
/*
half2 uv = IN.texcoord;
//uv旋转
half t = _Time.y*2;
t= ( (0.6-length(uv-half2(0.5,0.5)))*2 ) *t;
uv = uv-half2(0.5,0.5);
half x = uv.x*cos(t)-uv.y*sin(t);
half y = uv.x*sin(t)+uv.y*cos(t);
half2 newUV = half2(x,y)+half2(0.5,0.5);
half4 color = tex2D(_CloudTex,newUV ) * IN.color;
*/
/*
half2 uv = IN.texcoord;
half4 col = tex2D(_CloudTex,uv ) * IN.color;
half r = 0.393*col.r+0.769*col.g+0.189*col.b;
half g = 0.349*col.r+0.686*col.g+0.168*col.b;
half b = 0.272*col.r+0.534*col.g+0.131*col.b;
half4 color = half4(r,g,b,col.a);
*/
/*
half2 uv = IN.texcoord;
half4 color = tex2D(_CloudTex,uv ) * IN.color;
//左上像素
half4 lcol = tex2D(_CloudTex,float2(uv.x-0.001,uv.y-0.001));
half4 newCol = color-lcol;
half4 gcol = tex2D(_GrayTex,uv ) * IN.color;;//half4(color.r*0.3,color.g*0.59,color.b*0.11,color.a);
gcol+=newCol;
clip (gcol.a - 0.01);
return gcol;
*/
/*
half2 uv = IN.texcoord;
half4 color = tex2D(_CloudTex,uv ) * IN.color;
half4 wcol = tex2D(_WhiteTex,uv ) * IN.color;
//灰度图取R值
half gray = color.r;
//取上方像素的R
half tg = tex2D(_CloudTex,float2(uv.x,uv.y+0.001) ) * IN.color;
half rg = 6*(gray-tg);
wcol.rbg-=half3(rg,rg,rg);
clip (wcol.a - 0.01);
return wcol;
*/
half2 uv = IN.texcoord;
//切成100*100方格
//取格子中间像素
fixed x = (uv.x/1)*100;
fixed y = (uv.y/1)*100;
fixed indexX =0;
fixed indexY =0;
modf(x,indexX);
modf(y,indexY);
half4 color = tex2D(_CloudTex,half2(indexX/100,indexY/100) ) * IN.color;
clip (color.a - 0.01);
return color;
}
ENDCG
}
}
}
/*
Mosaic 效果: 马赛克 原理: n x n方块内取同一颜色
*/