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 その5。「2値化(Binarization)」

Last updated at Posted at 2020-05-12

Twitterにアップした2値化ImageEffectのシェーダコードを貼っておきます。

Twitterに書いたとおり、
Monochromeで白黒にした画像をPosterizationで2色に粗階調化しました。
ただそうなると、黒と灰色に分解されてしまうのでceil()で灰色を白に引き上げたりもしています。
※修正
そういえばZero/Oneでわけるならstep()使えば良いじゃん、ということを思い出したので修正します。
過去の自分の記事を無理やり使おうとしてしまった。。反省。

MonochromeについてはシンプルなImageEffectその2。モノクロ。
PosterizationについてはシンプルなImageEffectその3。Posterization
ceil()についてはcg - ceil
をご参照下さい。

また、今回黒い部分と白い部分にそれぞれ色を載せられるように、
_ZeroColor、_OneColorの2つの色パラメータも追加しておきました。
白黒のみで良いなら必要ないですが、色々使い途が増やせそうなので残しておきます。

Binarization.shader
Shader "ScreenPocket/ImageEffect/Binarization"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _ZeroColor ("Zero Color", Color) = (0,0,0,1)
        _OneColor ("One Color", Color) = (1,1,1,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;
            fixed4 _ZeroColor;
            fixed4 _OneColor;

            fixed4 frag (v2f i) : SV_Target
            {
                half4 col = tex2D( _MainTex, i.uv );
                fixed luminance = Luminance(col.rgb);
                col.rgb = step(0.5, luminance);
                return lerp(_ZeroColor, _OneColor, col.r);
            }
            ENDCG
        }
    }
}

輝度(luminance)にオフセット値を加えて、黒になる範囲を調節出来るようにするのも有りかもしれないですね。
fixed luminance = Luminance(col.rgb) + _Offset;
な感じで。
step(0.5,luminance);
の0.5を調整できるようにすれば、黒になる範囲が調整できるかと。

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?