When considering the visibility of coordinates in world space to a perspective camera, it is important to understand the inner workings of a modern 3D rendering pipeline. This process involves a series of matrix transforms that convert an object from world space to screen space.
A crucial component in this transformation is the view-projection matrix, which maps a world space coordinate [x,y,z] to "almost" screen space [x,y,z,w]. By dividing x, y, z by w, the resulting coordinates establish a system where x=-1 represents the left side of the screen and x=+1 symbolizes the right side. Similarly, y=-1 corresponds to the bottom of the screen, while y=+1 indicates the top.
The responsibility of calculating the view-projection matrix typically falls on the perspective camera. Additionally, the matrix possesses the property of being invertible, meaning it can convert screen space back to world space. Therefore, to determine the visible range of x,y coordinates, one can input [-1,-1,z] as the bottom left of the screen, apply the inverse of the view-projection matrix, and perform the necessary /w division to obtain the corresponding world coordinates for the object’s position.
In a simplified form using pseudo code:
vec3.toWorldSpace = function(out, v3, viewProjectionMatrix){
var invViewProj = mat4.inverse(vec3._m, viewProjectionMatrix);
var m = invViewProj;
vec3.transformByMat(out, v3, m);
var w = m[3] * x + m[7] * y + m[11] * z + m[15];
if (w !== 0){
var invW = 1.0/w;
out[0] *= invW;
out[1] *= invW;
out[2] *= invW;
}
return out;
};