Commit 7f038bbd authored by Nathan De Castro's avatar Nathan De Castro

Ripple script commented!

parent c770e24d
//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
using UnityEngine;
......@@ -35,8 +36,7 @@ public class RippleEffect : MonoBehaviour
vertex_indices = new int[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 MODFIED IT) */
/* ************************************************************************************************************************************** */
/* ***************** Initializes and Populates the buffers (I DID NOT WRITE THIS PART, I ONLY RE-WROTE IT TO WORK FOR MY IDEA) ******************** */
int vertices_index = 0;
for (vertices_index = 0; vertices_index < vertices.Length; vertices_index++)
{
......@@ -91,55 +91,60 @@ public class RippleEffect : MonoBehaviour
{
splash(); //Currently checks if the space bar has been pressed (will later change this to be according to volume of the mic)
int[] currentBuffer;
int[] current_buffer;
if (swapMe)
{
// process the ripples for this frame
ripple_effect(buffer1, buffer2);
currentBuffer = buffer2;
current_buffer = buffer2;
}
else
{
ripple_effect(buffer2, buffer1);
currentBuffer = buffer1;
current_buffer = buffer1;
}
swapMe = !swapMe;
/* ************************************************************************************************************************************** */
// apply the ripples to our buffer
Vector3[] current_vertices = new Vector3[vertices.Length];
int vertIndex;
// THE CODE THAT RAISES THE WAVE
Vector3[] current_vertices = new Vector3[vertices.Length]; // initiates a array of verctor3 per vertices
int current_index; //Will store the current index of a vertex
int i = 0;
for (i = 0; i < currentBuffer.Length; i++)
for (i = 0; i < current_buffer.Length; i++) //For all the vertices in the current buffer
{
vertIndex = vertex_indices[i];
current_vertices[vertIndex] = vertices[vertIndex];
current_vertices[vertIndex].y += (currentBuffer[i] * 1.0f / splash_force) * wave_height;
current_index = vertex_indices[i];
current_vertices[current_index] = vertices[current_index];
current_vertices[current_index].y += (current_buffer[i] * 1.0f / splash_force) * wave_height; //This is what raises the current "tile"'s Y value by the splash force times the maximum height of the wave
}
mesh.vertices = current_vertices;
GetComponent<MeshFilter>().mesh = mesh;
// swap buffers
mesh.vertices = current_vertices; //Updates the mesh! :)
GetComponent<MeshFilter>().mesh = mesh; //Updates it to unity
}
void splash()
{
if (Input.GetKeyDown(KeyCode.Space))
if (Input.GetKeyDown(KeyCode.Space)) //Will change for microphone output
{
RaycastHit hit;
Vector3 down = PlayerSettings.instance.player.transform.TransformDirection(Vector3.down);
if (Physics.Raycast(PlayerSettings.instance.player.transform.position, down, out hit))
/* Ray cast function to set the splash position */
RaycastHit hit; //Will store all the info on the object the ray will collide
Vector3 down = 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, out hit)) // Draws a ray dowards from the player and stores the impact location
{
// First we must init the tiles
Bounds bounds = mesh.bounds;
float xStep = (bounds.max.x - bounds.min.x) / cols;
float zStep = (bounds.max.z - bounds.min.z) / rows;
float xCoord = (bounds.max.x - bounds.min.x) - ((bounds.max.x - bounds.min.x) * hit.textureCoord.x);
float zCoord = (bounds.max.z - bounds.min.z) - ((bounds.max.z - bounds.min.z) * hit.textureCoord.y);
float column = (xCoord / xStep);// + 0.5;
float row = (zCoord / zStep);// + 0.5;
splashAtPoint((int)column, (int)row);
// float xStep = (bounds.max.x - bounds.min.x) / cols;
// float zStep = (bounds.max.z - bounds.min.z) / rows;
float xCoord = (bounds.max.x - bounds.min.x) - ((bounds.max.x - bounds.min.x) * hit.textureCoord.x); //Sets the X coordinates for the splash
float zCoord = (bounds.max.z - bounds.min.z) - ((bounds.max.z - bounds.min.z) * hit.textureCoord.y); //Sets the Y coordinates for the splash
float column = (xCoord /* / xStep*/);
float row = (zCoord /* / zStep*/);
splashAtPoint((int)column, (int)row); //Generates the splash!
}
}
}
//THE CODE THAT MAKES THE WAVE MOVE AND FADE OUT
//From Ben Britten's code
void ripple_effect(int[] source, int[] dest)
{
int x = 0;
......@@ -150,13 +155,9 @@ public class RippleEffect : MonoBehaviour
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]);
dest[position] = (int)(dest[position] * dampner);
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] = (int)(dest[position] * dampner); //Makes the wave fade out
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment