1
\$\begingroup\$

I'm trying to create a geometry generator for a "Chamfered Cube". I've got the vertices working, but I'm stuck with creating the faces. I don't really know how I could re-arrange my algorithm to create the vertices in the right order for a triangle list / strip / fan / whatever.

This is what I've done so far: starting from the 8 vertices of a normal cube, I "push" out vertices in the shape of an eight of a sphere:

//start 0,0,0
var width = 8;
var radius = 3; 
var segments = 16;

var half = width / 2;
var segmentStepSize = Math.PI / 2 / segments;

//displacement
var w = Math.sqrt(Math.pow((half-radius),2)*2);

// the 8 original vertex vectors
var startPoints = [
    [ 1,  1,  1],
    [ 1,  1, -1],
    [ 1, -1,  1],
    [ 1, -1, -1],
    [-1,  1,  1],
    [-1,  1, -1],
    [-1, -1,  1],
    [-1, -1, -1]
]

var x, y, z, v, x1, y1, z1, j, k;

var points = [];

for(var i = 0; i < 8; i++) {
    v = startPoints[i];
    x = w * v[0];
    y = w * v[1];
    z = w * v[2];

    for(j = 0; j < segments +1; j++) { 

        for(k = 0; k < segments+1; k++) {

            x1 = x + radius * Math.cos(k*segmentStepSize) * Math.sin(j*segmentStepSize) * v[0];
            y1 = y + radius * Math.sin(k*segmentStepSize) * Math.sin(j*segmentStepSize) * v[1];
            z1 = z + radius * Math.cos(j*segmentStepSize) * v[2];
            points.push(x1);
            points.push(y1);
            points.push(z1);

        }
    }
}

module.exports = points;

This code results in this: http://requirebin.com/?gist=fdea95e3bfe069d7f363

Edit

for anyone who's interested: I've shared the code here: https://github.com/ToastCommunicationLab/mesh-primitive-chamfercube

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Why don't you take a piece of paper and draw the faces placement for one side/corner, then replicate it for the rest ;-) \$\endgroup\$ Commented Nov 24, 2014 at 15:46

1 Answer 1

4
\$\begingroup\$

Always start with a triangle list... it's most general and easiest to think about.

You'll end up with four categories of faces:

  • cube faces (quad = 2 triangles each)
  • edge bevels (quads = 2 triangles each)
  • corner bevels (quads = 2 triangles each)
  • corner tips (last triangle)

There's different numbers of each of these, so they'll probably be handled code-wise in their own sections.

The beveling process seems to turn each of the 8 original vertices into 3 collections of vertices, one along each edge that touches that particular corner. If you keep track of these (8 * 3 * segments) piles of points, it should be clear sailing to assemble the various faces from them.

One last tip -- don't be ashamed of writing some straight-line code to bang it out. The elegant symmetries and for-loops, if they exist, may become more apparent after doing it the "dumb" way.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.