[Unity实战]制作卷动的波浪

参考链接:http://tieba.baidu.com/p/2655013091#40457365538l


效果图:




这里,我们制作的波浪是通过改变mesh上的顶点来实现的。更准确的说,是改变mesh上顶点的y值,从而形成一种高度变化的效果。

1.通过观察,我们发现每个顶点的y值变化的情况都不一样,因此,很容易想到将顶点的y值与该顶点的x,z值关联起来。

2.通过观察,我们发现第一图的波浪数较少,第二图的波浪数较多,波浪数较小说明各顶点的y值差异较大。可以通过“放大”x,z值来增加不同顶点之间的差异。


using UnityEngine;
using System.Collections;

public class WaterWave : MonoBehaviour {

    public float scale = 0.5f;
    public float speed = 1f;
    public bool isMultiply = false;//若为true则波浪数变多

    private Mesh mesh;
    private Vector3[] baseVertex;
    private Vector3[] nowVertex;

	// Use this for initialization
	void Start () 
    {
        mesh = GetComponent<MeshFilter>().mesh;
        baseVertex = mesh.vertices;
        nowVertex = mesh.vertices;
	}
	
	// Update is called once per frame
	void Update () 
    {
        for (int i = 0; i < baseVertex.Length; i++)
        {
            nowVertex[i] = baseVertex[i];

            if (isMultiply)
            {
                nowVertex[i].y += Mathf.Sin(Time.time * speed + baseVertex[i].x + baseVertex[i].z) * scale;
            }
            else
            {
                nowVertex[i].y += Mathf.Sin(Time.time * speed + baseVertex[i].x) * scale +
                                  Mathf.Sin(Time.time * speed + baseVertex[i].z) * scale;
            }
        }

        mesh.vertices = nowVertex;
	}
}


  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值