**Intro**

Mapping a sphere to a cube becomes a fundamental issue in CS4, where vertex to UV data is a one-to-one map. Typical UV data brought in from Blender or 3DSMax, which have multiple uv data points per vertex (or triangle fan), will not work with the drawTriangles method of CS4. There are work-a-rounds, but eventually it all boils down to creating some type of one-to-one mapping scheme.

An illustrative step in this process is the creation of a **polar cube** – a polar cube created completely from the polar parametric equations of an octagonal cubic (not pieced together with six planes (or 8 planes in this case) as is done in Papervision3D and other such software). Got a little ahead of myself here – the techniques described can be used to make any number of sides. I kept referring to a cube but created an octagonal as was pointed out by makc3d in the comments. The equations can be used to created a sculpty prim maker which will be posted soon.

Not 8 separate sides, all one structure.

**Polar Cube Derivation**

The parametric mapping equations (for a polar cube) are simple, and for a 2D boil down to

x = r(θ)*cos(θ)

y = r(θ)*sin(θ)

and in 3D,

x = r(θ, Φ)*cos(θ)sin(Φ)

y = r(θ, Φ)*sin(θ)sin(Φ)

z = r(θ, Φ)*cos(Φ)

where r is function of angle (not constant as in the spherical case).

Solving for the 2D case, r(θ) is given by

θ: 0 to 90, r(θ) = 1/(sin(θ) + cos(θ))

θ: 90 to 180, r(θ) = 1/(sin(θ) – cos(θ))

θ: 180 to 270, r(θ) = -1/(sin(θ) + cos(θ))

θ: 270 to360, r(θ) = -1/(sin(θ) – cos(θ))

These solutions are easily obtained by substitution x = r(θ)*cos(θ), and y = r(θ)*sin(θ) into the linear equations shown in the figure below and solving for r(θ).

The generalized solution is

r(a, b, θ) = a/(sin(θ) + b*cos(θ))

where

θ: 0 to 90 (a=1, b=1), θ: 90 to 180 (a=1, b=-1), θ: 180 to 270 (a=-1, b=-1), θ: 270 to360 (a=-1, b=-1) Of interest, the values of the a, b parameters correspond to all possible combinations of 1 and -1, similar two coins which gives (HH, HT, TH, TT) where H=1, and T=-1.

**3D Case**

Following the approach above, for the 3D case (where 8 possible planes corresponding to the 8 sides of the polar cube), the general solution is given by

r(a, b, c, θ, Φ) = a/(cos(Φ)+b*sin(θ)sin(Φ)+c*cos(θ)sin(Φ))

where the parameters a, b, c correspond to flipping three coins at the same time with 8 possible outcomes (HHH, HHT, HTH, HTT, THH, THT, TTH, TTT or 111, 11-1, 1-11, 1-1-1, -111,- 11-1, -1-11, -1-1-1), corresponding to the 8 sides of our polar cube.

The eight possibilities used to map the vertices of the cube are given below:

θ: 0 to 90, Φ:0 to 90 (-1,-1,-1)

θ: 90 to 180, Φ:0 to 90 (-1,-1,1)

θ: 180 to 270, Φ:0 to 90 (-1,1,1)

θ: 270 to360, Φ:0 to 90 (-1,1,-1)

θ: 0 to 90, Φ:90 to 180 (1,1,1)

θ: 90 to 180, Φ:90 to 180 (1,1,-1)

θ: 180 to 270, Φ:90 to 180 (1,-1,-1)

θ: 270 to360, Φ:90 to 180 (1,-1,1)

**Coding the Parametric Equations**

The coding is straightforward. The cube’s vertices are derived from the parametric form given above and its indices are derived from a one-to-one mapping of a sphere onto a cube. That’s why the spherically mapped image, shown in the image above, lays on the cube without distortion.

for (var i:int = 0 ; i!=rows; i++) {

ix= i/(rows-1)*Math.PI*2.0;

for (var j:int = 0 ; j!=cols; j++) {

iy= (j/(cols-1)-0.5)*Math.PI;

// 8 planes 8case

if(ix>=0 && ix<2*Math.PI/4){

** //θ: 0-90, Φ: 0-90, (-1,-1,-1)**

if(iy>=-2*Math.PI/4 && iy<0){

varMyrIs=rFunc(-1,-1,-1,ix,iy);

}else{

** //θ: 0-90, Φ: 90-180, (1,1,1)**

varMyrIs=rFunc(1,1,1,ix,iy);}

}

if(ix>=2*Math.PI/4 && ix<2*Math.PI/2){

** //θ: 90-180, Φ: 0-90, (-1,-1,1)**

if(iy>=-2*Math.PI/4 && iy<0){

varMyrIs=rFunc(-1,-1,1,ix,iy);

}else{

** //θ: 90-180, Φ: 90-180, (1,1,-1)
**

varMyrIs=rFunc(1,1,-1,ix,iy);}

}

if(ix>=2*Math.PI/2 && ix<6*Math.PI/4){

** //θ: 180-270, Φ: 0-90, (1,-1,1)**

if(iy>=-2*Math.PI/4 && iy<0){

varMyrIs=rFunc(-1,1,1,ix,iy);

}else{

** //θ: 180-270, Φ: 90-180, (-1,-1,1)
**

varMyrIs=rFunc(1,-1,-1,ix,iy);}

}

if(ix>=6*Math.PI/4 && ix<=2*Math.PI){

** //θ: 270-360, Φ: 0-90, (-1,1,-1)**

if(iy>=-2*Math.PI/4 && iy<0){

varMyrIs=rFunc(-1,1,-1,ix,iy);

}else{

** //θ: 270-360, Φ: 90-180, (1,-1,1)**

varMyrIs=rFunc(1,-1,1,ix,iy);}

}

//Polar Cube

paraVec = new Vector3D(

varMyrIs*Math.cos(iy)*Math.cos(ix), varMyrIs*Math.sin(iy), varMyrIs*Math.cos(iy)*Math.sin(ix));

//Collect vetices in the verts Vector array

verts.push(radx*paraVec.x,rady*paraVec.y,radz*paraVec.z);

//Load uvt data

uvtData.push( i/(rows-1),j/(cols-1), 0.0);

//Initialize projected vertices

projectedVerts.push(0.0,0.0);

}

}

Now you’ll ready to start Knowledge Space. To see the entire code click the more link below: