Beispiel #1
0
        public static void FillTriangleSimple(PaintTexture bitmap, Rectangle canvas, int x0, int y0, int x1, int y1, int x2, int y2, int color)
        {
            var c = (float)(color / 255f);

            TexturePoint[] pixels = bitmap.GetPixels(canvas).ToArray();
            int            width  = canvas.Width;
            int            height = canvas.Height;

            // sort the points vertically
            if (y1 > y2)
            {
                Swap(ref x1, ref x2);
                Swap(ref y1, ref y2);
            }
            if (y0 > y1)
            {
                Swap(ref x0, ref x1);
                Swap(ref y0, ref y1);
            }
            if (y1 > y2)
            {
                Swap(ref x1, ref x2);
                Swap(ref y1, ref y2);
            }

            double dx_far   = Convert.ToDouble(x2 - x0) / (y2 - y0 + 1);
            double dx_upper = Convert.ToDouble(x1 - x0) / (y1 - y0 + 1);
            double dx_low   = Convert.ToDouble(x2 - x1) / (y2 - y1 + 1);
            double xf       = x0;
            double xt       = x0 + dx_upper; // if y0 == y1, special case

            for (int y = y0; y <= (y2 > height - 1 ? height - 1 : y2); y++)
            {
                if (y >= 0)
                {
                    for (int x = (xf > 0 ? Convert.ToInt32(xf) : 0); x <= (xt < width ? xt : width - 1); x++)
                    {
                        pixels[Convert.ToInt32(x + y * width)].Value = c;
                    }
                    for (int x = (xf < width ? Convert.ToInt32(xf) : width - 1); x >= (xt > 0 ? xt : 0); x--)
                    {
                        pixels[Convert.ToInt32(x + y * width)].Value = c;
                    }
                }
                xf += dx_far;
                if (y < y1)
                {
                    xt += dx_upper;
                }
                else
                {
                    xt += dx_low;
                }
            }
        }
        private static void Paint(Voronoi graph, PaintTexture texture)
        {
            //Parallel.ForEach(graph.SitesIndexedByLocation, site =>
            //{
            foreach (var site in graph.SitesIndexedByLocation)
            {
                texture.PolygonFill(site.Value.OrderedColourPoints.Select(p => p.Position).ToList(),
                                    site.Value.MapWeight);
            }

            //});
        }
        private static void GenerateTerrain(int numberOfPoints, PaintTexture texture, int seed)
        {
            var canvas = texture.Canvas;

            var sw  = new Stopwatch();
            var sw2 = new Stopwatch();

            sw2.Start();

            sw.Start();
            var points = GetRandomPoints(numberOfPoints, canvas, seed);

            Debug.Log($"It took {sw.Elapsed} to Generate Points");

            sw.Restart();
            var graph = new Voronoi(points.ToList(),
                                    new Rectf(canvas.StartPoint.x, canvas.StartPoint.y, canvas.EndPoint.x, canvas.EndPoint.y), 1);

            Debug.Log($"It took {sw.Elapsed} to Generate the Graph");

            sw.Restart();
            SewBorder(graph);
            Debug.Log($"It took {sw.Elapsed} to SewBorders");

            sw.Restart();
            GenerateTectonics(graph, 64, seed);
            Debug.Log($"It took {sw.Elapsed} to Generate Tectonic Plates");

            sw.Restart();
            SmoothSites(graph, 2);
            Debug.Log($"It took {sw.Elapsed} to Smooth sites");

            sw.Restart();
            GenerateTectonicShifts(graph, 2);
            Debug.Log($"It took {sw.Elapsed} to Push the plates around");

            sw.Restart();
            graph.Plates.ForEach(p => p.EqualisePlateHeight(5));
            Debug.Log($"It took {sw.Elapsed} to Equalise Plates");

            sw.Restart();
            SmoothSites(graph, 4);
            Debug.Log($"It took {sw.Elapsed} to Smooth sites");

            sw.Restart();
            Paint(graph, texture);
            Debug.Log($"It took {sw.Elapsed} to Paint");

            Debug.Log($"It took {sw2.Elapsed} to Do everything\n\n");
        }
        /// <summary>
        /// This is a very slow process that takes a Sphere_Template file and
        /// processes a heightmap from the regions. Run this async so it doesn't
        /// kill your framerate.
        /// </summary>
        /// <param name="material">The renderer material</param>
        /// <param name="mainTexturePixels"></param>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="seed"></param>
        /// <returns></returns>
        public static ThreadTexture GenerateHeightmap(Material material, Color[] mainTexturePixels, int height,
                                                      int width, int seed)
        {
            var texture   = new ThreadTexture(mainTexturePixels, width, height);
            var paintable = new PaintTexture(texture, seed);

            const int NUMBER_OF_POINTS = 1024;

            GenerateTerrain(NUMBER_OF_POINTS, paintable, seed);

            paintable.Paint();
            texture = paintable.Texture;
            return(texture);
        }