Beispiel #1
0
    public static void SeveToMesh()
    {
        Transform[] transforms = Selection.transforms;
        foreach (Transform transform in transforms)
        {
            EditorUtility.DisplayProgressBar("Generate...", "heightmap", 0f);
            Terrain terrain    = transform.GetComponent <Terrain>();
            Vector3 size       = terrain.terrainData.size;
            int     w          = terrain.terrainData.heightmapWidth;
            int     h          = terrain.terrainData.heightmapHeight;
            int     resolution = terrain.terrainData.heightmapResolution;
            float[,] heightmap = terrain.terrainData.GetHeights(0, 0, w, h);

            EditorUtility.DisplayProgressBar("Generate...", "MeshRect", 0.1f);
            //网格密度
            int meshx = 32;
            int meshz = 32;

            Mesh mesh = CodeMesh.GetCellXZRect(meshx, meshz, 0, 0, 1f, 1f, size.x / (float)meshx, size.z / (float)meshz);

            EditorUtility.DisplayProgressBar("Generate...", "SetHeight", 0.6f);

            CodeMesh.SetHeight(terrain, mesh, (int)size.x, (int)size.z);

            EditorUtility.DisplayProgressBar("Generate...", "Recalculate", 0.9f);
            //修改完高度以后重新运算法线
            mesh.RecalculateNormals();
            //修改完高度以后重新运算切线
            mesh.RecalculateTangents();
            //修改完高度以后重新运算包围盒子
            mesh.RecalculateBounds();

            EditorUtility.DisplayProgressBar("Generate...", "SaveMesh", 0.9f);

            AssetDatabase.CreateAsset(mesh, "Assets/T4MOBJ/" + terrain.name + ".asset");
            AssetDatabase.Refresh();

            EditorUtility.ClearProgressBar();
        }
    }
Beispiel #2
0
    public static void SeveToMeshCell()
    {
        Transform[] transforms = Selection.transforms;
        foreach (Transform transform in transforms)
        {
            EditorUtility.DisplayProgressBar("Generate...", "Heightmap", 0f);

            Terrain terrain    = transform.GetComponent <Terrain>();
            Vector3 size       = terrain.terrainData.size;
            int     w          = terrain.terrainData.heightmapWidth;
            int     h          = terrain.terrainData.heightmapHeight;
            int     resolution = terrain.terrainData.heightmapResolution;
            float[,] heightmap = terrain.terrainData.GetHeights(0, 0, w, h);

            EditorUtility.DisplayProgressBar("Generate...", "Cutting", 0.1f);

            /*
             * int meshx = 32;
             * int meshz = 32;
             *
             * Mesh mesh = CodeMesh.GetCellXZRect(meshx, meshz);
             * CodeMesh.SetHeight(terrain, mesh, meshx, meshz);
             */

            List <MeshCell> list = new List <MeshCell>();
            //地图块密度
            int cellxNum = 8;
            int cellzNum = 8;
            CodeMesh.Cutting(terrain, cellxNum, cellzNum, list);

            EditorUtility.DisplayProgressBar("Generate...", "TerrainMeshCell", 0.7f);

            GameObject terrainMeshCellRoot = new GameObject("TerrainMeshCellRoot");
            if (terrain.gameObject.transform.parent != null)
            {
                terrainMeshCellRoot.transform.parent = terrain.gameObject.transform.parent;
            }
            terrainMeshCellRoot.transform.position = terrain.gameObject.transform.position;

            for (int i = 0; i < list.Count; i++)
            {
                MeshCell meshCell = list[i];

                Mesh mesh = meshCell.mesh;
                //修改完高度以后重新运算法线
                //mesh.RecalculateNormals();
                //修改完高度以后重新运算切线
                //mesh.RecalculateTangents();
                //修改完高度以后重新运算包围盒子
                //mesh.RecalculateBounds();

                GameObject terrainMeshCell = new GameObject("TerrainMeshCell_" + mesh.name);
                terrainMeshCell.transform.parent        = terrainMeshCellRoot.transform;
                terrainMeshCell.transform.localPosition = new Vector3(meshCell.xIndex * size.x / cellxNum, 0, meshCell.zIndex * size.z / cellzNum);

                MeshFilter meshFilter = terrainMeshCell.AddComponent <MeshFilter>();
                meshFilter.sharedMesh = mesh;
                terrainMeshCell.AddComponent <MeshRenderer>();
                terrainMeshCell.AddComponent <MeshCollider>();
                //AssetDatabase.CreateAsset(mesh, "Assets/T4MOBJ/" + terrain.name +"_"+ mesh.name + ".asset");
                //AssetDatabase.Refresh();
            }

            if (true)
            {
                Vector3 terrainSize = terrain.terrainData.size;
                int     meshx       = (int)terrainSize.x;
                int     meshz       = (int)terrainSize.z;

                Mesh terrainMesh = CodeMesh.GetCellXZRect(meshx, meshz);
                CodeMesh.SetHeight(terrain, terrainMesh, meshx, meshz);
                //修改完高度以后重新运算法线
                terrainMesh.RecalculateNormals();
                //修改完高度以后重新运算切线
                terrainMesh.RecalculateTangents();
                //修改完高度以后重新运算包围盒子
                terrainMesh.RecalculateBounds();

                GameObject terrainMeshCell = new GameObject("TerrainMesh");
                terrainMeshCell.transform.parent        = terrainMeshCellRoot.transform;
                terrainMeshCell.transform.localPosition = new Vector3(0, 0, 0);

                MeshFilter meshFilter = terrainMeshCell.AddComponent <MeshFilter>();
                meshFilter.sharedMesh = terrainMesh;
                MeshRenderer meshRenderer = terrainMeshCell.AddComponent <MeshRenderer>();
            }
        }

        EditorUtility.ClearProgressBar();
    }
