Skip to content

Instantly share code, notes, and snippets.

@mamemame360
Last active April 23, 2019 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemame360/762151e83f67f90850b11dac270a1c48 to your computer and use it in GitHub Desktop.
Save mamemame360/762151e83f67f90850b11dac270a1c48 to your computer and use it in GitHub Desktop.
create capsule mesh on Houdini VEX
#include "math.h"
// parameters
float hsheight1 = ch("../hsheightx");
float hsheight2 = ch("../hsheighty");
vector size = chv("../size");
int hsrows1 = chi("../hsrowsx");
int hsrows2 = chi("../hsrowsy");
int tubesplitnum = chi("../tubesplitnum");
int cols = chi("../cols");
// constant values
float radx = size.x * 0.5;
float radz = size.z * 0.5;
float sizey_half = size.y * 0.5;
// ===================
// create points
// ===================
// polar points
addpoint(0, set(0, sizey_half + hsheight1, 0));
addpoint(0, set(0, -sizey_half - hsheight2, 0));
// upper hemisphere
for (int row = 0; row < hsrows1; row++)
{
float rt = M_PI * 0.5 * ((float)(row+1) / hsrows1);
float cy = cos(rt);
float sy = sin(rt);
for (int col = 0; col < cols; col++)
{
float ct = M_PI * 2.0 * ((float)col / cols);
vector pos = {0, 0, 0};
pos.x = radx * cos(ct) * sy;
pos.y = hsheight1 * cy + sizey_half;
pos.z = radz * -sin(ct) * sy;
addpoint(0, pos);
}
}
// tube
float tube_row_height = size.y / (tubesplitnum+1);
for (int row = 0; row < tubesplitnum; row++)
{
float rt = tube_row_height * (row + 1);
float cy = sizey_half - rt;
for (int col = 0; col < cols; col++)
{
float ct = M_PI * 2.0 * ((float)col / cols);
vector pos = {0, 0, 0};
pos.x = radx * cos(ct);
pos.y = cy;
pos.z = radz * -sin(ct);
addpoint(0, pos);
}
}
// lower hemisphere
for (int row = 0; row < hsrows2; row++)
{
float rt = M_PI * 0.5 * (1 - ((float)row / hsrows2));
float cy = cos(rt);
float sy = sin(rt);
for (int col = 0; col < cols; col++)
{
float ct = M_PI * 2.0 * ((float)col / cols);
vector pos = {0, 0, 0};
pos.x = radx * cos(ct) * sy;
pos.y = -hsheight2 * cy - sizey_half;
pos.z = radz * -sin(ct) * sy;
addpoint(0, pos);
}
}
// =======================
// create primitives
// =======================
// upper polar triangles
for (int col = 0; col < cols; col++)
{
int p1 = 0;
int p2 = 2 + ((col + 1) % cols);
int p3 = 2 + col;
addprim(0, "poly", p1, p2, p3);
}
// quads
int primrows = hsrows1 + hsrows2 + tubesplitnum - 1;
for (int row = 0; row < primrows; row++)
{
for (int col = 0; col < cols; col++)
{
int ofs = row * cols;
int p1 = 2 + col + ofs;
int p2 = 2 + ((col + 1) % cols) + ofs;
int p3 = p2 + cols;
int p4 = p1 + cols;
addprim(0, "poly", p1, p2, p3, p4);
}
}
// lower polar triangles
int ofs = primrows * cols;
for (int col = 0; col < cols; col++)
{
int p1 = 1;
int p2 = 2 + col + ofs;
int p3 = 2 + ((col + 1) % cols) + ofs;
addprim(0, "poly", p1, p2, p3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment