Fix bug with adding fog to a fog shape with a hole in it

This commit is contained in:
Mitchell McCaffrey 2021-06-21 08:02:24 +10:00
parent ffbded8e9d
commit 1ffe24ae1b
3 changed files with 26 additions and 20 deletions

View File

@ -4,27 +4,25 @@ import Action from "./Action";
import {
addPolygonDifferenceToShapes,
addPolygonIntersectionToShapes,
shapeToGeometry,
} from "../helpers/actions";
class CutShapeAction extends Action {
constructor(shapes) {
super();
this.update = (shapesById) => {
const actionGeom = shapes.map((actionShape) => [
actionShape.data.points.map(({ x, y }) => [x, y]),
]);
let actionGeom = shapes.map(shapeToGeometry);
let cutShapes = {};
for (let shape of Object.values(shapesById)) {
const shapePoints = shape.data.points.map(({ x, y }) => [x, y]);
const shapeHoles = shape.data.holes.map((hole) =>
hole.map(({ x, y }) => [x, y])
);
let shapeGeom = [[shapePoints, ...shapeHoles]];
const shapeGeom = shapeToGeometry(shape);
try {
const difference = polygonClipping.difference(shapeGeom, actionGeom);
const difference = polygonClipping.difference(
shapeGeom,
...actionGeom
);
const intersection = polygonClipping.intersection(
shapeGeom,
actionGeom
...actionGeom
);
addPolygonDifferenceToShapes(shape, difference, cutShapes);
addPolygonIntersectionToShapes(shape, intersection, cutShapes);

View File

@ -1,24 +1,24 @@
import polygonClipping from "polygon-clipping";
import Action from "./Action";
import { addPolygonDifferenceToShapes } from "../helpers/actions";
import {
addPolygonDifferenceToShapes,
shapeToGeometry,
} from "../helpers/actions";
class SubtractShapeAction extends Action {
constructor(shapes) {
super();
this.update = (shapesById) => {
const actionGeom = shapes.map((actionShape) => [
actionShape.data.points.map(({ x, y }) => [x, y]),
]);
const actionGeom = shapes.map(shapeToGeometry);
let subtractedShapes = {};
for (let shape of Object.values(shapesById)) {
const shapePoints = shape.data.points.map(({ x, y }) => [x, y]);
const shapeHoles = shape.data.holes.map((hole) =>
hole.map(({ x, y }) => [x, y])
);
let shapeGeom = [[shapePoints, ...shapeHoles]];
const shapeGeom = shapeToGeometry(shape);
try {
const difference = polygonClipping.difference(shapeGeom, actionGeom);
const difference = polygonClipping.difference(
shapeGeom,
...actionGeom
);
addPolygonDifferenceToShapes(shape, difference, subtractedShapes);
} catch {
console.error("Unable to find difference for shapes");

View File

@ -42,3 +42,11 @@ export function addPolygonIntersectionToShapes(shape, intersection, shapes) {
};
}
}
export function shapeToGeometry(shape) {
const shapePoints = shape.data.points.map(({ x, y }) => [x, y]);
const shapeHoles = shape.data.holes.map((hole) =>
hole.map(({ x, y }) => [x, y])
);
return [[shapePoints, ...shapeHoles]];
}