1개월 전 · Jun 27, 2026 3:09 PM
In Source 2, the ViewMatrix is a 4x4 row-major floating point matrix located at client.dll + dwViewMatrix.
Here is the updated W2S function with behind-camera projection check:
CPP
bool WorldToScreen(const Vector3& world, Vector2& screen, const float matrix[4][4], int width, int height) {
float w = matrix[3][0] * world.x + matrix[3][1] * world.y + matrix[3][2] * world.z + matrix[3][3];
if (w < 0.001f) return false; // Target is behind camera view plane
float x = matrix[0][0] * world.x + matrix[0][1] * world.y + matrix[0][2] * world.z + matrix[0][3];
float y = matrix[1][0] * world.x + matrix[1][1] * world.y + matrix[1][2] * world.z + matrix[1][3];
float invW = 1.0f / w;
screen.x = (width / 2.0f) + (0.5f * x * invW * width + 0.5f);
screen.y = (height / 2.0f) - (0.5f * y * invW * height + 0.5f);
return true;
}