Unity 卷动的波浪

效果图:




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

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

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


[csharp]  view plain  copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class WaterWave : MonoBehaviour {  
  5.   
  6.     public float scale = 0.5f;  
  7.     public float speed = 1f;  
  8.     public bool isMultiply = false;//若为true则波浪数变多  
  9.   
  10.     private Mesh mesh;  
  11.     private Vector3[] baseVertex;  
  12.     private Vector3[] nowVertex;  
  13.   
  14.     // Use this for initialization  
  15.     void Start ()   
  16.     {  
  17.         mesh = GetComponent<MeshFilter>().mesh;  
  18.         baseVertex = mesh.vertices;  
  19.         nowVertex = mesh.vertices;  
  20.     }  
  21.       
  22.     // Update is called once per frame  
  23.     void Update ()   
  24.     {  
  25.         for (int i = 0; i < baseVertex.Length; i++)  
  26.         {  
  27.             nowVertex[i] = baseVertex[i];  
  28.   
  29.             if (isMultiply)  
  30.             {  
  31.                 nowVertex[i].y += Mathf.Sin(Time.time * speed + baseVertex[i].x + baseVertex[i].z) * scale;  
  32.             }  
  33.             else  
  34.             {  
  35.                 nowVertex[i].y += Mathf.Sin(Time.time * speed + baseVertex[i].x) * scale +  
  36.                                   Mathf.Sin(Time.time * speed + baseVertex[i].z) * scale;  
  37.             }  
  38.         }  
  39.   
  40.         mesh.vertices = nowVertex;  
  41.     }  
  42. }  
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值