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)); } }
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); }
//void OnDrawGizmosSelected() void OnDrawGizmos() { Gizmos.color = Color.red; Vector3 offset = FogOfWarConversion.FogPlaneToWorld(mapOffset.x, mapOffset.y, 0, plane); Vector3 size = FogOfWarConversion.FogPlaneToWorld(mapSize, mapSize, 0, plane); Gizmos.DrawWireCube(offset, size); Gizmos.color = new Color(1, 0, 0, 0.2f); Gizmos.DrawCube(offset, size); }
public Vector2i WorldPositionToFogPosition(Vector3 position) { Vector2 mappos = FogOfWarConversion.WorldToFogPlane(position, plane) - mapOffset; mappos.Scale(mapResolution.vector2 / mapSize); Vector2i mapposi = new Vector2i(mappos); mapposi += new Vector2i(mapResolution.x >> 1, mapResolution.y >> 1); return(mapposi); }
public bool[] CalculateLineOfSightCells(FogOfWar fow, FogOfWarPhysics physicsmode, Vector3 eyepos) { if (physicsmode == FogOfWarPhysics.Physics3D) { Debug.LogWarning("Physics3D is not supported with cells!", this); return(null); } int rad = Mathf.RoundToInt(radius); 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); 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; 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 > radius) { _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(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.foward = FogOfWarConversion.TransformFogPlaneForward(_transform, fow.plane); shape.offset = offset; shape.radius = radius; }
// 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); }