unity shader - 马赛克特效

shader代码:

Shader "Unlit/MaSaiKe"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _TillSize("Till Size",Range(0.001,0.05))=0.005
    }
    SubShader
    {
        Pass{
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct a2v{
                float4 vertex:POSITION;
                float2 texcoord:TEXCOORD0;
            };
            struct v2f{
                float4 pos:POSITION;
                float2 uv:TEXCOORD0;
            };

            sampler2D _MainTex;
            float _TillSize;

            v2f vert(a2v v){
                v2f o;
                o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
                o.uv=v.texcoord;
                return o;
            }

            fixed4 frag(v2f i):SV_Target{
                i.uv.x = floor(i.uv.x/_TillSize) * _TillSize;
                i.uv.y = floor(i.uv.y/_TillSize) * _TillSize;

                fixed4 col = tex2D(_MainTex,i.uv);
                return col;
            }

            ENDCG
        }           
    }
}


原理:将uv的坐标除以一个缩放值,然后向下(floor)或者向上(ceil)取值,在乘以缩放值,即可得到马赛克效果。

你可能感兴趣的:(Shader)