1. 程式人生 > >通過建立GameObject 改變Mesh進行網格繪製

通過建立GameObject 改變Mesh進行網格繪製

有節操的引用……上面為本日誌引用來源本文做了些許改動

//我使用的功能讓我可以不關心如何應用高度,所以這次是節選引用

//公共類=檔名

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;