在上一篇文章中,我们定位了通用的Shader序列帧动画的通用起始点位置。
在这篇文章中,我们实现一下在U方向上的走格。
o.uv.x +=_Time.y;
o.uv.x += floor(_Time.y);
o.uv.x += floor(_Time.z *_Sequence.y)/_Sequence.y;
_Sequence(“Row(X) Column(Y) Speed(Z)”,Vector) = (1,1,1,1)
o.uv.x += floor(_Time.z * _Sequence.z *_Sequence.y )/_Sequence.y;
因为,我们需要实现走完一行再走下一行的效果。
所以,就需要把 v 方向上的 x 扩大到原来的 Colum(列) 倍
v ( t ) = u ( t C o l u m n ) = ? C o l u m n ? t C o l u m n ? C o l u m n v(t) = u(\frac{t}{Column}) = \frac{\lfloor \frac{Column·t}{Column} \rfloor}{Column} v(t)=u(Columnt?)=Column?ColumnColumn?t???
并且,因为 v 方向是从上到下移动
所以,这里需要减去该函数值而不是加上
o.uv.y -= floor(_Time.y *_Sequence.y * _Sequence.z / _Sequence.y)/_Sequence.x;
Shader "MyShader/URP/P3_9"
{
Properties
{
_Color("Color",Color) = (1,1,1,1)
_MainTex("MainTex",2D) = "white"{}
_Sequence("Row(X) Column(Y) Speed(Z)",Vector) = (1,1,1,1)
}
SubShader
{
Tags
{
//告诉引擎,该Shader只用于 URP 渲染管线
"RenderPipeline"="UniversalPipeline"
//渲染类型
"RenderType"="Transparent"
//渲染队列
"Queue"="Transparent"
}
Blend SrcAlpha OneMinusSrcAlpha Zwrite On
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Attribute
{
float3 vertexOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varying
{
float4 vertexCS : SV_POSITION;
float2 uv : TEXCOORD1;
float fogCoord : TEXCOORD2;
};
CBUFFER_START(UnityPerMaterial)
float4 _Color;
float4 _MainTex_ST;
half4 _Sequence;
CBUFFER_END
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
Varying vert(Attribute v)
{
Varying o;
o.vertexCS = TransformObjectToHClip(v.vertexOS);
o.uv = float2(v.uv.x/_Sequence.y,v.uv.y/_Sequence.x + (_Sequence.x - 1)/_Sequence.x);
o.uv.x += floor(_Time.y *_Sequence.y * _Sequence.z)/_Sequence.y;
o.uv.y -= floor(_Time.y *_Sequence.y * _Sequence.z / _Sequence.y)/_Sequence.x;
//o.uv.x += floor(_Time.y);
//o.uv = float2(v.uv.x/4,v.uv.y/4);
//o.uv = TRANSFORM_TEX(v.uv,_MainTex);
o.fogCoord = ComputeFogFactor(o.vertexCS.z);
return o;
}
half4 frag(Varying i) : SV_Target
{
float4 mainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv);
float4 col = mainTex * _Color;
col.rgb = MixFog(col,i.fogCoord);
return col;
}
ENDHLSL
}
}
}