示例#1
0
        public Barycentric(Vector2[] points, int Count)
        {
            rand = new System.Random();

            dimensions = DrawTriangles.getDim(points);

            for (int i = 0; i < Count; i++)
            {
                DrawTriangles.BaryIterate();
                UnityEngine.Color[] randColors = DrawTriangles.randomColors(points.Length, rand);
                BaryFillTriangle(points, randColors);
            }
        }
示例#2
0
        public SysDrawing(Vector2[] points, int Count)
        {
            rand = new System.Random();

            dimensions = DrawTriangles.getDim(points);
            bitmap     = new Bitmap((int)Mathf.Round(DrawTriangles.ImageSize.width), (int)Mathf.Round(DrawTriangles.ImageSize.height));
            graphics   = System.Drawing.Graphics.FromImage(bitmap);

            for (int i = 0; i < Count; i++)
            {
                DrawTriangles.SysIterate();

                UnityEngine.Color[] randColors = DrawTriangles.randomColors(points.Length, rand);
                DrawTriangle(points, randColors);
            }
        }
示例#3
0
        public bool PointInTriangle(Vector2 loc, Vector2[] corners, UnityEngine.Color[] corner_eles)        //(Point A, Point B, Point C, Point P)
        {
            double s1 = corners[2].y - corners[0].y;
            double s2 = corners[2].x - corners[0].x;
            double s3 = corners[1].y - corners[0].y;
            double s4 = loc.y - corners[0].y;

            double w1 = (corners[0].x * s1 + s4 * s2 - loc.x * s1) / (s3 * s2 - (corners[1].x - corners[0].x) * s1);

            if (w1 > 1 || w1 < 0)
            {
                return(false);
            }
            double w2 = (s4 - w1 * s3) / s1;

            if (w2 > 1 || w2 < 0)
            {
                return(false);
            }
            double w3 = 1 - w2 - w1;

            if (w3 > 1 || w3 < 0)
            {
                return(false);
            }

            UnityEngine.Color newColor = new UnityEngine.Color();
            newColor.r = (float)(((w1 * corner_eles[0].r) + (w2 * corner_eles[1].r) + (w3 * corner_eles[2].r)));
            newColor.g = (float)(((w1 * corner_eles[0].g) + (w2 * corner_eles[1].g) + (w3 * corner_eles[2].g)));
            newColor.b = (float)(((w1 * corner_eles[0].b) + (w2 * corner_eles[1].b) + (w3 * corner_eles[2].b)));

            DrawTriangles.queueColorChange(loc, newColor);
            //bgVal[(y * bg.width) + x] = newColor;
            //ushort newEle = (ushort)(((w1 * corner_eles[0]) + (w2 * corner_eles[1]) + (w3 * corner_eles[2])));
            //hm.RequestsetHeight(new Vector2(x, y), newEle);

            return(true);
        }
示例#4
0
        public void DrawTriangle(Vector2[] points, UnityEngine.Color[] colors)
        {
            System.Drawing.Point[] d_points = new System.Drawing.Point[points.Length];

            Vector2 start = new Vector2(dimensions.x - (dimensions.width * 0.5f), dimensions.y - (dimensions.height * 0.5f));

            for (int i = 0; i < points.Length; i++)
            {
                d_points[i] = new System.Drawing.Point((int)Mathf.Round(points[i].x - start.x), (int)Mathf.Round(points[i].y - start.y));
            }
            System.Drawing.Color[] d_colors = new System.Drawing.Color[colors.Length];
            int totR = 0;
            int totG = 0;
            int totB = 0;

            for (int i = 0; i < colors.Length; i++)
            {
                d_colors[i] = System.Drawing.Color.FromArgb(255, (int)Mathf.Round(colors[i].r * 255), (int)Mathf.Round(colors[i].g * 255), (int)Mathf.Round(colors[i].b * 255));
                totR       += d_colors[i].R;
                totG       += d_colors[i].G;
                totB       += d_colors[i].B;
            }
            totR = totR / colors.Length;
            totG = totG / colors.Length;
            totB = totB / colors.Length;
            System.Drawing.Color c_color = System.Drawing.Color.FromArgb(255, totR, totG, totB);
            GraphicsPath         path    = new GraphicsPath();

            path.AddLines(d_points);
            PathGradientBrush pthGrBrush = new PathGradientBrush(path);

            pthGrBrush.SurroundColors = d_colors;
            pthGrBrush.CenterColor    = c_color;
            graphics.FillPath(pthGrBrush, path);

            BitmapData data  = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
            int        bytes = data.Stride * bitmap.Height;

            byte[] pixelData = new byte[bytes];
            Marshal.Copy(data.Scan0, pixelData, 0, bytes);
            bitmap.UnlockBits(data);

            int texWidth  = bitmap.Width;
            int lineWidth = texWidth * 4;

            Vector2 curr = new Vector2();

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    curr.y = y + start.y;
                    curr.x = x + start.x;
                    int pixelDataPos           = ((y * lineWidth) + (x * 4));
                    UnityEngine.Color newColor = new UnityEngine.Color(pixelData[pixelDataPos] / 255f, pixelData[pixelDataPos + 1] / 255f, pixelData[pixelDataPos + 2] / 255f);
                    if (newColor.r > 0 || newColor.b > 0 || newColor.g > 0)
                    {
                        DrawTriangles.queueColorChange(curr, newColor);
                    }
                }
            }
        }