unity 根据灰度图形成地形并导出obj文件

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

//根据灰度图创建mesh地形
public class MeshTerrainCreate : MonoBehaviour
{
    public Texture textureGray;//灰度图
    public Texture textureGrass;//草地贴图
    private int tGrayWidth =0, tGrayHeight = 0;//灰度图的宽和高
    private bool bCreate = false;//是否完成创建
    private List<GameObject> meshList;//mesh对象集合
    private Texture2D texture2dGray;
    public float zScale = 100;//高度参数

    [Tooltip("传入mesh使用的材质")]
    public Material meshMaterial;

    void Start()
    {
        //StartCoroutine(loadImage("IGray.png", (t) => textureGray = t));
        //StartCoroutine(loadImage("IGrass.jpg", (t) => textureGrass = t));
        meshList = new List<GameObject>();
    }

    void Update()
    {
        if (textureGray != null && textureGrass != null)
        {
            if (bCreate == false)
            {
                tGrayWidth = textureGray.width;
                tGrayHeight = textureGray.height;
                meshMaterial.mainTexture = textureGrass;//设置材质贴图
                //mesh顶点数目最大65000,则取mesh顶点为250*250=62500
                int xNum = 1 + tGrayWidth / 250;//x方向mesh个数
                int yNum = 1 + tGrayHeight / 250; //y方向mesh个数
                texture2dGray = (Texture2D)textureGray;
                //根据灰度图创建mesh
                for (int i = 0; i < xNum; i++)
                {
                    for (int j = 0; j < yNum; j++)
                    {
                        if (i < xNum - 1 && j < yNum - 1)
                        {
                            meshList.Add(
                                createMesh("meshX" + i.ToString() + "Y" + j.ToString(), 251, 251,
                                i * new Vector3(2500, 0, 0) + j * new Vector3(0, 2500, 0),
                                (i + 1) * new Vector3(2500, 0, 0) + (j + 1) * new Vector3(0, 2500, 0) + new Vector3(10, 10, 0),
                                i * new Vector2(250, 0) + j * new Vector2(0, 250),
                                (i + 1) * new Vector2(250, 0) + (j + 1) * new Vector2(0, 250) + new Vector2(1, 1)));
                        }
                        else if (i == xNum - 1 && j < yNum - 1)
                        {
                            meshList.Add(createMesh("meshX" + i.ToString() + "Y" + j.ToString(), tGrayWidth % 250, 251,
                            i * new Vector3(2500, 0, 0) + j * new Vector3(0, 2500, 0),
                            i * new Vector3(2500, 0, 0) + new Vector3(10 * (tGrayWidth % 250), 10, 0) + (j + 1) * new Vector3(0, 2500, 0),
                            i * new Vector2(250, 0) + j * new Vector2(0, 250),
                            i * new Vector2(250, 0) + new Vector2(tGrayWidth % 250, 1) + (j + 1) * new Vector2(0, 250)));
                        }
                        else if (i < xNum - 1 && j == yNum - 1)
                        {
                            meshList.Add(createMesh("meshX" + i.ToString() + "Y" + j.ToString(), 251, tGrayHeight % 250,
                              i * new Vector3(2500, 0, 0) + j * new Vector3(0, 2500, 0),
                             (i + 1) * new Vector3(2500, 0, 0) + j * new Vector3(0, 2500, 0) + new Vector3(10, 10 * (tGrayHeight % 250), 0),
                              i * new Vector2(250, 0) + j * new Vector2(0, 250),
                             (i + 1) * new Vector2(250, 0) + j * new Vector2(0, 150) + new Vector2(1, tGrayHeight % 250)));
                        }
                        else if (i == xNum - 1 && j == yNum - 1)
                        {
                            meshList.Add(createMesh("meshX" + i.ToString() + "Y" + j.ToString(), tGrayWidth % 250, tGrayHeight % 250,
                             i * new Vector3(2500, 0, 0) + j * new Vector3(0, 2500, 0),
                             i * new Vector3(2500, 0, 0) + j * new Vector3(0, 2500, 0) + new Vector3(10 * (tGrayWidth % 250), 10 * (tGrayHeight % 250), 0),
                             i * new Vector2(250, 0) + j * new Vector2(0, 250),
                             i * new Vector2(250, 0) + j * new Vector2(0, 250) + new Vector2(tGrayWidth % 250, tGrayHeight % 250)));
                        }
                    }
                }
                bCreate = true;
            }
        }
    }

