- Home /
How to get the 2d Poisition (in camera) of the mesh vertices
Hi
I have to take 4 screenshots of a 3d object (diffrent point of view) using 04 fixed unity camera in the scene. i did this and i can save the pictures as png.
the problem is that, i want for each vertex in the mesh (my character object) to have the exact 2d position in each picture.
any solution please?
Answer by PedramAUS · May 20, 2013 at 10:03 AM
From each camera you should calculate the projection of the point on the screen using Camera.WorldToScreenPoint.
For intance:
Vector3 ProjectFromCam1 = Camera1.WorldToScreenPoint(target.position);
Vector3 ProjectFromCam2 = Camera2.WorldToScreenPoint(target.position);
Vector3 ProjectFromCam3 = Camera3.WorldToScreenPoint(target.position);
Vector3 ProjectFromCam4 = Camera4.WorldToScreenPoint(target.position);
Answer by Bunny83 · May 20, 2013 at 10:09 AM
After reading your question for the 5th time i guess all you want is:
Transform.TransformPoint for converting the vertex positions into world space
Camera.WorldToScreenPoint for projecting the world space positions into screenspace.
So a script like this should do what you want:
// C#
using UnityEngine;
using System.Collections;
public class MeshVertexPositions : MonoBehaviour
{
public Vector2[] Calculate2DPositions(Camera aCam)
{
Mesh m = null;
var MF = Getcomponent<MeshFilter>();
if (MF != null)
m = MF.sharedMesh;
var SMR = GetComponent<SkinnedMeshRenderer>():
if (SMR != null)
m = SMR.sharedMesh;
if (m == null)
return null;
Transform obj = transform;
Vector3[] verts = m.vertices;
Vector2[] result = new Vector2[verts.Length];
for(int i = 0; i < verts.Length; +ii)
{
result[i] = aCam.WorldToScreenPoint(obj.TransformPoint(verts[i]));
}
return result;
}
}
If your character is made up of several meshes you have to do this for everyone.
Answer by mbougaa · May 20, 2013 at 11:07 AM
Thanks for your replay and sorry for not being clear a lot, i'll try to be more clear - My object is a 3d human body - Suppose that i use only one camera (positionned in the scene to get the frontal picture) - I want to have the exact 2d positions (pixels) in this image that matches with the object vertices (to simplify, i say the 2d projection of all vertices in the screenshot). - i use the main camera with the default view (perspective) - i tried to use WorldToScreenPoint an print the 2d position i get on the picture, but the problem is that it dont give me the projection i wanted - the camera gives me this screenshot (picture1) - and when i print the vertices projection that i get like that Vector3 screenpose = Camera.main.WorldToScreenPoint(vertices[l])
it gives me the (picture2) - as you can see the 2d projection are not in the correct place thanks in advance
Your answer
Follow this Question
Related Questions
WorldToViewportPoint and ViewportToWorldPoint math 2 Answers
fluent animation from orthographic to perspective projection 2 Answers
Zoom to center of Pinch Gesture 1 Answer
Modified Mobile TapControl.js logic flow works, but No Apparent Cam Zoom/Rotate Response 1 Answer
Isometric game camera limits 4 Answers