If everybody had an ocean
Across the U.S.A.
Then everybody'd be surfin'
Like California
You'd see 'em wearing their baggies
Huarachi sandals too
A bushy bushy blond hairdo
Surfin' U.S.A.
If we are going to make the Beach Boys happy then our cube needs to be able to surfin' big waves, and not just bounce up and down from the buoyancy force. So let's add waves! We also need an endless sea because a small square will not make a boat happy.
There are many ways to generate waves, but I suspect the easiest way is to use our old friend Sinus X. So create a new script called something like WaveTypes to which we can add new wave types if we want to. This assumes that you've already added the WaterController script from the last tutorial.
using UnityEngine; using System.Collections; //Different wavetypes public class WaveTypes { //Sinus waves public static float SinXWave( Vector3 position, float speed, float scale, float waveDistance, float noiseStrength, float noiseWalk, float timeSinceStart) { float x = position.x; float y = 0f; float z = position.z; //Using only x or z will produce straight waves //Using only y will produce an up/down movement //x + y + z rolling waves //x * z produces a moving sea without rolling waves float waveType = z; y += Mathf.Sin((timeSinceStart * speed + waveType) / waveDistance) * scale; //Add noise to make it more realistic y += Mathf.PerlinNoise(x + noiseWalk, y + Mathf.Sin(timeSinceStart * 0.1f)) * noiseStrength; return y; } }
It is easy to modify the script to create other wave formations. Just replace the first wavetype with one of the suggestions like "x * z" to produce less flowing waves.
To create an endless ocean we are going to use 9 squares. The center square will always be close to the boat and it will have the highest resolution. The 8 surrounding squares will have lower resolution because they are far away so we won't see any details anyway. Because the 9 squares are not attached to each other and they have different resolution you will see an ugly seam between them. To solve this problem we have to make sure the surrounding squares are a little bit lower than the center square.
What you need to remember is that the resolution of the center square (the number of triangles you have in the sea mesh) has to be as high as the resolution of the sinus waves. Otherwise the sea may miss a sinus wave and the boat will behave unrealistically.
To make this work we need 2 scripts: EndlessWaterSquare and WaterSquare. You also need an empty gameobject with a meshfilter and a meshrenderer attached to it. This object will be one water square.
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Threading; //Creates an endless water system with squares public class EndlessWaterSquare : MonoBehaviour { //The object the water will follow public GameObject boatObj; //One water square public GameObject waterSqrObj; //Water square data private float squareWidth = 800f; private float innerSquareResolution = 5f; private float outerSquareResolution = 25f; //The list with all water mesh squares == the entire ocean we can see List<WaterSquare> waterSquares = new List<WaterSquare>(); //Stuff needed for the thread //The timer that keeps track of seconds since start to update the water because we cant use Time.time in a thread float secondsSinceStart; //The position of the boat Vector3 boatPos; //The position of the ocean has to be updated in the thread because it follows the boat //Is not the same as pos of boat because it moves with the same resolution as the smallest water square resolution Vector3 oceanPos; //Has the thread finished updating the water so we can add the stuff from the thread to the main thread bool hasThreadUpdatedWater; void Start() { //Create the sea CreateEndlessSea(); //Init the time secondsSinceStart = Time.time; //Update the water in the thread ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateWaterWithThreadPooling)); //Start the coroutine StartCoroutine(UpdateWater()); } void Update() { //UpdateWaterNoThread(); //Update these as often as possible because we don't know when the thread will run because of pooling //and we always need the latest version //Update the time since start to get correct wave height which depends on time since start secondsSinceStart = Time.time; //Update the position of the boat to see if we should move the water boatPos = boatObj.transform.position; } //Update the water with no thread to compare void UpdateWaterNoThread() { //Update the position of the boat boatPos = boatObj.transform.position; //Move the water to the boat MoveWaterToBoat(); //Add the new position of the ocean to this transform transform.position = oceanPos; //Update the vertices for (int i = 0; i < waterSquares.Count; i++) { waterSquares[i].MoveSea(oceanPos, Time.time); } } //The loop that gives the updated vertices from the thread to the meshes //which we can't do in its own thread IEnumerator UpdateWater() { while (true) { //Has the thread finished updating the water? if (hasThreadUpdatedWater) { //Move the water to the boat transform.position = oceanPos; //Add the updated vertices to the water meshes for (int i = 0; i < waterSquares.Count; i++) { waterSquares[i].terrainMeshFilter.mesh.vertices = waterSquares[i].vertices; waterSquares[i].terrainMeshFilter.mesh.RecalculateNormals(); } //Stop looping until we have updated the water in the thread hasThreadUpdatedWater = false; //Update the water in the thread ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateWaterWithThreadPooling)); } //Don't need to update the water every frame yield return new WaitForSeconds(Time.deltaTime * 3f); } } //The thread that updates the water vertices void UpdateWaterWithThreadPooling(object state) { //Move the water to the boat MoveWaterToBoat(); //Loop through all water squares for (int i = 0; i < waterSquares.Count; i++) { //The local center pos of this square Vector3 centerPos = waterSquares[i].centerPos; //All the vertices this square consists of Vector3[] vertices = waterSquares[i].vertices; //Update the vertices in this square for (int j = 0; j < vertices.Length; j++) { //The local position of the vertex Vector3 vertexPos = vertices[j]; //Can't use transformpoint in a thread, so to find the global position of the vertex //we just add the position of the ocean and the square because rotation and scale is always 0 and 1 Vector3 vertexPosGlobal = vertexPos + centerPos + oceanPos; //Get the water height vertexPos.y = WaterController.current.GetWaveYPos(vertexPosGlobal, secondsSinceStart); //Save the new y coordinate, but x and z are still in local position vertices[j] = vertexPos; } } hasThreadUpdatedWater = true; //Debug.Log("Thread finished"); } //Move the endless water to the boat's position in steps that's the same as the water's resolution void MoveWaterToBoat() { //Round to nearest resolution float x = innerSquareResolution * (int)Mathf.Round(boatPos.x / innerSquareResolution); float z = innerSquareResolution * (int)Mathf.Round(boatPos.z / innerSquareResolution); //Should we move the water? if (oceanPos.x != x || oceanPos.z != z) { //Debug.Log("Moved sea"); oceanPos = new Vector3(x, oceanPos.y, z); } } //Init the endless sea by creating all squares void CreateEndlessSea() { //The center piece AddWaterPlane(0f, 0f, 0f, squareWidth, innerSquareResolution); //The 8 squares around the center square for (int x = -1; x <= 1; x += 1) { for (int z = -1; z <= 1; z += 1) { //Ignore the center pos if (x == 0 && z == 0) { continue; } //The y-Pos should be lower than the square with high resolution to avoid an ugly seam float yPos = -0.5f; AddWaterPlane(x * squareWidth, z * squareWidth, yPos, squareWidth, outerSquareResolution); } } } //Add one water plane void AddWaterPlane(float xCoord, float zCoord, float yPos, float squareWidth, float spacing) { GameObject waterPlane = Instantiate(waterSqrObj, transform.position, transform.rotation) as GameObject; waterPlane.SetActive(true); //Change its position Vector3 centerPos = transform.position; centerPos.x += xCoord; centerPos.y = yPos; centerPos.z += zCoord; waterPlane.transform.position = centerPos; //Parent it waterPlane.transform.parent = transform; //Give it moving water properties and set its width and resolution to generate the water mesh WaterSquare newWaterSquare = new WaterSquare(waterPlane, squareWidth, spacing); waterSquares.Add(newWaterSquare); } }
This script will generate a water mesh with the resolution of your choice.
using UnityEngine; using System.Collections; using System.Collections.Generic; //Generates a plane with a specific resolution and transforms the plane to make waves public class WaterSquare { public Transform squareTransform; //Add the wave mesh to the MeshFilter public MeshFilter terrainMeshFilter; //The total size in m private float size; //Resolution = Width of one square public float spacing; //The total number of vertices we need to generate based on size and spacing private int width; //For the thread to update the water //The local center position of this square to fake transformpoint in a thread public Vector3 centerPos; //The latest vertices that belong to this square public Vector3[] vertices; public WaterSquare(GameObject waterSquareObj, float size, float spacing) { this.squareTransform = waterSquareObj.transform; this.size = size; this.spacing = spacing; this.terrainMeshFilter = squareTransform.GetComponent<MeshFilter>(); //Calculate the data we need to generate the water mesh width = (int)(size / spacing); //Because each square is 2 vertices, so we need one more width += 1; //Center the sea float offset = -((width - 1) * spacing) / 2; Vector3 newPos = new Vector3(offset, squareTransform.position.y, offset); squareTransform.position += newPos; //Save the center position of the square this.centerPos = waterSquareObj.transform.localPosition; //Generate the sea //To calculate the time it took to generate the terrain float startTime = System.Environment.TickCount; GenerateMesh(); //Calculate the time it took to generate the terrain in seconds float timeToGenerateSea = (System.Environment.TickCount - startTime) / 1000f; Debug.Log("Sea was generated in " + timeToGenerateSea.ToString() + " seconds"); //Save the vertices so we can update them in a thread this.vertices = terrainMeshFilter.mesh.vertices; } //If we are updating the square from outside of a thread public void MoveSea(Vector3 oceanPos, float timeSinceStart) { Vector3[] vertices = terrainMeshFilter.mesh.vertices; for (int i = 0; i < vertices.Length; i++) { Vector3 vertex = vertices[i]; //From local to global //Vector3 vertexGlobal = squareTransform.TransformPoint(vertex); Vector3 vertexGlobal = vertex + centerPos + oceanPos; //Unnecessary because no rotation nor scale //Vector3 vertexGlobalTest2 = squareTransform.rotation * Vector3.Scale(vertex, squareTransform.localScale) + squareTransform.position; //Debug if (i == 0) { //Debug.Log(vertexGlobal + " " + vertexGlobalTest); } //Get the water height at this coordinate vertex.y = WaterController.current.GetWaveYPos(vertexGlobal, timeSinceStart); //From global to local - not needed if we use the saved local x,z position //vertices[i] = transform.InverseTransformPoint(vertex); //Don't need to go from global to local because the y pos is always at 0 vertices[i] = vertex; } terrainMeshFilter.mesh.vertices = vertices; terrainMeshFilter.mesh.RecalculateNormals(); } //Generate the water mesh public void GenerateMesh() { //Vertices List<Vector3[]> verts = new List<Vector3[]>(); //Triangles List<int> tris = new List<int>(); //Texturing //List<Vector2> uvs = new List<Vector2>(); for (int z = 0; z < width; z++) { verts.Add(new Vector3[width]); for (int x = 0; x < width; x++) { Vector3 current_point = new Vector3(); //Get the corrdinates of the vertice current_point.x = x * spacing; current_point.z = z * spacing; current_point.y = squareTransform.position.y; verts[z][x] = current_point; //uvs.Add(new Vector2(x,z)); //Don't generate a triangle the first coordinate on each row //Because that's just one point if (x <= 0 || z <= 0) { continue; } //Each square consists of 2 triangles //The triangle south-west of the vertice tris.Add(x + z * width); tris.Add(x + (z-1) * width); tris.Add((x-1) + (z-1) * width); //The triangle west-south of the vertice tris.Add(x + z * width); tris.Add((x-1) + (z-1) * width); tris.Add((x-1) + z * width); } } //Unfold the 2d array of verticies into a 1d array. Vector3[] unfolded_verts = new Vector3[width * width]; int i = 0; foreach (Vector3[] v in verts) { //Copies all the elements of the current 1D-array to the specified 1D-array v.CopyTo(unfolded_verts, i * width); i++; } //Generate the mesh object Mesh newMesh = new Mesh(); newMesh.vertices = unfolded_verts; //newMesh.uv = uvs.ToArray(); newMesh.triangles = tris.ToArray(); //Ensure the bounding volume is correct newMesh.RecalculateBounds(); //Update the normals to reflect the change newMesh.RecalculateNormals(); //Add the generated mesh to this GameObject terrainMeshFilter.mesh.Clear(); terrainMeshFilter.mesh = newMesh; terrainMeshFilter.mesh.name = "Water Mesh"; Debug.Log(terrainMeshFilter.mesh.vertices.Length); } }
Another alternative to what you just did is to update the water with a shader. To make this work you need to use the UpdateWaterNoThread(); method and comment out the part of that method where you update the vertices. You also need to add the following to the WaterController script:
void Update() { Shader.SetGlobalFloat("_WaterScale", scale); Shader.SetGlobalFloat("_WaterSpeed", speed); Shader.SetGlobalFloat("_WaterDistance", waveDistance); Shader.SetGlobalFloat("_WaterTime", Time.time); Shader.SetGlobalFloat("_WaterNoiseStrength", noiseStrength); Shader.SetGlobalFloat("_WaterNoiseWalk", noiseWalk); }
The above code will send data to the shader we are going to write. So create a new shader called something like WaterSurfaceShader, and add the following.
Shader "Custom/WaterSurfaceShader" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex("Main Texture", 2D) = "white" {} _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 _NoiseTex("Noise Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM //Physically based Standard lighting model, and enable shadows on all light types //- Standard means standard lightning //- vertex:vert to be able to modify the vertices //- addshadow to make the shadows look correct after modifying the vertices #pragma surface surf Standard vertex:vert addshadow //Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 #pragma glsl sampler2D _MainTex; half _Glossiness; half _Metallic; fixed4 _Color; sampler2D _NoiseTex; //Water parameters float _WaterScale; float _WaterSpeed; float _WaterDistance; float _WaterTime; float _WaterNoiseStrength; float _WaterNoiseWalk; struct Input { float2 uv_MainTex; }; //The wave function float3 getWavePos(float3 pos) { pos.y = 0.0; float waveType = pos.z; pos.y += sin((_WaterTime * _WaterSpeed + waveType) / _WaterDistance) * _WaterScale; //Add noise pos.y += tex2Dlod(_NoiseTex, float4(pos.x, pos.z + sin(_WaterTime * 0.1), 0.0, 0.0) * _WaterNoiseWalk).a * _WaterNoiseStrength; return pos; } void vert(inout appdata_full IN) { //Get the global position of the vertice float4 worldPos = mul(_Object2World, IN.vertex); //Manipulate the position float3 withWave = getWavePos(worldPos.xyz); //Convert the position back to local float4 localPos = mul(_World2Object, float4(withWave, worldPos.w)); //Assign the modified vertice IN.vertex = localPos; } void surf (Input IN, inout SurfaceOutputStandard o) { //Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; //Metallic and smoothness come from slider variables o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
Add the above shader to a material and add it to the prefab we are using to generate the sea. The noise texture is just a gray noise texture you can find by googling "perlin noise texture." The reason that we are not using Unity's noise function that we used before is that it's not available in the shader. The sea should now move in the same way as when we updated the sea with a thread.
If you now press play, you should see that your cube is bouncing up and down with the waves.