コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】頂点シェーダで済む処理はフラグメントシェーダではなく頂点シェーダで記述する

概要

v2f vert(appdata_t v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    o.color = v.color;
    return o;
}

fixed4 frag(v2f i): SV_Target
{
    fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
    return col;
}

例えば、上記のようにフラグメントシェーダで色の計算を行うのではなく

v2f vert(appdata_t v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    o.color = 2.0f * v.color * _TintColor;
    return o;
}

fixed4 frag(v2f i): SV_Target
{
    fixed4 col = i.color * tex2D(_MainTex, i.texcoord);
    return col;
}

事前に頂点シェーダで色の計算を済ませるほうが良いパフォーマンスになる