Ejemplo n.º 1
0
    private void LoadImageMaze(string filePath, int newWidth, int newHeight)
    {
        Clear();
        byte[]    fileData = File.ReadAllBytes(filePath);
        Texture2D tex      = new Texture2D(2, 2, TextureFormat.RGBA32, false);

        tex.LoadImage(fileData);

        tex = TextureScaler.Scaled(tex, newWidth, newHeight);

        Color32[] colors = tex.GetPixels32();
        Color32   color;
        int       c;
        int       max = 256 * 256 * 256 - 1;
        int       red = 255 << 16;

        float x, z;
        float y = transform.position.y;
        float mx = 0, mz = 0;
        int   mc = 0;

        int W = tex.width;
        int H = tex.height;
        int X = -W / 2;
        int Y = -H / 2;

        float h, s, v;

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                color = colors[i * W + j];

                if (color.r < 255 && color.g < 255 && color.b < 255)
                {
                    x = X + j;
                    z = Y + i;

                    Color.RGBToHSV(color, out h, out s, out v);
                    h *= 360;
                    s *= 100;
                    v *= 100;

                    // Vermelho (Robô)
                    if (h < 20 && s > 50 && v > 50)
                    {
                        mx += x;
                        mz += z;
                        mc++;
                    }
                    // Amarelo (Linha)
                    else if (h > 40 && h < 70 && s > 50 && v > 50)
                    {
                        /*
                         * mx += x;
                         * mz += z;
                         * mc++;
                         */
                        Instantiate(Cell, new Vector3(x, y, z), Quaternion.identity, transform);
                    }
                    else
                    {
                        Instantiate(Cell, new Vector3(x, y, z), Quaternion.identity, transform);
                    }
                }
            }
        }
        if (mc > 0)
        {
            mx            /= mc;
            mz            /= mc;
            Robot.position = new Vector3(mx, 0.86f, mz);
            Robot.rotation = Quaternion.Euler(0, 0, 0);
        }
    }