准备灰度图

1、高程按比例对应hue色相(hsv)生成mesh效果
o.color = float4(hsv2rgb(float3(v.vertex.y/100.0, 0.5, 0.75)), 1.0);

unity shader

Shader "Unlit/vertexColor 1"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100 Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float4 color:COLOR;
}; sampler2D _MainTex;
float4 _MainTex_ST; float3 hsv2rgb(float3 c)
{
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * lerp(K.xxx, saturate(p - K.xxx), c.y);
} v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = float4(hsv2rgb(float3(v.vertex.y/100.0, 0.5, 0.75)), 1.0);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
// sample the texture
//fixed4 col = tex2D(_MainTex, i.uv);
fixed4 col = i.color;
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}

  

2、高程按比例对应色带生成mesh效果

准备色带图

unity shder

Shader "Unlit/colorRamp"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ColorRamp("Color Ramp", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100 Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float2 colorUV : TEXCOORD1;
}; sampler2D _MainTex;
sampler2D _ColorRamp;
float4 _MainTex_ST; v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
o.colorUV = float2(v.vertex.y / 100.0,0);
return o;
} fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_ColorRamp,i.colorUV);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}

  

mesh创建脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class meshCreate2 : MonoBehaviour { private Texture textureGray;//灰度图
private 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,则取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)));
}
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)));
}
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)));
}
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)));
}
}
}
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>
/// <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 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); 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;
}
}

  相机漫游控制脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class MyCameraControl : MonoBehaviour
{
public Camera mainCam;
//旋转变量;
private float m_deltX = 0f;
private float m_deltY = 0f;
//缩放变量;
private float m_distance = 10f;
private float m_mSpeed = 5f;
//移动变量;
private Vector3 m_mouseMovePos = Vector3.zero;
//平移速度
float Speed = 200f; void Start()
{
// transform.localPosition = new Vector3(0, m_distance, 0);
} void Update()
{ if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * Time.deltaTime * Speed);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * Time.deltaTime * Speed);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.forward * Time.deltaTime * -Speed);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.left * Time.deltaTime * -Speed);
}
//鼠标左键控制旋转
if (Input.GetMouseButton(0))
{
m_deltX += Input.GetAxis("Mouse X") * m_mSpeed;
m_deltY -= Input.GetAxis("Mouse Y") * m_mSpeed;
m_deltX = ClampAngle(m_deltX, -360, 360);//旋转幅度 左右
m_deltY = ClampAngle(m_deltY, -70, 70);//旋转幅度 上下
transform.rotation = Quaternion.Euler(m_deltY, m_deltX, 0);
}
//鼠标滑轮缩放
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
//自由缩放方式;
m_distance = Input.GetAxis("Mouse ScrollWheel") * 10f;
transform.localPosition = transform.position + transform.forward * m_distance;
} //相机位置跳到点击处;
if (Input.GetMouseButtonDown(1)) //0-左键 1-右键 2-滑轮
{
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
m_mouseMovePos = hitInfo.point;
transform.localPosition = m_mouseMovePos;
}
} } float ClampAngle(float angle, float minAngle, float maxAgnle)
{
if (angle <= -360)
angle += 360;
if (angle >= 360)
angle -= 360; return Mathf.Clamp(angle, minAngle, maxAgnle);
} }

本文链接

https://www.cnblogs.com/gucheng/p/10945429.html

