Hey all,
I’m attempting to implement a single Gerstner wave so that it deforms vertices of the mesh properly, but at the moment it just moves the entire mesh around in circles, with its speed changing as i change the amplitude and time parameters.
Is this an issue with the way I’m changing the array of vertices? Or an issue with my implementation of the wave formula? Please forgive me for any bad coding practices or obvious errors, I’m very new to unity and coding in general, and this is all an amalgamation of several different articles and tutorials.
It’s all in a single script right now, attached to the plane:
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using Unity.Mathematics;
using UnityEngine.UIElements;
using UnityEngine.ProBuilder;
using System.Runtime.InteropServices.WindowsRuntime;
public class GerstnerCodeTest : MonoBehaviour
{
private MeshFilter meshFilter;
[SerializeField] private Vector3 direction = Vector3.zero;
[SerializeField] private Vector3 vertexPosition = Vector3.zero;
[SerializeField] private float amplitude = 1f;
[SerializeField] private float time = 1f;
[SerializeField] private float phase = 1f;
[SerializeField] private float frequency = 1f;
[SerializeField] private float gravity = 9.81f;
[SerializeField] private float depth = 1f;
private void Awake()
{
meshFilter = GetComponent<MeshFilter>();
}
private void FixedUpdate()
{
Vector3[] vertices = meshFilter.mesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = Displacement(vertices[i]);
}
meshFilter.mesh.vertices = vertices;
meshFilter.mesh.RecalculateNormals();
}
private Vector3 Displacement(Vector3 position)
{
Vector3 displacement = Gerstner(direction, vertexPosition, amplitude, time, gravity, phase, depth);
Vector3 newPosition = displacement + position;
return newPosition;
}
private Vector3 Gerstner(Vector3 direction, Vector3 vertexPosition, float amplitude, float time, float gravity, float phase, float depth)
{
//X COMPONENT
float xComponent =
-1 * ((direction.x / direction.magnitude) * (amplitude / (math.tanh(direction.magnitude * depth)))
* Mathf.Sin(Theta(direction, vertexPosition, gravity, depth, time, phase)));
//Y COMPONENT
float yComponent = amplitude * Mathf.Cos(Theta(direction, vertexPosition, gravity, depth, time, phase));
//Z COMPONENT
float zComponent =
-1 * ((direction.z / direction.magnitude) * (amplitude / (math.tanh(direction.magnitude * depth)))
* Mathf.Sin(Theta(direction, vertexPosition, gravity, depth, time, phase)));
return new Vector3 (xComponent, yComponent, zComponent);
}
private float Theta(Vector3 direction, Vector3 vertexPosition, float gravity, float depth, float time, float phase)
{
float theta = (direction.x * vertexPosition.x + direction.z * vertexPosition.z) - (AngularFrequency(gravity, direction, depth) * time * Time.time - phase);
return theta;
}
private float AngularFrequency(float gravity, Vector3 direction, float depth)
{
float angularFrequency = Mathf.Sqrt(gravity * direction.magnitude * math.tanh(direction.magnitude * depth));
return angularFrequency;
}
}