Skip to content

Instantly share code, notes, and snippets.

@nothke
Created August 8, 2018 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nothke/b0fd447cd3d3e60b13c14e6d8bba9205 to your computer and use it in GitHub Desktop.
Save nothke/b0fd447cd3d3e60b13c14e6d8bba9205 to your computer and use it in GitHub Desktop.
public class MeshSetAtStart : MonoBehaviour
{
MeshFilter meshFilter;
// Reference to the mesh
Mesh mesh;
void Start()
{
meshFilter = gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
// Create the mesh once, at start
mesh = new Mesh();
meshFilter.mesh = mesh;
}
void Update()
{
Vector3[] vertices = new Vector3[4];
// Create vertices of the quad, and add a random position to each vertex
vertices[0] = new Vector3(-1, -1, 0) + Random.insideUnitSphere * 0.1f;
vertices[1] = new Vector3(-1, 1, 0) + Random.insideUnitSphere * 0.1f;
vertices[2] = new Vector3(1, -1, 0) + Random.insideUnitSphere * 0.1f;
vertices[3] = new Vector3(1, 1, 0) + Random.insideUnitSphere * 0.1f;
// Triangles
int[] triangles = new int[6] {
0, 1, 2,
1, 3, 2 };
mesh.vertices = vertices;
mesh.triangles = triangles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment