首页 > 其他分享 >贴图填满相机视口

贴图填满相机视口

时间:2023-01-29 00:22:54浏览次数:44  
标签:贴图 填满 MainTex corners uv vertex texcoord 视口 float2

效果图

1) FillCamera.shader和材质FillCamera.mat,将材质贴图设置为bg 2) 场景中创建一个Quad(Plane不行),材质设置为FillCamera.mat

 

FillCamera.shader

Shader "My/FillCamera"
{
    Properties
    {
        _MainTex("Base (RGB)", 2D) = "white" {}
    }

    SubShader
    {
        Tags
        { 
            "RenderType" = "Opaque" 
        }
        LOD 100

        Pass
        {
            Cull Off

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0

            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 texcoord : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            
            float4 _ProjectPos[4];

            v2f vert(appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.vertex.xyzw = o.vertex.xyzw / o.vertex.w;

                float2 uv = v.texcoord;
                int index = floor(uv.x * 2 + uv.y); //(0, 0), (0, 1), (1, 0), (1, 1)

                float2 corners[4]; // lt lb rt rb
                corners[0] = float2(-1, -1);
                corners[1] = float2(-1, 1);
                corners[2] = float2(1, -1);
                corners[3] = float2(1, 1);
                o.vertex.xy = corners[index];

                #if UNITY_UV_STARTS_AT_TOP
                uv.y = 1.0 - uv.y;
                #endif
                o.texcoord = TRANSFORM_TEX(uv, _MainTex);
                return o;
            }

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

            ENDCG
        }
    }
}

动态图

 

标签:贴图,填满,MainTex,corners,uv,vertex,texcoord,视口,float2
From: https://www.cnblogs.com/sailJs/p/16573334.html

相关文章