//The Plane Vertices calculations have been made by Ben Britten (Open source project: http://benbritten.com/2009/05/19/fun-with-unity/)
//And then modified and re-wrote to suit my needs
// An extremely dilute part of this script has been written thanks to the help of some contributors on Stack Overflow.
usingUnityEngine;
...
...
@@ -7,7 +6,7 @@ public class RippleEffect : MonoBehaviour {
privateint[]buffer1;//Is the first buffer of vertices
privateint[]buffer2;//Is the second buffer of vertices
privateint[]vertex_indices;//Sotres the indices of the vertices
privateint[]vertex_indexes;//Stores the indexes of the vertices
//Because we want a whole plane to distort, we need the plane have more then just 4 vertices to simulate the wave
//This is why we need to create our own plane with many many vertices
...
...
@@ -20,8 +19,9 @@ public class RippleEffect : MonoBehaviour {
privateintsplash_force_mic=0;//Variable that will update the splash_force according to the mic
privateboolswap_buffer=true;//Switches between the first and the second buffer
privateboolticked=false;
publicinttick_at=120;
publicintupdate_rate=120;
publicintmic_threshold=200;
publicintspread_bit_shift=1;
publicintcols=128;//Number of the columns we want our plane to have (The bigger this is, the more computer intensive the calculations will be)
publicintrows=128;//Number of the rows we want our plane to have
...
...
@@ -30,19 +30,19 @@ public class RippleEffect : MonoBehaviour {
MeshFiltermesh_filter=GetComponent<MeshFilter>();//Attaches the mesh filter in he object to the mesh filter variable
mesh=mesh_filter.mesh;//Sets mesh as the mesh of the mesh filter
vertices=mesh.vertices;//Sets the vertices variable to the vertices of the the created mesh
buffer1=newint[vertices.Length];//Initialises both buffers with the number of vertices in the mesh
buffer1=newint[vertices.Length];//Initializes both buffers with the number of vertices in the mesh
buffer2=newint[vertices.Length];
Boundsbounds=mesh.bounds;
floatxStep=(bounds.max.x-bounds.min.x)/cols;//Sets the number of tiles to annimate on the X axies according to the number of columns we selected and the size of the mesh
floatzStep=(bounds.max.z-bounds.min.z)/rows;//Sets the number of tiles to annimate on the Y axies according to the number of columns we selected and the size of the mesh
floatxStep=(bounds.max.x-bounds.min.x)/cols;//Sets the number of tiles to animate on the X axis according to the number of columns we selected and the size of the mesh
floatzStep=(bounds.max.z-bounds.min.z)/rows;//Sets the number of tiles to animate on the Y axis according to the number of columns we selected and the size of the mesh
vertex_indices=newint[vertices.Length];// Create an int array and allocates it's size to the number of vertices
vertex_indexes=newint[vertices.Length];// Create an int array and allocates it's size to the number of vertices
/* ***************** Initializes and Populates the buffers (I DID NOT WRITE THIS PART, I ONLY RE-WROTE IT TO WORK FOR MY IDEA) ******************** */
/* ***************** Initializes and Populates the buffers (These two for loops have originally been written by Ben Britten) ******************** */
current_vertices[current_index].y+=(current_buffer[i]*1.0f/splash_force);//This is what raises the current "tile"'s Y value by the splash force times the maximum height of the wave}
//print(current_vertices[current_index].y);
...
...
@@ -124,7 +124,7 @@ public class RippleEffect : MonoBehaviour {
/* Ray cast function to set the splash position */
RaycastHithit;//Will store all the info on the object the ray will collide
Vector3down=PlayerSettings.instance.player.transform.TransformDirection(Vector3.down);//Create a raycast from the player's position downwards
if(Physics.Raycast(PlayerSettings.instance.player.transform.position,down,outhit))// Draws a ray dowards from the player and stores the impact location
if(Physics.Raycast(PlayerSettings.instance.player.transform.position,down,outhit))// Draws a ray downwards from the player and stores the impact location
{
// First we must init the tiles
Boundsbounds=mesh.bounds;
...
...
@@ -141,7 +141,6 @@ public class RippleEffect : MonoBehaviour {
}
//THE CODE THAT MAKES THE WAVE MOVE AND FADE OUT
//From Ben Britten's code
voidripple_effect(int[]source,int[]dest){
intx=0;
inty=0;
...
...
@@ -149,7 +148,7 @@ public class RippleEffect : MonoBehaviour {
for(y=1;y<rows-1;y++){
for(x=1;x<cols;x++){
position=(y*(cols+1))+x;
dest[position]=(((source[position-1]+source[position+1]+source[position-(cols+1)]+source[position+(cols+1)])>>1)-dest[position]);//Makes the wave move forward
dest[position]=(((source[position-1]+source[position+1]+source[position-(cols+1)]+source[position+(cols+1)])>>spread_bit_shift)-dest[position]);//Makes the wave move forward
dest[position]=(int)(dest[position]*dampner);//Makes the wave fade out
}
}
...
...
@@ -157,9 +156,9 @@ public class RippleEffect : MonoBehaviour {
//Timer function to control how has you want to update the splash while using th mic