2
2

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 その7。SV_VertexIDを使った頂点カラー加算

Posted at

Twitterにアップした頂点カラー加算ImageEffectのシェーダコードを貼っておきます

SV_VertexIDを使用するので #pragma target 3.5 が必要です。
画面4角の色を指定して配列に突っ込んで、頂点番号で色を引っ張っている実装です。

VertexColorAdditive.shader
Shader "ScreenPocket/ImageEffect/VertexColor Additive"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _ltColor ( "Left Top Color", Color ) = (0,0,0,0)
        _rtColor ( "Right Top Color", Color ) = (0,0,0,0)
        _lbColor ( "Left Bottom Color", Color ) = (0,0,0,0)
        _rbColor ( "Right Bottom Color", Color ) = (0,0,0,0)
    }
    
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.5

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                uint vid : SV_VertexID; // vertex ID、 uint の必要があります
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR0;
                float4 vertex : SV_POSITION;
            };

            fixed4 _ltColor;
            fixed4 _rtColor;
            fixed4 _lbColor;
            fixed4 _rbColor;
            
            v2f vert (appdata v)
            {
                fixed4 colorArray[4] = {_lbColor, _ltColor, _rtColor, _rbColor};
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = colorArray[ v.vid ];
                return o;
            }

            sampler2D _MainTex;

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

            ENDCG
        }
    }
}

return tex2D( _MainTex, i.uv ) + i.color;

return tex2D( _MainTex, i.uv ) * i.color;
にすれば乗算合成出来るので、暗い色を載せたい場合はそちらをご準備下さい。

もしかするとiOS実機に持っていくと上下反転しちゃうかも?
その場合は
プラットフォーム特有のレンダリングの違い
にある

#if UNITY_UV_STARTS_AT_TOP

を使えば対応できそうですが、現状未検証です。。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?