实现思路:
在顶点着色器中获取物体在世界空间下的某一轴的坐标传递给片元着色器,
o.val = v.vertex.x;
然后在片元着色器中采样两张图片,根据输入的偏移值的更换两张图片的显示。
fixed4 t1 = tex2D(_MainTex, i.texcoord);
fixed4 t2 = tex2D(_SubTex, i.texcoord1);
fixed4 col = t1 * max(0,sign(_Offset - i.val)) + t2 * max(0,sign(i.val - _Offset));
实现效果:
完整代码:
Shader "CustomTex"
{
Properties {
_MainTex ("MainTex", 2D) = "white" {}
_SubTex ("SubTex",2D) = "white"{}
_Offset("Offset",Range(-10,10)) = 1
}
SubShader {
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
float val : TEXCOORD2;
};
sampler2D _MainTex;
uniform float4 _MainTex_ST;
sampler2D _SubTex;
uniform float4 _SubTex_ST;
float _Offset;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.texcoord1 = TRANSFORM_TEX(v.texcoord1, _SubTex);
o.val = v.vertex.x;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 t1 = tex2D(_MainTex, i.texcoord);
fixed4 t2 = tex2D(_SubTex, i.texcoord1);
fixed4 col = t1 * max(0,sign(_Offset - i.val)) + t2 * max(0,sign(i.val - _Offset));
return col;
}
ENDCG
}
}
}
标签:MainTex,val,fixed4,UnityShaderLab,vertex,两张,切换,Offset,SubTex From: https://blog.csdn.net/weixin_50702814/article/details/144007853