国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity3D網格功能生成球體網格模型

2020-01-24 00:09:45
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Unity3D網格功能生成球體網格模型的具體代碼,供大家參考,具體內容如下

前面已經講過怎樣使用mesh生成一個自己的網格,那么本文將會講述怎樣將這個網格變換成自己想要的形狀,比如一個球體。

我們需要知道一個從平面坐標到球體坐標的映射公式。假設平面坐標是(x,y),球體坐標是(x0,y0,z0),則

球體坐標(x0,y0,z0)可以通過以下代碼得到,(x,y) 對應vertices[i].x和vertices[i].y。

 v.x = r * Mathf.Cos(vertices[i].x / width * 2 * Mathf.PI) * Mathf.Cos(vertices[i].y / height * Mathf.PI - Mathf.PI / 2); v.y = r * Mathf.Sin(vertices[i].x / width * 2 * Mathf.PI) * Mathf.Cos(vertices[i].y / height * Mathf.PI - Mathf.PI / 2);v.z = r * Mathf.Sin(vertices[i].y / height * Mathf.PI - Mathf.PI / 2);

完整代碼在最后,將完整的腳本綁定到一個空物體上,把棋盤格圖案chessboard.jpg放到Assets/Resources下,還要在場景中生成一個sphere作為顯示網格頂點的輔助物體。一切就緒后,運行效果如下:

using UnityEngine;using System.Collections; public class BallMesh : MonoBehaviour{  Mesh mesh; Vector3[] vertices; Vector2[] uv; int[] triangles; Vector3[] normals; public GameObject sphere; GameObject[] spheres;  void Start() {  gameObject.AddComponent<MeshFilter>();  gameObject.AddComponent<MeshRenderer>();  Texture img = (Texture)Resources.Load("tm");  gameObject.GetComponent<Renderer>().material.mainTexture = img;  mesh = new Mesh();  int m = 25; //row   int n = 50; //col   float width = 8;  float height = 6;  vertices = new Vector3[(m + 1) * (n + 1)];//the positions of vertices   spheres = new GameObject[(m + 1) * (n + 1)];  uv = new Vector2[(m + 1) * (n + 1)];  normals = new Vector3[(m + 1) * (n + 1)];  triangles = new int[6 * m * n];  for (int i = 0; i < vertices.Length; i++)  {   float x = i % (n + 1);   float y = i / (n + 1);   float x_pos = x / n * width;   float y_pos = y / m * height;   vertices[i] = new Vector3(x_pos, y_pos, 0);   float u = x / n;   float v = y / m;   uv[i] = new Vector2(u, v);  }  for (int i = 0; i < 2 * m * n; i++)  {   int[] triIndex = new int[3];   if (i % 2 == 0)   {    triIndex[0] = i / 2 + i / (2 * n);    triIndex[1] = triIndex[0] + 1;    triIndex[2] = triIndex[0] + (n + 1);   }   else   {    triIndex[0] = (i + 1) / 2 + i / (2 * n);    triIndex[1] = triIndex[0] + (n + 1);    triIndex[2] = triIndex[1] - 1;    }   triangles[i * 3] = triIndex[0];   triangles[i * 3 + 1] = triIndex[1];   triangles[i * 3 + 2] = triIndex[2];  }    int r = 10;  for (int i = 0; i < vertices.Length; i++)  {   spheres[i] = Instantiate( sphere,this.transform) as GameObject;   Vector3 v ;   v.x = r * Mathf.Cos(vertices[i].x / width * 2 * Mathf.PI) * Mathf.Cos(vertices[i].y / height * Mathf.PI - Mathf.PI / 2);   v.y = r * Mathf.Sin(vertices[i].x / width * 2 * Mathf.PI) * Mathf.Cos(vertices[i].y / height * Mathf.PI - Mathf.PI / 2);   v.z = r * Mathf.Sin(vertices[i].y / height * Mathf.PI - Mathf.PI / 2);   //v = vertices[i];    vertices[i] = v;   spheres[i].transform.localPosition = v;    normals[i] = new Vector3(0,1,0);  }    mesh.vertices = vertices;  mesh.normals = normals;  mesh.uv = uv;  mesh.triangles = triangles;  this.GetComponent<MeshFilter>().mesh = mesh;  } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万年县| 城步| 九江县| 铜陵市| 桓仁| 兴城市| 比如县| 教育| 渭源县| 德兴市| 淳安县| 黑河市| 乐都县| 永寿县| 乐昌市| 邯郸县| 永福县| 建德市| 临洮县| 华安县| 甘肃省| 贺州市| 彭山县| 油尖旺区| 红安县| 宜兰市| 湛江市| 西畴县| 增城市| 肇州县| 灵武市| 友谊县| 元氏县| 武穴市| 白银市| 宁河县| 延津县| 阳江市| 德格县| 赫章县| 曲阜市|