- Home /
Generate a mesh from randomly positioned points
Hi, Im trying to make a mesh from a list of seemingly randomly positioned points that are moving. So the idea is to make a single 3d model to model data from a simulation and particles work but i can only go so far with that before frame rate starts dropping drastically. Obviously, a single model is a bit easier to render. I know how meshes work and i can edit their points and re-write the mesh accordingly but it looks like a mangled mess because im telling it to pick a random point in the list of randomly placed points as a vertex. That being said, i am keeping track of the original vertex order in the array 'verts' so i think that could help when drawing it back.
Is there any way of figuring out what order to draw them and then also disregarding points that would be inside the mesh, because there are some there. Here is my current code if it helps at all. i am assuming i would need to compare the position of one point to all the other points and then use that somehow, to at least ignore the internal points but i dont even know where to begin with that. if not i guess i can deal with particles until i can think of something else.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sphereMeshChange : MonoBehaviour {
public Mesh mesh;
public Vector3[] verts;
public Vector3[] editVerts;
public Vector3[] origVertices;
public int[] triangles;
public int[] origTriangles;
public float timeToWait;
public float curTime;
public int numOfVertsToEdit;
int time;
public int delay;
public GameObject cube;
public GameObject[] points;
// Use this for initialization
void Start () {
mesh = GetComponent<MeshFilter>().mesh;
verts = mesh.vertices;
triangles = mesh.triangles;
origTriangles = mesh.triangles;
origVertices = mesh.vertices;
points = new GameObject[100];
editVerts = new Vector3[515];
time = delay;
verts = mesh.vertices;
}
// Update is called once per frame
void Update () {
if (GameObject.FindGameObjectWithTag("dataPoint") != null) {
points = GameObject.FindGameObjectsWithTag("dataPoint");
}
for (int i = 0; i < verts.Length; i++)
{
int randInt = Random.Range(0,points.Length);
if (points[randInt] != null) { editVerts[i] = points[randInt].transform.position; }
}
vertsOfWorld = editVerts;
createMesh();
}
void createMesh()
{
mesh.vertices = vertsOfWorld;
mesh.triangles = triangles;
}
void restoreMesh()
{
print("Restoring");
verts = origVertices;
triangles = origTriangles;
createMesh();
}
}
Thanks for any help!
You may want to look into Gift-Wrapping, as well as other Convex Hull Algorithms for ideas on creating a mesh around an arbitrary series of vertices.
This isn't an easy task and has a tendency to work well when implemented perfectly, or else result in especially disastrous meshes.