// FUNCTIONS /////////////////////////////////////////////////////////////////////////// public DrawInfo(FogMap map, FogShape shape, float xradius, float yradius) { // convert size to fog space fogForward = shape.Forward; forwardAngle = FogTools.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 = FogConversion.WorldToFog(FogConversion.WorldToFogPlane(shape.EyePosition, map.Plane) + relativeoffset, map.Offset, map.Resolution, map.Size); fogEyePos = new Vector2_1(FogConversion.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 = FogConversion.SnapToNearestFogPixel(fogCenterPos); fogEyePos = new Vector2_1(FogConversion.SnapToNearestFogPixel(FogConversion.WorldToFog(shape.EyePosition, map.Offset, map.Resolution, map.Size))); Vector2_1 pos = new Vector2_1(Mathf.RoundToInt(fogCenterPos.x), Mathf.RoundToInt(fogCenterPos.y)); Vector2_1 rad = new Vector2_1(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 = FogConversion.WorldToFog(bounds.min, Plane, MapOffset, MapResolution, MapSize); rect.max = FogConversion.WorldToFog(bounds.max, Plane, MapOffset, MapResolution, MapSize); Unfog(rect); }
//////////////////////////////////////////////////////////////////////////////// /// <summary> /// Draw map area bounds. /// </summary> void OnDrawGizmos() { Gizmos.color = Color.red; Vector3 offset = FogConversion.FogPlaneToWorld(MapOffset.x, MapOffset.y, 0, Plane); Vector3 size = FogConversion.FogPlaneToWorld(MapSize, MapSize, 0, Plane); Gizmos.DrawWireCube(offset, size); Gizmos.color = new Color(1, 0, 0, 0.2f); Gizmos.DrawCube(offset, size); }
//////////////////////////////////////////////////////////////////////////////// public Vector2_1 WorldPositionToFogPosition(Vector3 position) { Vector2 mappos = FogConversion.WorldToFogPlane(position, Plane) - MapOffset; mappos.Scale(MapResolution.vector2 / MapSize); Vector2_1 mapposi = new Vector2_1(mappos); mapposi += new Vector2_1(MapResolution.x >> 1, MapResolution.y >> 1); return(mapposi); }
/////////////////////////////////////////////////////////////////////////// void FillShape(FogManager fow, FogShape shape) { if (AntiFlicker) { // snap to nearest fog pixel shape.EyePosition = FogConversion.SnapWorldPositionToNearestFogPixel(fow, FogConversion.WorldToFogPlane(_Transform.position, fow.Plane), fow.MapOffset, fow.MapResolution, fow.MapSize); shape.EyePosition = FogConversion.FogPlaneToWorld(shape.EyePosition.x, shape.EyePosition.y, _Transform.position.y, fow.Plane); } else { shape.EyePosition = _Transform.position; } shape.Forward = FogConversion.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 = FogConversion.WorldToFog(worldbounds.min, Plane, MapOffset, MapResolution, MapSize); Vector2 max = FogConversion.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); }