贴图填满相机视口
效果图
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 //cull需要关闭, 否则将不显示 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; 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]; //贴图(0, 0)对应相机左下角, (0, 1)对应相机左上角, 以此类推 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 } } }
动态图
使用图集Sprite也没问题