http://blog.csdn.net/familycsd000/article/details/45844893
有节操的引用……上面为本日志引用来源 本文做了些许改动
//我使用的功能让我可以不关心如何应用高度,所以这次是节选引用
//公共类=文件名
publicclassMesh_JQM {
privateVector3[] vertexes;//顶点数组:存储所有顶点信息,x、y、z值.
privateint[] triangles;//构成所有的三角形的顶点数目.
/*--------------CreateMesh()------------------
* 功能:创建Mesh.
*
* 参数:HeightMap_JQMhmap,读取高度图的信息,如灰度(高程),总字节数,2个字节代表一个高程值.数据存储方式为小端模式,低字节保存在低地址(读数据的计算高程的时候要注意).
* float width,地形的宽度.
* float length,地形的长度.
*-----------------------------------------------
*/
//这个函数流程几乎可以被照搬到大多渲染场合
publicbool CreateMesh(HeightMap_JQM hmap,float width, float length)
{
ComputeVertexes(hmap, width, length);//计算顶点数量
ComputeTriangle(hmap);//计算三角关系
RenderMesh();//渲染Mesh
returntrue;
}
/*--------------ComputeVertexes()------------------
* 功能:通过高度图,计算顶点信息,xyz.
*
* 参数:HeightMap_JQMhmap,读取高度图的信息.
* float width,地形的宽度.
* float length,地形的长度.
*-------------------------------------------------------
*/
privateVector3[] ComputeVertexes(HeightMap_JQM hmap,float width, float length)
{
float w = width/(hmap.m_HeightMap.m_iSize-1);//顶点之间的行间距.
float l =length /(hmap.m_HeightMap.m_iSize);//顶点之间的列间距.
int size_X= hmap.m_HeightMap.m_iSize;//在x方向的顶点数.
int size_Y= hmap.m_HeightMap.m_iSize;//在Y方向的顶点数.
int size =(int)Mathf.Pow(hmap.m_HeightMap.m_iSize,2);//顶点总数.
//mathf.pow pow(a,b) a的b次方
//创建顶点结构(顶点个数个三维坐标存放顶点坐标)
vertexes = newVector3[size];
//行列循环把坐标填进去 x,y不变 z就是高度
for (int i = 0; i <hmap.m_HeightMap.m_iSize; i++)//行.
{
for (int j = 0; j < hmap.m_HeightMap.m_iSize; j++)//列.
{
float h = hmap.m_HeightMap.m_Data[2 * (i * size_Y + j)] +hmap.m_HeightMap.m_Data[2 * (i * size_Y + j) + 1] * 16 * 16;
h = h / 65536*600;//高程.
vertexes[i * size_Y + j] = newVector3(j * w,h , i * l);
}
}
returnvertexes;
}
/*--------------ComputeTriangle()------------------
* 功能:计算三角行数目和索引.
*
* 参数:HeightMap_JQMhmap,读取高度图的信息.
*------------------------------------------------------
*/
publicint[] ComputeTriangle(HeightMap_JQM hmap)
{
int sum =(hmap.m_HeightMap.m_iSize - 1) * (hmap.m_HeightMap.m_iSize - 1) * 6;//三角形顶点总数
triangles = newint[sum];
uint index =0;
for (int i = 0; i <hmap.m_HeightMap.m_iSize-1; i++)
{
for (int j = 0; j < hmap.m_HeightMap.m_iSize-1; j++)
{
int role =Mathf.FloorToInt(hmap.m_HeightMap.m_iSize);
int self = j + (i * role);
int next = j + ((i + 1) * role);
//顺时针
//写索引 这个地方我有些疑问 为什么是六个一组
//详见: http://blog.csdn.net/lihuozhiling0101/article/details/43453435
//解释了为什么六个一组 (是以两个方格为一组实现的)
triangles[index] = self;
triangles[index + 1] = next +1;
triangles[index + 2] = self +1;
triangles[index + 3] = self;
triangles[index + 4] = next;
triangles[index + 5] = next +1;
index += 6;
}
}
returntriangles;
}
/*--------------RenderMesh()------------------
* 功能:Unity3D渲染,显示Mesh.
*------------------------------------------------
*/
publicvoid RenderMesh()
{
/*对象->网格->材质->网格渲染*/
GameObject m_mesh= newGameObject();
m_mesh.name = "createMesh_JQM";
Mesh me =m_mesh.AddComponent<MeshFilter>().mesh;
Material ma = newMaterial(Shader.Find("Diffuse"));
m_mesh.AddComponent<MeshRenderer>().material = ma;
me.Clear();
me.vertices = vertexes;
me.triangles = triangles;
//包络面函数
me.RecalculateBounds();
//重新计算法线
me.RecalculateNormals();
}
}
一个粗糙的尝试:
//给绘制的东西加个green
// Create amaterial with transparent diffuse shader
//创建一个带有transparent diffuse着色器的材质
var material= new Material (Shader.Find ("Transparent/Diffuse"));
material.color= Color.green;
// assignthe material to the renderer
//指定材质到渲染器
//renderer.material= material;
//嵌入到本程序中的时候应该是:
m_mesh.AddComponent<MeshRenderer>().material = material;