Hi, I’m relatively new to threejs. I’m trying to create an web app, where you have a fog of war capability. So basically my idea is to have the whole map (canvas) be black. One user then can reveal the map with various forms, mostly polygons.
The application calculates the points inside the revealed polygons, calculates a DataTexture and uses this one as alpha map on an object spanning the whole map, which is at the moment of the size 7000x7000.
This is working for now, but it is really slow. Is there a better way of doing it?
Below is the relevant code for generating the alpha-map:
const dataSize = size * size;
const hiddenValue = 255;
for (let i = 0; i < dataSize; i++) {
data[i * 2 + 1] = hiddenValue;
}
const positionDiff = new Point(size / 2, size / 2).subtract(position.x, position.y);
const positionedFogOfWarArea = fogOfWarArea.copy().moveAt(positionDiff);
areas?.forEach((area) => {
const dataValue = area.isClearArea ? 0 : hiddenValue;
new Face(...area.points.map(p => new Point(p.x, p.y).add(positionDiff))).getAllPointsInside().forEach((point: Point) => {
if (!positionedFogOfWarArea.containsPoint(point) || point.x === positionedFogOfWarArea.p2.x || point.y === positionedFogOfWarArea.p2.y) {
return;
}
const i = Math.round((point.y) * size + point.x);
data[i * 2 + 1] = dataValue;
});
});
dataTexture.needsUpdate = true;
size is 7000 and dataTexture is set on a material as alphaMap