Hello, I’ve been coding for a few years in processing and I’ve been developing some simple simulations, however I wanted to take my work and do it in 3D, my pieces are quite simple (just a rectangle with 2 semi circles in each end) at first I tried to use the extrude library but it was way too slow and had a lot of restrictions (the extruction size could only be an integer) so I tried to make my own function to extrude, however it ended way slower, any tips on any library that allows me to extrude or a way to make this code better?
/*Function that returns a PShape[], its arguments are the original face, where the extruded
version will be store and the size of the extrusion
*/
PShape[] extrusion(PShape baseExtrusion, PShape[] parte, float extrudeT) {
//copies the face to the extruded version
parte=new PShape[0];
parte=(PShape[])append(parte, baseExtrusion);
//creates an offset copy of the face
PShape temp;
temp=createShape();
parte=(PShape[])append(parte, temp);
parte[1].beginShape();
for (int j=0; j<parte[0].getVertexCount(); j++) {
PVector v = parte[0].getVertex(j).copy();
v.z+=extrudeT;
parte[1].vertex(v.x, v.y, v.z);
}
parte[1].endShape(CLOSE);
//creates n faces that conects the base with the offset one
//I tried to make this one just two lines however they wouldn't show for some reason
for (int i=0; i<parte[0].getVertexCount()-1; i++) {
temp=createShape();
parte=(PShape[])append(parte, temp);
PVector v = parte[0].getVertex(i);
PVector vNext;
vNext=parte[0].getVertex(i+1);
parte[i+2].beginShape();
parte[i+2].vertex(v.x, v.y, v.z);
parte[i+2].vertex(vNext.x, vNext.y, vNext.z);
parte[i+2].vertex(vNext.x, vNext.y, vNext.z+extrudeT);
parte[i+2].vertex(v.x, v.y, v.z+extrudeT);
parte[i+2].endShape(CLOSE);
}
return parte;
}
Might as well post this, I found a way to do an extrusion which doesn’t consume a lot of resources, however beware since because of the way is constructed the fill() function doesn’t work properly
//function for extrusion, returns a single PShape which will be the whole solid
//takes as arguments the figure to extrude and the thickness
PShape extrusion(PShape baseExtrusion, float extrudeT) {
//initialize the result and generates a vector that will have the base vertices
PShape parte=new PShape();
PVector[] v=new PVector[0];
//fill the vector with the vertices
for (int i=0; i<baseExtrusion.getVertexCount(); i++) {
v=(PVector[])append(v,baseExtrusion.getVertex(i));
}
//starts constructing the whole solid
parte=createShape();
parte.beginShape();
for (int i=0; i<baseExtrusion.getVertexCount(); i++) {
/*
creates the lateral rectangles of the extrusion in a single motion by looping around itself*/
if (i+1<baseExtrusion.getVertexCount()) {
parte.vertex(v[i+1].x, v[i+1].y, v[i+1].z);
parte.vertex(v[i+1].x, v[i+1].y, v[i+1].z+extrudeT);
parte.vertex(v[i].x, v[i].y, v[i].z+extrudeT);
parte.vertex(v[i].x, v[i].y, v[i].z);
parte.vertex(v[i+1].x, v[i+1].y, v[i+1].z);
} else {
//when is at the last rectangle it just conects to the start
parte.vertex(v[0].x, v[0].y, v[0].z);
parte.vertex(v[0].x, v[0].y, v[0].z+extrudeT);
parte.vertex(v[i].x, v[i].y, v[i].z+extrudeT);
}
}
parte.endShape();
return parte;
}
}