/// <summary> /// Projects a 3D vector from object space into screen space. /// </summary> /// <param name="source">The vector to project.</param> /// <param name="matrix">A combined WorldViewProjection matrix.</param> /// <param name="vector">The projected vector.</param> public void Project(ref MyVector3 source, ref MyMatrix matrix, out MyVector3 vector) { MyVector3.Transform(ref source, ref matrix, out vector); float a = (((source.X * matrix.M14) + (source.Y * matrix.M24)) + (source.Z * matrix.M34)) + matrix.M44; if (!MyMathf.IsOne(a)) { vector = (vector / a); } vector.X = (((vector.X + 1f) * 0.5f) * Width) + X; vector.Y = (((-vector.Y + 1f) * 0.5f) * Height) + Y; vector.Z = (vector.Z * (MaxDepth - MinDepth)) + MinDepth; }
/// <summary> /// Converts a screen space point into a corresponding point in world space. /// </summary> /// <param name="source">The vector to project.</param> /// <param name="matrix">An inverted combined WorldViewProjection matrix.</param> /// <param name="vector">The unprojected vector.</param> public void Unproject(ref MyVector3 source, ref MyMatrix matrix, out MyVector3 vector) { vector.X = (((source.X - X) / (Width)) * 2f) - 1f; vector.Y = -((((source.Y - Y) / (Height)) * 2f) - 1f); vector.Z = (source.Z - MinDepth) / (MaxDepth - MinDepth); float a = (((vector.X * matrix.M14) + (vector.Y * matrix.M24)) + (vector.Z * matrix.M34)) + matrix.M44; MyVector3.Transform(ref vector, ref matrix, out vector); if (!MyMathf.IsOne(a)) { vector = (vector / a); } }