Hello
I have a Problem, for a Project I need to find the World Space Coordinates of a mesh.
I’m importing .fbx models into Unity consisting of a ParentObject and three Childobjects with a Mesh attached to each of them.
The .fbx Model should be exchangable, so for User friendlyness I only want to attach one script to the Parent Object.
The endgoal would be to find the corners of the Model so I could measure it’s dimensions. So I need to find all Points on the three Meshes, which have the same World Space Coordinates.
So I wrote my script, getting the Meshes from the MeshFilter Component of the Child Objects, turn the Local Vetices into World Coordinates using transform.TransformPoint() and store them in a new Array.
The Program doesn’t give any Errors but the World Coordinates don’t seem quite right.
To find the Bug I build a Cube Parent Object with three Quads for front, right side and top.
The Parent Object has the world Position (1,-1,5) and Rotaion (0,0,0)
Quad 1 has the local Position (0,0,-0.5) and Rotation (0,0,0)
Quad 2 has the local Position (0,0.5,0) and Rotation (90,0,0)
Quad 3 has the local Position (0.5,0,0) and Rotation (0,-90,0)
The local Coordinates for the four Points of each Mesh are:
P1 (-0.5,-0.5,0)
P2 (0.5,-0.5,0)
P3 (-0.5,0.5,0)
P4 (0.5,0.5,0)
Running my script I get the same World Coordinates for all three Meshes:
P1(0.5,-1.5,5)
P2(1.5,-1.5,5)
P3(0.5,-0.5,5)
P4(1.5,-0.5,5)
The real World Position of P1 should be
Mesh1 (0.5,-1.5,4.5)
Mesh2 (0.5,-0.5,4.5)
Mesh3 (1.5,-1.5,4.5)
Seems like transform.TransformPoint() simply adds the World Position of the ParentObject to the local Vertex Position of the Mesh without considering their Position or Rotation.
Is there a better Way to transform into World space or a better way to access the Mesh Data.
I’m still quite new to scripting, thanks for the help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LocalToWorld : MonoBehaviour
{
private Mesh Mesh1;
private Mesh Mesh2;
private Mesh Mesh3;
int a;
// Start is called before the first frame update
void Start()
{
//Get Meshes from Child Objects
Mesh1 = gameObject.transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh;
Mesh2 = gameObject.transform.GetChild(1).GetComponent<MeshFilter>().sharedMesh;
Mesh3 = gameObject.transform.GetChild(2).GetComponent<MeshFilter>().sharedMesh;
//Create new Arrays for World Coordinates
a = Mesh1.vertexCount;
Vector3[] WorldVert1 = new Vector3[a];
a = Mesh1.vertexCount;
Vector3[] WorldVert2 = new Vector3[a];
a = Mesh1.vertexCount;
Vector3[] WorldVert3 = new Vector3[a];
//Transfer Local Vertex Coordinates into World Coordinates
for (int i = 0; i < Mesh1.vertexCount; i++)
{
WorldVert1[i] = gameObject.transform.TransformPoint(Mesh1.vertices[i]);
}
for (int i = 0; i < Mesh2.vertexCount; i++)
{
WorldVert2[i] = gameObject.transform.TransformPoint(Mesh2.vertices[i]);
}
for (int i = 0; i < Mesh3.vertexCount; i++)
{
WorldVert3[i] = gameObject.transform.TransformPoint(Mesh3.vertices[i]);
}
Debug.Log("Mesh1 Vertex0" + "\nLocal" + Mesh1.vertices[0] + " World" + WorldVert1[0]);
Debug.Log("Mesh2 Vertex0" + "\nLocal" + Mesh2.vertices[0] + " World" + WorldVert2[0]);
Debug.Log("Mesh3 Vertex0" + "\nLocal" + Mesh3.vertices[0] + " World" + WorldVert3[0]);
}
// Update is called once per frame
void Update()
{
}
}