Issue with Modifying Vertices of multiple Game objects
Hi,
I have a script which loads a mesh and basically outputs a game object per triangle. That part works great.
Now I am trying to add noise to these individual Game object triangles vertices in Update() only for my vertices to take the vertices previous positions and add the noise to that.
What I want it to do is only ever add the noise to the original vertex positions which were set in Start() and come from the original meshes triangles vertices.
If i pause The game before running i see that the first frame does exactly what i want, but i notice that the triangles disappear into space as the game updates.
To keep the question short I will only post the update loop since I am fairly sure that is where the issue lies.
The code is a little bit of a mess at the moment as its all wip so please excuse it, Also I am fairly new to Unity and rusty in general at C#
void Update() { // Generate some Noise to apply to vertices later setNoise();
// Loop over all Triangle Game Objects
for (int i = 0; i < _triangles.Length; i++)
{
// Grab the Mesh Component
_triangleMeshFilter = _triangles[i].GetComponent<MeshFilter>();
Mesh mesh = _triangleMeshFilter.mesh;
Vector3[] triangleVerts = new Vector3[3];
// Set the vertices to a temporay array
triangleVerts = mesh.vertices;
// Loop over the vertices in the GameObject (Should only be three)
for (int j = 0; j < triangleVerts.Length; j++)
{
// Set a Color to the UV locations of the Vertices
Color tri_Noise = texture.GetPixelBilinear(mesh.uv[j].x, mesh.uv[j].y);
// The temporay vertex array is modified by multiplying the Color value by the current vertex values
var temp = triangleVerts[j];
triangleVerts[j] = Vector3.Scale(temp, new Vector3(tri_Noise.r, tri_Noise.g, tri_Noise.b));
}
// Set the actual mesh vertices to updated vertices
mesh.vertices = triangleVerts;
}
}
What I have tried so far to no avail is:
I have created a giant vertex array when i create the triangle initially and then looped over that in the update instead. this was messed up somehow but the vertices did not fly into space.
I copied the array of triangle Gameobjects into a new array in Start() and used that as the lookup in Update() for every loop of triangle objects
I am assuming that the issue is here:
triangleVerts = mesh.vertices;
Am I missing something really simple or is this just not possible?
Here is a link to a sample scene incase I am not very Clear above. Click pause before play and you will see what i expect to always see, A Cube with noise applies and distorted. But if you continue it disappears into the abyss
Thank you for any help you can give :)