Example #1
0
    /// <summary>
    /// 发起Win会话读取选择的图片
    /// </summary>
    public static void OpenImage(int mapWidth, uint mapPD, string savePath, string mapName)
    {
        string path = EditorUtility.OpenFilePanelWithFilters("选择文件", "C:\\Users\\Administrator\\Desktop", new string[] { "图片格式", "png,jpg,jpeg", "All files", "*" });
        //创建文件读取流
        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);

        fileStream.Seek(0, SeekOrigin.Begin);
        //创建文件长度缓冲区
        byte[] bytes = new byte[fileStream.Length];
        //读取文件
        fileStream.Read(bytes, 0, (int)fileStream.Length);
        //释放文件读取流
        fileStream.Close();
        fileStream.Dispose();
        fileStream = null;
        Texture2D texture = new Texture2D(1, 1);

        bool isload = texture.LoadImage(bytes);
        //Sprite tempSp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
        //GameObject.Find("TestImage").GetComponent<Image>().sprite = tempSp;

        //GameObject tGO = new GameObject("testTerrain");
        //DrawMesh(tGO, texture, mapWidth, mapPD);
        XLZMapMesh xLZMap = new XLZMapMesh(mapName, texture, savePath);

        xLZMap.CreatMesh(mapWidth, mapWidth, mapPD, mapPD, -10, 10);
    }
Example #2
0
    /// <summary>
    /// 发起Win会话读取选择的图片
    /// </summary>
    public static void OpenImage2(int mapWidth, uint mapPD, int mapCount, string mapName)
    {
        string folderPath = EditorUtility.OpenFolderPanel("选择文件夹", "Assets/", "");

        string[] paths = Directory.GetFiles(folderPath);

        GameObject[] gameObjects = new GameObject[paths.Length];

        for (int i = 0; i < paths.Length; i++)
        {
            int remainder = i % mapCount;                   //余数
            int multiple  = Mathf.FloorToInt(i / mapCount); //倍数

            //创建文件读取流
            FileStream fileStream = new FileStream(paths[i], FileMode.Open, FileAccess.Read);
            fileStream.Seek(0, SeekOrigin.Begin);
            //创建文件长度缓冲区
            byte[] bytes = new byte[fileStream.Length];
            //读取文件
            fileStream.Read(bytes, 0, (int)fileStream.Length);
            //释放文件读取流
            fileStream.Close();
            fileStream.Dispose();
            fileStream = null;
            Texture2D texture = new Texture2D(1, 1);

            bool isload = texture.LoadImage(bytes);

            XLZMapMesh xLZMap = new XLZMapMesh(mapName + "_" + i, texture, "");
            xLZMap.CreatMesh(mapWidth, mapWidth, mapPD, mapPD, -10, 10);

            GameObject go = xLZMap.MMesh;
            go.transform.position = new Vector3(-mapWidth * multiple, 0, remainder * mapWidth);
            gameObjects[i]        = go;
        }
        float maxDis = mapWidth / mapPD;
        int   pd     = (int)mapPD;
        int   pdMax  = pd * (pd + 1);

        for (int i = 0; i < mapCount; i++)
        {
            for (int j = 0; j < mapCount; j++)
            {
                if (j < mapCount - 1)                                                                      //先Z轴贴合
                {
                    Mesh mesh1 = gameObjects[i * mapCount + j].GetComponent <MeshFilter>().sharedMesh;     //网格
                    Mesh mesh2 = gameObjects[i * mapCount + j + 1].GetComponent <MeshFilter>().sharedMesh; //网格

                    Mesh nmesh1 = new Mesh();
                    Mesh nmesh2 = new Mesh();
                    nmesh1.name = mesh1.name;
                    nmesh2.name = mesh2.name;
                    Vector3[] m1vs = mesh1.vertices;
                    Vector3[] m2vs = mesh2.vertices;

                    // Vector3[] m1vsz = new Vector3[mapPD+1];
                    for (int k = 0; k < pd + 1; k++)
                    {
                        Vector3 v1 = m1vs[pdMax + k];
                        Vector3 v2 = m2vs[k];

                        Vector3 wv1 = gameObjects[i * mapCount + j].transform.TransformPoint(v1);
                        Vector3 wv2 = gameObjects[i * mapCount + j + 1].transform.TransformPoint(v2);

                        Vector3 v = (wv1 + wv2) / 2;

                        m1vs[pdMax + k] = gameObjects[i * mapCount + j].transform.InverseTransformPoint(v);
                        m2vs[k]         = gameObjects[i * mapCount + j + 1].transform.InverseTransformPoint(v);
                    }
                    nmesh1.Clear();//更新
                    nmesh1.vertices  = m1vs;
                    nmesh1.triangles = mesh2.triangles;
                    nmesh1.RecalculateNormals();
                    nmesh1.RecalculateBounds();
                    nmesh2.Clear();//更新
                    nmesh2.vertices  = m2vs;
                    nmesh2.triangles = mesh2.triangles;
                    nmesh2.RecalculateNormals();
                    nmesh2.RecalculateBounds();
                    gameObjects[i * mapCount + j].GetComponent <MeshFilter>().mesh     = nmesh1;
                    gameObjects[i * mapCount + j + 1].GetComponent <MeshFilter>().mesh = nmesh2;
                    //mesh2.vertices = m2vs;
                }
                if (i < mapCount - 1)                                                                             //再X轴贴合
                {
                    Mesh mesh1 = gameObjects[i * mapCount + j].GetComponent <MeshFilter>().sharedMesh;            //网格
                    Mesh mesh2 = gameObjects[i * mapCount + j + mapCount].GetComponent <MeshFilter>().sharedMesh; //网格

                    Mesh nmesh1 = new Mesh();
                    Mesh nmesh2 = new Mesh();
                    nmesh1.name = mesh1.name;
                    nmesh2.name = mesh2.name;
                    Vector3[] m1vs = mesh1.vertices;
                    Vector3[] m2vs = mesh2.vertices;

                    // Vector3[] m1vsz = new Vector3[mapPD+1];
                    for (int k = 0; k < pd + 1; k++)
                    {
                        Vector3 v1 = m1vs[(pd + 1) * k];//
                        Vector3 v2 = m2vs[(pd + 1) * k + pd];

                        Vector3 wv1 = gameObjects[i * mapCount + j].transform.TransformPoint(v1);
                        Vector3 wv2 = gameObjects[i * mapCount + j + mapCount].transform.TransformPoint(v2);

                        Vector3 v = (wv1 + wv2) / 2;

                        m1vs[(pd + 1) * k]      = gameObjects[i * mapCount + j].transform.InverseTransformPoint(v);
                        m2vs[(pd + 1) * k + pd] = gameObjects[i * mapCount + j + mapCount].transform.InverseTransformPoint(v);
                    }
                    nmesh1.Clear();//更新
                    nmesh1.vertices  = m1vs;
                    nmesh1.triangles = mesh2.triangles;
                    nmesh1.RecalculateNormals();
                    nmesh1.RecalculateBounds();
                    nmesh2.Clear();//更新
                    nmesh2.vertices  = m2vs;
                    nmesh2.triangles = mesh2.triangles;
                    nmesh2.RecalculateNormals();
                    nmesh2.RecalculateBounds();
                    gameObjects[i * mapCount + j].GetComponent <MeshFilter>().mesh            = nmesh1;
                    gameObjects[i * mapCount + j + mapCount].GetComponent <MeshFilter>().mesh = nmesh2;
                }
            }
        }
    }