Exemplo n.º 1
0
        private void CreateShadowPolygonPoints()
        {
            ShadowPolygonPoints = ShadowPolygonPoints ?? new List <Vector2>();
            ShadowPolygonPoints.Clear();
            if (_visiblitySet.Output.Any())
            {
                Vector2 previous = Vector2.one * 1000000;
                for (int index = 0; index < _visiblitySet.Output.Count; index += 1)
                {
                    Vector2 point = _visiblitySet.Output[index];
                    if (!ShadowMathUtils.Approximately(point, previous))
                    {
                        ShadowPolygonPoints.Add(point);
                    }
                    previous = point;
                }
                ShadowPolygonPoints.Add(_visiblitySet.Output.First());
            }

            if (_drawGizmos)
            {
                Gizmos.color = Color.yellow;
                for (int index = 0; index < ShadowPolygonPoints.Count - 1; index++)
                {
                    Vector2 va = ShadowPolygonPoints[index];
                    Vector2 vb = ShadowPolygonPoints[index + 1];
                    Gizmos.DrawLine(va * 1.05f, vb * 1.05f);
                }
                Gizmos.DrawLine(ShadowPolygonPoints.Last() * 1.05f, ShadowPolygonPoints.First() * 1.05f);
            }
        }
Exemplo n.º 2
0
        private void AddTriangle(float angle1, float angle2, Segment segment, Segment previous)
        {
            Vector2 delta1 = new Vector2(Mathf.Cos(angle1), Mathf.Sin(angle1));
            Vector2 delta2 = new Vector2(Mathf.Cos(angle2), Mathf.Sin(angle2));

            Vector2 p1 = Center;
            Vector2 p2 = p1 + delta1;
            Vector2 p3 = new Vector2(0.0f, 0.0f);
            Vector2 p4 = new Vector2(0.0f, 0.0f);

            Gizmos.color = Color.blue;
            if (segment != null)
            {
                // Stop the triangle at the intersecting segment
                p3 = segment.Start.Point;
                p4 = segment.End.Point;
            }
            else
            {
                // Stop the triangle at a fixed distance; this probably is
                // not what we want, but it never gets used in the demo
                p3 = p1 + delta1 * 500;
                p4 = p1 + delta2 * 500;
            }

            Vector2 pBegin = ShadowMathUtils.LineIntersection(p3, p4, p1, p2);

            p2 = p1 + delta2;
            Vector2 pEnd = ShadowMathUtils.LineIntersection(p3, p4, p1, p2);

            if (_drawGizmos)
            {
                Gizmos.color = Color.green;
                Gizmos.DrawLine(p1, pBegin);
                Gizmos.DrawLine(p2, pEnd);

                //Gizmos.DrawLine(pBegin, pEnd);
                Gizmos.color = Color.yellow;
                Gizmos.DrawSphere(pBegin, 0.1f);
                Gizmos.color = Color.blue;
                Gizmos.DrawSphere(pEnd, 0.1f);
            }

            if (previous != null &&
                ShadowMathUtils.Approximately(segment.Slope, previous.Slope) &&
                ShadowMathUtils.Approximately(Output.Last(), pBegin))
            {
                // It's a continuation of the previous segment!
                Output.RemoveAt(Output.Count - 1);
                Output.Add(pEnd);
            }
            else
            {
                Output.Add(pBegin);
                Output.Add(pEnd);
            }
        }
Exemplo n.º 3
0
        public void LateUpdate()
        {
            _meshRenderer = _meshRenderer ?? GetComponent <MeshRenderer>();


            if (_meshRenderer != null && onlyUpdateWhenVisible && !_meshRenderer.isVisible)
            {
                return;
            }

            _visiblitySet = _visiblitySet ?? new VisiblitySet();

            // Can't rotate the lights at the moment
            transform.rotation = Quaternion.identity;

            Vector2 newPosition   = transform.position;
            Vector2 oldScreenSize = _screenSize;

            _screenSize = new Vector2(Screen.width, Screen.height);

            bool rebuild = updateOnEachUpdate;

            rebuild = rebuild || _screenSize != oldScreenSize || !Mathf.Approximately(size, _oldSize);
            rebuild = rebuild || !Mathf.Approximately(Camera.main.orthographicSize, _oldOrthographicSize);
            rebuild = rebuild || !Mathf.Approximately(Camera.main.fieldOfView, _oldFieldOfView);
            rebuild = rebuild || updateOnChangedLocation && !ShadowMathUtils.Approximately(_oldPosition, newPosition);
            rebuild = rebuild || updateWhenCollidersChange && GetHaveCollidersChanged();

            _oldSize = size;

            if (rebuild)
            {
                RebuildShadow();
                _oldOrthographicSize = Camera.main.orthographicSize;
                _oldFieldOfView      = Camera.main.fieldOfView;
            }
            else
            {
                if (updateUvs &&
                    !ShadowMathUtils.Approximately(Camera.main.transform.position, _oldCameraPosition))
                {
                    _visiblitySet.UpdateUvs();
                    _oldCameraPosition = Camera.main.transform.position;
                }
            }

            _oldPosition = newPosition;
        }
