Hi,
I have an array of Tree meshes called myTrees. I am trying to get each mesh’s viewport coordinates and write to files so I can later post process in Python. There is only one camera in the project and the screenshot script takes exactly the correct thing on the viewport. But the files written come out to be totally different (example: say I have one tree mesh in view, the screenshot would capture it correctly but the file writes like 41 tree mesh’s vertices’ coordinates. I suspect I am not understanding some kind of Viewport/World/Scene point transformations. Any help would be amazing. Thanks. The relevant code snippet is below. Also, I am checking if x and y are between 0 and 1 to make sure they are inside the viewport. FYI. my viewport and target size is fixed at 832x832 and I have set up the scene like so. Any advice would be great. You can ignore everything after line 20.
for (int i = 0; i < myTrees.Count; i++) {
Mesh meshT = myTrees*.GetComponent<MeshFilter>().sharedMesh;*
-
Vector3[] verticesT = meshT.vertices;*
-
int[] triangles = meshT.triangles;*
_ string treeId = myTrees*.GetInstanceID().ToString();_
_ int tid=0;_
_ for (int j = 0; j < triangles.Length; j++) {*_
* if (j % 3 == 0) {*
* int triangleId = tid++;*
//Converting to World Point
Vector3 world_t = transform.TransformPoint(meshT.vertices[j]);
//Converting World Point to ViewPort Point
* Vector3 world_v = Camera.main.WorldToViewportPoint(world_t);*
* float x = (float) world_v.x ;
float y = (float) world_v.y;
float z = (float) world_v.z;
_ float r = meshT.colors32 [j].r;_
_ float g = meshT.colors32 [j].g;_
_ float b = meshT.colors32 [j].b;_
_//Check to see if inside viewport i.e., inside [0,1]_
_ if (x >= 0f && y >= 0f && x<=1f && y<=1f ) {_
_ string col = “” + r.ToString () + “,” + g.ToString () + “,” + b.ToString ();*_
* string position = x.ToString (“0.0000”) + “,” + y.ToString (“0.0000”) + “,” + z.ToString (“0.0000”);*
* string fullId = treeId + “-” + triangleId.ToString ();*
* line = fullId + “,” + treeId + “,” + triangleId.ToString () + “,” + col + “,” + position;// + “,” +cam.pixelWidth.ToString()+“,”+cam.pixelHeight.ToString()+“,”+cam.pixelRect.ToString();*
* writer.WriteLine (line);*
* }*
* }*
* }*
* }*