片段着色器:
1.纹理采样
2.灯光作用
材料 需要 河流模型 fbx 和 河流贴图
河流效果
让 纹理 滚动起来 (不是让模型动起来)
在片段着色器 入口 代码重点
fixed4 frag (v2f i) : SV_Target
{
float2 tmpUV = i.uv;
tmpUV.x += _Time.x;
tmpUV.y += _Time.y;
fixed4 col = tex2D(_MainTex,tmpUV);
return col;
}
UV(笛卡尔坐标系):
图片贴图 跟 填充到屏幕显示 一对一匹配填充
tex2D(_MainTex,i.uv)
_MainTex : 一张图片
i.uv : 表示比例 UV坐标(2维坐标)。[0,1]
河流图片 Unity菜单 Wrap Mold设置 为 Repeat
Clamp : 表示 UV坐标 超过1 永远取值 1地方的像素,如果 UV 比 0 小的 永远取值 0 地方的像素。
例如 UV -1 取值 为0 , UV 1.5 取值 为 1
Repeat : 表示 UV 超过 1 的 取小数部分 跟 【0,1】(重置获取像素)
例如 UV 1.5 取值为 0.5 , UV 2.3 取值 为 0.3 , UV 3 取值 为 0;
以下图解 (方便理解 Clamp 和 Repeat 参数说明)
源代码 献上 :
Shader "Custom/River" {
Properties {
_ MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float2 tmpUV = i.uv;
tmpUV.x += _Time.x;
tmpUV.y += _Time.y;
fixed4 col = tex2D(_MainTex,tmpUV);
return col;
}
ENDCG
}
}
}