C# snippet:
public static class BoxOutline
{
private static readonly int [] OUTLINE = new int [43]
{
0x00000000, // 00 ... inside
0x00003F04, // 01 ... left ................... 0,4,7,3
0x00005C8C, // 02 ... right .................. 1,2,6,5
0x00000000, // 03
0x00004A44, // 04 ... bottom ................. 0,1,5,4
0x000FCA46, // 05 ... bottom, left ........... 0,1,5,4,7,3
0x0012E446, // 06 ... bottom, right .......... 0,1,2,6,5,4
0x00000000, // 07
0x00006ED4, // 08 ... top .................... 2,3,7,6
0x0001ADE6, // 09 ... top, left .............. 4,7,6,2,3,0
0x0006EED6, // 10 ... top, right ............. 2,3,7,6,5,1
0x00000000, // 11
0x00000000, // 12
0x00000000, // 13
0x00000000, // 14
0x00000000, // 15
0x000014C4, // 16 ... front .................. 0,3,2,1
0x000FC056, // 17 ... front, left ............ 2,1,0,4,7,3
0x0006E4C6, // 18 ... front, right ........... 0,3,2,6,5,1
0x00000000, // 19
0x001294C6, // 20 ... front, bottom .......... 0,3,2,1,5,4
0x000FCA56, // 21 ... front, bottom, left .... 2,1,5,4,7,3
0x0012E4C6, // 22 ... front, bottom, right ... 0,3,2,6,5,4
0x00000000, // 23
0x00056EC6, // 24 ... front, top ............. 0,3,7,6,2,1
0x00056F06, // 25 ... front, top, left ....... 0,4,7,6,2,1
0x0006EEC6, // 26 ... front, top, right ...... 0,3,7,6,5,1
0x00000000, // 27
0x00000000, // 28
0x00000000, // 29
0x00000000, // 30
0x00000000, // 31
0x00007D64, // 32 ... back ................... 4,5,6,7
0x0001FD66, // 33 ... back, left ............. 4,5,6,7,3,0
0x00167C8E, // 34 ... back, right ............ 1,2,6,7,4,5
0x00000000, // 35
0x0013EA46, // 36 ... back, bottom ........... 0,1,5,6,7,4
0x000FEA46, // 37 ... back, bottom, left ..... 0,1,5,6,7,3
0x0013E446, // 38 ... back, bottom, right .... 0,1,2,6,7,4
0x00000000, // 39
0x001ACED6, // 40 ... back, top .............. 2,3,7,4,5,6
0x000D6B06, // 41 ... back, top, left ........ 0,4,5,6,2,3
0x0016768E // 42 ... back, top, right ....... 1,2,3,7,4,5
};
private static readonly Vector3 [] m_Corners = new Vector3[8];
//-----------------------------------------------------------------------------------------------------------------
static public int GetOutline( Vector3 boxMin, Vector3 boxMax, Vector3 camPos, Vector3 [/*6*/] outline )
{
int idx = ( camPos.x < boxMin.x ? 1 : 0 ) // 1 = left
+ ( camPos.x > boxMax.x ? 2 : 0 ) // 2 = right
+ ( camPos.y < boxMin.y ? 4 : 0 ) // 4 = bottom
+ ( camPos.y > boxMax.y ? 8 : 0 ) // 8 = top
+ ( camPos.z < boxMin.z ? 16 : 0 ) // 16 = front
+ ( camPos.z > boxMax.z ? 32 : 0 ); // 32 = back
if ( idx >= OUTLINE.Length )
return 0;
int enc = OUTLINE[ idx ];
if ( enc == 0 )
return 0;
GetCorners( boxMin, boxMax, m_Corners );
int num = enc & 7; // decodes count of vertices in outline
for ( idx = 0; idx < num; ++idx )
{
enc >>= 3; outline[ idx ] = m_Corners[ enc & 7 ]; // decodes outline vertices one by one
}
return num;
}
//-----------------------------------------------------------------------------------------------------------------
public static void GetCorners( Vector3 boxMin, Vector3 boxMax, Vector3 [/*8*/] corners )
{
corners[ 0 ] = new Vector3( boxMin.x, boxMin.y, boxMin.z ); // 3--------------2
corners[ 1 ] = new Vector3( boxMax.x, boxMin.y, boxMin.z ); // | \ / |
corners[ 2 ] = new Vector3( boxMax.x, boxMax.y, boxMin.z ); // | 7 ------ 6 |
corners[ 3 ] = new Vector3( boxMin.x, boxMax.y, boxMin.z ); // | | | |
// | | | |
corners[ 4 ] = new Vector3( boxMin.x, boxMin.y, boxMax.z ); // | | | |
corners[ 5 ] = new Vector3( boxMax.x, boxMin.y, boxMax.z ); // | 4 ------ 5 |
corners[ 6 ] = new Vector3( boxMax.x, boxMax.y, boxMax.z ); // | / \ |
corners[ 7 ] = new Vector3( boxMin.x, boxMax.y, boxMax.z ); // 0 -------------1
}
}
EDIT 2015-07-14 : simplified
EDIT 2015-07-17 : added comments so it is clear how it works :)