public bool[] CalculateLineOfSightCells(FogOfWar fow, FogOfWarPhysics physicsmode, Vector3 eyepos, float distance) { if (physicsmode == FogOfWarPhysics.Physics3D) { Debug.LogWarning("Physics3D is not supported with cells!", this); return(null); } int rad = Mathf.RoundToInt(distance); int width = rad + rad + 1; if (_visibleCells == null || _visibleCells.Length != width * width) { _visibleCells = new bool[width * width]; } Vector2 cellsize = (fow.mapResolution.vector2 * 1.1f) / fow.mapSize; // do 1.1 to bring it away from the collider a bit so the raycast won't hit it Vector2 playerpos = FogOfWarConversion.SnapWorldPositionToNearestFogPixel(fow, _transform.position, fow.mapOffset, fow.mapResolution, fow.mapSize); //playerpos.y += 5; for (int y = -rad; y <= rad; ++y) { for (int x = -rad; x <= rad; ++x) { Vector2i offset = new Vector2i(x, y); // find the nearest point in the cell to the player and raycast to that point Vector2 fogoffset = offset.vector2 - new Vector2(Sign(offset.x) * cellsize.x, Sign(offset.y) * cellsize.y) * 0.5f; //fogoffset.y -= 5; Vector2 worldoffset = FogOfWarConversion.FogToWorldSize(fogoffset, fow.mapResolution, fow.mapSize); Vector2 worldpos = playerpos + worldoffset; Debug.DrawLine(playerpos, worldpos); int idx = (y + rad) * width + x + rad; // if it is out of range if (worldoffset.magnitude > distance) { _visibleCells[idx] = false; } else { _visibleCells[idx] = true; RaycastHit2D hit = Physics2D.Raycast(playerpos, worldoffset.normalized, Mathf.Max(worldoffset.magnitude - lineOfSightPenetration, 0.00001f), lineOfSightMask); _visibleCells[idx] = hit.collider == null; } } } return(_visibleCells); }
void FillShape(FogOfWarShape shape) { if (antiFlicker) { // snap to nearest fog pixel shape.eyePosition = FogOfWarConversion.SnapWorldPositionToNearestFogPixel(FogOfWarConversion.WorldToFogPlane(_transform.position, FogOfWar.current.plane), FogOfWar.current.mapOffset, FogOfWar.current.mapResolution, FogOfWar.current.mapSize); shape.eyePosition = FogOfWarConversion.FogPlaneToWorld(shape.eyePosition.x, shape.eyePosition.y, _transform.position.y, FogOfWar.current.plane); } else { shape.eyePosition = _transform.position; } shape.foward = _transform.forward; shape.offset = offset; shape.radius = radius; }
void FillShape(FogOfWar fow, FogOfWarShape shape) { if (antiFlicker) { // snap to nearest fog pixel shape.eyePosition = FogOfWarConversion.SnapWorldPositionToNearestFogPixel(fow, FogOfWarConversion.WorldToFogPlane(_transform.position, fow.plane), fow.mapOffset, fow.mapResolution, fow.mapSize); shape.eyePosition = FogOfWarConversion.FogPlaneToWorld(shape.eyePosition.x, shape.eyePosition.y, _transform.position.y, fow.plane); } else { shape.eyePosition = _transform.position; } shape.brightness = brightness; shape.foward = FogOfWarConversion.TransformFogPlaneForward(_transform, fow.plane); shape.absoluteOffset = absoluteOffset; shape.offset = offset; shape.radius = circleRadius; shape.size = boxSize; }