Exemplo n.º 1
0
 private void Start()
 {
     if (!universe)
     {
         universe = FindObjectOfType <InfiniteUniverse>();
     }
     if (localPosition != transform.position)
     {
         universalPosition = BigVec3.create(transform.position * precision);
         localPosition     = transform.position;
     }
 }
    void Start()
    {
        if (!universe)
        {
            universe = FindObjectOfType <InfiniteUniverse>();
        }

        if (!entity)
        {
            entity = GetComponent <UniverseEntity>();
        }

        var seed = entity.UniversalPosition.GetHashCode();
        var rand = new System.Random(seed);
        int n    = rand.Next(24, 64);

        double galaxyWindow  = regionSize / 100;
        double galaxyDensity = 1 / (galaxyWindow * galaxyWindow * galaxyWindow);

        nodes.Clear();
        for (int i = 0; i < n; i++)
        {
            nodes.Add(new Node {
                pos     = vec(0.6 * Lerp(-regionSize, regionSize, rand.NextDouble()), 0.6 * Lerp(-regionSize, regionSize, rand.NextDouble()), 0.6 * Lerp(-regionSize, regionSize, rand.NextDouble())),
                radius  = ff(Lerp(regionSize / 2000, regionSize / 100, rand.NextDouble())),
                density = ff(Lerp(galaxyDensity / 100, galaxyDensity * 10, rand.NextDouble()))
            });
        }

        // we should generate m segments
        // each segment gets a random pair of nodes
        // we perform k passes wherein we choose m segments at random and attempt to replace the nodes with a closer pair (if the randomly chosen pair is further apart we keep what we had)
        int m = rand.Next(16, 64);

        segments.Clear();
        for (int i = 0; i < m; i++)
        {
            var a = nodes[rand.Next(0, n)];
            var b = nodes[rand.Next(0, n)];
            segments.Add(new Segment {
                a       = a, b = b,
                radius  = ff(0.9 * Lerp(a.radius, b.radius, 0.5)),
                density = ff(0.9 * Lerp(a.density, b.density, 0.5))
            });
        }

        int k = 2 * n + m;

        for (int i = 0; i < k; i++)
        {
            var   seg = segments[rand.Next(0, m)];
            var   a   = nodes[rand.Next(0, n)];
            var   b   = nodes[rand.Next(0, n)];
            float d0  = (seg.a.pos - seg.b.pos).sqrMagnitude;
            float d1  = (a.pos - b.pos).sqrMagnitude;
            if (seg.a == seg.b || a != b && d1 < d0)
            {
                seg.a = a;
                seg.b = b;
            }
        }

        for (int i = m - 1; i >= 0; i--)
        {
            var seg = segments[i];
            if (seg.a == seg.b)
            {
                segments.RemoveAt(i);
            }
            else
            {
                for (int j = 0; j < i; j++)
                {
                    var segJ = segments[j];
                    if (seg.a == segJ.a && seg.b == segJ.b || seg.a == segJ.b && seg.b == segJ.a)
                    {
                        segments.RemoveAt(i);
                        break;
                    }
                }
            }
        }

        CreateFacade();
        ShowFacade();
    }