表面着色器实现:
Shader "ScrollUVs"
{
Properties
{
_MainTex("Main Tex",2D) = "white"{}
_MainTint("Main Tint",Color) = (1,1,1,1)
_ScrollXSpeed("Scroll X Speed",Range(0,10)) = 2
_ScrollYSpeed("Scroll Y Speed",Range(0,10)) = 2
}
SubShader
{
Tags{"RenderType" = "Opaque"}
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _MainTint;
fixed _ScrollXSpeed;
fixed _ScrollYSpeed;
struct Input
{
half2 uv_MainTex;
};
void surf(Input IN,inout SurfaceOutput o)
{
half2 scrollUV = IN.uv_MainTex;
fixed xScrollValue = _ScrollXSpeed * _Time;
fixed yScrollValue = _ScrollYSpeed * _Time;
scrollUV += half2(xScrollValue,yScrollValue);
fixed4 col = tex2D(_MainTex,scrollUV) * _MainTint;
o.Albedo = col.rgb;
o.Alpha = col.a;
}
ENDCG
}
FallBack "Diffuse"
}
顶点-片元着色器实现:
Shader "ScrollUVs"
{
Properties
{
_MainTex("Main Tex",2D) = "white"{}
_MainTint("Main Tint",Color) = (1,1,1,1)
_ScrollXSpeed("Scroll X Speed",Range(0,10)) = 2
_ScrollYSpeed("Scroll Y Speed",Range(0,10)) = 2
}
SubShader
{
Pass
{
Tags{"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
sampler2D _MainTex;
fixed4 _MainTex_ST;
fixed4 _MainTint;
fixed _ScrollXSpeed;
fixed _ScrollYSpeed;
struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos:SV_POSITION;
half2 uv:TEXCOORD0;
float3 worldPos:TEXCOORD1;
float3 worldNormal:TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
o.worldNormal = mul(unity_ObjectToWorld,v.normal);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag(v2f i):SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
half2 scrollUV = i.uv;
fixed xScrollValue = _ScrollXSpeed * _Time;
fixed yScrollValue = _ScrollYSpeed * _Time;
scrollUV += half2(xScrollValue,yScrollValue);
fixed3 albedo = tex2D(_MainTex,scrollUV) * _MainTint.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * dot(worldNormal,lightDir);
return fixed4(ambient+diffuse,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}