unity 讀取灰度圖生成三維地形並貼圖衛星影像


從 https://earthexplorer.usgs.gov/ 下載高程數據

 從谷歌地球上保存對應地區衛星圖像

從灰度圖創建地形模型,並將衛星影像作為貼圖

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mapMeshCreate : MonoBehaviour {
    private Texture textureGray;//灰度圖
    private Texture textureSatellite;//衛星影像貼圖
    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("T1.jpg", (t) => textureGray = t));
        StartCoroutine(loadImage("T2.png", (t) => textureSatellite = t));
        meshList = new List<GameObject>();
    }

    void Update()
    {
        if (textureGray != null && textureSatellite != null)
        {
            if (bCreate == false)
            {
                tGrayWidth = textureGray.width;
                tGrayHeight = textureGray.height;
                meshMaterial.mainTexture = textureSatellite;//設置材質貼圖
                //mesh頂點數目最大65000,則取mes為250*250=62500
                int xNum = 1 + tGrayWidth / 250;//x方向mesh個數
                int zNum = 1 + tGrayHeight / 250; //z方向mesh個數
                texture2dGray = (Texture2D)textureGray;
                //根據灰度圖創建mesh
                for (int i = 0; i < xNum; i++)
                {
                    for (int j = 0; j < zNum; j++)
                    {
                        if (i < xNum - 1 && j < zNum - 1)
                        {
                            meshList.Add(
                                createMesh("meshX" + i.ToString() + "Z" + j.ToString(), 251, 251,
                                i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0, 2500),
                                (i + 1) * new Vector3(2500, 0, 0) + (j + 1) * new Vector3(0, 0, 2500) + new Vector3(10, 0, 10),
                                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),i,j,tGrayWidth,tGrayHeight));
                        }
                        else if (i == xNum - 1 && j < zNum - 1)
                        {
                            meshList.Add(createMesh("meshX" + i.ToString() + "Z" + j.ToString(), tGrayWidth % 250, 251,
                            i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0, 2500),
                            i * new Vector3(2500, 0, 0) + new Vector3(10 * (tGrayWidth % 250), 0, 10) + (j + 1) * new Vector3(0, 0, 2500),
                            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),i,j, tGrayWidth, tGrayHeight));
                        }
                        else if (i < xNum - 1 && j == zNum - 1)
                        {
                            meshList.Add(createMesh("meshX" + i.ToString() + "Z" + j.ToString(), 251, tGrayHeight % 250,
                              i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0, 2500),
                             (i + 1) * new Vector3(2500, 0, 0) + j * new Vector3(0, 0, 2500) + new Vector3(10, 0, 10 * (tGrayHeight % 250)),
                              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),i,j, tGrayWidth, tGrayHeight));
                        }
                        else if (i == xNum - 1 && j == zNum - 1)
                        {
                            meshList.Add(createMesh("meshX" + i.ToString() + "Z" + j.ToString(), tGrayWidth % 250, tGrayHeight % 250,
                             i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0, 2500),
                             i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0, 2500) + new Vector3(10 * (tGrayWidth % 250), 0, 10 * (tGrayHeight % 250)),
                             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),i,j, tGrayWidth, tGrayHeight));
                        }
                    }
                }
                bCreate = true;
            }
        }
    }

    //加載圖片
    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);
        }
    }


    /// <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>
    /// <param name="xWidthIndex">橫向索引</param>
    /// <param name="zHeightIndex">縱向索引</param>
    /// <param name="width">橫向總寬度</param>
    /// <param name="height">縱向總高度</param>
    /// <returns></returns>
    /// 


    private GameObject createMesh(string meshName, int row, int col, Vector3 minPoint, Vector3 maxPoint, Vector2 minImgPosition, Vector2 maxImgPosition,int xWidthIndex,int zHeightIndex,int width,int height)
    {
        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 zSetp = new Vector3(0, 0, (maxPoint.z - minPoint.z) / colF);
        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 + zSetp * j + new Vector3(0, tempZ * zScale, 0);

                // uvs[i + j * row] = new Vector2((float)i / rowF, (float)j / colF);
                uvs[i + j * row] = new Vector2((float)(i+ xWidthIndex*250) / width, (float)(j+zHeightIndex*250) / height);

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

                    triangles[k++] = j * row + i + row;
                    triangles[k++] = j * row + i + row + 1;
                    triangles[k++] = j * row + i + 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;
    }
}

效果如下

 

 

 

本文鏈接 https://www.cnblogs.com/gucheng/p/10951918.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM