[SOLVED] How to convert Mesh from local to World Space? CODE FIXED!
Hi. I spawn cube on each click and when the cube spawned and the cube send its own world space vertices to Chunk gameobject for store all cube data into it and to draw. And this Chunk gameobject draws with given world space vertices. I can get my cubes' vertices with World Position using Matrix4X4. And then I can draw mesh with given world space vertices. It draws very well. But it draws well when the Chunk gameobject position in the 0, 0, 0. But when I change position of Chunk gameobject then it draws at another position because my Chunk gameobject do not convert mesh to world space. How to convert mesh to world space?
I send vertices with world space but when I change Chunk gameobject and it draws at another position. How to fix this? I spent 2 days to find this issue but I could not find
CODE to draw mesh: This script called "GlobalChunkScript"
[System.Serializable]
public class MeshData
{
public float[] Vertices;
public int[] Triangles;
}
[System.Serializable]
public class MeshDatabase
{
public List<MeshData> MeshDatas;
}
public class GlobalChunkScript : MonoBehaviour
{
public MeshFilter MeshFilter;
public MeshDatabase MeshDatabase;
public void DrawMesh()
{
Mesh mesh = new Mesh();
List<Vector3> VertiexList = new List<Vector3>();
List<int> TriangleList = new List<int>();
int[] Triangles;
for (int x = 0; x < MeshDatabase.MeshDatas.Count; x++)
{
// Vertices
for (int i = 0; i < MeshDatabase.MeshDatas[x].MeshTop.vertices.Length / 3; i++)
{
VertiexList.Add(new Vector3(
MeshDatabase.MeshDatas[x].MeshTop.vertices[i * 3] - transform.position.x,
MeshDatabase.MeshDatas[x].MeshTop.vertices[i * 3 + 1] - transform.position.y,
MeshDatabase.MeshDatas[x].MeshTop.vertices[i * 3 + 2] - transform.position.z
));
}
// Triangles
for (int i = 0; i < MeshDatabase.MeshDatas[x].Triangles.Length; i++)
{
TriangleList.Add(MeshDatabase.MeshDatas[x].Triangles[i]);
}
}
Triangles = TriangleList.ToArray();
mesh.SetVertices(VertiexList);
mesh.triangles = Triangles;
mesh.Optimize();
mesh.RecalculateNormals();
MeshFilter.mesh = mesh;
}
}
And this CODE send data to GlobalChunkScript:
public void SendMeshData
{
MeshFilter MeshFilter;
MeshFilter = GetComponentInChildren<MeshFilter>();
GlobalChunkScript GlobalChunkScript;
MeshData MeshData;
float[] vertices;
int[] triangles;
Matrix4x4 LocalToWorld = transform.localToWorldMatrix;
vertices = new float[MeshFilter.mesh.vertexCount * 3];
for (int i = 0; i < MeshFilter.mesh.vertexCount; i++)
{
vertices[i * 3] = LocalToWorld.MultiplyPoint3x4(MeshFilter.mesh.vertices[i]).x;
vertices[i * 3 + 1] = LocalToWorld.MultiplyPoint3x4(MeshFilter.mesh.vertices[i]).y;
vertices[i * 3 + 2] = LocalToWorld.MultiplyPoint3x4(MeshFilter.mesh.vertices[i]).z;
}
triangles = new int[MeshFilter.mesh.triangles.Length];
for (int i = 0; i < MeshFilter.mesh.triangles.Length; i++)
{
triangles[i] = MeshFilter.mesh.triangles[i] + 24 * GlobalChunkScript.MeshDatabase.MeshDatas.Count;
}
MeshData = new MeshData()
{
Vertices = vertices,
Triangles = triangles
};
GlobalChunkScript.MeshDatabase.MeshDatas.Add(MeshData);
}