Unity Shader 入门精要 学习笔记 滚动的背景 没有滚动起来

今天在学习Unity Shader 入门精要 第11章 滚动的背景时,按照书中所讲,写下的shader 却没有达到预期的效果

没有实现滚动,而是滚一会儿之后又跳回初始点

先上shader

Shader "Custom/RollBg"
{
    Properties
    {
		_MainTex ("Base Layer (RGB)", 2D) = "white" {}
		_DetailTex ("2nd Layer (RGB)", 2D) = "white" {}
		_ScrollX ("Base layer Scroll Speed", Float) = 1.0
		_Scroll2X ("2nd layer Scroll Speed", Float) = 1.0
		_Multiplier ("Layer Multiplier", Float) = 1
    }
    SubShader
    {
        Tags {"RenderType"="Opaque" "Queue"="Geometry"}
        Pass
		{
			Tags{"LightMode"="ForwardBase"}

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _DetailTex;
			float4 _DetailTex_ST;
			float _ScrollX;
			float _Scroll2X;
			float _Multiplier;

			struct a2v
			{
				float4 vertex : POSITION;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float4 pos : SV_POSITION;
				float4 uv : TEXCOORD0;
			};


			v2f vert (a2v v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex) + frac(float2(_ScrollX,0.0)*_Time.y);
				o.uv.zw = TRANSFORM_TEX(v.texcoord,_DetailTex) + frac(float2(_Scroll2X,0.0)*_Time.y);
				return o;
			}

			fixed4 frag (v2f i):SV_Target
			{
				fixed4 firstLayer = tex2D(_MainTex,i.uv.xy);
				fixed4 secondLayer = tex2D(_DetailTex,i.uv.zw);

				fixed4 c = lerp(firstLayer,secondLayer,secondLayer.a);
				c.rgb *= _Multiplier;
				return c;
			}

			ENDCG
		}
    }
    FallBack "VertexLit"
}

最后才发现需要在背景图片设置里面将

Wrap Mode 由Clamp 改为Repeat

 

这样就可以了。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页