Skip to content

Commit

Permalink
Add a check to see if a hole is actually inside the geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSom committed Nov 23, 2023
1 parent b9cf8b3 commit 0816ab8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/scene/graphics/shared/utils/buildContextBatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ function addTextureToGeometryData(
batches.push(graphicsBatch);
}

function isPointInPolygon(x: number, y: number, vertices: number[]): boolean
{
let inside = false;
const n = vertices.length;

for (let i = 0, j = n - 2; i < n; j = i, i += 2)
{
const xi = vertices[i];
const yi = vertices[i + 1];
const xj = vertices[j];
const yj = vertices[j + 1];

const intersect = ((yi > y) !== (yj > y))
&& (x < ((xj - xi) * (y - yi) / (yj - yi)) + xi);

if (intersect) inside = !inside;
}

return inside;
}

function addShapePathToGeometryData(
shapePath: ShapePath,
style: ConvertedFillStyle,
Expand Down Expand Up @@ -193,6 +214,13 @@ function addShapePathToGeometryData(

holeArrays.forEach((holePoints) =>
{
const [x, y] = holePoints;

if (!isPointInPolygon(x, y, otherPoints))
{
return;
}

holeIndices.push(otherPoints.length / 2);
otherPoints.push(...holePoints);
});
Expand Down

0 comments on commit 0816ab8

Please sign in to comment.