Assigning uvs to "procedural" mesh.
This mesh is not technically procedural as in it's a script that creates a new mesh but it's a low poly water script that produces waves using the given mesh's vertices. I did not create this script and it can actually be downloaded for free HERE. The only annoying concern is that it doesn't do anything at all to effect the uvs. This is my current version of the script.
PLEASE KEEP IN MIND, I AM NEW TO CODING, So try to dumb it down for me please lol also I know what uvs are and all that I just have no idea of the code I need to write to actually SET the uvs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LowPolyWater
{
public class LowPolyWater : MonoBehaviour
{
public float waveHeight = 0.5f;
public float waveFrequency = 0.5f;
public float waveLength = 0.75f;
public bool clampY = true;
public bool _Gizmos = false;
// private float _NewGizmoY;
public float _GizmoY = 5f;
public Color32 _GizmoColor;
//Position where the waves originate from
public Vector3 waveOriginPosition = new Vector3 (0.0f, 0.0f, 0.0f);
private Mesh mesh;
private MeshCollider meshCollider;
private Vector3[] vertices;
private MeshFilter meshFilter;
private void Awake ()
{
//Get the Mesh Filter of the gameobject
meshFilter = GetComponent<MeshFilter> ();
meshCollider = gameObject.GetComponent<MeshCollider> ();
}
void Start ()
{
// _NewGizmoY = transform.position.y + _GizmoY;
CreateMeshLowPoly (meshFilter);
// Debug.Log (transform.position.y);
// Debug.Log (transform.position.y + _GizmoY);
}
/// <summary>
/// Rearranges the mesh vertices to create a 'low poly' effect
/// </summary>
/// <param name="mf">Mesh filter of gameobject</param>
/// <returns></returns>
MeshFilter CreateMeshLowPoly (MeshFilter mf)
{
mesh = mf.sharedMesh;
//Get the original vertices of the gameobject's mesh
Vector3[] originalVertices = mesh.vertices;
//Get the list of triangle indices of the gameobject's mesh
int[] triangles = mesh.triangles;
//Create a vector array for new vertices
Vector3[] vertices = new Vector3[triangles.Length];
//Assign vertices to create triangles out of the mesh
for (int i = 0; i < triangles.Length; i++)
{
vertices[i] = originalVertices[triangles[i]];
triangles[i] = i;
}
//Update the gameobject's mesh with new vertices
mesh.vertices = vertices;
mesh.SetTriangles (triangles, 0);
mesh.RecalculateBounds ();
mesh.RecalculateNormals ();
mesh.GetType ();
this.vertices = mesh.vertices;
mesh.uv = new Vector2[mesh.vertexCount];
meshCollider.sharedMesh = mesh;
return mf;
}
void Update ()
{
GenerateWaves ();
}
private void OnDrawGizmos ()
{
if (_Gizmos)
{
Gizmos.color = _GizmoColor;
Gizmos.DrawWireMesh (mesh, -1, this.transform.position + new Vector3 (this.transform.position.x,this.transform.position.x + _GizmoY, this.transform.position.z), gameObject.transform.rotation, this.transform.localScale);
}
}
/// <summary>
/// Based on the specified wave height and frequency, generate
/// wave motion originating from waveOriginPosition
/// </summary>
void GenerateWaves ()
{
//Count the vertices?
for (int i = 0; i < vertices.Length; i++)
{
Vector3 v = vertices[i];
//Initially set the wave height to 0
v.y = 0.0f;
//Get the distance between wave origin position and the current vertex
float distance = Vector3.Distance (v, waveOriginPosition);
distance = (distance % waveLength) / waveLength;
//Oscilate the wave height via sine to create a wave effect
v.y = waveHeight * Mathf.Sin (Time.time * Mathf.PI * 4.0f * waveFrequency +
(Mathf.PI * 2.0f * distance));
//CLAMP THE Y TO MINIMUM OF "waveOriginPosition.y" so the waves do not go below original y coordinates
if (clampY)
v.y = Mathf.Clamp (v.y, waveOriginPosition.y, waveHeight * 4.0f + waveOriginPosition.y);
//Update the vertex
vertices[i] = v;
}
//Update the mesh properties
mesh.MarkDynamic ();
mesh.vertices = vertices;
mesh.RecalculateNormals ();
meshCollider.sharedMesh = mesh;
meshFilter.mesh = mesh;
// OnDrawGizmos ();
// mesh.uv.Initialize ();
}
}
}
Answer by eses · Aug 24, 2018 at 11:43 AM
Hi, @Cinabutts0
The question has been asked before: https://answers.unity.com/questions/64410/generating-uvs-for-a-scripted-mesh.html
Maybe it is helpful? It's pretty much just plotting the vert positions in UV space.
Answer by Cinabutts0 · Aug 24, 2018 at 07:24 PM
@eses Yes that does help me but I can't figure out where and how to get vertices[I].x and y..