1
3

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その2。モノクロ。

Last updated at Posted at 2020-04-15

twitterでアップしたモノクロImageEffectのシェーダコードを貼っておきます。

輝度抽出としてはRGBのYCbCr変換のY値を使うのが定石ですけど、
Unityは標準でLuminance()という関数がすでに用意されているのでそれを使います。
https://docs.unity3d.com/ja/2018.4/Manual/SL-BuiltinFunctions.html

Monochrome.shader
Shader "ScreenPocket/ImageEffect/Monochrome"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ( "MainColor", 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 _Color;

            fixed4 frag (v2f i) : SV_Target
            {
                half4 col = tex2D( _MainTex, i.uv );
                col.rgb = Luminance(col.rgb) * _Color;
                return col;
            }
            ENDCG
        }
    }
}

ついでに色も乗算。後でGB風ImageEffectシェーダを作る時に使うので。

1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?