原理很简单,根据时间对uv中的x轴进行位移,对于地面和云层,取不同的移动速度,分别计算对应的uv坐标,再根据uv坐标从地面和云层的纹理中分别取出对应的值,最后把两者用lerp函数进行混合作为最终结果。
下边的代码包含两个版本,一个是vertex fragment shader,是不带光照的,一个是surface shader版本,带Lambert光照,运行时场景里加上光照才会显得很明亮
Shader "Study/2_Earth"
{Properties{_Color("Main Color", Color) = (1,1,1,0.5)_MainTex("Texture", 2D) = "white" { } //地面纹理_Cloud("_Cloud", 2D) = "white" { } //云层的纹理_SpeedE("SpeedE", Float) = 0.25 //地面移动速度_SpeedC("SpeedC", Float) = 0.5 //云层移动速度}///vertex fargment shader版本/*SubShader{Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" } //其实设为常用的Opaque也是可以的Pass{CGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"float4 _Color;sampler2D _MainTex;sampler2D _Cloud;uniform float _SpeedE;uniform float _SpeedC;struct v2f{float4 pos : SV_POSITION;float2 uv : TEXCOORD0;};float4 _MainTex_ST;v2f vert(appdata_base v){v2f o;o.pos = mul(UNITY_MATRIX_MVP, v.vertex);o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);return o;}half4 frag(v2f i) : COLOR{//移动地面uv坐标并取出纹理值float u = i.uv.x + -_SpeedE * _Time;float2 uv_tmp = float2(u , i.uv.y);half4 texcol = tex2D(_MainTex, uv_tmp);texcol = texcol;//移动云层uv坐标并取出纹理值u = i.uv.x + -_SpeedC * _Time;uv_tmp = float2(u , i.uv.y);half4 texcol3 = tex2D(_Cloud, uv_tmp);half4 texcol0 = float4(1,1,1,0) * (texcol3.x);//这步操作是因为使用的图片云层颜色并不是白色// 混合二者作为最终输出half4 res = lerp(texcol, texcol0, 0.5);return res;}ENDCG}}*////带光照的surface shader版本SubShader{Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" } //其实设为常用的Opaque也是可以的CGPROGRAM#pragma surface surf Lambert #include "UnityCG.cginc"float4 _Color;sampler2D _MainTex;sampler2D _Cloud;uniform float _SpeedE;uniform float _SpeedC;// 获取_MainTex的UV信息定义输入结构体struct Input{// 在贴图变量前加上uv表示提取uv值(二维坐标)float2 uv_MainTex;};void surf(Input IN, inout SurfaceOutput o){//移动地面uv坐标并取出纹理值float u = IN.uv_MainTex.x + -_SpeedE * _Time;float2 uv_tmp = float2(u, IN.uv_MainTex.y);half4 texcol = tex2D(_MainTex, uv_tmp);texcol = texcol;//移动云层uv坐标并取出纹理值u = IN.uv_MainTex.x + -_SpeedC * _Time;uv_tmp = float2(u, IN.uv_MainTex.y);half4 texcol3 = tex2D(_Cloud, uv_tmp);half4 texcol0 = float4(1, 1, 1, 0) * (texcol3.x);//这步操作是因为使用的图片云层颜色并不是白色// 混合二者作为最终输出half4 res = lerp(texcol, texcol0, 0.5) * _Color;// RGBo.Albedo = res.rgb;// 透明度o.Alpha = 1;}ENDCG}
}
带光照的Surface版本 :
不带光照的Vertext Fragment版本:
最后附上工程文件:http://download.csdn.net/detail/yinfourever/9564149