LoginSignup
11
7

More than 5 years have passed since last update.

Unityで、実写を絵っぽくするshaderを書きたかったけど、上手くいかなかったから途中経過を残す

Posted at

20161110001.jpg
この画像を下みたいにはできた。

スクリーンショット 2017-03-19 1.16.54.png

動画だと、
こんな感じ。 全然絵っぽくない。
50b6eb6d421ec11f622d68058bfc9bbf.gif

でもやった途中経過は残しときたい。

参考

qiita.hlsl

Shader "Unlit/IROENPITU"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}

       _MainTex2 ("Texture2", 2D) = "white" {}

        Uoffset ("Uoffset", float) = 0.1

        Voffset ("Voffset", float) = 0.1

        _Flip("Flip",Range(-1, 1)) = 0

            _Range("Range",Range(0.0001,1)) = 0.005


    }
    SubShader
    {
        Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
        Blend SrcAlpha OneMinusSrcAlpha        
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            sampler2D _MainTex2;

            float4 _MainTex_ST;
            float Uoffset;
            float Voffset;
                float _Range;
             float _Flip;



            float peek(float x, float y)
    {
        return tex2D(_MainTex, float2(x, y)).r;
    }

    const float dx = 0.001953125;
    const float dy = 0.001953125;


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


            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                                float s = _Time*10;

                fixed4 col2 = tex2D(_MainTex2, i.uv+float2(0,0));

                float4 txl0=tex2D(_MainTex,i.uv+float2(0.0f,0.0f));
float4 txl1=tex2D(_MainTex,i.uv+float2(-Uoffset,-Voffset))*0.1f; float4 txl2=tex2D(_MainTex,i.uv+float2(0.0f,-Voffset))*0.1f; float4 txl3=tex2D(_MainTex,i.uv+float2(+Uoffset,-Voffset))*0.1f;
float4 txl4=tex2D(_MainTex,i.uv+float2(-Uoffset,0.0f))*0.1f; float4 txl5=tex2D(_MainTex,i.uv+float2(+Uoffset,0.0f))*0.1f;
float4 txl6=tex2D(_MainTex,i.uv+float2(-Uoffset,+Voffset))*0.1f; float4 txl7=tex2D(_MainTex,i.uv+float2(0.0f,+Voffset))*0.1f; float4 txl8=tex2D(_MainTex,i.uv+float2(+Uoffset,+Voffset))*0.1f;
float4 color1=txl0+txl1+txl2+txl3+txl4+txl5+txl6+txl7+txl8;

                // apply fog

                //return color1;

                float x = i.uv.x;
        float y = i.uv.y;
        float l0_y = i.uv.x-_Flip;
        clip(i.uv.y - l0_y);


    float3x3 m = float3x3(
        peek(x - _Range, y - _Range), peek(x, y - _Range), peek(x + _Range, y - _Range),
        peek(x - _Range, y), peek(x, y), peek(x + _Range, y),
        peek(x - _Range, y + _Range), peek(x, y + _Range), peek(x + _Range, y + _Range)
        );

    float4 h = float4
        (
            m[0][0] - m[0][2] + (m[1][0] - m[1][2]) * 2 + m[2][0] - m[2][2],
            m[0][0] - m[2][0] + (m[0][1] - m[2][1]) * 2 + m[0][2] - m[2][2],
            2 * m[0][0] - m[0][2] + (m[1][0] - m[1][2]) + m[2][0] - 2 * m[2][2],
            m[0][0] - 2 * m[2][0] + (m[0][1] - m[2][1]) + 2 * m[0][2] - m[2][2]
            );
    // sample the 
    float d = length(h);

        if (d < 50) {
            float d = length(h);
            float4 c =color1*float4(d, d, d, 1);
            if (c.r < 0.5&&c.g < 0.5&&c.b < 0.5) {

            return color1/1.4+2*c*col2+2*c*col2;
            }else{

            //return  1 – 2 * (1 – col2)*(1 – c);
            return color1;

            }

        }
        else {
            return float4(1,1,1,1);
        }

                }



            ENDCG
        }
    }
}

やったことは、参考サイトにあるphotoshopの手順をやってみたり、
オーバーレイを改造したり色々やった感じです。

ちなみにオーバーレイについては、僕のもう一つのサイトで作ったコード載っけてるんで参考までに。。。

あとソーベルと、うーんとぼかしと、、、

とりあえず一旦飽きたのでまたの機会に挑戦します。

11
7
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
11
7