Beispiel #1
0
 private void SetTexture()
 {
     FoWTexture.Apply();
     fogOfWarRenderer.GetPropertyBlock(_mpb);
     _mpb.SetTexture(_textureID, FoWTexture);
     fogOfWarRenderer.SetPropertyBlock(_mpb);
 }
Beispiel #2
0
    public void UpdateFogOfWar(Vector3 playerPosition)
    {
        Vector3Int tilePosition = WorldToTile(playerPosition - new Vector3(0.5f, 0.5f, 0f));
        int        intViewRange = (int)_viewRange + 1;
        BoundsInt  fowBounds    = new BoundsInt(tilePosition - new Vector3Int(intViewRange, intViewRange, 0), new Vector3Int(2 * intViewRange + 1, 2 * intViewRange + 1, 0));

        float[,] visibility = new float[fowBounds.size.x, fowBounds.size.y];
        ShadowCast.CalculateVisibility(playerPosition - _origin, fowBounds, _tiles, visibility);

        for (int x = fowBounds.xMin; x < fowBounds.xMax; x++)
        {
            if (x < 0 || x >= _size.x)
            {
                continue;
            }

            for (int y = fowBounds.yMin; y < fowBounds.yMax; y++)
            {
                if (y < 0 || y >= _size.y)
                {
                    continue;
                }

                Vector3Int pos      = new Vector3Int(x, y, 0);
                TileType   tileType = _tiles[x, y];

                float distance    = Vector3.Distance(playerPosition - new Vector3(0.5f, 0.5f, 0f), TileToWorld(pos));
                Color pixelColor  = FoWTexture.GetPixel(pos.x, pos.y);
                Color targetColor = pixelColor;
                float fuzzRange   = 2f;
                float vis         = visibility[x - fowBounds.xMin, y - fowBounds.yMin];
                if (distance < _viewRange - fuzzRange)
                {
                    targetColor.r = 1f;
                    //targetColor.g = 1f;
                }
                else if (distance > _viewRange)
                {
                    targetColor.r = 0f;
                }
                else
                {
                    float frac = (_viewRange - distance) / fuzzRange;
                    targetColor.r = frac;
                    //targetColor.g = Mathf.Max(pixelColor.g, frac);
                }

                targetColor.r *= vis;
                targetColor.g  = Mathf.Max(targetColor.g, targetColor.r);

                if (targetColor.r > 0f)
                {
                    if (x == tilePosition.x && y == tilePosition.y)
                    {
                        targetColor.b = 1.0f;
                    }
                    else if (tileType.HasFlag(TileType.Wall))
                    {
                        targetColor.b = 0.75f;
                    }
                    else if (tileType.HasFlag(TileType.Floor))
                    {
                        targetColor.b = 0.5f;
                    }
                }

                FoWTexture.SetPixel(pos.x, pos.y, targetColor);
            }
        }


        SetTexture();
    }