Imagine a camera that shows a randomly rotated cube at an arbitrary position: The cube's pivot is in the center of the cube. This way its six faces could be referred as positive/negative X, Y or Z.

How can you tell which of the faces is most visible to the camera? What's the math to calculate this? Your help is very much appreciated.

Thank you!

public enum CubeFace {

public static GetFaceToward(Transform cube, Vector3 observerPosition) {
    var toObserver = cube.InverseTransformPoint(observerPosition);

    var absolute = new Vector3(

    if (absolute.x >= absolute.y) {
        if (absolute.x >= absolute.z) {
            return toObserver.x > 0 ? CubeFace.Right : CubeFace.Left;
        } else {
            return toObserver.z > 0 ? CubeFace.Front : CubeFace.Back;
    } else if (absolute.y >= absolute.z) {
        return toObserver.y > 0 ? CubeFace.Top : CubeFace.Bottom;
    } else {
        return toObserver.z > 0 ? CubeFace.Front : CubeFace.Back;
  • \$\begingroup\$ Wow, awesome. Thank you! \$\endgroup\$ – SePröbläm Nov 17 '20 at 23:43
  • \$\begingroup\$ A code only answer. By a moderator. I'm not down-voting. I'm not flagging. 2020 is weird. \$\endgroup\$ – Theraot Nov 19 '20 at 8:03
  • 1
    \$\begingroup\$ Couldn't think of anything to say about it at the time that wouldn't just repeat what the code already says. 😉 Open to suggestions/edits for what text might be helpful to add! \$\endgroup\$ – DMGregory Nov 19 '20 at 12:02
  • \$\begingroup\$ Warning: I write long answer. The code is good at telling what it does and how. That leaves the why. Why do it this way? I would reason it, and end up repeating. So, in general, the most visible face of the cube is going to be the one oriented towards it. That is, the face which normal line is closer to the point. In object space the question is what is the most significant axis of the position of the point. Which is easier to check: the face is on the axis with highest absolute value, the sign tells us which. And that's why we transform the point to object space, and not the other way around. \$\endgroup\$ – Theraot Nov 19 '20 at 12:44
  • \$\begingroup\$ Ah, and of course, I did not need you to explain that to come up with. Thus, if I'm right on that explanation, it also shows that it is not necessary. Well, if I'm wrong, then you would know what needs epxlaining. \$\endgroup\$ – Theraot Nov 19 '20 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.