unity 读取灰度图生成按高程分层设色地形模型的更多相关文章

  1. unity读取灰度图生成三维地形mesh

    准备灰度图 IGray.png及草地贴图 IGrass.jpg ,放入Assets下StreamingAssets文件夹中.     创建空材质,用作参数传入脚本.   脚本如下,挂载并传入材质球即可 ...

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

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

  3. unity读取灰度图生成等值线图

    准备灰度图 grayTest.png,放置于Assets下StreamingAssets文件夹中.   在场景中添加RawImage用于显示最后的等值线图.   生成等值线的过程,使用Marching ...

  4. opengl读取灰度图生成三维地形并添加光照

    转自:https://www.cnblogs.com/gucheng/p/10152889.html 准备第三方库 glew.freeglut.glm.opencv 准备一张灰度图 最终效果 代码如下 ...

  5. ue4读取灰度图生成三维地形mesh

    转自:https://www.cnblogs.com/gucheng/p/10116857.html 新建ue c++工程. 在Build.cs中添加"ProceduralMeshCompo ...

  6. opengl读取灰度图生成三维地形

    准备第三方库 glew.freeglut.glm.opencv 准备灰度图片和草地贴图 最终效果 代码包括主程序源文件mainApp.cpp.顶点着色器shader.vs.片元着色器shader.fs ...

  7. blender导入灰度图生成地形模型

    安装软件 在此处下载blender并安装. 添加平面 1.打开blender,右键删除初始的立方体. 2.shift+a选择平面添加进场景: 3.按下s键鼠标拖动调节平面大小确定后按下鼠标左键: 4. ...

  8. unity 读取excel表 生成asset资源文件

    做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...

  9. c语言实现灰度图转换为二值图

    将上篇得到的灰度图转换为二值图,读取像素数据,低于某一值置0,否则设置为255,为得到更好的效果不同图片应采用不同的值 /* 2015年6月2日11:16:22 灰度图转换为二值图 blog:http ...

随机推荐

  1. JavaScript 页面渲染

    1. 从输入url到得到html的详细过程 1.1 加载资源的形式      输入 URL 或跳转页面 加载 html 1.2 加载一个资源的过程 浏览器根据DNS服务器得到域名的IP地址 向这个IP ...

  2. 使用python批量造测试数据

    # -*- coding:utf-8 -*- import json import os import time class Virtual_Data: def __init__(self): sel ...

  3. pycharm分辨率&清晰度

    三个月前换了新电脑,安装pycharm.又换了新的大屏幕. pycharm界面拖到大屏上时,字大小和字体都没有变化,清晰度低了很多,就像打了一层薄薄的马赛克一样.拖回到原本的屏幕又清晰了. 一直以为是 ...

  4. IE版本判断条件注释

    IE下判断IE版本的语句...[if lte IE 8]……[endif]  <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见   < ...

  5. Shiro 中的 SecurityUtils(转)

    在 Shiro 中 SecurityUtils 是一个抽象类.并且没有任何子类.在其中声明了一个静态属性,三个静态方法. 静态属性 securityManager private static Sec ...

  6. 023_统计当前 Linux 系统中可以登录计算机的账户有多少个

    #!/bin/bash #方法 1: grep "bash$" /etc/passwd | wc -l #方法 2: #-F END都要大写! awk -F: '/bash$/{x ...

  7. 鬼子进村 fhq-treap

    鬼子进村 fhq-treap 题面 观察题目发现可用平衡树做:每次鬼子拆家即从平衡树中加入被拆的节点:每次村民修房子都向平衡树中删除该节点:每次查询时,只需要求出其后驱与前驱,易知nxt-pre-1为 ...

  8. leveldb源码分析之内存池Arena

    转自:http://luodw.cc/2015/10/15/leveldb-04/ 这篇博客主要讲解下leveldb内存池,内存池很多地方都有用到,像linux内核也有个内存池.内存池的存在主要就是减 ...

  9. UOJ #164 [清华集训2015]V (线段树)

    题目链接 http://uoj.ac/problem/164 题解 神仙线段树题. 首先赋值操作可以等价于减掉正无穷再加上\(x\). 假设某个位置从前到后的操作序列是: \(x_1,x_2,..., ...

  10. Robot Framework(十九) 附录

    6附录 6.1测试数据中的所有可用设置 6.1.1设置表 Setting表用于导入测试库,资源文件和变量文件,以及定义测试套件和测试用例的元数据.它可以包含在测试用例文件和资源文件中.请注意,在资源文 ...