Created
May 25, 2015 09:30
-
-
Save Oxygamer/7cf831fcb54493fd64d4 to your computer and use it in GitHub Desktop.
Generation of wave from dynamic mesh in Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class MeshWave : MonoBehaviour { | |
private MeshFilter MeshF; | |
private Mesh DynamicMesh; | |
//Arrays for mesh geometry | |
public GameObject MeshObject; | |
private Vector3[] MeshVertices = new Vector3[4]; | |
private Vector3[] MeshNormals = new Vector3[4]; | |
private Vector2[] UV = new Vector2[4]; | |
private int[] Tri = new int[4]; | |
//Size of mesh parts | |
private float mesh_scale = 10f; | |
public int MeshLength; | |
public float WaveDeep=1f; | |
public float WaveLength; | |
float Alpha; | |
//this move sin wave function | |
private float counter = 0f; | |
public float WaveSpeed = 1f; | |
//Speed of cycle. 25 looks smooth and good for performance | |
public float RefreshRate = 25f; | |
// Use this for initialization | |
void Start () { | |
//Wave parameter to save wave form independent from mesh size | |
Alpha = 1f / MeshLength*WaveLength; | |
//Generating mesh | |
LoadMesh(); | |
//Run cicle that update mesh | |
InvokeRepeating("MoveMesh", 1f, 1f/RefreshRate); | |
} | |
void LoadMesh() | |
{ | |
Vector3 pos = Vector3.zero; | |
pos.z = 0.01f; | |
float x = 0f; | |
float y = 0f; | |
int quad_count = 0; | |
//Init arrays- size depends from mesh size | |
Tri = new int[MeshLength * 6]; | |
MeshVertices = new Vector3[MeshLength * 4]; | |
UV = new Vector2[MeshLength * 4]; | |
//Calculate texture coordinates scale | |
float texture_part = (float)1f / MeshLength; | |
//Generates mesh : one loop - one quad | |
for (int w = 0; w < MeshLength; w++) | |
{ | |
int index = quad_count * 4; | |
MeshVertices[index] = new Vector3(x, y, pos.z); | |
MeshVertices[index + 1] = new Vector3(x + mesh_scale, y, pos.z); | |
MeshVertices[index + 2] = new Vector3(x, y + mesh_scale, pos.z); | |
MeshVertices[index + 3] = new Vector3(x + mesh_scale, y + mesh_scale, pos.z); | |
//Calculate texture cordinates | |
UV[index] = new Vector2(w * texture_part,0f); | |
UV[index + 1] = new Vector2(w * texture_part, texture_part); | |
UV[index + 2] = new Vector2((w + 1) * texture_part, 0f); | |
UV[index + 3] = new Vector2((w + 1) * texture_part, texture_part); | |
int tri_index = quad_count * 6; | |
//Assign vertex indexes to trianles array. | |
//Every triangle polygon = 3 vertices. | |
//Every quad polygon= 6 vertices. | |
Tri[tri_index] = index; | |
Tri[tri_index + 1] = index + 1; | |
Tri[tri_index + 2] = index + 2; | |
Tri[tri_index + 3] = index + 1; | |
Tri[tri_index + 4] = index + 3; | |
Tri[tri_index + 5] = index + 2; | |
quad_count++; | |
//Increase our wave in Y direction | |
y += mesh_scale; | |
} | |
//Create new mesh | |
DynamicMesh = new Mesh(); | |
DynamicMesh.name = "DynamicMesh"; | |
DynamicMesh.vertices = MeshVertices; | |
DynamicMesh.uv = UV; | |
DynamicMesh.triangles = Tri; | |
DynamicMesh.RecalculateNormals(); | |
DynamicMesh.MarkDynamic(); | |
MeshF = MeshObject.GetComponent<MeshFilter>(); | |
//Assign mesh to MeshFilter and MeshCollider of our GameObject | |
MeshF.sharedMesh = DynamicMesh; | |
MeshObject.GetComponent<MeshCollider>().sharedMesh = DynamicMesh; | |
} | |
//Update circle | |
public void MoveMesh() | |
{ | |
if (DynamicMesh != null) | |
{ | |
//Get vertices from mesh | |
MeshVertices = DynamicMesh.vertices; | |
int w = 0; | |
float deep0; | |
float deep1; | |
int quad_count = 0; | |
// Move every vertex | |
for (w = 0; w < MeshLength; w++) | |
{ | |
//Get depth value from wave function | |
deep0 = RockNRoll(w+counter); // depth of polygon edge 1 | |
deep1 = RockNRoll(w+1+counter); //depth of polygon edge 2 | |
//Assign deep values | |
int index = quad_count * 4; | |
MeshVertices[index].z = deep0; | |
MeshVertices[index + 1].z = deep0; | |
MeshVertices[index + 2].z = deep1; | |
MeshVertices[index + 3].z = deep1; | |
quad_count++; | |
} | |
//This move wave function | |
counter += WaveSpeed; | |
//Assign all calculates vertices to our mesh | |
DynamicMesh.vertices = MeshVertices; | |
//After this mesh reflect light properly | |
DynamicMesh.RecalculateNormals(); | |
} | |
} | |
//Wave function | |
float RockNRoll(float x) | |
{ | |
//Use Sin function for wave movement. You can also play with other math functions and parameters ) | |
return Mathf.Sin(x * 2f * Mathf.PI * Alpha*WaveLength) * WaveDeep; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment