Hello i would like to know how can i modify a mesh filter in unity (i have gone to the meshFilter API but doesnt say on how to modify the current mesh)???
Hello buddy, alright, the first thing that you must know probably it’s how to access the mesh, them you’ll need to know how a mesh is structured (vertices, triangles, color, normal, UV, etc.) for you starting to modify meshes.
Here is a little MonoBehaviour showing how that works.
public class MeshModifier : MonoBehaviour
{
private MeshFilter meshFilter;
private void Awake ()
{
meshFilter = gameObject.GetComponent<MeshFilter> ();
ModifyMesh (meshFilter.mesh);
}
private void ModifyMesh (Mesh mesh)
{
if (!mesh)
{
return;
}
Vector3[] vertices = mesh.vertices;
// Modify vertices (Move all the vertices one unit to the right)
for (int i = 0; i < mesh.vertexCount; i++)
{
vertices*.x++;*
-
}*
-
mesh.vertices = vertices;*
-
}*
}
Sorry for my bad English.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Update() {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
int i = 0;
while (i < vertices.Length) {
vertices <em>+= normals _* Mathf.Sin(Time.time);_</em>
i++;
}
mesh.vertices = vertices;
}
}
_*From here: https://docs.unity3d.com/ScriptReference/Mesh.html*_