1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

シンプルなImageEffectその1。モザイク。

Last updated at Posted at 2020-04-14

Twitterに貼ったシンプルなモザイクImageEffectのコードを貼っておきます。

floor(パラメータ * _DivideNum ) / _DivideNum
とすることで、パラメータを階段化(粗階調化)できるのは色々使えるので覚えておくと良いかと。

下記のコードではuvを階段のように刻むことで、モザイク化を実現しています。

Mosaic.shader
Shader "ScreenPocket/ImageEffect/Mosaic"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _DivideNum ( "Divide Num", Float ) = 1
    }
    
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            float _DivideNum;

            fixed4 frag (v2f i) : SV_Target
            {
                return tex2D( _MainTex, floor(i.uv * _DivideNum ) / _DivideNum );
            }
            ENDCG
        }
    }
}

DivideNumには画面のモザイクタイル分割数を入れてあげてください。
もし、モザイクを正方形にしたいなら、画面比でAspect値をを求めて、_DivideNumを補正してあげると実現できるはず。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?