I’m creating a sea based on a plane that I deform with a sine wave (cycling through the vertices and offsetting the y value).
Code:
void CalculateWaves ()
{
if (m_BaseHeight == null)
m_BaseHeight = m_WaterPlane.vertices;
Vector3[] WaterVertices = new Vector3[m_BaseHeight.Length];
for (int i=0; i< WaterVertices.Length; i++)
{
Vector3 Vertex = m_BaseHeight*;*
Vertex.y += Mathf.Sin(Time.time * m_WaveSpeed+ m_BaseHeight.x+ m_BaseHeight.y + m_BaseHeight.z)m_WaveScale;
_ WaterVertices = Vertex;
}_
m_WaterPlane.vertices = WaterVertices;
m_WaterPlane.RecalculateNormals ();*
However, this only allows the sine wave to go diagonally over the plane from top right to bottom left (and reverse if you make m_WaveSpeed negative).
Ideally I’d like this to be controlled by a vector, so I can later create a wind class that provides the input.
I’m also looking for a way to use a noisemap texture to influence these waves.
I haven’t tried this out yet, but my idea would be basically this:
float incrementAmount= texture.getpixels.length/mesh.getvertices.length;
int j=0;
for (int i=0; i<vertices; i++)
{
vertices*.y+=pixels[j].r;*
j+=incrementAmount;
}
Is this a good way, or are there more efficient/accurate ways to go about this?
Well, at the moment you use the local position within the mesh to offset your sin function. Since you add x and z together you get the diagonal behaviour since it doesn’t matter if you increase x or z.
All you have to do is rotate the system around the y axis:
Quaternion rotation;
void CalculateWaves ()
{
if (m_BaseHeight == null)
m_BaseHeight = m_WaterPlane.vertices;
Vector3[] WaterVertices = new Vector3[m_BaseHeight.Length];
for (int i=0; i< WaterVertices.Length; i++)
{
Vector3 Vertex = m_BaseHeight*;*
Vector3 dir = rotation * Vertex;
Vertex.y += Mathf.Sin(Time.time * m_WaveSpeed+ dir.x + dir.y + dir.z) * m_WaveScale;
WaterVertices = Vertex;
}
m_WaterPlane.vertices = WaterVertices;
m_WaterPlane.RecalculateNormals ();
}
edit
I’ve just played around a bit and extended my test script a bit
Now you can have 1 or multiple wave systems overlapping. The test script is designed for two systems at the moment since i’ve hardcoded the direction setting:
// MeshWave.cs
// C#
using UnityEngine;
using System.Collections;
public class MeshWave : MonoBehaviour
{
* [System.Serializable]*
* public class WaveSystem*
* {*
* private Quaternion m_Rotation;
private Vector3 m_Direction;
_ public float Length;_
_ public float Speed;_
_ public float Scale;_
_ public Vector3 Direction*_
* {*
* get {return m_Direction;}
_ set*_
* {*
* m_Direction = value.normalized;
m_Rotation = Quaternion.Inverse(Quaternion.LookRotation(-m_Direction));
_ }_
_ }*_
* public Vector3 ApplyHeight(Vector3 aPos)*
* {*
Vector3 dir = m_Rotation * aPos;
* if (Length != 0)*
_ dir.z = 1.0f/Length;
aPos.y += Mathf.Sin(Time.time * Speed+ dir.z ) * Scale;_
_ return aPos;_
_ }*_
* }*
* public Transform Target1;*
* public Transform Target2;*
* public WaveSystem[] waves;*
* Vector3 m_BaseHeight;
Mesh m_WaterPlane;*
* void CalculateWaves ()*
* {*
* waves[0].Direction = Target1.position - transform.position;*
* waves[1].Direction = Target2.position - transform.position;*
* if (m_BaseHeight == null)
m_BaseHeight = m_WaterPlane.vertices;
Vector3 WaterVertices = new Vector3[m_BaseHeight.Length];
_ for (int i=0; i< WaterVertices.Length; i++)_
_ {_
Vector3 Vertex = m_BaseHeight;
_ foreach(var W in waves)
{
Vertex = W.ApplyHeight(Vertex);
}
WaterVertices = Vertex;
}_
m_WaterPlane.vertices = WaterVertices;
m_WaterPlane.RecalculateNormals ();
_ }
void Start ()
{_
m_WaterPlane = GetComponent().mesh;
_ }*_
* void Update ()*
* {*
* CalculateWaves();*
* }*
}