    //Mesh数据变成一个固定格式的字符串流
    private string MeshToString(MeshFilter mf, Vector3 scale)
    {
        Mesh mesh = mf.mesh;
        Material[] sharedMaterials = mf.GetComponent<Renderer>().sharedMaterials;
        Vector2 textureOffset = mf.GetComponent<Renderer>().material.GetTextureOffset("_MainTex");
        Vector2 textureScale = mf.GetComponent<Renderer>().material.GetTextureScale("_MainTex");

        StringBuilder stringBuilder = new StringBuilder().Append("mtllib design.mtl")
            .Append("\n")
            .Append("g ")
            .Append(mf.name)
            .Append("\n");

        Vector3[] vertices = mesh.vertices;
        for (int i = 0; i < vertices.Length; i++)
        {
            Vector3 vector = vertices[i];
            stringBuilder.Append(string.Format("v {0} {1} {2}\n", vector.x * scale.x, vector.y * scale.y, vector.z * scale.z));
        }

        stringBuilder.Append("\n");

        Dictionary<int, int> dictionary = new Dictionary<int, int>();

        if (mesh.subMeshCount > 1)
        {
            int[] triangles = mesh.GetTriangles(1);

            for (int j = 0; j < triangles.Length; j += 3)
            {
                if (!dictionary.ContainsKey(triangles[j]))
                {
                    dictionary.Add(triangles[j], 1);
                }

                if (!dictionary.ContainsKey(triangles[j + 1]))
                {
                    dictionary.Add(triangles[j + 1], 1);
                }

                if (!dictionary.ContainsKey(triangles[j + 2]))
                {
                    dictionary.Add(triangles[j + 2], 1);
                }
            }
        }

        for (int num = 0; num != mesh.uv.Length; num++)
        {
            Vector2 vector2 = Vector2.Scale(mesh.uv[num], textureScale) + textureOffset;

            if (dictionary.ContainsKey(num))
            {
                stringBuilder.Append(string.Format("vt {0} {1}\n", mesh.uv[num].x, mesh.uv[num].y));
            }
            else
            {
                stringBuilder.Append(string.Format("vt {0} {1}\n", vector2.x, vector2.y));
            }
        }

        for (int k = 0; k < mesh.subMeshCount; k++)
        {
            stringBuilder.Append("\n");

            if (k == 0)
            {
                stringBuilder.Append("usemtl ").Append("Material_design").Append("\n");
            }

            if (k == 1)
            {
                stringBuilder.Append("usemtl ").Append("Material_logo").Append("\n");
            }

            int[] triangles2 = mesh.GetTriangles(k);

            for (int l = 0; l < triangles2.Length; l += 3)
            {
                stringBuilder.Append(string.Format("f {0}/{0} {1}/{1} {2}/{2}\n", triangles2[l] + 1, triangles2[l + 2] + 1, triangles2[l + 1] + 1));
            }
        }
        
        return stringBuilder.ToString();
        
    }
    //MeshFilter组件里Mesh数据变成一个固定格式的字符串流。写入到本地文件就是一个obj模型
    void Writeobj(MeshFilter mf)
    {
        using (StreamWriter streamWriter = new StreamWriter(string.Format("{0}{1}.obj", Application.streamingAssetsPath, mf.gameObject.name)))
        {
            streamWriter.Write(MeshToString(mf, new Vector3(-1f, 1f, 1f)));
            streamWriter.Close();
        }
        AssetDatabase.Refresh();
        CreatPrefabs(mf);
    }
    //加载obj模型创建预设体保存
    void CreatPrefabs(MeshFilter mf)
    {
        // create prefab  
        Mesh mesh = AssetDatabase.LoadAssetAtPath<Mesh>(string.Format("{0}{1}.obj", Application.streamingAssetsPath, mf.gameObject.name));
        mf.mesh = mesh;

        PrefabUtility.SaveAsPrefabAsset(this.gameObject,string.Format("{0}{1}.prefab", Application.streamingAssetsPath, mf.gameObject.name));
        AssetDatabase.Refresh();
    }
    //加载图片
    //IEnumerator loadImage(string imagePath, System.Action<Texture> action)
    //{
    //    WWW www = new WWW("file://" + Application.streamingAssetsPath + "/" + imagePath);
    //    yield return www;
    //    if (www.error == null)
    //    {
    //        action(www.texture);
    //    }
    //}

    private void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 200, 200), "baocun"))
        {
            //Writeobj(meshList[0].GetComponent<MeshFilter>(),"0");
            for (int i = 0; i < meshList.Count; i++)
            {
                Writeobj(meshList[i].GetComponent<MeshFilter>());
            }
        }
    }
    /// <summary>
    ///创建mesh
    /// </summary>
    /// <param name="meshName">mesh名称</param>
    /// <param name="row">行数</param>
    /// <param name="col">列数</param>
    /// <param name="minPoint">最小点位置</param>
    /// <param name="maxPoint">最大点位置</param>
    /// <param name="minImgPosition">最小点灰度图位置</param>
    /// <param name="maxImgPosition">最大点灰度图位置</param>
    /// <returns></returns>
    ///


    private GameObject createMesh(string meshName, int row, int col, Vector3 minPoint, Vector3 maxPoint, Vector2 minImgPosition, Vector2 maxImgPosition)
    {
        GameObject meshObject = new GameObject(meshName);

        int verticeNum = row * col;
        Vector3[] vertices = new Vector3[verticeNum];//顶点数组大小
        int[] triangles = new int[verticeNum * 3 * 2];//三角集合数组,保存顶点索引
                                                      // Vector3[] normals = new Vector3[verticeNum];//顶点法线数组大小
        Vector2[] uvs = new Vector2[verticeNum];
        float rowF = (float)row;
        float colF = (float)col;
        Vector3 xStep = new Vector3((maxPoint.x - minPoint.x) / rowF, 0, 0);
        Vector3 ySetp = new Vector3(0, (maxPoint.y - minPoint.y) / colF, 0);
        int k = 0;

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                float tempZ = texture2dGray.GetPixel((int)minImgPosition.x + i, (int)minImgPosition.y + j).grayscale;
                vertices[i + j * row] = minPoint + xStep * i + ySetp * j + new Vector3(0, 0, tempZ * zScale);

                uvs[i + j * row] = new Vector2((float)i / rowF, (float)j / colF);

                if (j < col - 1 && i < row - 1)
                {
                    triangles[k++] = j * row + i;
                    triangles[k++] = j * row + i + 1;
                    triangles[k++] = j * row + i + row;

                    triangles[k++] = j * row + i + row;
                    triangles[k++] = j * row + i + 1;
                    triangles[k++] = j * row + i + row + 1;
                }
            }
        }
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        // mesh.normals = normals;
        mesh.uv = uvs;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        meshObject.AddComponent<MeshFilter>();
        meshObject.AddComponent<MeshRenderer>();
        meshObject.GetComponent<MeshFilter>().mesh = mesh;
        meshObject.GetComponent<MeshRenderer>().material = meshMaterial;

        return meshObject;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值