Beispiel #3
0
    public static void Cutting(Terrain terrain, int cellxNum, int cellzNum, List <MeshCell> list)
    {
        Vector3 size = terrain.terrainData.size;

        int meshx = (int)size.x;
        int meshz = (int)size.z;

#if UNITY_EDITOR
        EditorUtility.DisplayProgressBar("Generate...", "terrainMesh GetCellRect", 0.15f);
#endif

        Mesh terrainMesh = CodeMesh.GetCellXZRect(meshx, meshz);

#if UNITY_EDITOR
        EditorUtility.DisplayProgressBar("Generate...", "terrainMesh SetHeight", 0.2f);
#endif

        CodeMesh.SetHeight(terrain, terrainMesh, meshx, meshz);

#if UNITY_EDITOR
        EditorUtility.DisplayProgressBar("Generate...", "terrainMesh Recalculate Normals,Tangents", 0.3f);
#endif

        //修改完高度以后重新运算法线
        terrainMesh.RecalculateNormals();
        //修改完高度以后重新运算切线
        terrainMesh.RecalculateTangents();

#if UNITY_EDITOR
        EditorUtility.DisplayProgressBar("Generate...", "save Recalculate Normals,Tangents", 0.35f);
#endif
        normal  = new Vector3[meshx + 1, meshz + 1];
        tangent = new Vector4[meshx + 1, meshz + 1];

        for (int i = 0; i < terrainMesh.vertices.Length; i++)
        {
            Vector3 vertice = terrainMesh.vertices[i];
            int     x       = (int)vertice.x;
            int     z       = (int)vertice.z;
            normal[x, z]  = terrainMesh.normals[i];
            tangent[x, z] = terrainMesh.tangents[i];
        }
        //HeightMapDic.Add(v2, vertice.y);

        int heightmapWidth  = terrain.terrainData.heightmapWidth;
        int heightmapHeight = terrain.terrainData.heightmapHeight;

        float heightmapWidth_size_x  = heightmapWidth / size.x;
        float heightmapHeight_size_z = heightmapHeight / size.z;
        //单个地图块的尺寸
        int cellxSize = Mathf.RoundToInt(size.x / cellxNum);
        int cellzSize = Mathf.RoundToInt(size.z / cellzNum);

        int heightmapXSize = Mathf.RoundToInt(cellxSize * heightmapWidth_size_x);
        int heightmapZSize = Mathf.RoundToInt(cellzSize * heightmapHeight_size_z);

        float uOffset = 0;
        float vOffset = 0;
        float uSize   = 1f / cellxNum;
        float vSize   = 1f / cellzNum;

        Vector2 key = new Vector2();
#if UNITY_EDITOR
        EditorUtility.DisplayProgressBar("Generate...", "terrainMesh Cell", 0.38f);
#endif
        for (int i = 0; i < cellxNum; i++)
        {
#if UNITY_EDITOR
            EditorUtility.DisplayProgressBar("Generate...", "terrainMesh Cell", 0.38f + 0.6f * (float)i / (float)cellxNum);
#endif
            for (int j = 0; j < cellzNum; j++)
            {
                uOffset = i * uSize;
                vOffset = j * vSize;

                Mesh mesh = GetCellXZRect(cellxSize, cellzSize, uOffset, vOffset, uSize, vSize);

                Vector3[] vertices = mesh.vertices;
                Vector3[] normals  = new Vector3[vertices.Length];
                Vector4[] tangents = new Vector4[vertices.Length];

                for (int k = 0; k < vertices.Length; k++)
                {
                    Vector3 p = vertices[k];

                    int x = (int)p.x + i * cellxSize;
                    int z = (int)p.z + j * cellzSize;

                    key.x = x;
                    key.y = z;
                    //Vector2 v2 = new Vector2(vertice.x, vertice.z);
                    p.y = HeightMapDic[x, z];

                    Debug.LogWarning("key = >>" + key + ",HeightMapDic = " + HeightMapDic[x, z]);

                    vertices[k] = p;

                    normals[k]  = normal[x, z];
                    tangents[k] = tangent[x, z];
                }

                mesh.vertices = vertices;
                //修改完高度以后重新运算法线
                mesh.normals = normals;
                //修改完高度以后重新运算切线
                mesh.tangents = tangents;
                //修改完高度以后重新运算包围盒子
                mesh.RecalculateBounds();

                /*
                 * heightmapXOffset = i * Mathf.RoundToInt(cellxSize * heightmapWidth_size_x);
                 * heightmapZOffset = j * Mathf.RoundToInt(cellzSize * heightmapHeight_size_z);
                 *
                 * float sy = terrain.terrainData.size.y;
                 * float[,] heightmap = terrain.terrainData.GetHeights(heightmapXOffset, heightmapZOffset, heightmapXSize, heightmapZSize);
                 *
                 * SetHeightMap(mesh, cellxSize + 1, cellzSize + 1, heightmap, heightmapXSize, heightmapZSize, sy);
                 */
                list.Add(new MeshCell(i, j, mesh));
            }
        }
#if UNITY_EDITOR
        EditorUtility.DisplayProgressBar("Generate...", "terrainMesh Cell", 0.38f + 0.6f + 0.02f);
#endif
    }