// 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)); } }
/////////////////////////////////////////////////////////////////////////// bool LineOfSightCanSee(FogShape shape, Vector2 offset, float fogradius) { if (shape.LineOfSight == null) { return(true); } float idx = FogTools.ClockwiseAngle(Vector2.up, offset) * shape.LineOfSight.Length / 360.0f; if (idx < 0) { idx += shape.LineOfSight.Length; } // sampling float value; if (_Map.FilterMode == FilterMode.Point) { value = shape.LineOfSight[Mathf.RoundToInt(idx) % shape.LineOfSight.Length]; } else { int idxlow = Mathf.FloorToInt(idx); int idxhigh = (idxlow + 1) % shape.LineOfSight.Length; value = Mathf.LerpUnclamped(shape.LineOfSight[idxlow], shape.LineOfSight[idxhigh], idx % 1); } float dist = value * fogradius; return(offset.sqrMagnitude < dist * dist); }
//////////////////////////////////////////////////////////////////////////////// public Texture Apply(Texture2D fogtexture, Vector2_1 resolution, int amount, int iterations, FogBlurType type) { if (amount <= 0 || iterations <= 0) { return(fogtexture); } if (_blurMaterial == null) { _blurMaterial = new Material(Shader.Find("Hidden/FogOfWarBlurShader")); } _blurMaterial.SetFloat("_BlurAmount", amount); _blurMaterial.SetKeywordEnabled("GAUSSIAN3", type == FogBlurType.Gaussian3); _blurMaterial.SetKeywordEnabled("GAUSSIAN5", type == FogBlurType.Gaussian5); _blurMaterial.SetKeywordEnabled("ANTIALIAS", type == FogBlurType.Antialias); SetupRenderTarget(resolution, ref _target); if (iterations > 1) { SetupRenderTarget(resolution, ref _source); } RenderTexture lastrt = RenderTexture.active; RenderTexture.active = _target; Graphics.Blit(fogtexture, _blurMaterial); for (int i = 1; i < iterations; ++i) { FogTools.Swap(ref _target, ref _source); RenderTexture.active = _target; Graphics.Blit(_source, _blurMaterial); } RenderTexture.active = lastrt; return(_target); }