1.下面主要拿一张类似上面的底层图片,通过控制外部参数 xScroll Speed ,yScroll Speed来调整水流速度参数。
Shader "SeaShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ScrollXSpeed ("X Scrll Speed", Range(0, 10)) = 2
_ScrollYSpeed ("Y Scrll Speed", Range(0, 10)) = 2
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _ScrollXSpeed;
fixed _ScrollYSpeed;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
fixed xScrollValue = _ScrollXSpeed * _Time;
fixed yScrollValue = _ScrollYSpeed * _Time;
v.uv += fixed2(xScrollValue, yScrollValue);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
创建海岸效果起先要有一个模型类似环岛的如下橙色区域,以及类似下面的渐变图片。
.
图片属性要调整如下
5.好了用代码控制UV偏移,放在海岸模型上,海岸模型shader为上面截图的透明shader。
using UnityEngine;
using System.Collections;
public class water : MonoBehaviour
{
[Header("水波速度")]
public float scrollSpeed;
private Vector2 offset;
private bool isBack;
// Scroll main texture based on time
//根据时间滚动主纹理
void Update()
{
offset = gameObject.GetComponent<Renderer>().material.mainTextureOffset;
if (!isBack)
{
offset.y += scrollSpeed * Time.deltaTime;
if (offset.y >= 0.15)
isBack = true;
}
else
{
offset.y -= scrollSpeed * Time.deltaTime;
if (offset.y <= -0.2 )
isBack = false;
}
gameObject.GetComponent<Renderer>().material.mainTextureOffset = offset;
}
}
- 里面有一个问题要注意是,海岸图片要设置为CLAMP 属性,否则会一直白边。