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

View File

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