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