LoginSignup
5
2

More than 3 years have passed since last update.

鉛筆画風のImageEffectシェーダコード

Posted at

Twitterにアップした鉛筆画風のImageEffectシェーダコードを貼っておきます

エッジ抽出で全然別のシェーダを触っていたのですが、
ふと「パラメータ調整したら鉛筆画っぽく出来るかも?」と思い色々触ったところ、
中々気に入った見た目が出来たので貼っておきます。

Pencil.shader
Shader "ScreenPocket/ImageEffect/Pencil"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }

    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 Gather(sampler2D tex, float2 coord, float2 offset)
            {
                return (tex2D( tex, coord + offset)
                      + tex2D( tex, coord + float2(offset.x, -offset.y))
                      + tex2D( tex, coord + float2(-offset.x, offset.y))
                      + tex2D( tex, coord - offset)) / 4.0;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 color = tex2D( _MainTex, i.uv );

                //隣接ピクセルの平均色を取得
                fixed4 neighbor = Gather(_MainTex, i.uv, _ScreenParams.zw - 1.0);
                //隣接色との差を絶対値で取る
                fixed4 neighborDistance = abs(color - neighbor);

                //RGBの中で一番大きい差を求める
                fixed maxValue = max(neighborDistance.r, neighborDistance.g);
                maxValue = max(maxValue, neighborDistance.b);

                //平滑化させつつ値調整
                return (1.0 - (saturate( maxValue + 0.9 ) - 0.9) * 6);
            }
            ENDCG
        }
    }
}

スクリーンショット 2020-05-12 22.06.38.png
スクリーンショット 2020-05-12 22.19.30.png

マジックナンバーが多くて申し訳ないけど、とりあえず版ということで。

5
2
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
5
2