KITASENJU DESIGN BLOG

memo, html, javascript, unity

Quadを全画面にするシェーダー

Quadに変換マトリクスは使わず、スクリーン座標-1<xyz<1を見越して、全画面になるように変形。 でもカリングされると消える。その場合はmesh.boundsを大きくしとくか、カメラの子供にして視界から外れないようにする

Shader "Unlit/FullScreenQuad"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _PosZ("PosZ",Range(0,1)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100
        cull off

        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;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_TexelSize;
            float _PosZ;
            
            v2f vert (appdata v)
            {
                v2f o;
                
                o.vertex = v.vertex;
                o.vertex.x *= 2.0;
                o.vertex.y *= 2.0;
                o.vertex.z = _PosZ;//ゼロなら最背面、1なら最前面
                o.vertex.w = 1.0;

                // デバイスによって反転する対策
                // we're rendering with upside-down flipped projection,
                // so flip the vertical UV coordinate too
                // https://docs.unity3d.com/ja/540/Manual/SL-PlatformDifferences.html
                if (_ProjectionParams.x < 0)
                    v.uv.y = 1 - v.uv.y;
   
                o.uv = v.uv;

                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                
                clip(col.a-0.5);

   
 
                return col;
            }
            ENDCG
        }
    }
}

"FOOTER"