Пример #1
0
    void RemoveVoxels(CancellationToken token)
    {
        int lastFitness = int.MinValue;

        for (int i = 0; i < 100000; i++)
        {
            if (token.IsCancellationRequested)
            {
                break;
            }

            var actives = _grid.GetVoxels().Where(v => v.IsSkin).ToList();
            var random  = Drawing.Random.Next(0, actives.Count);
            var active  = actives[random];

            active.IsActive = false;

            if (_grid.GetConnectedComponents() != 1)
            {
                active.IsActive = true;
                continue;
            }

            int fitness = 0;

            foreach (var voxel in _grid.GetVoxels())
            {
                if (!voxel.IsActive)
                {
                    continue;
                }

                var neighbourCount = voxel
                                     .GetFaceNeighbours()
                                     .Count(n => n.IsActive);

                if (neighbourCount == 2)
                {
                    fitness += 1;
                }
            }

            if (fitness < lastFitness)
            {
                active.IsActive = true;
                continue;
            }

            lastFitness = fitness;

            lock (_centers)
                UpdateActiveVoxels();

            // Debug.Log(fitness);
        }
    }
Пример #2
0
    IEnumerator GrowGrid()
    {
        int count       = 100000;
        int lastFitness = 0;

        while (count-- > 0)
        {
            var skinVoxels = _grid.GetVoxels().Where(v => v.IsSkin).ToList();

            if (skinVoxels.Count == 0)
            {
                break;
            }

            var index     = Random.Range(0, skinVoxels.Count);
            var candidate = skinVoxels[index];

            candidate.IsActive = false;

            int componentCount = _grid.GetConnectedComponents();
            if (componentCount != 1)
            {
                Debug.Log("Tried to remove a voxel that disconnected the structure.");
                candidate.IsActive = true;
                yield return(null);
            }

            int fitness = 0;

            foreach (var voxel in _grid.GetVoxels())
            {
                if (!voxel.IsActive)
                {
                    continue;
                }
                int neighbourCount = voxel.GetFaceNeighbours().Count(n => n.IsActive);

                if (neighbourCount == 2)
                {
                    fitness += 1;
                }
            }

            if (fitness < lastFitness)
            {
                Debug.Log("Fitness decreased.");
                candidate.IsActive = true;
                continue;
            }

            lastFitness = fitness;
            UpdateCenters();
            yield return(new WaitForSeconds(0.001f));
        }
    }