unity 读取灰度图生成三维地形并贴图卫星影像

unity 读取灰度图生成三维地形并贴图卫星影像,第1张

概述从 https://earthexplorer.usgs.gov/ 下载高程数据  从谷歌地球上保存对应地区卫星图像 从灰度图创建地形模型,并将卫星影像作为贴图 using System.Collections;using System.Collections.Generic;using UnityEngine;public class mapMeshCreate : MonoBe

从 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,i * new Vector3(2500,0) + j * new Vector3(0,2500),(i + 1) * new Vector3(2500,0) + (j + 1) * new Vector3(0,2500) + new Vector3(10,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,0) + new Vector3(10 * (tGrayWIDth % 250),10) + (j + 1) * new Vector3(0,0) + new Vector2(tGrayWIDth % 250,1) + (j + 1) * new Vector2(0,tGrayHeight));                        }                        else if (i < xnum - 1 && j == zNum - 1)                        {                            meshList.Add(createMesh("meshX" + i.ToString() + "Z" + j.ToString(),tGrayHeight % 250,10 * (tGrayHeight % 250)),150) + new Vector2(1,tGrayHeight % 250),tGrayHeight));                        }                        else if (i == xnum - 1 && j == zNum - 1)                        {                            meshList.Add(createMesh("meshX" + i.ToString() + "Z" + j.ToString(),2500) + new Vector3(10 * (tGrayWIDth % 250),250) + new Vector2(tGrayWIDth % 250,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);        Vector3 zSetp = new Vector3(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

总结

以上是内存溢出为你收集整理的unity 读取灰度图生成三维地形并贴图卫星影像全部内容,希望文章能够帮你解决unity 读取灰度图生成三维地形并贴图卫星影像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1075554.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存