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?

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);

After reading your question for the 5th time i guess all you want is:

  • [Transform.TransformPoint][1] for converting the vertex positions into world space
  • [Camera.WorldToScreenPoint][2] 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 _= aCam.WorldToScreenPoint(obj.TransformPoint(verts*));*_

}
return result;
}
}
If your character is made up of several meshes you have to do this for everyone.
_[1]: http://docs.unity3d.com/Documentation/ScriptReference/Transform.TransformPoint.html*_
_
[2]: http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html*_

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