2023-08-02 17:10:40 +00:00
|
|
|
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 02:54:21 +00:00
|
|
|
void Update()
|
2023-08-02 17:10:40 +00:00
|
|
|
{
|
|
|
|
Mesh mesh = GetComponent<MeshFilter>().mesh;
|
|
|
|
Vector3[] vertices = mesh.vertices;
|
2023-08-03 02:54:21 +00:00
|
|
|
Vector3[] normals = mesh.normals;
|
2023-08-02 17:10:40 +00:00
|
|
|
|
|
|
|
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;
|
2023-08-03 02:54:21 +00:00
|
|
|
|
|
|
|
// Calculate the normal at the vertex
|
|
|
|
float cosPhase = Mathf.Cos(wavePhase);
|
|
|
|
float dx = -waveNumber * amplitude * direction.x * cosPhase;
|
|
|
|
float dz = -waveNumber * amplitude * direction.y * cosPhase;
|
|
|
|
normals[i] = new Vector3(dx, 1.0f, dz).normalized;
|
2023-08-02 17:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mesh.vertices = vertices;
|
2023-08-03 02:54:21 +00:00
|
|
|
mesh.normals = normals;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector3 GetWaveNormal(Vector3 position)
|
|
|
|
{
|
|
|
|
float waveNumber = 2.0f * Mathf.PI / waveLength;
|
|
|
|
float phaseConstant = speed * waveNumber;
|
|
|
|
float dotProduct = Vector2.Dot(new Vector2(position.x + transform.position.x, position.z + transform.position.z), direction);
|
|
|
|
float wavePhase = waveNumber * dotProduct + phaseConstant * Time.time;
|
|
|
|
|
|
|
|
float cosPhase = Mathf.Cos(wavePhase);
|
|
|
|
|
|
|
|
// We differentiate the wave function to get the slope at the current point
|
|
|
|
float dx = -waveNumber * amplitude * direction.x * cosPhase;
|
|
|
|
float dz = -waveNumber * amplitude * direction.y * cosPhase;
|
|
|
|
|
|
|
|
Vector3 normal = new Vector3(dx, 1.0f, dz).normalized;
|
|
|
|
return normal;
|
2023-08-02 17:10:40 +00:00
|
|
|
}
|
|
|
|
}
|