OldBlueWater/BlueWater/Assets/02.Scripts/WaterAndShip/Gerstner wave/GerstnerWave.cs

32 lines
969 B
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GerstnerWave : MonoBehaviour
{
public float waveLength = 0.1f;
public float amplitude = 0.1f;
public float speed = 1f;
public Vector2 direction = new Vector2(1.0f, 0.0f);
2023-08-03 01:03:08 +00:00
public void Update()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float waveNumber = 2.0f * Mathf.PI / waveLength;
float phaseConstant = speed * waveNumber;
for (int i = 0; i < vertices.Length; i++)
{
Vector3 vertex = vertices[i];
float dotProduct = Vector2.Dot(new Vector2(vertex.x, vertex.z), direction);
float wavePhase = waveNumber * dotProduct + phaseConstant * Time.time;
vertex.y = amplitude * Mathf.Sin(wavePhase);
vertices[i] = vertex;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
}
}