需要的效果
最终实现的效果大概如下:
4个贴图上去
这里随便放一个切分的图。
Shader代码如下
直接上代码:
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)// Unlit shader. Simplest possible textured shader.
// - no lighting
// - no lightmap support
// - no per-material colorShader "Unlit/TextureMy" {
Properties {_MainTex ("Base (RGB)", 2D) = "white" {}_MainTex0 ("Base0 (RGB)", 2D) = "white" {}_MainTex1 ("Base1 (RGB)", 2D) = "white" {}_MainTex2 ("Base2 (RGB)", 2D) = "white" {}_MainTex3 ("Base3 (RGB)", 2D) = "white" {}
}SubShader {Tags { "RenderType"="Opaque" }LOD 100Pass {CGPROGRAM#pragma vertex vert#pragma fragment frag#pragma target 2.0#pragma multi_compile_fog#include "UnityCG.cginc"struct appdata_t {float4 vertex : POSITION;float2 texcoord : TEXCOORD0;UNITY_VERTEX_INPUT_INSTANCE_ID};struct v2f {float4 vertex : SV_POSITION;float2 texcoord : TEXCOORD0;UNITY_FOG_COORDS(1)UNITY_VERTEX_OUTPUT_STEREO};sampler2D _MainTex;float4 _MainTex_ST;sampler2D _MainTex0;sampler2D _MainTex1;sampler2D _MainTex2;sampler2D _MainTex3;v2f vert (appdata_t v){v2f o;UNITY_SETUP_INSTANCE_ID(v);UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);o.vertex = UnityObjectToClipPos(v.vertex);o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);UNITY_TRANSFER_FOG(o,o.vertex);return o;}fixed4 frag (v2f i) : SV_Target{fixed4 col;float2 uv = i.texcoord;float2 uvFix = uv;if (uv.x < 0.5){if(uv.y < 0.5){uvFix.x = uv.x * 2;uvFix.y = uv.y * 2;col = tex2D (_MainTex2 , uvFix );}else{uvFix.x = uv.x * 2;uvFix.y = uv.y * 2 - 1;col = tex2D (_MainTex0 , uvFix ); }}else{if(uv.y < 0.5){uvFix.x = uv.x * 2 - 1;uvFix.y = uv.y * 2 ;col = tex2D (_MainTex3 , uvFix );}else{uvFix.x = uv.x * 2 - 1;uvFix.y = uv.y * 2 - 1;col = tex2D (_MainTex1 , uvFix ); }}//fixed4 col = tex2D(_MainTex, i.texcoord);UNITY_APPLY_FOG(i.fogCoord, col);UNITY_OPAQUE_ALPHA(col.a);return col;}ENDCG}
}}
uv左下角是0,0,所以第一个if判定再左下角,使用_MainTex2 纹理,那么如果铺满屏幕问题,需要*2.
其他部分的类似。
按照这个思路可以扩充8*8等其他。
特别注意
这里要注意,需要把图的WrapMode改为Clamp,repeat会有接缝。