You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
```function triangleClipAgainstPlane(planeP, planeN, inTri) {
|
|
var outTri1 = Tri(), outTri2 = Tri();
|
|
|
|
planeN = normVector(planeN);
|
|
|
|
var d = function(p) {
|
|
p = normVector(p);
|
|
return(dotVector(planeN, p) - dotVector(planeN, planeP));
|
|
};
|
|
|
|
var inside_points = Tri(),
|
|
nInsidePointCount = 0;
|
|
var outside_points = Tri(),
|
|
nOutsidePointCount = 0;
|
|
|
|
var d0 = d(inTri[0]);
|
|
var d1 = d(inTri[1]);
|
|
var d2 = d(inTri[2]);
|
|
|
|
if (d0 >= 0) {
|
|
inside_points[nInsidePointCount] = inTri[0];
|
|
nInsidePointCount++;
|
|
} else {
|
|
outside_points[nOutsidePointCount] = inTri[0];
|
|
nOutsidePointCount++;
|
|
}
|
|
if (d1 >= 0) {
|
|
inside_points[nInsidePointCount] = inTri[1];
|
|
nInsidePointCount++;
|
|
} else {
|
|
outside_points[nOutsidePointCount] = inTri[1];
|
|
nOutsidePointCount++;
|
|
}
|
|
if (d2 >= 0) {
|
|
inside_points[nInsidePointCount] = inTri[2];
|
|
nInsidePointCount++;
|
|
} else {
|
|
outside_points[nOutsidePointCount] = inTri[2];
|
|
nOutsidePointCount++;
|
|
}
|
|
if (nInsidePointCount === 0) {
|
|
return [0];
|
|
}
|
|
|
|
if (nInsidePointCount === 3) {
|
|
outTri1 = inTri;
|
|
|
|
return [1, [[outTri1[0], outTri1[1], outTri1[2]]]];
|
|
}
|
|
|
|
if (nInsidePointCount === 1 && nOutsidePointCount === 2) {
|
|
outTri1[0] = inside_points[0];
|
|
|
|
outTri1[1] = planeIntersectVector(planeP, planeN, inside_points[0], outside_points[0]);
|
|
outTri1[2] = planeIntersectVector(planeP, planeN, inside_points[0], outside_points[1]);
|
|
|
|
return [1, [[outTri1[0], outTri1[1], outTri1[2]]]];
|
|
}
|
|
|
|
if (nInsidePointCount === 2 && nOutsidePointCount === 1) {
|
|
outTri1[0] = inside_points[0];
|
|
outTri1[1] = inside_points[1];
|
|
outTri1[2] = planeIntersectVector(planeP, planeN, inside_points[0], outside_points[0]);
|
|
|
|
outTri2[0] = inside_points[1];
|
|
outTri2[1] = outTri1[2];
|
|
outTri2[2] = planeIntersectVector(planeP, planeN, inside_points[1], outside_points[0]);
|
|
|
|
return [2, [[outTri1[0], outTri1[1], outTri1[2]], [outTri2[0], outTri2[1], outTri2[2]]]];
|
|
}
|
|
};``` |