- Home /
Add values to Int[] (SetTriangles)
Hi, First, I'm sorry for my english, I'm french, I'll do my best to express myself.
I have to change some triangles of a submesh into another one, when the player hit it. That's how I did it :
//TriInfo[2] is the index of the triangle in the submesh 0 I want to assign to the submesh 1
//GoMesh is the mesh I'm working on
//Define an Array wich will contain the triangles of the submesh 1 and the new one
int[] AddTriangle = new int[GoMesh.GetTriangles (1).Length + 3];
for (int i=0; i<GoMesh.GetTriangles (1).Length; i++){
AddTriangle[i]=GoMesh.GetTriangles(1)[i];
}
AddTriangle[GoMesh.GetTriangles(1).Length]=GoMesh.GetTriangles(0)[TriInfo [2]*3];
AddTriangle[GoMesh.GetTriangles(1).Length+1]=GoMesh.GetTriangles(0)[TriInfo [2]*3 + 1];
AddTriangle[GoMesh.GetTriangles(1).Length+2]=GoMesh.GetTriangles(0)[TriInfo [2]*3 + 2];
//I assign the new array AddTriangle to the submesh 1
GoMesh.SetTriangles (AddTriangle, 1);
It works, but it is a frequent request and, when the int[] gets long, the game lags. It has to be a int[] and not a list (for the function SetTriangle), and i didn't find a better way to add values to it.
Thank you for reading, and if you have a solution, i would be really grateful.
Is it always the same submesh, or are you doing different submeshes? How frequent...every frame?
In this situation it is always from the submesh 0 to the submesh 1, yes. (I have 4 submeshes, and in specific situations, the triangles will go from 1 to 2, and from 2 to 3)
The context : The player is walking on a sphere. Every frame, I make a raycast hit to know on which triangle he is. If this triangle is on the submesh 0, I assign it to the submesh 1. So, the frequency depends of the movement of the player, but it can be some times per seconds (and at the end I have more than 2000 values in my array...)
Thanks for replying
Answer by brentatplayful · Nov 04, 2014 at 03:28 PM
I would look at Buffer.BlockCopy I'm not sure about these sub meshes you indexing but something like this will be faster
const INT_LENGTH_IN_BYTES = 4;
int[] subMesh0Tris = GoMesh.GetTriangles(0);
int[] subMesh1Tris = GoMesh.GetTriangles(1);
int[] newTriangles = new int[subMesh1Tris.Length + 3];
Buffer.BlockCopy(subMesh1Tris, 0, newTriangles, 0, subMesh1Tris.Length * INT_LENGTH_IN_BYTES);
newTriangles[newTriangles.Length-3]=subMesh0Tris[TriInfo[2]*3];
newTriangles[newTriangles.Length-2]=subMesh0Tris[TriInfo[2]*3 + 1];
newTriangles[newTriangles.Length-1]=subMesh0Tris[TriInfo[2]*3 + 2];
//I assign the new array AddTriangle to the submesh 1
GoMesh.SetTriangles (newTriangles, 1);
I did not test this but it should be close to what you're looking for
It's perfect, thank you so much ! It didn't work in a first time, because I was using System.Collections. Now that I am using System, it's ok (I'm a beginner). Some methods I found on the $$anonymous$$SDN wich did'nt work would probably work now too.
Thank you again :)
Your answer
Follow this Question
Related Questions
Array value not changing? 1 Answer
Search an array? 2 Answers
Keep adding targets to a list 2 Answers
Check if all elements of an array contain the same value? 3 Answers