Exemplo n.º 4
0
        private bool InnerUpdateColliders(Vector2 topLeft, Vector2 bottomRight, LayerMask layerMask)
        {
            _updateOnFrame = Time.frameCount;

            Collider2D[] oldColliders = colliders;
            colliders = Physics2D.OverlapAreaAll(topLeft, bottomRight, layerMask);
            // Sort them so we get them in the same order as before. This is required for the comparison we do later on.
            Array.Sort(colliders, (c1, c2) => c1.GetInstanceID() - c2.GetInstanceID());

            Vector3[] oldPositions = positions;
            positions = colliders.Select(c => c.transform.position).ToArray();
            Quaternion[] oldRotations = rotations;
            rotations = colliders.Select(c => c.transform.rotation).ToArray();

            // Ease fast checks first
            if (oldColliders == null || oldColliders.Length != colliders.Length)
            {
                return(true);
            }

            for (int index = 0; index < colliders.Length; index++)
            {
                Collider2D newCollider = colliders[index];
                Collider2D oldCollider = oldColliders[index];

                if (newCollider != oldCollider)
                {
                    return(true);
                }

                if (!ShadowMathUtils.Approximately(oldPositions[index], newCollider.transform.position))
                {
                    return(true);
                }

                if (!ShadowMathUtils.Approximately(oldRotations[index], newCollider.transform.rotation))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 5
0
        private void SplitSegmentsThatOverlap()
        {
            // O(N^2)
            Queue <Segment> open = new Queue <Segment>(Segments);

            Segments.Clear();
            while (open.Any())
            {
                if (Segments.Count > 300)
                {
                    Debug.Log("Too many segment splits!");
                    return;
                }
                Segment segment   = open.Dequeue();
                bool    splitFree = true;
                foreach (Segment testing in Segments)
                {
                    Segment.IntersectionResult intersection = Segment.IsInFrontOf(segment, testing, Center);

                    if (intersection == Segment.IntersectionResult.Intersects)
                    {
                        // Do they *really* overlap, there seems to be some issues with the IsInFrontOf method.
                        if (ShadowMathUtils.Approximately(testing.Start.Point, segment.Start.Point) ||
                            ShadowMathUtils.Approximately(testing.End.Point, segment.End.Point) ||
                            ShadowMathUtils.Approximately(testing.End.Point, segment.Start.Point) ||
                            ShadowMathUtils.Approximately(testing.Start.Point, segment.End.Point))
                        {
                            //intersection = Segment.IsInFrontOf(segment, testing, Center);
                            //Debug.Log("They're the same point!"+intersection);
                            continue;
                        }

                        // segment && testing overlap, we must split one of them but both must go back on the queue
                        Segments.Remove(testing);
                        open.Enqueue(testing);

                        Vector2 position =
                            ShadowMathUtils.LineIntersection(
                                segment.Start.Point,
                                segment.End.Point,
                                testing.Start.Point,
                                testing.End.Point);

                        if (_drawGizmos)
                        {
                            Gizmos.color = Color.red;
                            Gizmos.DrawSphere(position, 0.2f);
                        }

                        // Split segments and add both parts back to the queue
                        Split(segment, position, open);
                        splitFree = false;
                        break;
                    }
                }
                if (splitFree)
                {
                    Segments.Add(segment);
                }
            }
        }
Exemplo n.º 6
0
        private void DoMergeCollinearSegments()
        {
            if (!MergeCollinearSegments)
            {
                return;
            }
            //int merges = 0;
            //int compares = 0;
            //int before = Segments.Count;

            //Stopwatch stopwatch = Stopwatch.StartNew();

            // Log of performance optimization of this very rouine.
            // Merged 287 to 149 segments (138 merges, 1710596 compares). 322 ms.
            // Merged 287 to 149 segments (138 merges, 105329 compares). 14 ms.
            // Merged 287 to 149 segments (138 merges, 84767 compares, 2 tries). 14 ms.
            // Merged 287 to 149 segments (138 merges, 84767 compares, 2 tries). 13 ms.
            // Merged 287 to 149 segments (138 merges, 1968 compares, 6 tries). 5 ms.
            // Merged 287 to 149 segments (138 merges, 1968 compares, 6 tries). 4 ms.

            int tries = 0;
            ILookup <Vector2, Segment> startpointLookup =
                Segments
                .ToLookup(s => s.Start.Point, s => s);

            while (tries++ < 4)
            {
                bool merged = false;
                for (int index = 0; index < Segments.Count; index++)
                {
                    Segment segment = Segments[index];
                    if (segment.Merged)
                    {
                        continue;
                    }

                    IEnumerable <Segment> startpoints = startpointLookup[segment.End.Point];

                    foreach (Segment other in startpoints)
                    {
                        //compares++;
                        if (other.Merged)
                        {
                            continue;
                        }

                        if (ShadowMathUtils.Approximately(segment.Slope, other.Slope))
                        {
                            other.Merged      = true;
                            segment.End.Point = other.End.Point;
                            //merges++;
                            merged = true;
                        }
                    }
                }

                Segments.RemoveAll(s => s.Merged);
                if (!merged)
                {
                    break;
                }
            }

            //Debug.LogFormat("Merged {0} to {1} segments ({2} merges, {3} compares, {5} tries). {4} ms.", before, Segments.Count, merges, compares, stopwatch.ElapsedMilliseconds, tries);
        }