Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

Untitled

By: a guest on May 13th, 2009  |  syntax: C#  |  size: 1.09 KB  |  hits: 93  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1.         /// <summary>
  2.         /// Project a 3D point given it's world coordinates and the viewing <see cref="Camera"/> to 2D screen space.
  3.         /// </summary>
  4.         /// <remarks>
  5.         /// The screen space is assumed to be in range [0..1] in both x and y dimensions, where 0 means the left/top and 1 the right/bottom edge.
  6.         /// </remarks>
  7.         /// <param name="camera">The viewing camera</param>
  8.         /// <param name="position">World position of the point to be projected</param>
  9.         /// <returns></returns>
  10.         public static Vector2 GetScreenCoords(Camera camera, Vector3 position)
  11.         {
  12.             // projection calculation
  13.             Vector4 v4 = new Vector4(position.x, position.y, position.z, 1.0f);
  14.             Vector4 pos = camera.ProjectionMatrix * camera.ViewMatrix * v4;
  15.             Vector2 screenPos = new Vector2(pos.x / pos.w, -pos.y / pos.w);
  16.            
  17.             // normalize screen position
  18.             screenPos.x = (screenPos.x + 1.0f) * 0.5f;
  19.             screenPos.y = (screenPos.y + 1.0f) * 0.5f;
  20.  
  21.             return screenPos;
  22.         }