LoginSignup
11
10

More than 5 years have passed since last update.

[Unity]Shaderでかっこよさげな(?)シーン遷移 ~その2~

Last updated at Posted at 2017-01-15

できるもの

1月-15-2017 16-20-06.gif
バトルシーンへの遷移に使えそう。
少なくとも一瞬でパッと変わるよりはマシかなぁ?
ちなみに雑ですがアンチエイリアスはかけました。
(スクリプトから進行度合いの値を入れる必要があります。詳しくはここ)
(それが嫌なら_Time.yに演算を少し施して使うといいかもです。)

コード

Shader "Original/NewImageEffectShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        percent ("percent", Range(0, 1)) = 0.5
        cycletime ("cycletime", int) = 3
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always
        // 追加
        CGINCLUDE

        #define PI 3.14159

        float mod(float  a, float  b) { return a-b*floor(a/b); } 
        ENDCG

        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 = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            uniform float percent;
            uniform int cycletime;

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

                float2 r = 2.0 * (i.uv - 0.5);
                float aspectRatio = _ScreenParams.x / _ScreenParams.y;
                r.x *= aspectRatio;

                fixed4 bgCol = float4(0.0, 0.0, 0.0, 0.0); // black

                float4 cola,colb,colc,cold;
                cola = lerp(col, bgCol, 1.0 - step(PI * percent / cycletime, mod(atan((r.y + 0.001) / (r.x + 0.001)), PI / cycletime)));
                colb = lerp(col, bgCol, 1.0 - step(PI * percent / cycletime, mod(atan((r.y + 0.001) / (r.x - 0.001)), PI / cycletime)));
                colc = lerp(col, bgCol, 1.0 - step(PI * percent / cycletime, mod(atan((r.y - 0.001) / (r.x + 0.001)), PI / cycletime)));
                cold = lerp(col, bgCol, 1.0 - step(PI * percent / cycletime, mod(atan((r.y - 0.001) / (r.x - 0.001)), PI / cycletime)));
                col = (cola + colb + colc + cold) / 4;

                return col;
            }
            ENDCG
        }
    }
}

cycletimeの値を変えれば細分化の度合いを変えることができます。

仕組み

atanでX正方向との角度を取得しその角度に応じて塗り分けます。
4回少しずらして処理をしているのはアンチエイリアスです。

おまけ

Percentを0.5付近、cycletimeを3桁とかにすると不思議な模様が出てきます。(少なくとも私の環境では)
(意図はしてませんがこれはこれでかっこいいかもです)

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