Beispiel #1
0
        public List <Vector2> Generate(MyRectangle generationArea, float generationCount,
                                       float exclusionRadius, int maxTries, int maxPerGridCount, IIntensitySamplingProvider intensityProvider)
        {
            var random = new UnsafeRandomProvider(); //todo!
            var offsetGenerationArea = new MyRectangle(0, 0, generationArea.Width, generationArea.Height);

            float cellSize = exclusionRadius;

            var width  = generationArea.Width;
            var height = generationArea.Height;

            var grid = new CountingGenerationGrid(
                cellSize,
                new IntVector2(Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize))
                );

            var processList    = new GenerationRandomQueue <Vector2>();
            var acceptedPoints = new List <Vector2>(1000);

            var firstPoint = new Vector2(random.Next(0, width), random.Next(0, height));

            processList.Add(firstPoint);
            acceptedPoints.Add(firstPoint);
            grid.Increment(firstPoint);


            int tryCount = 0;

            while (!processList.Empty)
            {
                var point = processList.RandomPop();
                for (int i = 0; i < generationCount; i++)
                {
                    Vector2 newPoint = GenerateRandomPointAround(point, exclusionRadius, random);

                    if (offsetGenerationArea.Contains(newPoint))
                    {
                        var maxPointsInGrid =
                            intensityProvider.Sample(
                                RectangleUtils.CalculateSubelementUv(offsetGenerationArea, newPoint)) *
                            maxPerGridCount;
                        if (grid.Retrive(newPoint) < maxPointsInGrid)
                        {
                            processList.Add(newPoint);
                            acceptedPoints.Add(newPoint);
                            grid.Increment(newPoint);
                        }
                    }
                    tryCount++;
                }
                if (tryCount > maxTries)
                {
                    break;
                }
            }
            return(acceptedPoints.Select(c => c + generationArea.DownLeftPoint).ToList());
        }
        public void CreateDebugIntensityTexture(IIntensitySamplingProvider provider)
        {
            var tex = new Texture2D(16, 16, TextureFormat.ARGB32, false, true);

            for (int x = 0; x < 16; x++)
            {
                for (int y = 0; y < 16; y++)
                {
                    tex.SetPixel(x, y, new Color(provider.Sample(new Vector2(x / 15f, y / 15f)), 0, 0, 1));
                }
            }
            tex.Apply(false);
            SavingFileManager.SaveTextureToPngFile($@"C:\inz2\Intensity.png", tex);
        }
Beispiel #3
0
        public List <Vector2> Generate(MyRectangle generationArea, int generationCount, float maxTries,
                                       float exclusionRadius, bool collisionsAreChecked, IIntensitySamplingProvider intensitySamplingProvider)
        {
            var random = new UnsafeRandomProvider(); //todo!
            var offsetGenerationArea = new MyRectangle(0, 0, generationArea.Width, generationArea.Height);

            float cellSize = exclusionRadius / Mathf.Sqrt(2);

            var width  = generationArea.Width;
            var height = generationArea.Height;

            var grid = new SingleElementGenerationGrid(
                cellSize,
                Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize),
                exclusionRadius
                );

            var acceptedPoints = new List <Vector2>(generationCount);

            int generatedPointsCount = 0;
            int triesCount           = 0;

            while (generatedPointsCount < generationCount && triesCount < maxTries)
            {
                var randomPoint = new Vector2(random.Next(0, width), random.Next(0, height));

                var randomPointValue = random.NextValue;
                var intensity        =
                    intensitySamplingProvider.Sample(
                        RectangleUtils.CalculateSubelementUv(offsetGenerationArea, randomPoint));
                if (intensity >= randomPointValue)
                {
                    if (!collisionsAreChecked || !grid.Collides(randomPoint))
                    {
                        if (collisionsAreChecked)
                        {
                            grid.Set(randomPoint);
                        }

                        acceptedPoints.Add(randomPoint);
                        generatedPointsCount++;
                    }
                }

                triesCount++;
            }
            return(acceptedPoints.Select(c => c + generationArea.DownLeftPoint).ToList());
        }
Beispiel #4
0
        private float?CalculateExclusionRadius(
            Vector2 point,
            MyRectangle offsetGenerationArea,
            MyRange exclusionRadiusRange,
            IIntensitySamplingProvider intensityProvider)
        {
            var uv     = RectangleUtils.CalculateSubelementUv(offsetGenerationArea, point);
            var sample = intensityProvider.Sample(uv);

            var exclusionRadius = exclusionRadiusRange.Lerp(1 - sample);

            if (sample > 0.01f)
            {
                return(exclusionRadius);
            }
            else
            {
                return(null);
            }
        }