Sign in to follow this  
gomidas

C# SharpDX convert 3D coordinate to 2D

Recommended Posts

I want to select my meshes with mouse on screen. I think I wiil need 2d coords of mesh vertices I use vector4 for it. Is there a simple way to do it ?

 

CoordinateConverter class

        public Vector2 Convert_3Dto2D(Vector3 position, Matrix viewProj, RenderForm_EX form)
        {
            var project = Vector3.Project(position, 0, 0, form.ClientSize.Width, form.ClientSize.Height, 0, 1, viewProj);
            Console.WriteLine(project.X+" "+ project.Y);
            return new Vector2(project.X, project.Y);
        }

Inside RenderLoop

if (MEListener.MouseDown)
{
   var viewProj = Matrix.Multiply(Meshes.MeshCollection[0].ViewLayer.Camera.getView(), proj);
   for (int _iV = 0; _iV < Meshes.MeshCollection[0].VertexData.Length; _iV++)
   {
     CoordConverter.Convert_3Dto2D((Vector3)Meshes.MeshCollection[0].VertexData[_iV].Position + Meshes.MeshCollection[0].Position, viewProj, form);
   }
}

I think I did it correct. When I move mesh I can get still correct result I think. But what happens if I rotate or scale mesh ?

For translation I do it like: 

So I +add Position of mesh

(Vector3)Meshes.MeshCollection[0].VertexData[_iV].Position + Meshes.MeshCollection[0].Position,...

What should I Do to get coorect results ?

Edited by gomidas

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this