public float[] CalculateLineOfSight(FogOfWarPhysics physicsmode, Vector3 eyepos, FogOfWarPlane plane)
        {
            if (lineOfSightMask == 0)
            {
                return(null);
            }

            if (_distances == null)
            {
                _distances = new float[256];
            }

            if (physicsmode == FogOfWarPhysics.Physics2D)
            {
                if (CalculateLineOfSight2D(eyepos, radius, lineOfSightPenetration, lineOfSightMask, _distances))
                {
                    return(_distances);
                }
            }
            else if (plane == FogOfWarPlane.XZ) // 3D
            {
                if (CalculateLineOfSight3D(eyepos, radius, lineOfSightPenetration, lineOfSightMask, _distances, Vector3.up, Vector3.forward))
                {
                    return(_distances);
                }
            }
            else if (plane == FogOfWarPlane.XY) // 3D
            {
                if (CalculateLineOfSight3D(eyepos, radius, lineOfSightPenetration, lineOfSightMask, _distances, Vector3.back, Vector3.up))
                {
                    return(_distances);
                }
            }
            return(null);
        }
Example #2
0
        public float[] CalculateLineOfSight(FogOfWarPhysics physicsmode, Vector3 eyepos)
        {
            if (lineOfSightMask == 0)
            {
                return(null);
            }

            if (_distances == null)
            {
                _distances = new float[256];
            }

            if (physicsmode == FogOfWarPhysics.Physics2D)
            {
                if (CalculateLineOfSight2D(eyepos, radius, lineOfSightPenetration, lineOfSightMask, _distances))
                {
                    return(_distances);
                }
            }
            else // 3D
            {
                if (CalculateLineOfSight3D(eyepos, radius, lineOfSightPenetration, lineOfSightMask, _distances))
                {
                    return(_distances);
                }
            }
            return(null);
        }
Example #3
0
        public FogOfWarShape GetShape(FogOfWar fow, FogOfWarPhysics physics, FogOfWarPlane plane)
        {
            FogOfWarShape shape = CreateShape(fow);

            if (shape == null)
            {
                return(null);
            }
            shape.lineOfSight  = CalculateLineOfSight(physics, shape.eyePosition, plane);
            shape.visibleCells = null;
            return(shape);
        }
Example #4
0
 public void Set(FogOfWar fow)
 {
     resolution    = fow.mapResolution;
     size          = fow.mapSize;
     offset        = fow.mapOffset;
     pixelSize     = resolution.x / size;
     pixelCount    = resolution.x * resolution.y;
     plane         = fow.plane;
     physics       = fow.physics;
     filterMode    = fow.filterMode;
     multithreaded = fow.multithreaded;
 }
Example #5
0
        public bool[] CalculateLineOfSightCells(FogOfWar fow, FogOfWarPhysics physicsmode, Vector3 eyepos, float distance)
        {
            if (physicsmode == FogOfWarPhysics.Physics3D)
            {
                Debug.LogWarning("Physics3D is not supported with cells!", this);
                return(null);
            }

            int rad   = Mathf.RoundToInt(distance);
            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);

            //playerpos.y += 5;
            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;
                    //fogoffset.y -= 5;
                    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 > distance)
                    {
                        _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);
        }
Example #6
0
        public float[] CalculateLineOfSight(FogOfWarPhysics physicsmode, Vector3 eyepos, FogOfWarPlane plane)
        {
            if (lineOfSightMask == 0)
            {
                return(null);
            }

            if (_distances == null)
            {
                _distances = new float[256];
            }
            if (CalculateLineOfSight3D(eyepos, radius, lineOfSightPenetration, lineOfSightMask, _distances, Vector3.up, Vector3.forward))
            {
                return(_distances);
            }
            return(null);
        }
        public FogOfWarShape GetShape(FogOfWarPhysics physics, FogOfWarPlane plane)
        {
            FogOfWarShape shape = CreateShape();

            if (shape == null)
            {
                return(null);
            }

            if (cellBased)
            {
                shape.lineOfSight  = null;
                shape.visibleCells = CalculateLineOfSightCells(physics, shape.eyePosition);
            }
            else
            {
                shape.lineOfSight  = CalculateLineOfSight(physics, shape.eyePosition, plane);
                shape.visibleCells = null;
            }
            return(shape);
        }