OldBlueWater/BlueWater/Assets/02.Scripts/WaterAndShip/Gerstner wave/GerstnerWave.cs
2023-08-03 10:03:08 +09:00

32 lines
969 B
C#

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);
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();
}
}