public DrawInfo(FogOfWarMap map, FogOfWarShape shape, float xradius, float yradius) { // convert size to fog space fogForward = shape.foward; forwardAngle = FogOfWarUtils.ClockwiseAngle(Vector2.up, fogForward) * Mathf.Deg2Rad; float sin = Mathf.Sin(-forwardAngle); float cos = Mathf.Cos(-forwardAngle); Vector2 relativeoffset = new Vector2(shape.offset.x * cos - shape.offset.y * sin, shape.offset.x * sin + shape.offset.y * cos); fogCenterPos = FogOfWarConversion.WorldToFog(FogOfWarConversion.WorldToFogPlane(shape.eyePosition, map.plane) + relativeoffset, map.offset, map.resolution, map.size); fogEyePos = new Vector2i(FogOfWarConversion.WorldToFog(shape.eyePosition, map.plane, map.offset, map.resolution, map.size)); // find ranges if (shape.visibleCells == null) { xMin = Mathf.Max(0, Mathf.RoundToInt(fogCenterPos.x - xradius)); xMax = Mathf.Min(map.resolution.x - 1, Mathf.RoundToInt(fogCenterPos.x + xradius)); yMin = Mathf.Max(0, Mathf.RoundToInt(fogCenterPos.y - yradius)); yMax = Mathf.Min(map.resolution.y - 1, Mathf.RoundToInt(fogCenterPos.y + yradius)); } else { fogCenterPos = FogOfWarConversion.SnapToNearestFogPixel(fogCenterPos); fogEyePos = new Vector2i(FogOfWarConversion.SnapToNearestFogPixel(FogOfWarConversion.WorldToFog(shape.eyePosition, map.offset, map.resolution, map.size))); Vector2i pos = new Vector2i(Mathf.RoundToInt(fogCenterPos.x), Mathf.RoundToInt(fogCenterPos.y)); Vector2i rad = new Vector2i(Mathf.RoundToInt(xradius), Mathf.RoundToInt(yradius)); xMin = Mathf.Max(0, Mathf.RoundToInt(pos.x - rad.x)); xMax = Mathf.Min(map.resolution.x - 1, Mathf.RoundToInt(pos.x + rad.x)); yMin = Mathf.Max(0, Mathf.RoundToInt(pos.y - rad.y)); yMax = Mathf.Min(map.resolution.y - 1, Mathf.RoundToInt(pos.y + rad.y)); } }
// Converts a world position to a fog pixel position. Values will be between 0 and mapResolution. public Vector2i WorldPositionToFogPosition(Vector3 position) { Vector2 fogplanepos = FogOfWarConversion.WorldToFogPlane(position, plane); Vector2 mappos = FogOfWarConversion.WorldToFog(fogplanepos, mapOffset, mapResolution, mapSize); return(new Vector2i(mappos)); }
/// <summary> /// Set the fog for a square area of the map. Positions are all in world coordinates. 0 is fully unfogged and 255 if fully fogged. /// </summary> /// <param name="bounds"></param> /// <param name="value"></param> public void SetFog(Bounds bounds, byte value) { Rect rect = new Rect(); rect.min = FogOfWarConversion.WorldToFog(bounds.min, plane, mapOffset, mapResolution, mapSize); rect.max = FogOfWarConversion.WorldToFog(bounds.max, plane, mapOffset, mapResolution, mapSize); int xmin = (int)Mathf.Max(rect.xMin, 0); int xmax = (int)Mathf.Min(rect.xMax, mapResolution.x); int ymin = (int)Mathf.Max(rect.yMin, 0); int ymax = (int)Mathf.Min(rect.yMax, mapResolution.y); // if it is not visible on the map if (xmin >= mapResolution.x || xmax < 0 || ymin >= mapResolution.y || ymax < 0) { return; } for (int y = ymin; y < ymax; ++y) { for (int x = xmin; x < xmax; ++x) { _fogValuesTotal[y * mapResolution.x + x] = value; } } }
public void Unfog(Bounds bounds) { Rect rect = new Rect(); rect.min = FogOfWarConversion.WorldToFog(bounds.min, plane, mapOffset, mapResolution, mapSize); rect.max = FogOfWarConversion.WorldToFog(bounds.max, plane, mapOffset, mapResolution, mapSize); Unfog(rect); }
// Set the fog for a square area of the map. 0 is fully unfogged and 1 if fully fogged. public void SetFog(Bounds bounds, byte value) { Rect rect = new Rect(); rect.min = FogOfWarConversion.WorldToFog(bounds.min, plane, mapOffset, mapResolution, mapSize); rect.max = FogOfWarConversion.WorldToFog(bounds.max, plane, mapOffset, mapResolution, mapSize); _drawer.SetFog(rect, value); }
public DrawInfo(FogOfWarMap map, FogOfWarShape shape) { // convert size to fog space Vector2 radius = shape.CalculateRadius() * map.pixelSize; fogForward = shape.foward; Vector2 relativeoffset; if (shape.absoluteOffset) { forwardAngle = 0; relativeoffset = shape.offset; } else { forwardAngle = FogOfWarUtils.ClockwiseAngle(Vector2.up, fogForward) * Mathf.Deg2Rad; float sin = Mathf.Sin(-forwardAngle); float cos = Mathf.Cos(-forwardAngle); relativeoffset = new Vector2(shape.offset.x * cos - shape.offset.y * sin, shape.offset.x * sin + shape.offset.y * cos); } fogCenterPos = FogOfWarConversion.WorldToFog(FogOfWarConversion.WorldToFogPlane(shape.eyePosition, map.plane) + relativeoffset, map.offset, map.resolution, map.size); fogEyePos = FogOfWarConversion.WorldToFog(shape.eyePosition, map.plane, map.offset, map.resolution, map.size).ToInt(); // find ranges if (shape.visibleCells == null) { xMin = Mathf.Max(0, Mathf.RoundToInt(fogCenterPos.x - radius.x)); xMax = Mathf.Min(map.resolution.x - 1, Mathf.RoundToInt(fogCenterPos.x + radius.x)); yMin = Mathf.Max(0, Mathf.RoundToInt(fogCenterPos.y - radius.y)); yMax = Mathf.Min(map.resolution.y - 1, Mathf.RoundToInt(fogCenterPos.y + radius.y)); } else { fogCenterPos = FogOfWarConversion.SnapToNearestFogPixel(fogCenterPos); fogEyePos = FogOfWarConversion.SnapToNearestFogPixel(FogOfWarConversion.WorldToFog(shape.eyePosition, map.offset, map.resolution, map.size)).ToInt(); Vector2Int pos = fogCenterPos.ToInt(); Vector2Int rad = radius.ToInt(); xMin = Mathf.Max(0, Mathf.RoundToInt(pos.x - rad.x)); xMax = Mathf.Min(map.resolution.x - 1, Mathf.RoundToInt(pos.x + rad.x)); yMin = Mathf.Max(0, Mathf.RoundToInt(pos.y - rad.y)); yMax = Mathf.Min(map.resolution.y - 1, Mathf.RoundToInt(pos.y + rad.y)); } }
// Checks the visibility of an area, where a value of 0 is fully unfogged and 1 if fully fogged public float VisibilityOfArea(Bounds worldbounds) { Vector2 min = FogOfWarConversion.WorldToFog(worldbounds.min, plane, mapOffset, mapResolution, mapSize); Vector2 max = FogOfWarConversion.WorldToFog(worldbounds.max, plane, mapOffset, mapResolution, mapSize); int xmin = Mathf.Clamp(Mathf.RoundToInt(min.x), 0, mapResolution.x); int xmax = Mathf.Clamp(Mathf.RoundToInt(max.x), 0, mapResolution.x); int ymin = Mathf.Clamp(Mathf.RoundToInt(min.y), 0, mapResolution.y); int ymax = Mathf.Clamp(Mathf.RoundToInt(max.y), 0, mapResolution.y); float total = 0; int count = 0; for (int y = ymin; y < ymax; ++y) { for (int x = xmin; x < xmax; ++x) { ++count; total += _fogValuesCopy[y * mapResolution.x + x] / 255.0f; } } return(total / count); }