bool LineOfSightCanSee(FogOfWarShape shape, Vector2 offset, float fogradius) { if (shape.lineOfSight == null) { return(true); } float idx = FogOfWarUtils.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 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 Texture Apply(Texture2D fogtexture, Vector2i resolution, int amount, int iterations, FogOfWarBlurType 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 == FogOfWarBlurType.Gaussian3); _blurMaterial.SetKeywordEnabled("GAUSSIAN5", type == FogOfWarBlurType.Gaussian5); _blurMaterial.SetKeywordEnabled("ANTIALIAS", type == FogOfWarBlurType.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) { FogOfWarUtils.Swap(ref _target, ref _source); RenderTexture.active = _target; Graphics.Blit(_source, _blurMaterial); } RenderTexture.active = lastrt; return(_target); }